Updating oauth token handlers

revert-70aa11f8
dilanua 8 years ago
parent 1172e759ca
commit 4b39a877ad

@ -23,7 +23,7 @@ var uri = request.getRequestURI();
var uriMatcher = new URIMatcher(String(uri));
var devicemgtProps = require("/app/conf/reader/main.js")["conf"];
var serviceInvokers = require("/app/modules/backend-service-invoker.js")["backendServiceInvoker"];
var serviceInvokers = require("/app/modules/backend-service-invoker.js")["invokers"];
function appendQueryParam (url, queryParam , value) {
if (url.indexOf("?") > 0) {

@ -26,7 +26,7 @@ var deviceModule = require("/app/modules/device.js").deviceModule;
var utility = require("/app/modules/utility.js").utility;
var devicemgtProps = require("/app/conf/reader/main.js")["conf"];
var userModule = require("/app/modules/user.js").userModule;
var serviceInvokers = require("/app/modules/backend-service-invoker.js").backendServiceInvoker;
var serviceInvokers = require("/app/modules/backend-service-invoker.js")["invokers"];
var user = session.get(constants.USER_SESSION_KEY);
var result;

@ -24,7 +24,7 @@ var log = new Log("api/device-api.jag");
var constants = require("/app/modules/constants.js");
var utility = require("/app/modules/utility.js").utility;
var devicemgtProps = require("/app/conf/reader/main.js")["conf"];
var serviceInvokers = require("/app/modules/backend-service-invoker.js").backendServiceInvoker;
var serviceInvokers = require("/app/modules/backend-service-invoker.js")["invokers"];
var user = session.get(constants.USER_SESSION_KEY);
var result;

@ -24,7 +24,7 @@ var uriMatcher = new URIMatcher(String(uri));
var constants = require("/app/modules/constants.js");
var devicemgtProps = require("/app/conf/reader/main.js")["conf"];
var serviceInvokers = require("/app/modules/backend-service-invoker.js")["backendServiceInvoker"];
var serviceInvokers = require("/app/modules/backend-service-invoker.js")["invokers"];
if (uriMatcher.match("/{context}/api/invoker/execute/")) {
var restAPIRequestDetails = request.getContent();

@ -22,7 +22,7 @@ var uriMatcher = new URIMatcher(String(uri));
var log = new Log("api/operation-api.jag");
var serviceInvokers = require("/app/modules/backend-service-invoker.js")["backendServiceInvoker"];
var serviceInvokers = require("/app/modules/backend-service-invoker.js")["invokers"];
var devicemgtProps = require("/app/conf/reader/main.js")["conf"];
if (uriMatcher.match("/{context}/api/operation/paginate")) {

@ -21,7 +21,7 @@
@Deprecated - new
*/
// var apiWrapperUtil = require("/app/modules/api-wrapper-util.js").apiWrapperUtil;
// var apiWrapperUtil = require("/app/modules/api-wrapper-util.js")["handlers"];
// var tokenCookie = apiWrapperUtil.refreshToken();
// print(tokenCookie);
%>

@ -27,7 +27,7 @@ var devicemgtProps = require("/app/conf/reader/main.js")["conf"];
var userModule = require("/app/modules/user.js").userModule;
var deviceModule = require("/app/modules/device.js").deviceModule;
var utility = require("/app/modules/utility.js").utility;
var apiWrapperUtil = require("/app/modules/api-wrapper-util.js").apiWrapperUtil;
var apiWrapperUtil = require("/app/modules/api-wrapper-util.js")["handlers"];
var util = require("/app/modules/util.js").util;
var responseProcessor = require('utils').response;

@ -16,8 +16,15 @@
* 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 constants = require("/app/modules/constants.js");
@ -28,23 +35,27 @@ var apiWrapperUtil = function () {
privateMethods.setUpEncodedTenantBasedClientCredentials = function (username) {
if (!username) {
log.error("Could not set up encoded tenant based client credentials " +
"to session context. No username is found as input.");
throw new Error("{/app/modules/token-handlers.js} Could not set up encoded tenant based " +
"client credentials to session context. No username is found as " +
"input - setUpEncodedTenantBasedClientCredentials(x)");
} else {
var dynamicClientCredentials = tokenUtil.getDynamicClientCredentials();
if (!dynamicClientCredentials) {
log.error("Could not set up encoded tenant based client credentials " +
"to session context as the server is unable to obtain dynamic client credentials.");
throw new Error("{/app/modules/token-handlers.js} Could not set up encoded tenant based " +
"client credentials to session context as the server is unable to obtain " +
"dynamic client credentials - setUpEncodedTenantBasedClientCredentials(x)");
} else {
var jwtToken = tokenUtil.getTokenWithJWTGrantType(dynamicClientCredentials);
if (!jwtToken) {
log.error("Could not set up encoded tenant based client credentials " +
"to session context as the server is unable to obtain a jwt token.");
throw new Error("{/app/modules/token-handlers.js} Could not set up encoded tenant based " +
"client credentials to session context as the server is unable to obtain " +
"a jwt token - setUpEncodedTenantBasedClientCredentials(x)");
} else {
var tenantBasedClientCredentials = tokenUtil.getTenantBasedAppCredentials(username, jwtToken);
if (!tenantBasedClientCredentials) {
log.error("Could not set up encoded tenant based client credentials " +
"to session context as the server is unable to obtain such credentials.");
throw new Error("{/app/modules/token-handlers.js} Could not set up encoded tenant " +
"based client credentials to session context as the server is unable " +
"to obtain such credentials - setUpEncodedTenantBasedClientCredentials(x)");
} else {
var encodedTenantBasedClientCredentials =
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) {
if (!username || !password) {
log.error("Could not set up access token pair by password grant type. " +
"Either username, password or both are missing as input.");
throw new Error("{/app/modules/token-handlers.js} Could not set up access token pair by " +
"password grant type. Either username, password or both are missing as " +
"input - setupAccessTokenPairByPasswordGrantType(x, y)");
} else {
privateMethods.setUpEncodedTenantBasedClientCredentials(username);
var encodedClientCredentials = session.get(constants["ENCODED_CLIENT_KEYS_IDENTIFIER"]);
if (!encodedClientCredentials) {
log.error("Could not set up access token pair by password grant type. " +
"Encoded client credentials are missing.");
throw new Error("{/app/modules/token-handlers.js} Could not set up access token pair by " +
"password grant type. Encoded client credentials are " +
"missing - setupAccessTokenPairByPasswordGrantType(x, y)");
} else {
var accessTokenPair;
// accessTokenPair will include current access token as well as current refresh token
@ -97,7 +92,9 @@ var apiWrapperUtil = function () {
getTokenWithPasswordGrantType(username,
encodeURIComponent(password), encodedClientCredentials, stringOfScopes);
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 {
// setting up access token pair into session context as a string
session.put(constants["ACCESS_TOKEN_PAIR_IDENTIFIER"], stringify(accessTokenPair));
@ -108,21 +105,25 @@ var apiWrapperUtil = function () {
publicMethods.setupAccessTokenPairBySamlGrantType = function (username, samlToken) {
if (!username || !samlToken) {
log.error("Could not set up access token pair by saml grant type. " +
"Either username, samlToken or both are missing as input.");
throw new Error("{/app/modules/token-handlers.js} Could not set up access token pair by " +
"saml grant type. Either username, samlToken or both are missing as " +
"input - setupAccessTokenPairByPasswordGrantType(x, y)");
} else {
privateMethods.setUpEncodedTenantBasedClientCredentials(username);
var encodedClientCredentials = session.get(constants["ENCODED_CLIENT_KEYS_IDENTIFIER"]);
if (!encodedClientCredentials) {
log.error("Could not set up access token pair by saml grant type. " +
"Encoded client credentials are missing.");
throw new Error("{/app/modules/token-handlers.js} Could not set up access token pair " +
"by saml grant type. Encoded client credentials are " +
"missing - setupAccessTokenPairByPasswordGrantType(x, y)");
} else {
var accessTokenPair;
// accessTokenPair will include current access token as well as current refresh token
accessTokenPair = tokenUtil.
getTokenWithSAMLGrantType(samlToken, encodedClientCredentials, "PRODUCTION");
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 {
// setting up access token pair into session context as a string
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;
}();

@ -17,9 +17,13 @@
*/
/**
* This backendServiceInvoker contains the wrappers for back end jaggery calls.
* ----------------------------------------------------------------------------
* Following module includes invokers
* at Jaggery Layer for calling Backend Services, protected by OAuth Tokens.
* These Services include both REST and SOAP Services.
* ----------------------------------------------------------------------------
*/
var backendServiceInvoker = function () {
var invokers = function () {
var log = new Log("/app/modules/backend-service-invoker.js");
var publicXMLHTTPInvokers = {};
@ -34,7 +38,7 @@ var backendServiceInvoker = function () {
var devicemgtProps = require("/app/conf/reader/main.js")["conf"];
var constants = require("/app/modules/constants.js");
var userModule = require("/app/modules/user.js")["userModule"];
var tokenUtil = require("/app/modules/api-wrapper-util.js")["apiWrapperUtil"];
var tokenUtil = require("/app/modules/api-wrapper-util.js")["handlers"];
/**
* This method reads the token pair from the session and return the access token.

@ -23,7 +23,7 @@ deviceModule = function () {
var utility = require('/app/modules/utility.js').utility;
var constants = require('/app/modules/constants.js');
var devicemgtProps = require("/app/conf/reader/main.js")["conf"];
var serviceInvokers = require("/app/modules/backend-service-invoker.js").backendServiceInvoker;
var serviceInvokers = require("/app/modules/backend-service-invoker.js")["invokers"];
var ArrayList = Packages.java.util.ArrayList;
var Properties = Packages.java.util.Properties;

@ -24,7 +24,7 @@ var groupModule = {};
var constants = require('/app/modules/constants.js');
var devicemgtProps = require("/app/conf/reader/main.js")["conf"];
var utility = require("/app/modules/utility.js").utility;
var serviceInvokers = require("/app/modules/backend-service-invoker.js").backendServiceInvoker;
var serviceInvokers = require("/app/modules/backend-service-invoker.js")["invokers"];
var groupServiceEndpoint = devicemgtProps["httpsURL"] + constants.ADMIN_SERVICE_CONTEXT + "/groups";

@ -24,7 +24,7 @@ var onFail;
var constants = require("/app/modules/constants.js");
onSuccess = function (context) {
var utility = require("/app/modules/utility.js").utility;
var apiWrapperUtil = require("/app/modules/api-wrapper-util.js").apiWrapperUtil;
var apiWrapperUtil = require("/app/modules/api-wrapper-util.js")["handlers"];
if (context.input.samlToken) {
apiWrapperUtil.setupAccessTokenPairBySamlGrantType(context.input.username, context.input.samlToken);
} else {

@ -21,7 +21,7 @@ var operationModule = function () {
var utility = require('/app/modules/utility.js').utility;
var constants = require('/app/modules/constants.js');
var devicemgtProps = require("/app/conf/reader/main.js")["conf"];
var serviceInvokers = require("/app/modules/backend-service-invoker.js").backendServiceInvoker;
var serviceInvokers = require("/app/modules/backend-service-invoker.js")["invokers"];;
var publicMethods = {};
var privateMethods = {};

@ -26,7 +26,7 @@ policyModule = function () {
var constants = require('/app/modules/constants.js');
var utility = require("/app/modules/utility.js")["utility"];
var devicemgtProps = require("/app/conf/reader/main.js")["conf"];
var serviceInvokers = require("/app/modules/backend-service-invoker.js").backendServiceInvoker;
var serviceInvokers = require("/app/modules/backend-service-invoker.js")["invokers"];
var publicMethods = {};
var privateMethods = {};

@ -25,7 +25,7 @@ var userModule = function () {
var constants = require("/app/modules/constants.js");
var utility = require("/app/modules/utility.js")["utility"];
var devicemgtProps = require("/app/conf/reader/main.js")["conf"];
var serviceInvokers = require("/app/modules/backend-service-invoker.js")["backendServiceInvoker"];
var serviceInvokers = require("/app/modules/backend-service-invoker.js")["invokers"];
/* Initializing user manager */
var carbon = require("carbon");

Loading…
Cancel
Save