1 var REST = library.REST.REST();
  2 var myExpires = true;
  3 var mySecondsUntilExpiration;
  4 var kSecondsInMs = 1000;
  5 var kRestKeyName = "Rest API";
  6 var myData = {};
  7 
  8 function getApiKey(theUser) {
  9   var aKey = "";
 10   if (myExpires) {
 11     var aToday = new Date().getTime();
 12     var anExpiration = isNaN(mySecondsUntilExpiration)
 13       ? ""
 14       : aToday + mySecondsUntilExpiration * kSecondsInMs;
 15     aKey = apiKeyManager.getOrCreateExpiringApiKey(
 16       theUser,
 17       anExpiration,
 18       kRestKeyName
 19     );
 20   } else {
 21     aKey = apiKeyManager.getOrCreateApiKey(kRestKeyName, theUser);
 22   }
 23   if (aKey == "") {
 24     REST.submitError(theUser, "Failed to get an Api Key for this user");
 25   } else {
 26     myData[theUser] = aKey;
 27   }
 28 }
 29 
 30 /**
 31  * Creates or retrieves an api key for the given user
 32  *
 33  * @description  Creates or retrieves an Api Key for the user with the username in 'src' . The results  parsed into a
 34  * text object keyed by AssetID from the JSON file in which they are contained, and by default are displayed to the
 35  * user.
 36  * @example 'MBurl'/wf/restapi/1/getApiKey?src=["root"]&expires=true
 37  * @example <a target="_blank" href=http://127.0.0.1:55555/wf/restapi/1/getApiKey?src=["root"]>http://127.0.0.1:55555/wf/restapi/1/getApiKey?src=["root"]</a>
 38  * @example 'MBurl'/wf/restapi/1/GetApiKey?user=root&pass=test&src=["root"]&expires=true
 39  * @class Creates or retrieves an api key for the given user
 40  * @name GetApiKey
 41  * @param src the username/usernames of the users to create the api keys for
 42  * @param expires true/false determines if the key created will expire or not. default is true
 43  * @param secondsValid Seconds until the key expires.  if parameter is not given and expires is true, a default expiration will be created
 44  * @returns ( {'username': "ApiKey",...} )
 45  */
 46 function main() {
 47   var aParameters = REST.getParametersToIterate("src");
 48   myExpires = REST.getParameter("expires", false);
 49   mySecondsUntilExpiration = context.getParameter("secondsValid");
 50   if (myData.error != null) {
 51     return REST.formatResponse();
 52   }
 53   REST.iterateThroughParameters(aParameters, getApiKey);
 54   return REST.formatResponse();
 55 }
 56 main();
 57