Logging In and Out
Logging In and Out
This section describes how to log in and out of the CTERA Portal and switch between virtual portals, for purposes of using the API.
In this section
Logging In
Log in to a portal.
API
admin/api/login
URI
{BASE_URL}/login
HTTP Method
POST
Request Content-Type
application/x-www-form-urlencoded
Request Body
j_username=username&j_password=password
Response Body
<val>Login succeed</val>
Expected Response Status
200 OK and an HTTP session cookie which is then used for the duration of the session.
The session times out after 30 minutes of inactivity.
If a response such as 403 Forbidden is returned, check the user name and password provided.
Where:
j_username – Mandatory: The name of a user with administrative rights to the team portal with the URL portal_url.
j_password – Mandatory: The password for the user. The login is over HTTPS to ensure that the password is encrypted.
Upon receiving a successful login reply, the server sets HTTP session cookies. The client must return these cookies to the server in each subsequent request.
Note: To ensure that the session cookie returned by the CTERA API login is automatically returned in subsequent requests to the server, the same HttpClient object must be used for all future requests.
If you require a read-only API session, use a read-only administrator account.
Java Example
public static void login(HttpClient client, String baseURL, String username, String password) throws Exception {
  PostMethod postMethod = new PostMethod(baseURL + " /login");
  postMethod.setDoAuthentication(true);
  postMethod.addParameter("j_username", username);
  postMethod.addParameter("j_password", password);
  int statusCode = client.executeMethod(postMethod);
  if (statusCode == HttpServletResponse.SC_FORBIDDEN) {
    throw new Exception("Invalid Username or Password");
  }
  String result = new String(postMethod.getResponseBody());
  if (statusCode != HttpServletResponse.SC_OK)
    throw new Exception(result);
}
cURL Example
curl -X POST -H "Cache-Control: no-cache" -H "Postman-Token: f39d07b5-2e46-46d4-3a76-6832a08b4741" -H "Content-Type: application/x-www-form-urlencoded" -d 'j_username=username&j_password=password' "https://portal_url/admin/api/login"
Python Example
import http.client
conn = http.client.HTTPSConnection("portal_url")
payload = "j_username=username&j_password=password"
headers = {
  'cache-control': "no-cache",
  'postman-token': "e51d6e8a-6796-a795-fc54-106b23065be3",
  'content-type': "application/x-www-form-urlencoded"
}
conn.request("POST", "/admin/api/login", payload, headers)
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))
C# (RestSharp) Example
var client = new RestClient("https://portal_url/admin/api/login");
var request = new RestRequest(Method.POST);
request.AddHeader("content-type", "application/x-www-form-urlencoded");
request.AddHeader("postman-token", "4c406c08-d242-201c-5fb2-e22ff9df268d");
request.AddHeader("cache-control", "no-cache");
request.AddParameter("application/x-www-form-urlencoded", "j_username=username&j_password=password", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
Logging Out
Log out of a portal.
API
admin/api/logout
URI
{BASE_URL}/logout
HTTP Method
POST
Request Body
empty
Expected Response status
200 OK
Results
The session is terminated.
Note: The session is also terminated after 30 minutes of inactivity.
Java Example
private static void logout(HttpClient client, String baseURL) throws Exception {
  PostMethod postMethod = new PostMethod(baseURL + "/logout");
  int statusCode = client.executeMethod(postMethod);
  String res = new String(postMethod.getResponseBody());
  if (statusCode != HttpServletResponse.SC_OK)
  throw new Exception(res);
}
Changing the Current Virtual Portal
Switch the currently selected virtual portal.
API
admin/api/currentPortal
URI
{BASE_URL}/currentPortal
HTTP Method
PUT
Request Content-Type
application/xml
Request Body
A team or reseller portal: <val>portalName</val>
Global administration portal: <val></val>
Expected Response status
200 OK
Where:
portalName – The name of the portal to switch to.
Results
The specified virtual portal is set to be the current portal. All subsequent API commands operate in the scope of the selected portal.
Java Example
//login to some virtual portal
login(httpClient, baseURL, username, password);
 
//switch to portal1
putObject(httpClient, baseUrl+ "/currentPortal", "<val>portal1</val>")
 
// perform API commands in portal1 scope
...
//now switch to global administration portal
putObject(httpClient, baseUrl+ "/currentPortal", "<val></val>");
 
// perform more API commands in global administration scope
...