|
|
@ -23,9 +23,9 @@
|
|
|
|
* -----------------------------------------------------
|
|
|
|
* -----------------------------------------------------
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
var handlers = function () {
|
|
|
|
var handlers = function () {
|
|
|
|
var log = new Log("/app/modules/token-handlers.js");
|
|
|
|
var log = new Log("/app/modules/oauth/token-handlers.js");
|
|
|
|
|
|
|
|
|
|
|
|
var tokenUtil = require("/app/modules/util.js")["util"];
|
|
|
|
var tokenUtil = require("/app/modules/oauth/util.js")["util"];
|
|
|
|
var constants = require("/app/modules/constants.js");
|
|
|
|
var constants = require("/app/modules/constants.js");
|
|
|
|
var devicemgtProps = require("/app/conf/reader/main.js")["conf"];
|
|
|
|
var devicemgtProps = require("/app/conf/reader/main.js")["conf"];
|
|
|
|
|
|
|
|
|
|
|
@ -34,26 +34,26 @@ var handlers = function () {
|
|
|
|
|
|
|
|
|
|
|
|
privateMethods.setUpEncodedTenantBasedClientAppCredentials = function (username) {
|
|
|
|
privateMethods.setUpEncodedTenantBasedClientAppCredentials = function (username) {
|
|
|
|
if (!username) {
|
|
|
|
if (!username) {
|
|
|
|
throw new Error("{/app/modules/token-handlers.js} Could not set up encoded tenant based " +
|
|
|
|
throw new Error("{/app/modules/oauth/token-handlers.js} Could not set up encoded tenant based " +
|
|
|
|
"client credentials to session context. No username is found as " +
|
|
|
|
"client credentials to session context. No username is found as " +
|
|
|
|
"input - setUpEncodedTenantBasedClientAppCredentials(x)");
|
|
|
|
"input - setUpEncodedTenantBasedClientAppCredentials(x)");
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
var dynamicClientAppCredentials = tokenUtil.getDynamicClientAppCredentials();
|
|
|
|
var dynamicClientAppCredentials = tokenUtil.getDynamicClientAppCredentials();
|
|
|
|
if (!dynamicClientAppCredentials) {
|
|
|
|
if (!dynamicClientAppCredentials) {
|
|
|
|
throw new Error("{/app/modules/token-handlers.js} Could not set up encoded tenant based " +
|
|
|
|
throw new Error("{/app/modules/oauth/token-handlers.js} Could not set up encoded tenant based " +
|
|
|
|
"client credentials to session context as the server is unable to obtain " +
|
|
|
|
"client credentials to session context as the server is unable to obtain " +
|
|
|
|
"dynamic client credentials - setUpEncodedTenantBasedClientAppCredentials(x)");
|
|
|
|
"dynamic client credentials - setUpEncodedTenantBasedClientAppCredentials(x)");
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
var jwtToken = tokenUtil.getAccessTokenByJWTGrantType(dynamicClientAppCredentials);
|
|
|
|
var jwtToken = tokenUtil.getAccessTokenByJWTGrantType(dynamicClientAppCredentials);
|
|
|
|
if (!jwtToken) {
|
|
|
|
if (!jwtToken) {
|
|
|
|
throw new Error("{/app/modules/token-handlers.js} Could not set up encoded tenant based " +
|
|
|
|
throw new Error("{/app/modules/oauth/token-handlers.js} Could not set up encoded tenant based " +
|
|
|
|
"client credentials to session context as the server is unable to obtain " +
|
|
|
|
"client credentials to session context as the server is unable to obtain " +
|
|
|
|
"a jwt token - setUpEncodedTenantBasedClientAppCredentials(x)");
|
|
|
|
"a jwt token - setUpEncodedTenantBasedClientAppCredentials(x)");
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
var tenantBasedClientCredentials = tokenUtil.
|
|
|
|
var tenantBasedClientCredentials = tokenUtil.
|
|
|
|
getTenantBasedClientAppCredentials(username, jwtToken);
|
|
|
|
getTenantBasedClientAppCredentials(username, jwtToken);
|
|
|
|
if (!tenantBasedClientCredentials) {
|
|
|
|
if (!tenantBasedClientCredentials) {
|
|
|
|
throw new Error("{/app/modules/token-handlers.js} Could not set up encoded tenant " +
|
|
|
|
throw new Error("{/app/modules/oauth/token-handlers.js} Could not set up encoded tenant " +
|
|
|
|
"based client credentials to session context as the server is unable " +
|
|
|
|
"based client credentials to session context as the server is unable " +
|
|
|
|
"to obtain such credentials - setUpEncodedTenantBasedClientAppCredentials(x)");
|
|
|
|
"to obtain such credentials - setUpEncodedTenantBasedClientAppCredentials(x)");
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
@ -70,14 +70,14 @@ var handlers = function () {
|
|
|
|
|
|
|
|
|
|
|
|
publicMethods.setupAccessTokenPairByPasswordGrantType = function (username, password) {
|
|
|
|
publicMethods.setupAccessTokenPairByPasswordGrantType = function (username, password) {
|
|
|
|
if (!username || !password) {
|
|
|
|
if (!username || !password) {
|
|
|
|
throw new Error("{/app/modules/token-handlers.js} Could not set up access token pair by " +
|
|
|
|
throw new Error("{/app/modules/oauth/token-handlers.js} Could not set up access token pair by " +
|
|
|
|
"password grant type. Either username, password or both are missing as " +
|
|
|
|
"password grant type. Either username, password or both are missing as " +
|
|
|
|
"input - setupAccessTokenPairByPasswordGrantType(x, y)");
|
|
|
|
"input - setupAccessTokenPairByPasswordGrantType(x, y)");
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
privateMethods.setUpEncodedTenantBasedClientAppCredentials(username);
|
|
|
|
privateMethods.setUpEncodedTenantBasedClientAppCredentials(username);
|
|
|
|
var encodedClientCredentials = session.get(constants["ENCODED_CLIENT_KEYS_IDENTIFIER"]);
|
|
|
|
var encodedClientCredentials = session.get(constants["ENCODED_CLIENT_KEYS_IDENTIFIER"]);
|
|
|
|
if (!encodedClientCredentials) {
|
|
|
|
if (!encodedClientCredentials) {
|
|
|
|
throw new Error("{/app/modules/token-handlers.js} Could not set up access token pair by " +
|
|
|
|
throw new Error("{/app/modules/oauth/token-handlers.js} Could not set up access token pair by " +
|
|
|
|
"password grant type. Encoded client credentials are " +
|
|
|
|
"password grant type. Encoded client credentials are " +
|
|
|
|
"missing - setupAccessTokenPairByPasswordGrantType(x, y)");
|
|
|
|
"missing - setupAccessTokenPairByPasswordGrantType(x, y)");
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
@ -92,7 +92,7 @@ var handlers = function () {
|
|
|
|
getAccessTokenByPasswordGrantType(username,
|
|
|
|
getAccessTokenByPasswordGrantType(username,
|
|
|
|
encodeURIComponent(password), encodedClientCredentials, stringOfScopes);
|
|
|
|
encodeURIComponent(password), encodedClientCredentials, stringOfScopes);
|
|
|
|
if (!accessTokenPair) {
|
|
|
|
if (!accessTokenPair) {
|
|
|
|
throw new Error("{/app/modules/token-handlers.js} Could not set up access " +
|
|
|
|
throw new Error("{/app/modules/oauth/token-handlers.js} Could not set up access " +
|
|
|
|
"token pair by password grant type. Error in token " +
|
|
|
|
"token pair by password grant type. Error in token " +
|
|
|
|
"retrieval - setupAccessTokenPairByPasswordGrantType(x, y)");
|
|
|
|
"retrieval - setupAccessTokenPairByPasswordGrantType(x, y)");
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
@ -105,14 +105,14 @@ var handlers = function () {
|
|
|
|
|
|
|
|
|
|
|
|
publicMethods.setupAccessTokenPairBySamlGrantType = function (username, samlToken) {
|
|
|
|
publicMethods.setupAccessTokenPairBySamlGrantType = function (username, samlToken) {
|
|
|
|
if (!username || !samlToken) {
|
|
|
|
if (!username || !samlToken) {
|
|
|
|
throw new Error("{/app/modules/token-handlers.js} Could not set up access token pair by " +
|
|
|
|
throw new Error("{/app/modules/oauth/token-handlers.js} Could not set up access token pair by " +
|
|
|
|
"saml grant type. Either username, samlToken or both are missing as " +
|
|
|
|
"saml grant type. Either username, samlToken or both are missing as " +
|
|
|
|
"input - setupAccessTokenPairByPasswordGrantType(x, y)");
|
|
|
|
"input - setupAccessTokenPairByPasswordGrantType(x, y)");
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
privateMethods.setUpEncodedTenantBasedClientAppCredentials(username);
|
|
|
|
privateMethods.setUpEncodedTenantBasedClientAppCredentials(username);
|
|
|
|
var encodedClientCredentials = session.get(constants["ENCODED_CLIENT_KEYS_IDENTIFIER"]);
|
|
|
|
var encodedClientCredentials = session.get(constants["ENCODED_CLIENT_KEYS_IDENTIFIER"]);
|
|
|
|
if (!encodedClientCredentials) {
|
|
|
|
if (!encodedClientCredentials) {
|
|
|
|
throw new Error("{/app/modules/token-handlers.js} Could not set up access token pair " +
|
|
|
|
throw new Error("{/app/modules/oauth/token-handlers.js} Could not set up access token pair " +
|
|
|
|
"by saml grant type. Encoded client credentials are " +
|
|
|
|
"by saml grant type. Encoded client credentials are " +
|
|
|
|
"missing - setupAccessTokenPairByPasswordGrantType(x, y)");
|
|
|
|
"missing - setupAccessTokenPairByPasswordGrantType(x, y)");
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
@ -121,7 +121,7 @@ var handlers = function () {
|
|
|
|
accessTokenPair = tokenUtil.
|
|
|
|
accessTokenPair = tokenUtil.
|
|
|
|
getAccessTokenBySAMLGrantType(samlToken, encodedClientCredentials, "PRODUCTION");
|
|
|
|
getAccessTokenBySAMLGrantType(samlToken, encodedClientCredentials, "PRODUCTION");
|
|
|
|
if (!accessTokenPair) {
|
|
|
|
if (!accessTokenPair) {
|
|
|
|
throw new Error("{/app/modules/token-handlers.js} Could not set up access token " +
|
|
|
|
throw new Error("{/app/modules/oauth/token-handlers.js} Could not set up access token " +
|
|
|
|
"pair by password grant type. Error in token " +
|
|
|
|
"pair by password grant type. Error in token " +
|
|
|
|
"retrieval - setupAccessTokenPairByPasswordGrantType(x, y)");
|
|
|
|
"retrieval - setupAccessTokenPairByPasswordGrantType(x, y)");
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
@ -137,14 +137,14 @@ var handlers = function () {
|
|
|
|
// accessTokenPair includes current access token as well as current refresh token
|
|
|
|
// accessTokenPair includes current access token as well as current refresh token
|
|
|
|
var encodedClientCredentials = session.get(constants["ENCODED_CLIENT_KEYS_IDENTIFIER"]);
|
|
|
|
var encodedClientCredentials = session.get(constants["ENCODED_CLIENT_KEYS_IDENTIFIER"]);
|
|
|
|
if (!accessTokenPair || !encodedClientCredentials) {
|
|
|
|
if (!accessTokenPair || !encodedClientCredentials) {
|
|
|
|
throw new Error("{/app/modules/token-handlers.js} Error in refreshing tokens. Either the access " +
|
|
|
|
throw new Error("{/app/modules/oauth/token-handlers.js} Error in refreshing tokens. Either the access " +
|
|
|
|
"token pair, encoded client credentials or both input are not found under " +
|
|
|
|
"token pair, encoded client credentials or both input are not found under " +
|
|
|
|
"session context - refreshAccessToken()");
|
|
|
|
"session context - refreshAccessToken()");
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
var newTokenPair = tokenUtil.
|
|
|
|
var newTokenPair = tokenUtil.
|
|
|
|
getNewAccessTokenByRefreshToken(accessTokenPair["refreshToken"], encodedClientCredentials);
|
|
|
|
getNewAccessTokenByRefreshToken(accessTokenPair["refreshToken"], encodedClientCredentials);
|
|
|
|
if (!newTokenPair) {
|
|
|
|
if (!newTokenPair) {
|
|
|
|
log.error("{/app/modules/token-handlers.js} Error in refreshing access token. Unable to update " +
|
|
|
|
log.error("{/app/modules/oauth/token-handlers.js} Error in refreshing access token. Unable to update " +
|
|
|
|
"session context with new access token pair - refreshAccessToken()");
|
|
|
|
"session context with new access token pair - refreshAccessToken()");
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
session.put(constants["ACCESS_TOKEN_PAIR_IDENTIFIER"], stringify(newTokenPair));
|
|
|
|
session.put(constants["ACCESS_TOKEN_PAIR_IDENTIFIER"], stringify(newTokenPair));
|