From 87dbb5f9e14dd8b753614d55494ff3232b2b9362 Mon Sep 17 00:00:00 2001 From: dilanua Date: Mon, 25 Jul 2016 15:32:39 +0530 Subject: [PATCH] Refactoring ui token handling functions --- .../devicemgt/app/modules/api-wrapper-util.js | 73 ++++++++++--------- .../app/modules/invoker-request-wrapper.js | 2 +- 2 files changed, 41 insertions(+), 34 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/api-wrapper-util.js b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/api-wrapper-util.js index 55d80f5497..1a2f7be25f 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/api-wrapper-util.js +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/api-wrapper-util.js @@ -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, * Version 2.0 (the "License"); you may not use this file except * in compliance with the License. * 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, * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, + * either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ var apiWrapperUtil = function () { - var module = {}; - var tokenUtil = require("/app/modules/util.js").util; + // var log = new Log("/app/modules/api-wrapper-util.js"); + + var tokenUtil = require("/app/modules/util.js")["util"]; var constants = require("/app/modules/constants.js"); var devicemgtProps = require("/app/conf/reader/main.js")["conf"]; - var log = new Log("/app/modules/api-wrapper-util.js"); - module.refreshToken = function () { - var tokenPair = session.get(constants.ACCESS_TOKEN_PAIR_IDENTIFIER); - var clientData = session.get(constants.ENCODED_CLIENT_KEYS_IDENTIFIER); - tokenPair = tokenUtil.refreshToken(tokenPair, clientData); - session.put(constants.ACCESS_TOKEN_PAIR_IDENTIFIER, tokenPair); + var publicMethods = {}; + + publicMethods.refreshToken = function () { + var accessTokenPair = 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"]); + accessTokenPair = tokenUtil.refreshToken(accessTokenPair, encodedClientCredentials); + session.put(constants["ACCESS_TOKEN_PAIR_IDENTIFIER"], accessTokenPair); }; - module.setupAccessTokenPair = function (type, properties) { - var tokenPair; - var clientData = tokenUtil.getDyanmicCredentials(properties); - var jwtToken = tokenUtil.getTokenWithJWTGrantType(clientData); - clientData = tokenUtil.getTenantBasedAppCredentials(properties.username, jwtToken); - var encodedClientKeys = tokenUtil.encode(clientData.clientId + ":" + clientData.clientSecret); - session.put(constants.ENCODED_CLIENT_KEYS_IDENTIFIER, encodedClientKeys); - if (type == constants.GRANT_TYPE_PASSWORD) { - var scopes = devicemgtProps.scopes; - var scope = ""; - scopes.forEach(function(entry) { - scope += entry + " "; - }); - tokenPair = - tokenUtil.getTokenWithPasswordGrantType(properties.username, encodeURIComponent(properties.password), - encodedClientKeys, scope); - } else if (type == constants.GRANT_TYPE_SAML) { - tokenPair = tokenUtil. - getTokenWithSAMLGrantType(properties.samlToken, encodedClientKeys, "PRODUCTION"); + + publicMethods.setupAccessTokenPair = function (type, properties) { + var dynamicClientCredentials = tokenUtil.getDyanmicCredentials(properties); + var jwtToken = tokenUtil.getTokenWithJWTGrantType(dynamicClientCredentials); + var tenantBasedClientCredentials = tokenUtil.getTenantBasedAppCredentials(properties["username"], jwtToken); + var encodedTenantBasedClientCredentials = tokenUtil. + encode(tenantBasedClientCredentials["clientId"] + ":" + tenantBasedClientCredentials["clientSecret"]); + + session.put(constants["ENCODED_CLIENT_KEYS_IDENTIFIER"], encodedTenantBasedClientCredentials); + + var accessTokenPair; + // accessTokenPair will include current access token as well as current refresh token + if (type == constants["GRANT_TYPE_PASSWORD"]) { + var arrayOfScopes = devicemgtProps["scopes"]; + var stringOfScopes = ""; + arrayOfScopes.forEach(function (entry) { stringOfScopes += entry + " "; }); + accessTokenPair = tokenUtil.getTokenWithPasswordGrantType(properties["username"], + encodeURIComponent(properties["password"]), encodedTenantBasedClientCredentials, stringOfScopes); + } 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; }(); \ No newline at end of file diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/invoker-request-wrapper.js b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/invoker-request-wrapper.js index b4ccc62dc1..d191b1868d 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/invoker-request-wrapper.js +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/invoker-request-wrapper.js @@ -17,7 +17,7 @@ */ /* - @Deprecated + @Deprecated - new */ /**