VCF Automation Blog

from Stefan Schnell

Retrieve Access Bearer Token


JavaScript

/**
 * Delivers the csp-auth-token (CSP = Cloud Service Platform) or the
 * refresh_token with the bearer token for the HOL in the log with
 * fixed values of user name and user password.
 *
 * @author Stefan Schnell
 *
 * Checked with VMware Aria Automation 8.5.1 and 8.12.0
 */

var restHost = RESTHostManager.createHost("dynamicRequest");
var httpRestHost = RESTHostManager.createTransientHostFrom(restHost);

httpRestHost.operationTimeout = 60;
httpRestHost.connectionTimeout = 30;
httpRestHost.hostVerification = false;

/**
 * Access data for HOL with release 8.5.1
 *
 * httpRestHost.url = "https://vr-automation.corp.local";
 * var oLogin = {
 *   "username": "holadmin@corp.local",
 *   "password": "VMware1!"
 * }
 */

// Access data for HOL with release 8.12.0
httpRestHost.url = "https://aria-auto.corp.vmbeans.com";
var oLogin = {
  "username": "holadmin@corp.vmbeans.com",
  "password": "VMware1!"
}

var jsonLogin = JSON.stringify(oLogin);

// csp-auth-token
// var request = httpRestHost.createRequest("POST",
//   "/csp/gateway/am/api/login", jsonLogin);

// refresh_token
var request = httpRestHost.createRequest("POST",
  "/csp/gateway/am/api/login?access_token", jsonLogin);

request.contentType = "application/json";

// Execute REST Request
System.log("REST Request: POST " + request.fullUrl);
var response = request.execute();

// REST call output
System.log("REST Response Status Code: " + response.statusCode);
System.log("REST Response Content: " + response.contentAsString);

// Bearer token
var oRefreshToken = JSON.parse(response.contentAsString);
var refreshToken = "{\"refreshToken\":\"" + oRefreshToken.refresh_token +
  "\"}";

request = httpRestHost.createRequest("POST",
  "/iaas/api/login", refreshToken);
request.contentType = "application/json";

// Execute REST Request
System.log("REST Request: POST " + request.fullUrl);
response = request.execute();

// REST call output
System.log("REST Response Status Code: " + response.statusCode);
System.log("REST Response Content: " + response.contentAsString);


var oBearerToken = JSON.parse(response.contentAsString);
var bearerToken = oBearerToken.token;
System.log(bearerToken);

/**
 *
 * @module de.stschnell
 *
 * @version 0.1.0
 *
 * @param {string} in_userName
 * @param {SecureString} in_password
 *
 * @outputType String
 *
 */
function getBearerToken(in_userName, in_password) {

/**
 * Delivers the bearer token in the log with the user name and user
 * password as parameters.
 *
 * @author Stefan Schnell <mail@stefan-schnell.de>
 * @license MIT
 * @version 0.1.0
 *
 * Set com.vmware.scripting.javascript.allow-native-object in the
 * system properties to true.
 *
 * Checked with VMware Aria Automation 8.12.0.
 *
 * @example
 * var token = System.getModule("de.stschnell").getBearerToken(
 *   "holadmin@corp.vmbeans.com",
 *   "VMware1!"
 * );
 */

function getFQDN() {
  var fqdn = "";
  var jvmOpts = java.lang.System.getenv("JVM_OPTS");
  if (jvmOpts !== null) {
    var options = jvmOpts.split(" ");
    options.forEach( function(option) {
      if (option.substring(0, 19) === "-Dvco.app.hostname=") {
        fqdn = option.substring(19, option.length);
      }
    });
  }
  if (fqdn !== "") {
    return fqdn;
  } else {
    return null;
  }
}

function retrieveBearerToken(url, username, password) {
  var httpRestHost = RESTHostManager.createTransientHostFrom(
    RESTHostManager.createHost("dynamicRequest")
  );
  httpRestHost.operationTimeout = 60;
  httpRestHost.connectionTimeout = 30;
  httpRestHost.hostVerification = false;
  httpRestHost.url = url;
  var oLogin = {
    "username": username,
    "password": password
  };
  var jsonLogin = JSON.stringify(oLogin);
  var request = httpRestHost.createRequest(
    "POST",
    "/csp/gateway/am/api/login?access_token",
    jsonLogin
  );
  request.contentType = "application/json";
  var response = request.execute();
  if (response.statusCode === 200) {
    var oRefreshToken = JSON.parse(response.contentAsString);
    var refreshToken = "{\"refreshToken\":\"" +
      oRefreshToken.refresh_token + "\"}";
    request = httpRestHost.createRequest(
      "POST",
      "/iaas/api/login",
      refreshToken
    );
    request.contentType = "application/json";
    response = request.execute();
    if (response.statusCode === 200) {
      var oBearerToken = JSON.parse(response.contentAsString);
      var bearerToken = oBearerToken.token;
      return bearerToken;
    }
  }
}

// Main
if (
  String(in_userName).trim() !== "" &&
  String(in_password).trim() !== ""
) {
  var fqdn = getFQDN();
  if (fqdn !== null) {
    var bearerToken = retrieveBearerToken(
      "https://" + fqdn,
      in_userName,
      in_password
    );
    if (bearerToken !== null) {
      System.log(bearerToken);
      return bearerToken;
    }
  }
} else {
  throw new Error(
    "in_userName or in_password argument can not be null"
  );
}

}

/**
 *
 * @module de.stschnell
 *
 * @version 0.2.0
 *
 * @param {string} in_userName
 * @param {SecureString} in_password
 *
 * @outputType String
 *
 */
function getBearerToken(in_userName, in_password) {

/**
 * Delivers the bearer token in the log with the user name and user
 * password as parameters. This version is with an external getFQDN()
 * call instead of internal function.
 *
 * @author Stefan Schnell <mail@stefan-schnell.de>
 * @license MIT
 * @version 0.1.0
 *
 * Set com.vmware.scripting.javascript.allow-native-object in the
 * system properties to true.
 *
 * Checked with VMware Aria Automation 8.12.0.
 *
 * @example
 * var token = System.getModule("de.stschnell").getBearerToken(
 *   "holadmin@corp.vmbeans.com",
 *   "VMware1!"
 * );
 */

function retrieveBearerToken(url, username, password) {
  var httpRestHost = RESTHostManager.createTransientHostFrom(
    RESTHostManager.createHost("dynamicRequest")
  );
  httpRestHost.operationTimeout = 60;
  httpRestHost.connectionTimeout = 30;
  httpRestHost.hostVerification = false;
  httpRestHost.url = url;
  var oLogin = {
    "username": username,
    "password": password
  };
  var jsonLogin = JSON.stringify(oLogin);
  var request = httpRestHost.createRequest(
    "POST",
    "/csp/gateway/am/api/login?access_token",
    jsonLogin
  );
  request.contentType = "application/json";
  var response = request.execute();
  if (response.statusCode === 200) {
    var oRefreshToken = JSON.parse(response.contentAsString);
    var refreshToken = "{\"refreshToken\":\"" +
      oRefreshToken.refresh_token + "\"}";
    request = httpRestHost.createRequest(
      "POST",
      "/iaas/api/login",
      refreshToken
    );
    request.contentType = "application/json";
    response = request.execute();
    if (response.statusCode === 200) {
      var oBearerToken = JSON.parse(response.contentAsString);
      var bearerToken = oBearerToken.token;
      return bearerToken;
    }
  }
}

// Main
if (
  String(in_userName).trim() !== "" &&
  String(in_password).trim() !== ""
) {
  var fqdn = System.getModule("de.stschnell").getFQDN();
  if (fqdn !== null) {
    var bearerToken = retrieveBearerToken(
      "https://" + fqdn,
      in_userName,
      in_password
    );
    if (bearerToken !== null) {
      System.log(bearerToken);
      return bearerToken;
    }
  }
} else {
  throw new Error(
    "in_userName or in_password argument can not be null"
  );
}

}

Python

def handler(context, inputs):
    """ Delivers the bearer token in the log and as return value. """

    bearerToken = context['getToken']()
    print(bearerToken)

    outputs = {
        "status": "done",
        "bearerToken": bearerToken
    }

    return outputs

Additional Information

Hint: Please note that sometimes the Authorization field and sometimes the csp-auth-token is queried.

For testing purposes the currently logged-in users can be queried: Information about the users can be queried:
FieldInput
AuthorizationBearer eyJ0e...pq1yg
csp-auth-tokeneyJ0e...pq1yg

Important hint: The word Bearer must always be entered in the Authorization field.

Hint: CSP = Content Security Policy. A "Content-Security-Policy" is added to the HTTP header of a website and assigned values. These control which resources the user agent is allowed to load for this page. A good designed content policy helps to protect a site against cross-site scripting attacks.