From 864020f20e070c4c2009fe2340e73665385fbcfa Mon Sep 17 00:00:00 2001 From: Rasika Perera Date: Thu, 12 Jan 2017 22:39:47 +0530 Subject: [PATCH] Adding ability to add deviceType level scopes --- .../app/modules/oauth/token-handlers.js | 17 ++++++----- .../devicemgt/app/modules/utility.js | 28 +++++++++++++++++++ 2 files changed, 38 insertions(+), 7 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/oauth/token-handlers.js b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/oauth/token-handlers.js index 1365fed267..6ac07ce87c 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/oauth/token-handlers.js +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/oauth/token-handlers.js @@ -28,6 +28,7 @@ var handlers = function () { var tokenUtil = require("/app/modules/oauth/token-handler-utils.js")["utils"]; var constants = require("/app/modules/constants.js"); var devicemgtProps = require("/app/modules/conf-reader/main.js")["conf"]; + var utility = require("/app/modules/utility.js")["utility"]; var publicMethods = {}; var privateMethods = {}; @@ -49,6 +50,7 @@ var handlers = function () { var tokenData; // tokenPair will include current access token as well as current refresh token var arrayOfScopes = devicemgtProps["scopes"]; + arrayOfScopes = arrayOfScopes.concat(utility.getDeviceTypesScopesList()); var stringOfScopes = ""; arrayOfScopes.forEach(function (entry) { stringOfScopes += entry + " "; @@ -78,19 +80,20 @@ var handlers = function () { publicMethods["setupTokenPairBySamlGrantType"] = function (username, samlToken) { if (!username || !samlToken) { throw new Error("{/app/modules/oauth/token-handlers.js} Could not set up access token pair by " + - "saml grant type. Either username of logged in user, samlToken or both are missing " + - "as input - setupTokenPairByPasswordGrantType(x, y)"); + "saml grant type. Either username of logged in user, samlToken or both are missing " + + "as input - setupTokenPairBySamlGrantType(x, y)"); } else { privateMethods.setUpEncodedTenantBasedClientAppCredentials(username); privateMethods.setUpEncodedTenantBasedWebSocketClientAppCredentials(username); var encodedClientAppCredentials = session.get(constants["ENCODED_TENANT_BASED_CLIENT_APP_CREDENTIALS"]); if (!encodedClientAppCredentials) { throw new Error("{/app/modules/oauth/token-handlers.js} Could not set up access token pair " + - "by saml grant type. Encoded client credentials are " + - "missing - setupTokenPairByPasswordGrantType(x, y)"); + "by saml grant type. Encoded client credentials are " + + "missing - setupTokenPairBySamlGrantType(x, y)"); } else { var tokenData; var arrayOfScopes = devicemgtProps["scopes"]; + arrayOfScopes = arrayOfScopes.concat(utility.getDeviceTypesScopesList()); var stringOfScopes = ""; arrayOfScopes.forEach(function (entry) { stringOfScopes += entry + " "; @@ -98,11 +101,11 @@ var handlers = function () { // accessTokenPair will include current access token as well as current refresh token tokenData = tokenUtil. - getTokenPairAndScopesBySAMLGrantType(samlToken, encodedClientAppCredentials, stringOfScopes); + getTokenPairAndScopesBySAMLGrantType(samlToken, encodedClientAppCredentials, stringOfScopes); if (!tokenData) { throw new Error("{/app/modules/oauth/token-handlers.js} Could not set up token " + - "pair by password grant type. Error in token " + - "retrieval - setupTokenPairByPasswordGrantType(x, y)"); + "pair by password grant type. Error in token " + + "retrieval - setupTokenPairBySamlGrantType(x, y)"); } else { var tokenPair = {}; tokenPair["accessToken"] = tokenData["accessToken"]; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/utility.js b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/utility.js index 3b96ff0056..49b1a0d5b1 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/utility.js +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/utility.js @@ -125,5 +125,33 @@ utility = function () { return null; }; + publicMethods.getDeviceTypesScopesList = function () { + var dirs = new File("/app/units/").listFiles(); + var scopesList = []; + for (var i = 0; i < dirs.length; i++) { + var unitName = dirs[i].getName(); + if (unitName.match(/^cdmf\.unit\.device\.type\..*\.type-view$/g)) { + var deviceTypeConfigFile = new File("/app/units/" + unitName + "/private/config.json"); + if (deviceTypeConfigFile.isExists()) { + try { + deviceTypeConfigFile.open("r"); + var config = deviceTypeConfigFile.readAll(); + config = config.replace("%https.ip%", server.address("https")); + config = config.replace("%http.ip%", server.address("http")); + var deviceTypeConfig = parse(config); + if (deviceTypeConfig.deviceType && deviceTypeConfig.deviceType.scopes) { + scopesList = scopesList.concat(deviceTypeConfig.deviceType.scopes); + } + } catch (err) { + log.error("Error while reading device config file for `" + deviceType + "`: " + err); + } finally { + deviceTypeConfigFile.close(); + } + } + } + } + return scopesList; + }; + return publicMethods; }();