Refactoring ui token handling functions

revert-70aa11f8
dilanua 9 years ago
parent 7facff4b58
commit 87dbb5f9e1

@ -1,55 +1,62 @@
/* /*
* Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. * Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
* *
* WSO2 Inc. licenses this file to you under the Apache License, * WSO2 Inc. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except * Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. * in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, * Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an * software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* KIND, either express or implied. See the License for the * either express or implied. See the License for the
* specific language governing permissions and limitations * specific language governing permissions and limitations
* under the License. * under the License.
*/ */
var apiWrapperUtil = function () { var apiWrapperUtil = function () {
var module = {}; // var log = new Log("/app/modules/api-wrapper-util.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");
var devicemgtProps = require("/app/conf/reader/main.js")["conf"]; var devicemgtProps = require("/app/conf/reader/main.js")["conf"];
var log = new Log("/app/modules/api-wrapper-util.js");
module.refreshToken = function () { var publicMethods = {};
var tokenPair = session.get(constants.ACCESS_TOKEN_PAIR_IDENTIFIER);
var clientData = session.get(constants.ENCODED_CLIENT_KEYS_IDENTIFIER); publicMethods.refreshToken = function () {
tokenPair = tokenUtil.refreshToken(tokenPair, clientData); var accessTokenPair = session.get(constants["ACCESS_TOKEN_PAIR_IDENTIFIER"]);
session.put(constants.ACCESS_TOKEN_PAIR_IDENTIFIER, tokenPair); // accessTokenPair includes current access token as well as current refresh token
var encodedClientCredentials = session.get(constants["ENCODED_CLIENT_KEYS_IDENTIFIER"]);
accessTokenPair = tokenUtil.refreshToken(accessTokenPair, encodedClientCredentials);
session.put(constants["ACCESS_TOKEN_PAIR_IDENTIFIER"], accessTokenPair);
}; };
module.setupAccessTokenPair = function (type, properties) {
var tokenPair; publicMethods.setupAccessTokenPair = function (type, properties) {
var clientData = tokenUtil.getDyanmicCredentials(properties); var dynamicClientCredentials = tokenUtil.getDyanmicCredentials(properties);
var jwtToken = tokenUtil.getTokenWithJWTGrantType(clientData); var jwtToken = tokenUtil.getTokenWithJWTGrantType(dynamicClientCredentials);
clientData = tokenUtil.getTenantBasedAppCredentials(properties.username, jwtToken); var tenantBasedClientCredentials = tokenUtil.getTenantBasedAppCredentials(properties["username"], jwtToken);
var encodedClientKeys = tokenUtil.encode(clientData.clientId + ":" + clientData.clientSecret); var encodedTenantBasedClientCredentials = tokenUtil.
session.put(constants.ENCODED_CLIENT_KEYS_IDENTIFIER, encodedClientKeys); encode(tenantBasedClientCredentials["clientId"] + ":" + tenantBasedClientCredentials["clientSecret"]);
if (type == constants.GRANT_TYPE_PASSWORD) {
var scopes = devicemgtProps.scopes; session.put(constants["ENCODED_CLIENT_KEYS_IDENTIFIER"], encodedTenantBasedClientCredentials);
var scope = "";
scopes.forEach(function(entry) { var accessTokenPair;
scope += entry + " "; // accessTokenPair will include current access token as well as current refresh token
}); if (type == constants["GRANT_TYPE_PASSWORD"]) {
tokenPair = var arrayOfScopes = devicemgtProps["scopes"];
tokenUtil.getTokenWithPasswordGrantType(properties.username, encodeURIComponent(properties.password), var stringOfScopes = "";
encodedClientKeys, scope); arrayOfScopes.forEach(function (entry) { stringOfScopes += entry + " "; });
} else if (type == constants.GRANT_TYPE_SAML) { accessTokenPair = tokenUtil.getTokenWithPasswordGrantType(properties["username"],
tokenPair = tokenUtil. encodeURIComponent(properties["password"]), encodedTenantBasedClientCredentials, stringOfScopes);
getTokenWithSAMLGrantType(properties.samlToken, encodedClientKeys, "PRODUCTION"); } else if (type == constants["GRANT_TYPE_SAML"]) {
accessTokenPair = tokenUtil.getTokenWithSAMLGrantType(properties["samlToken"],
encodedTenantBasedClientCredentials, "PRODUCTION");
} }
session.put(constants.ACCESS_TOKEN_PAIR_IDENTIFIER, tokenPair);
session.put(constants["ACCESS_TOKEN_PAIR_IDENTIFIER"], accessTokenPair);
}; };
return module;
return publicMethods;
}(); }();
Loading…
Cancel
Save