|
|
@ -16,8 +16,15 @@
|
|
|
|
* under the License.
|
|
|
|
* under the License.
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
var apiWrapperUtil = function () {
|
|
|
|
/**
|
|
|
|
var log = new Log("/app/modules/api-wrapper-util.js");
|
|
|
|
* ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
* Following module includes invokers
|
|
|
|
|
|
|
|
* at Jaggery Layer for calling Backend Services, protected by OAuth Tokens.
|
|
|
|
|
|
|
|
* These Services include both REST and SOAP Services.
|
|
|
|
|
|
|
|
* ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
var handlers = function () {
|
|
|
|
|
|
|
|
var log = new Log("/app/modules/token-handlers.js");
|
|
|
|
|
|
|
|
|
|
|
|
var tokenUtil = require("/app/modules/util.js")["util"];
|
|
|
|
var tokenUtil = require("/app/modules/util.js")["util"];
|
|
|
|
var constants = require("/app/modules/constants.js");
|
|
|
|
var constants = require("/app/modules/constants.js");
|
|
|
@ -28,23 +35,27 @@ var apiWrapperUtil = function () {
|
|
|
|
|
|
|
|
|
|
|
|
privateMethods.setUpEncodedTenantBasedClientCredentials = function (username) {
|
|
|
|
privateMethods.setUpEncodedTenantBasedClientCredentials = function (username) {
|
|
|
|
if (!username) {
|
|
|
|
if (!username) {
|
|
|
|
log.error("Could not set up encoded tenant based client credentials " +
|
|
|
|
throw new Error("{/app/modules/token-handlers.js} Could not set up encoded tenant based " +
|
|
|
|
"to session context. No username is found as input.");
|
|
|
|
"client credentials to session context. No username is found as " +
|
|
|
|
|
|
|
|
"input - setUpEncodedTenantBasedClientCredentials(x)");
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
var dynamicClientCredentials = tokenUtil.getDynamicClientCredentials();
|
|
|
|
var dynamicClientCredentials = tokenUtil.getDynamicClientCredentials();
|
|
|
|
if (!dynamicClientCredentials) {
|
|
|
|
if (!dynamicClientCredentials) {
|
|
|
|
log.error("Could not set up encoded tenant based client credentials " +
|
|
|
|
throw new Error("{/app/modules/token-handlers.js} Could not set up encoded tenant based " +
|
|
|
|
"to session context as the server is unable to obtain dynamic client credentials.");
|
|
|
|
"client credentials to session context as the server is unable to obtain " +
|
|
|
|
|
|
|
|
"dynamic client credentials - setUpEncodedTenantBasedClientCredentials(x)");
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
var jwtToken = tokenUtil.getTokenWithJWTGrantType(dynamicClientCredentials);
|
|
|
|
var jwtToken = tokenUtil.getTokenWithJWTGrantType(dynamicClientCredentials);
|
|
|
|
if (!jwtToken) {
|
|
|
|
if (!jwtToken) {
|
|
|
|
log.error("Could not set up encoded tenant based client credentials " +
|
|
|
|
throw new Error("{/app/modules/token-handlers.js} Could not set up encoded tenant based " +
|
|
|
|
"to session context as the server is unable to obtain a jwt token.");
|
|
|
|
"client credentials to session context as the server is unable to obtain " +
|
|
|
|
|
|
|
|
"a jwt token - setUpEncodedTenantBasedClientCredentials(x)");
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
var tenantBasedClientCredentials = tokenUtil.getTenantBasedAppCredentials(username, jwtToken);
|
|
|
|
var tenantBasedClientCredentials = tokenUtil.getTenantBasedAppCredentials(username, jwtToken);
|
|
|
|
if (!tenantBasedClientCredentials) {
|
|
|
|
if (!tenantBasedClientCredentials) {
|
|
|
|
log.error("Could not set up encoded tenant based client credentials " +
|
|
|
|
throw new Error("{/app/modules/token-handlers.js} Could not set up encoded tenant " +
|
|
|
|
"to session context as the server is unable to obtain such credentials.");
|
|
|
|
"based client credentials to session context as the server is unable " +
|
|
|
|
|
|
|
|
"to obtain such credentials - setUpEncodedTenantBasedClientCredentials(x)");
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
var encodedTenantBasedClientCredentials =
|
|
|
|
var encodedTenantBasedClientCredentials =
|
|
|
|
tokenUtil.encode(tenantBasedClientCredentials["clientId"] + ":" +
|
|
|
|
tokenUtil.encode(tenantBasedClientCredentials["clientId"] + ":" +
|
|
|
@ -57,34 +68,18 @@ var apiWrapperUtil = function () {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
publicMethods.refreshToken = function () {
|
|
|
|
|
|
|
|
var accessTokenPair = parse(session.get(constants["ACCESS_TOKEN_PAIR_IDENTIFIER"]));
|
|
|
|
|
|
|
|
// accessTokenPair includes current access token as well as current refresh token
|
|
|
|
|
|
|
|
var encodedClientCredentials = session.get(constants["ENCODED_CLIENT_KEYS_IDENTIFIER"]);
|
|
|
|
|
|
|
|
if (!accessTokenPair || !encodedClientCredentials) {
|
|
|
|
|
|
|
|
log.error("Error in refreshing tokens. Either the access token pair, " +
|
|
|
|
|
|
|
|
"encoded client credentials or both input are not found under session context.");
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
var newAccessTokenPair = tokenUtil.refreshToken(accessTokenPair, encodedClientCredentials);
|
|
|
|
|
|
|
|
if (!newAccessTokenPair) {
|
|
|
|
|
|
|
|
log.error("Error in refreshing tokens. Unable to update " +
|
|
|
|
|
|
|
|
"session context with new access token pair.");
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
session.put(constants["ACCESS_TOKEN_PAIR_IDENTIFIER"], stringify(newAccessTokenPair));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
publicMethods.setupAccessTokenPairByPasswordGrantType = function (username, password) {
|
|
|
|
publicMethods.setupAccessTokenPairByPasswordGrantType = function (username, password) {
|
|
|
|
if (!username || !password) {
|
|
|
|
if (!username || !password) {
|
|
|
|
log.error("Could not set up access token pair by password grant type. " +
|
|
|
|
throw new Error("{/app/modules/token-handlers.js} Could not set up access token pair by " +
|
|
|
|
"Either username, password or both are missing as input.");
|
|
|
|
"password grant type. Either username, password or both are missing as " +
|
|
|
|
|
|
|
|
"input - setupAccessTokenPairByPasswordGrantType(x, y)");
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
privateMethods.setUpEncodedTenantBasedClientCredentials(username);
|
|
|
|
privateMethods.setUpEncodedTenantBasedClientCredentials(username);
|
|
|
|
var encodedClientCredentials = session.get(constants["ENCODED_CLIENT_KEYS_IDENTIFIER"]);
|
|
|
|
var encodedClientCredentials = session.get(constants["ENCODED_CLIENT_KEYS_IDENTIFIER"]);
|
|
|
|
if (!encodedClientCredentials) {
|
|
|
|
if (!encodedClientCredentials) {
|
|
|
|
log.error("Could not set up access token pair by password grant type. " +
|
|
|
|
throw new Error("{/app/modules/token-handlers.js} Could not set up access token pair by " +
|
|
|
|
"Encoded client credentials are missing.");
|
|
|
|
"password grant type. Encoded client credentials are " +
|
|
|
|
|
|
|
|
"missing - setupAccessTokenPairByPasswordGrantType(x, y)");
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
var accessTokenPair;
|
|
|
|
var accessTokenPair;
|
|
|
|
// accessTokenPair will include current access token as well as current refresh token
|
|
|
|
// accessTokenPair will include current access token as well as current refresh token
|
|
|
@ -97,7 +92,9 @@ var apiWrapperUtil = function () {
|
|
|
|
getTokenWithPasswordGrantType(username,
|
|
|
|
getTokenWithPasswordGrantType(username,
|
|
|
|
encodeURIComponent(password), encodedClientCredentials, stringOfScopes);
|
|
|
|
encodeURIComponent(password), encodedClientCredentials, stringOfScopes);
|
|
|
|
if (!accessTokenPair) {
|
|
|
|
if (!accessTokenPair) {
|
|
|
|
log.error("Could not set up access token pair by password grant type. Error in token retrieval.");
|
|
|
|
throw new Error("{/app/modules/token-handlers.js} Could not set up access " +
|
|
|
|
|
|
|
|
"token pair by password grant type. Error in token " +
|
|
|
|
|
|
|
|
"retrieval - setupAccessTokenPairByPasswordGrantType(x, y)");
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
// setting up access token pair into session context as a string
|
|
|
|
// setting up access token pair into session context as a string
|
|
|
|
session.put(constants["ACCESS_TOKEN_PAIR_IDENTIFIER"], stringify(accessTokenPair));
|
|
|
|
session.put(constants["ACCESS_TOKEN_PAIR_IDENTIFIER"], stringify(accessTokenPair));
|
|
|
@ -108,21 +105,25 @@ var apiWrapperUtil = function () {
|
|
|
|
|
|
|
|
|
|
|
|
publicMethods.setupAccessTokenPairBySamlGrantType = function (username, samlToken) {
|
|
|
|
publicMethods.setupAccessTokenPairBySamlGrantType = function (username, samlToken) {
|
|
|
|
if (!username || !samlToken) {
|
|
|
|
if (!username || !samlToken) {
|
|
|
|
log.error("Could not set up access token pair by saml grant type. " +
|
|
|
|
throw new Error("{/app/modules/token-handlers.js} Could not set up access token pair by " +
|
|
|
|
"Either username, samlToken or both are missing as input.");
|
|
|
|
"saml grant type. Either username, samlToken or both are missing as " +
|
|
|
|
|
|
|
|
"input - setupAccessTokenPairByPasswordGrantType(x, y)");
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
privateMethods.setUpEncodedTenantBasedClientCredentials(username);
|
|
|
|
privateMethods.setUpEncodedTenantBasedClientCredentials(username);
|
|
|
|
var encodedClientCredentials = session.get(constants["ENCODED_CLIENT_KEYS_IDENTIFIER"]);
|
|
|
|
var encodedClientCredentials = session.get(constants["ENCODED_CLIENT_KEYS_IDENTIFIER"]);
|
|
|
|
if (!encodedClientCredentials) {
|
|
|
|
if (!encodedClientCredentials) {
|
|
|
|
log.error("Could not set up access token pair by saml grant type. " +
|
|
|
|
throw new Error("{/app/modules/token-handlers.js} Could not set up access token pair " +
|
|
|
|
"Encoded client credentials are missing.");
|
|
|
|
"by saml grant type. Encoded client credentials are " +
|
|
|
|
|
|
|
|
"missing - setupAccessTokenPairByPasswordGrantType(x, y)");
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
var accessTokenPair;
|
|
|
|
var accessTokenPair;
|
|
|
|
// accessTokenPair will include current access token as well as current refresh token
|
|
|
|
// accessTokenPair will include current access token as well as current refresh token
|
|
|
|
accessTokenPair = tokenUtil.
|
|
|
|
accessTokenPair = tokenUtil.
|
|
|
|
getTokenWithSAMLGrantType(samlToken, encodedClientCredentials, "PRODUCTION");
|
|
|
|
getTokenWithSAMLGrantType(samlToken, encodedClientCredentials, "PRODUCTION");
|
|
|
|
if (!accessTokenPair) {
|
|
|
|
if (!accessTokenPair) {
|
|
|
|
log.error("Could not set up access token pair by password grant type. Error in token retrieval.");
|
|
|
|
throw new Error("{/app/modules/token-handlers.js} Could not set up access token " +
|
|
|
|
|
|
|
|
"pair by password grant type. Error in token " +
|
|
|
|
|
|
|
|
"retrieval - setupAccessTokenPairByPasswordGrantType(x, y)");
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
// setting up access token pair into session context as a string
|
|
|
|
// setting up access token pair into session context as a string
|
|
|
|
session.put(constants["ACCESS_TOKEN_PAIR_IDENTIFIER"], stringify(accessTokenPair));
|
|
|
|
session.put(constants["ACCESS_TOKEN_PAIR_IDENTIFIER"], stringify(accessTokenPair));
|
|
|
@ -131,5 +132,24 @@ var apiWrapperUtil = function () {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
publicMethods.refreshToken = function () {
|
|
|
|
|
|
|
|
var accessTokenPair = parse(session.get(constants["ACCESS_TOKEN_PAIR_IDENTIFIER"]));
|
|
|
|
|
|
|
|
// accessTokenPair includes current access token as well as current refresh token
|
|
|
|
|
|
|
|
var encodedClientCredentials = session.get(constants["ENCODED_CLIENT_KEYS_IDENTIFIER"]);
|
|
|
|
|
|
|
|
if (!accessTokenPair || !encodedClientCredentials) {
|
|
|
|
|
|
|
|
throw new Error("{/app/modules/token-handlers.js} Error in refreshing tokens. Either the access " +
|
|
|
|
|
|
|
|
"token pair, encoded client credentials or both input are not found under " +
|
|
|
|
|
|
|
|
"session context - refreshToken()");
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
var newAccessTokenPair = tokenUtil.refreshToken(accessTokenPair, encodedClientCredentials);
|
|
|
|
|
|
|
|
if (!newAccessTokenPair) {
|
|
|
|
|
|
|
|
log.error("{/app/modules/token-handlers.js} Error in refreshing tokens. Unable to update " +
|
|
|
|
|
|
|
|
"session context with new access token pair - refreshToken()");
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
session.put(constants["ACCESS_TOKEN_PAIR_IDENTIFIER"], stringify(newAccessTokenPair));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
return publicMethods;
|
|
|
|
return publicMethods;
|
|
|
|
}();
|
|
|
|
}();
|