Layer7 API Management

Basic Authentication using Custom Endpoint

By banty01 posted 04-27-2016 12:01 PM


The Custom Endpoint allows an API developer a great deal of flexibility. One request was to support Basic Authentication (passing username and password). The following code shows how to extract and decode the authentication string and how to use these values to create an LAC Authentication Token. This can then be used to make REST API calls to LAC and return JSON responses.

  1. Create a new Custom Endpoint - check GET and POST
  2. Enter the code below changing the authURL to your project
  3. Go to your favorite tool (e.g. curl, postman) and create a basic authentication (username: demo, password: Password1)
  4. If you do a verb GET - use restGET, if you use POST, use restPOST and pass the 'json' content.


Sample Code


var res = {};
var hdrs = headers.getRequestHeader('Authorization');

for (var i = 0; i < hdrs.size(); ++i) {
  var auth = hdrs.get(i);
  var userpw =;

   var split = userpw.split(":");
   var username = split[0];
   var password = split[1];
   var data = { 'username': username, 'password': password};
   var authURL = "http://localhost:8080/rest/default/demo/v1";
   var apikey = SysUtility.restPost(authURL +"/@authentication",null,null,data);
   var authtoken = JSON.parse(apikey).apikey;
   var settings = {headers: { "Authorization": "CALiveAPICreator "+authtoken+":1"}};
   var params = {};
   var url = authURL + "/demo:customer";
   res  = SysUtility.restGet(url,params,settings);


//FOR POST verb

  var reader = new;

  var json = "";

  var line = "";

  while ((line = reader.readLine()) != null) {

   json += line;


  res = SysUtility.restPost(url,params, settings, json);
  } //if auth
} //for loop
} //if hdrs - or throw exception
return JSON.stringify(res);