From c71e5ead007cafce9c9ef40d5bdc2fdd22d0e798 Mon Sep 17 00:00:00 2001 From: ayyoob Date: Fri, 29 Jul 2016 12:05:23 +0530 Subject: [PATCH 01/11] few fixes in JWT Client --- .../jwt/client/extension/util/JWTClientUtil.java | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/components/identity-extensions/org.wso2.carbon.identity.jwt.client.extension/src/main/java/org/wso2/carbon/identity/jwt/client/extension/util/JWTClientUtil.java b/components/identity-extensions/org.wso2.carbon.identity.jwt.client.extension/src/main/java/org/wso2/carbon/identity/jwt/client/extension/util/JWTClientUtil.java index 2786b7a00d..4f62dd6a62 100644 --- a/components/identity-extensions/org.wso2.carbon.identity.jwt.client.extension/src/main/java/org/wso2/carbon/identity/jwt/client/extension/util/JWTClientUtil.java +++ b/components/identity-extensions/org.wso2.carbon.identity.jwt.client.extension/src/main/java/org/wso2/carbon/identity/jwt/client/extension/util/JWTClientUtil.java @@ -116,10 +116,18 @@ public class JWTClientUtil { throws RegistryException, IOException, JWTClientConfigurationException { File configFile = new File(SUPERTENANT_JWT_CONFIG_LOCATION); if (configFile.exists()) { - InputStream propertyStream = configFile.toURI().toURL().openStream(); - Properties properties = new Properties(); - properties.load(propertyStream); - jwtClientManagerService.setDefaultJWTClient(properties); + InputStream propertyStream = null; + try { + propertyStream = configFile.toURI().toURL().openStream(); + Properties properties = new Properties(); + properties.load(propertyStream); + jwtClientManagerService.setDefaultJWTClient(properties); + } finally { + if (propertyStream != null) { + propertyStream.close(); + } + } + } } From 2efdc301774b36d9b80a80d7e759d8a58dac6644 Mon Sep 17 00:00:00 2001 From: dilanua Date: Fri, 29 Jul 2016 14:44:16 +0530 Subject: [PATCH 02/11] Refactoring OAuth utilities at UI Layer --- .../devicemgt/api/data-tables-invoker-api.jag | 2 +- .../jaggeryapps/devicemgt/api/device-api.jag | 2 +- .../jaggeryapps/devicemgt/api/group-api.jag | 2 +- .../jaggeryapps/devicemgt/api/invoker-api.jag | 6 +- .../devicemgt/api/operation-api.jag | 2 +- .../jaggeryapps/devicemgt/api/token.jag | 2 +- .../jaggeryapps/devicemgt/api/user-api.jag | 2 +- .../devicemgt/app/conf/config.json | 153 ++++--- .../devicemgt/app/modules/device.js | 2 +- .../devicemgt/app/modules/group.js | 2 +- .../app/modules/invoker-request-wrapper.js | 2 +- .../devicemgt/app/modules/login.js | 2 +- .../devicemgt/app/modules/operation.js | 2 +- .../devicemgt/app/modules/policy.js | 2 +- ...{api-wrapper-util.js => token-handlers.js} | 22 +- ...js => token-protected-service-invokers.js} | 4 +- .../jaggeryapps/devicemgt/app/modules/user.js | 2 +- .../jaggeryapps/devicemgt/app/modules/util.js | 403 ++++++++++-------- 18 files changed, 347 insertions(+), 267 deletions(-) rename components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/{api-wrapper-util.js => token-handlers.js} (91%) rename components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/{backend-service-invoker.js => token-protected-service-invokers.js} (99%) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/api/data-tables-invoker-api.jag b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/api/data-tables-invoker-api.jag index e52746e9df..59c142ff2c 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/api/data-tables-invoker-api.jag +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/api/data-tables-invoker-api.jag @@ -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")["invokers"]; +var serviceInvokers = require("/app/modules/token-protected-service-invokers.js")["invokers"]; function appendQueryParam (url, queryParam , value) { if (url.indexOf("?") > 0) { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/api/device-api.jag b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/api/device-api.jag index 1a163d76e4..a1d496f174 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/api/device-api.jag +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/api/device-api.jag @@ -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")["invokers"]; +var serviceInvokers = require("/app/modules/token-protected-service-invokers.js")["invokers"]; var user = session.get(constants.USER_SESSION_KEY); var result; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/api/group-api.jag b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/api/group-api.jag index d5f9bb2628..46ac2913f5 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/api/group-api.jag +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/api/group-api.jag @@ -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")["invokers"]; +var serviceInvokers = require("/app/modules/token-protected-service-invokers.js")["invokers"]; var user = session.get(constants.USER_SESSION_KEY); var result; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/api/invoker-api.jag b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/api/invoker-api.jag index 6fa133523e..29da7a76b9 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/api/invoker-api.jag +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/api/invoker-api.jag @@ -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")["invokers"]; +var serviceInvokers = require("/app/modules/token-protected-service-invokers.js")["invokers"]; if (uriMatcher.match("/{context}/api/invoker/execute/")) { var restAPIRequestDetails = request.getContent(); @@ -97,8 +97,8 @@ if (uriMatcher.match("/{context}/api/invoker/execute/")) { break; } } catch (e) { - log.error("Exception occurred while trying to access backend " + - "REST API services from Jaggery API invoker layer", e); + throw new Error("Exception occurred while trying to access " + + "backend REST API services from Jaggery API invoker layer", e); } } %> diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/api/operation-api.jag b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/api/operation-api.jag index 46b4595b66..a08a662588 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/api/operation-api.jag +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/api/operation-api.jag @@ -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")["invokers"]; +var serviceInvokers = require("/app/modules/token-protected-service-invokers.js")["invokers"]; var devicemgtProps = require("/app/conf/reader/main.js")["conf"]; if (uriMatcher.match("/{context}/api/operation/paginate")) { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/api/token.jag b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/api/token.jag index 45210346bb..557d4f5a77 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/api/token.jag +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/api/token.jag @@ -21,7 +21,7 @@ @Deprecated - new */ -// var apiWrapperUtil = require("/app/modules/api-wrapper-util.js")["handlers"]; +// var apiWrapperUtil = require("/app/modules/token-handlers.js")["handlers"]; // var tokenCookie = apiWrapperUtil.refreshToken(); // print(tokenCookie); %> \ No newline at end of file diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/api/user-api.jag b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/api/user-api.jag index cab3d3a32f..a797c2f3fa 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/api/user-api.jag +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/api/user-api.jag @@ -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")["handlers"]; +var apiWrapperUtil = require("/app/modules/token-handlers.js")["handlers"]; var util = require("/app/modules/util.js").util; var responseProcessor = require('utils').response; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/conf/config.json b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/conf/config.json index 8c732e33cd..ad95aa01d6 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/conf/config.json +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/conf/config.json @@ -1,68 +1,89 @@ { - "appContext" : "/devicemgt/", - "webAgentContext" : "/devicemgt-web-agent/", - "apiContext" : "api", - "httpsURL" : "https://localhost:8243", - "httpURL" : "%http.ip%", - "httpsWebURL" : "%https.ip%", - "wssURL" : "%https.ip%", - "wsURL" : "%http.ip%", - "dashboardserverURL" : "%https.ip%", - "enrollmentDir": "/emm-web-agent/enrollment", - "iOSConfigRoot" : "%https.ip%/ios-enrollment/", - "iOSAPIRoot" : "%https.ip%/ios/", - "dynamicClientRegistrationEndPoint" : "https://localhost:8243/dynamic-client-web/register/", - "adminService":"%https.ip%", - "idPServer":"https://localhost:8243", - "callBackUrl":"%https.ip%/devicemgt_admin", - "adminUser":"admin@carbon.super", - "adminRole":"admin", - "usernameLength":30, - "ssoConfiguration" : { - "enabled" : false, - "issuer" : "devicemgt", - "appName" : "devicemgt", - "identityProviderURL" : "%https.ip%/sso/samlsso.jag", - "responseSigningEnabled" : "true", - "keyStorePassword" : "wso2carbon", - "identityAlias" : "wso2carbon", - "keyStoreName" : "/repository/resources/security/wso2carbon.jks" - }, - "userValidationConfig" : { - "usernameJSRegEx" : "^[\\S]{3,30}$", - "usernameRegExViolationErrorMsg" : "Provided username is invalid.", - "usernameHelpMsg" : "Should be in minimum 3 characters long and do not include any whitespaces.", - "firstnameJSRegEx" : "^[\\S]{3,30}$", - "firstnameRegExViolationErrorMsg" : "Provided first name is invalid.", - "lastnameJSRegEx" : "^[\\S]{3,30}$", - "lastnameRegExViolationErrorMsg" : "Provided last name is invalid.", - "emailJSRegEx" : "/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/", - "emailRegExViolationErrorMsg" : "Provided email is invalid." - }, - "groupValidationConfig": { - "groupNameJSRegEx": "^[\\S]{3,30}$", - "groupNameRegExViolationErrorMsg": "Provided group name is invalid.", - "groupNameHelpMsg": "Should be in minimum 3 characters long and should not include any whitespaces." - }, - "roleValidationConfig" : { - "rolenameJSRegEx" : "^[\\S]{3,30}$", - "rolenameRegExViolationErrorMsg" : "Provided role name is invalid.", - "rolenameHelpMsg" : "should be in minimum 3 characters long and do not include any whitespaces." - }, - "generalConfig" : { - "host" : "https://localhost:9443", - "companyName" : "WSO2 Carbon Device Manager", - "browserTitle" : "WSO2 Device Manager", - "copyrightPrefix" : "\u00A9 %date-year%, ", - "copyrightOwner" : "WSO2 Inc.", - "copyrightOwnersSite" : "http://www.wso2.org", - "copyrightSuffix" : " All Rights Reserved." - }, - "scopes" : ["license-add", "license-view", "device-view", "device-info", "device-list", "device-view-own", - "device-modify", "device-search", "operation-install", "operation-view", "operation-modify", "operation-uninstall", - "group-add", "group-share", "group-modify", "group-view", "group-remove", "certificate-modify", "certificate-view", - "configuration-view", "configuration-modify", "policy-view", "policy-modify", "device-notification-view", - "device-notification-modify", "feature-view", "arduino_device", "arduino_user", " android_sense_user", - "virtual_firealarm_user", "raspberrypi_user", "roles-view", "roles-modify", "roles-remove", "roles-add", - "user-password-reset", "user-password-modify", "user-modify", "user-view", "user-invite", "user-remove", "user-add"] + "appContext": "/emm/", + "webAgentContext" : "/emm-web-agent/", + "apiContext": "api", + "httpsURL" : "%https.ip%", + "httpURL" : "%http.ip%", + "httpsWebURL" : "%https.ip%", + "wssURL" : "%https.ip%", + "wsURL" : "%http.ip%", + "dashboardServerURL" : "%https.ip%", + "enrollmentDir": "/emm-web-agent/enrollment", + "iOSConfigRoot" : "%https.ip%/ios-enrollment/", + "iOSAPIRoot" : "%https.ip%/ios/", + "adminService": "%https.ip%", + "oauthProvider": { + "appRegistration": { + "appType": "webapp", + "clientName": "emm", + "owner": "admin@carbon.super", + "dynamicClientAppRegistrationServiceURL": "%https.ip%/dynamic-client-web/register", + "apiManagerClientAppRegistrationServiceURL": "%https.ip%/api-application-registration/register/tenants", + "grantType": "password refresh_token urn:ietf:params:oauth:grant-type:saml2-bearer", + "tokenScope": "admin", + "callbackUrl": "%https.ip%/api/device-mgt/v1.0" + }, + "tokenServiceURL": "%https.ip%/oauth2/token" + }, + "adminUser":"admin@carbon.super", + "adminRole":"admin", + "usernameLength":30, + "pageSize":10, + "ssoConfiguration" : { + "enabled" : false, + "issuer" : "devicemgt", + "appName" : "devicemgt", + "identityProviderURL" : "%https.ip%/sso/samlsso.jag", + "responseSigningEnabled" : "true", + "keyStorePassword" : "wso2carbon", + "identityAlias" : "wso2carbon", + "keyStoreName" : "/repository/resources/security/wso2carbon.jks" + }, + "userValidationConfig" : { + "usernameJSRegEx" : "^[\\S]{3,30}$", + "usernameRegExViolationErrorMsg" : "Provided username is invalid.", + "usernameHelpMsg" : "Should be in minimum 3 characters long and do not include any whitespaces.", + "firstnameJSRegEx" : "^[\\S]{3,30}$", + "firstnameRegExViolationErrorMsg" : "Provided first name is invalid.", + "lastnameJSRegEx" : "^[\\S]{3,30}$", + "lastnameRegExViolationErrorMsg" : "Provided last name is invalid.", + "emailJSRegEx" : "/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/", + "emailRegExViolationErrorMsg" : "Provided email is invalid." + }, + "groupValidationConfig": { + "groupNameJSRegEx": "^[\\S]{3,30}$", + "groupNameRegExViolationErrorMsg": "Provided group name is invalid.", + "groupNameHelpMsg": "Should be in minimum 3 characters long and should not include any whitespaces." + }, + "roleValidationConfig" : { + "roleNameJSRegEx" : "^[\\S]{3,30}$", + "roleNameRegExViolationErrorMsg" : "Provided role name is invalid.", + "roleNameHelpMsg" : "should be in minimum 3 characters long and do not include any whitespaces." + }, + "generalConfig" : { + "host" : "https://localhost:9443", + "companyName" : "WSO2 Carbon Device Manager", + "browserTitle" : "WSO2 Device Manager", + "copyrightPrefix" : "\u00A9 %date-year%, ", + "copyrightOwner" : "WSO2 Inc.", + "copyrightOwnersSite" : "http://www.wso2.org", + "copyrightSuffix" : " All Rights Reserved." + }, + "scopes" : [ + "license-add", "license-view", "device-view", + "device-info", "device-list", "device-view-own", "device-modify", "device-search", + "operation-install", "operation-view", "operation-modify", "operation-uninstall", + "group-add", "group-share", "group-modify", "group-view", "group-remove", + "certificate-modify", "certificate-view", + "configuration-view", "configuration-modify", + "policy-view", "policy-modify", + "device-notification-view", "device-notification-modify", + "feature-view", + "roles-view", "roles-modify", "roles-remove", "roles-add", + "user-password-reset", "user-password-modify", "user-modify", "user-view", "user-invite", "user-remove", "user-add" + ], + "isOAuthEnabled" : true, + "backendRestEndpoints" : { + "deviceMgt" : "/api/device-mgt/v1.0" + } } \ 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/device.js b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/device.js index 464093343d..d6f24bd913 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/device.js +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/device.js @@ -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")["invokers"]; + var serviceInvokers = require("/app/modules/token-protected-service-invokers.js")["invokers"]; var ArrayList = Packages.java.util.ArrayList; var Properties = Packages.java.util.Properties; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/group.js b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/group.js index 61bfe9f4b7..06b8dcbf3d 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/group.js +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/group.js @@ -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")["invokers"]; + var serviceInvokers = require("/app/modules/token-protected-service-invokers.js")["invokers"]; var groupServiceEndpoint = devicemgtProps["httpsURL"] + constants.ADMIN_SERVICE_CONTEXT + "/groups"; 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 d191b1868d..cc88752ada 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 @@ -26,7 +26,7 @@ //var invokerRequestWrapper = function () { // // var constants = require("/modules/constants.js"); -// var serviceInvokers = require("/modules/backend-service-invoker.js").backendServiceInvoker; +// var serviceInvokers = require("/modules/token-protected-service-invokers.js").backendServiceInvoker; // // var publicWrappers = []; // diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/login.js b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/login.js index 429a6b9c1c..d282333644 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/login.js +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/login.js @@ -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")["handlers"]; + var apiWrapperUtil = require("/app/modules/token-handlers.js")["handlers"]; if (context.input.samlToken) { apiWrapperUtil.setupAccessTokenPairBySamlGrantType(context.input.username, context.input.samlToken); } else { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/operation.js b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/operation.js index 981a58f5ce..dac693543b 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/operation.js +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/operation.js @@ -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")["invokers"];; + var serviceInvokers = require("/app/modules/token-protected-service-invokers.js")["invokers"];; var publicMethods = {}; var privateMethods = {}; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/policy.js b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/policy.js index a46ce90b7c..60e53b3ae3 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/policy.js +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/policy.js @@ -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")["invokers"]; + var serviceInvokers = require("/app/modules/token-protected-service-invokers.js")["invokers"]; var publicMethods = {}; var privateMethods = {}; 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/token-handlers.js similarity index 91% rename from components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/api-wrapper-util.js rename to components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/token-handlers.js index 02df5fb3a1..8883cd6eed 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/token-handlers.js @@ -17,11 +17,10 @@ */ /** - * ---------------------------------------------------------------------------- - * Following module includes invokers - * at Jaggery Layer for calling Backend Services, protected by OAuth Tokens. - * These Services include both REST and SOAP Services. - * ---------------------------------------------------------------------------- + * ----------------------------------------------------- + * Following module includes handlers + * at Jaggery Layer for handling OAuth tokens. + * ----------------------------------------------------- */ var handlers = function () { var log = new Log("/app/modules/token-handlers.js"); @@ -39,19 +38,20 @@ var handlers = function () { "client credentials to session context. No username is found as " + "input - setUpEncodedTenantBasedClientCredentials(x)"); } else { - var dynamicClientCredentials = tokenUtil.getDynamicClientCredentials(); + var dynamicClientCredentials = tokenUtil.getDynamicClientAppCredentials(); if (!dynamicClientCredentials) { 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); + var jwtToken = tokenUtil.getAccessTokenByJWTGrantType(dynamicClientCredentials); if (!jwtToken) { 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); + var tenantBasedClientCredentials = tokenUtil. + getTenantBasedClientAppCredentials(username, jwtToken); if (!tenantBasedClientCredentials) { 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 " + @@ -89,7 +89,7 @@ var handlers = function () { stringOfScopes += entry + " "; }); accessTokenPair = tokenUtil. - getTokenWithPasswordGrantType(username, + getAccessTokenByPasswordGrantType(username, encodeURIComponent(password), encodedClientCredentials, stringOfScopes); if (!accessTokenPair) { throw new Error("{/app/modules/token-handlers.js} Could not set up access " + @@ -119,7 +119,7 @@ var handlers = function () { var accessTokenPair; // accessTokenPair will include current access token as well as current refresh token accessTokenPair = tokenUtil. - getTokenWithSAMLGrantType(samlToken, encodedClientCredentials, "PRODUCTION"); + getAccessTokenBySAMLGrantType(samlToken, encodedClientCredentials, "PRODUCTION"); if (!accessTokenPair) { throw new Error("{/app/modules/token-handlers.js} Could not set up access token " + "pair by password grant type. Error in token " + @@ -141,7 +141,7 @@ var handlers = function () { "token pair, encoded client credentials or both input are not found under " + "session context - refreshToken()"); } else { - var newAccessTokenPair = tokenUtil.refreshToken(accessTokenPair, encodedClientCredentials); + var newAccessTokenPair = tokenUtil.refreshToken(accessTokenPair["refreshToken"], 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()"); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/backend-service-invoker.js b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/token-protected-service-invokers.js similarity index 99% rename from components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/backend-service-invoker.js rename to components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/token-protected-service-invokers.js index 5fd0277d5d..f4688327c7 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/backend-service-invoker.js +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/token-protected-service-invokers.js @@ -24,7 +24,7 @@ * ---------------------------------------------------------------------------- */ var invokers = function () { - var log = new Log("/app/modules/backend-service-invoker.js"); + var log = new Log("/app/modules/token-protected-service-invokers.js"); var publicXMLHTTPInvokers = {}; var publicHTTPClientInvokers = {}; @@ -38,7 +38,7 @@ var invokers = 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")["handlers"]; + var tokenUtil = require("/app/modules/token-handlers.js")["handlers"]; /** * This method reads the token pair from the session and return the access token. diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/user.js b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/user.js index b2c840aae8..0cf67c3213 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/user.js +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/user.js @@ -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")["invokers"]; + var serviceInvokers = require("/app/modules/token-protected-service-invokers.js")["invokers"]; /* Initializing user manager */ var carbon = require("carbon"); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/util.js b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/util.js index d1afc795d3..f9b6bb77b7 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/util.js +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/util.js @@ -1,226 +1,285 @@ /* - * 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 util = function () { var log = new Log("/app/modules/util.js"); - var module = {}; + + var privateMethods = {}; + var publicMethods = {}; + var Base64 = Packages.org.apache.commons.codec.binary.Base64; var String = Packages.java.lang.String; - var devicemgtProps = require("/app/conf/reader/main.js")["conf"]; - var carbon = require('carbon'); + var deviceMgtProps = require("/app/conf/reader/main.js")["conf"]; + + var adminUser = deviceMgtProps["adminUser"]; + var constants = require("/app/modules/constants.js"); - var adminUser = devicemgtProps["adminUser"]; - var clientName = devicemgtProps["clientName"]; - - module.getDynamicClientCredentials = function () { - var payload = { - "callbackUrl": devicemgtProps.callBackUrl, - "clientName": clientName, - "tokenScope": "admin", - "owner": adminUser, - "applicationType": "webapp", - "grantType": "password refresh_token urn:ietf:params:oauth:grant-type:saml2-bearer", - "saasApp" :true + var carbon = require("carbon"); + + publicMethods.encode = function (payload) { + return new String(Base64.encodeBase64(new String(payload).getBytes())); + }; + + publicMethods.decode = function (payload) { + return new String(Base64.decodeBase64(new String(payload).getBytes())); + }; + + publicMethods.getDynamicClientAppCredentials = function () { + // setting up dynamic client application properties + var dcAppProperties = { + "applicationType": deviceMgtProps["oauthProvider"]["appRegistration"]["appType"], + "clientName": deviceMgtProps["oauthProvider"]["appRegistration"]["clientName"], + "owner": deviceMgtProps["oauthProvider"]["appRegistration"]["owner"], + "tokenScope": deviceMgtProps["oauthProvider"]["appRegistration"]["tokenScope"], + "grantType": deviceMgtProps["oauthProvider"]["appRegistration"]["grantType"], + "callbackUrl": deviceMgtProps["oauthProvider"]["appRegistration"]["callbackUrl"], + "saasApp" : true }; + // calling dynamic client app registration service endpoint + var requestURL = deviceMgtProps["oauthProvider"]["appRegistration"] + ["dynamicClientAppRegistrationServiceURL"]; + var requestPayload = dcAppProperties; + var xhr = new XMLHttpRequest(); - var tokenEndpoint = devicemgtProps.dynamicClientRegistrationEndPoint; - xhr.open("POST", tokenEndpoint, false); + xhr.open("POST", requestURL, false); xhr.setRequestHeader("Content-Type", "application/json"); - xhr.send(payload); - var clientData = {}; - if (xhr.status == 201) { - var data = parse(xhr.responseText); - clientData.clientId = data.client_id; - clientData.clientSecret = data.client_secret; - - } else if (xhr.status == 400) { - throw "Invalid client meta data"; + xhr.send(stringify(requestPayload)); + + var dynamicClientCredentials = {}; + if (xhr["status"] == 201 && xhr["responseText"]) { + var responsePayload = parse(xhr["responseText"]); + dynamicClientCredentials["clientId"] = responsePayload["client_id"]; + dynamicClientCredentials["clientSecret"] = responsePayload["client_secret"]; + } else if (xhr["status"] == 400) { + log.error("{/app/modules/util.js - getDynamicClientAppCredentials()} " + + "Bad request. Invalid data provided as dynamic client application properties."); + dynamicClientCredentials = null; } else { - throw "Error in obtaining client id and secret"; + log.error("{/app/modules/util.js - getDynamicClientAppCredentials()} " + + "Error in retrieving dynamic client credentials."); + dynamicClientCredentials = null; } - return clientData; + // returning dynamic client credentials + return dynamicClientCredentials; }; - /** - * Encode the payload in Base64 - * @param payload - * @returns {Packages.java.lang.String} - */ - module.encode = function (payload) { - return new String(Base64.encodeBase64(new String(payload).getBytes())); - } - - module.decode = function (payload) { - return new String(Base64.decodeBase64(new String(payload).getBytes())); - } - - /** - * Get an AccessToken pair based on username and password - * @param username - * @param password - * @param clientId - * @param clientSecret - * @param scope - * @returns {{accessToken: "", refreshToken: ""}} - */ - module.getTokenWithPasswordGrantType = function (username, password, encodedClientKeys, scope) { - var xhr = new XMLHttpRequest(); - var tokenEndpoint = devicemgtProps.idPServer; - xhr.open("POST", tokenEndpoint, false); - xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); - xhr.setRequestHeader("Authorization", "Basic " + encodedClientKeys); - xhr.send("grant_type=password&username=" + username + "&password=" + password + "&scope=" + scope); - delete password, delete clientSecret, delete encodedClientKeys; - var tokenPair = {}; - if (xhr.status == 200) { - var data = parse(xhr.responseText); - tokenPair.refreshToken = data.refresh_token; - tokenPair.accessToken = data.access_token; - } else if (xhr.status == 403) { - log.error("Error in obtaining token with Password grant type"); + publicMethods.getAccessTokenByPasswordGrantType = function (username, password, encodedClientCredentials, scopes) { + if (!username || !password || !encodedClientCredentials || !scopes) { + log.error("{/app/modules/util.js} Error in retrieving access token by password " + + "grant type. No username, password, encoded client credentials or scopes are " + + "found - getAccessTokenByPasswordGrantType(a, b, c, d)"); return null; } else { - log.error("Error in obtaining token with Password grant type"); - return null; + // calling oauth provider token service endpoint + var requestURL = deviceMgtProps["oauthProvider"]["tokenServiceURL"]; + var requestPayload = "grant_type=password&username=" + + username + "&password=" + password + "&scope=" + scopes; + + var xhr = new XMLHttpRequest(); + xhr.open("POST", requestURL, false); + xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); + xhr.setRequestHeader("Authorization", "Basic " + encodedClientCredentials); + xhr.send(requestPayload); + + if (xhr["status"] == 200 && xhr["responseText"]) { + var responsePayload = parse(xhr["responseText"]); + var tokenPair = {}; + tokenPair["accessToken"] = responsePayload["access_token"]; + tokenPair["refreshToken"] = responsePayload["refresh_token"]; + return tokenPair; + } else { + log.error("{/app/modules/util.js} Error in retrieving access token by password " + + "grant type - getAccessTokenByPasswordGrantType(a, b, c, d)"); + return null; + } } - return tokenPair; }; - module.getTokenWithSAMLGrantType = function (assertion, clientKeys, scope) { - - var assertionXML = module.decode(assertion) ; - var encodedExtractedAssertion; - var extractedAssertion; - //TODO: make assertion extraction with proper parsing. Since Jaggery XML parser seem to add formatting - //which causes signature verification to fail. - var assertionStartMarker = " Date: Fri, 29 Jul 2016 14:51:37 +0530 Subject: [PATCH 03/11] Removing obsolete UI modules --- .../jaggeryapps/devicemgt/api/token.jag | 27 ----- .../app/modules/invoker-request-wrapper.js | 98 ------------------- .../devicemgt/app/modules/serverAddress.js | 74 -------------- 3 files changed, 199 deletions(-) delete mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/api/token.jag delete mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/invoker-request-wrapper.js delete mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/serverAddress.js diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/api/token.jag b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/api/token.jag deleted file mode 100644 index 557d4f5a77..0000000000 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/api/token.jag +++ /dev/null @@ -1,27 +0,0 @@ -<% -/* - * 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 - * - * 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 - * specific language governing permissions and limitations - * under the License. - */ - -/* - @Deprecated - new - */ - -// var apiWrapperUtil = require("/app/modules/token-handlers.js")["handlers"]; -// var tokenCookie = apiWrapperUtil.refreshToken(); -// print(tokenCookie); -%> \ 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 deleted file mode 100644 index cc88752ada..0000000000 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/invoker-request-wrapper.js +++ /dev/null @@ -1,98 +0,0 @@ -/* - * 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 - * - * 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 - * specific language governing permissions and limitations - * under the License. - */ - -/* - @Deprecated - new - */ - -/** - * This invokerRequestWrapper contains the wrappers for invoker util requests. - */ -//var invokerRequestWrapper = function () { -// -// var constants = require("/modules/constants.js"); -// var serviceInvokers = require("/modules/token-protected-service-invokers.js").backendServiceInvoker; -// -// var publicWrappers = []; -// -// publicWrappers.initiate = function (method, url, payload) { -// switch (method) { -// case constants.HTTP_GET: -// var response = serviceInvokers.XMLHttp.get(url, function (responsePayload) { -// var response = {}; -// response.content = responsePayload["responseContent"]; -// response.status = "success"; -// return response; -// }, -// function (responsePayload) { -// var response = {}; -// response.content = responsePayload; -// response.status = "error"; -// return response; -// }); -// return response; -// break; -// case constants.HTTP_POST: -// var response = serviceInvokers.XMLHttp.post(url, payload, function (responsePayload) { -// var response = {}; -// response.content = responsePayload["responseContent"]; -// response.status = "success"; -// return response; -// }, -// function (responsePayload) { -// var response = {}; -// response.content = responsePayload; -// response.status = "error"; -// return response; -// }); -// return response; -// break; -// case constants.HTTP_PUT: -// var response = serviceInvokers.XMLHttp.put(url, payload, function (responsePayload) { -// var response = {}; -// response.content = responsePayload["responseContent"]; -// response.status = "success"; -// return response; -// }, -// function (responsePayload) { -// var response = {}; -// response.content = responsePayload; -// response.status = "error"; -// return response; -// }); -// return response; -// break; -// case constants.HTTP_DELETE: -// var response = serviceInvokers.XMLHttp.delete(url, function (responsePayload) { -// var response = {}; -// response.content = responsePayload["responseContent"]; -// response.status = "success"; -// return response; -// }, -// function (responsePayload) { -// var response = {}; -// response.content = responsePayload; -// response.status = "error"; -// return response; -// }); -// return response; -// break; -// } -// } -// -//}(); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/serverAddress.js b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/serverAddress.js deleted file mode 100644 index 7f8ec0aa76..0000000000 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/serverAddress.js +++ /dev/null @@ -1,74 +0,0 @@ -/* - * Copyright (c) 2016, 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 - * - * 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 - * specific language governing permissions and limitations - * under the License. - */ - -/* - @Deprecated - new - */ - -//var serverAddress = function () { -// var log = new Log("serverAddress.js"); -// var process = require("process"), -// host = process.getProperty('server.host'), -// ip = process.getProperty('carbon.local.ip'); -// var publicMethods = {}; -// publicMethods.getHTTPSAddress = function () { -// var port = process.getProperty('mgt.transport.https.proxyPort'); -// if (!port) { -// port = process.getProperty('mgt.transport.https.port'); -// } -// if (host === "localhost") { -// return "https://" + ip + ":" + port; -// } else { -// return "https://" + host + ":" + port; -// } -// }; -// publicMethods.getHPPTAddress = function () { -// var port = process.getProperty('mgt.transport.http.proxyPort'); -// if (!port) { -// port = process.getProperty('mgt.transport.http.port'); -// } -// if (host === "localhost") { -// return "http://" + ip + ":" + port; -// } else { -// return "http://" + host + ":" + port; -// } -// }; -// publicMethods.getWSSAddress = function () { -// var port = process.getProperty('mgt.transport.https.proxyPort'); -// if (!port) { -// port = process.getProperty('mgt.transport.https.port'); -// } -// if (host === "localhost") { -// return "wss://" + ip + ":" + port; -// } else { -// return "wss://" + host + ":" + port; -// } -// }; -// publicMethods.getWSAddress = function () { -// var port = process.getProperty('mgt.transport.http.proxyPort'); -// if (!port) { -// port = process.getProperty('mgt.transport.http.port'); -// } -// if (host === "localhost") { -// return "ws://" + ip + ":" + port; -// } else { -// return "ws://" + host + ":" + port; -// } -// }; -// return publicMethods; -//}(); \ No newline at end of file From a4c5aa59517ef410329b5308c6d229d96feb773f Mon Sep 17 00:00:00 2001 From: dilanua Date: Fri, 29 Jul 2016 15:15:02 +0530 Subject: [PATCH 04/11] Refactoring OAuth utilities at UI Layer --- .../devicemgt/app/modules/token-handlers.js | 9 +++++---- .../jaggeryapps/devicemgt/app/modules/util.js | 12 +++++------- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/token-handlers.js b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/token-handlers.js index 8883cd6eed..4aa17fb54b 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/token-handlers.js +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/token-handlers.js @@ -38,13 +38,13 @@ var handlers = function () { "client credentials to session context. No username is found as " + "input - setUpEncodedTenantBasedClientCredentials(x)"); } else { - var dynamicClientCredentials = tokenUtil.getDynamicClientAppCredentials(); - if (!dynamicClientCredentials) { + var dynamicClientAppCredentials = tokenUtil.getDynamicClientAppCredentials(); + if (!dynamicClientAppCredentials) { 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.getAccessTokenByJWTGrantType(dynamicClientCredentials); + var jwtToken = tokenUtil.getAccessTokenByJWTGrantType(dynamicClientAppCredentials); if (!jwtToken) { 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 " + @@ -141,7 +141,8 @@ var handlers = function () { "token pair, encoded client credentials or both input are not found under " + "session context - refreshToken()"); } else { - var newAccessTokenPair = tokenUtil.refreshToken(accessTokenPair["refreshToken"], encodedClientCredentials); + var newAccessTokenPair = tokenUtil. + getNewAccessTokenByRefreshToken(accessTokenPair["refreshToken"], 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()"); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/util.js b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/util.js index f9b6bb77b7..2236c44ddd 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/util.js +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/util.js @@ -24,10 +24,8 @@ var util = function () { var Base64 = Packages.org.apache.commons.codec.binary.Base64; var String = Packages.java.lang.String; - var deviceMgtProps = require("/app/conf/reader/main.js")["conf"]; - - var adminUser = deviceMgtProps["adminUser"]; + var deviceMgtProps = require("/app/conf/reader/main.js")["conf"]; var constants = require("/app/modules/constants.js"); var carbon = require("carbon"); @@ -225,7 +223,7 @@ var util = function () { "username - getTenantBasedClientAppCredentials(x, y)"); return null; } else { - var cachedTenantBasedClientAppCredentials = publicMethods. + var cachedTenantBasedClientAppCredentials = privateMethods. getCachedTenantBasedClientAppCredentials(tenantDomain); if (cachedTenantBasedClientAppCredentials) { return cachedTenantBasedClientAppCredentials; @@ -247,7 +245,7 @@ var util = function () { var tenantBasedClientAppCredentials = {}; tenantBasedClientAppCredentials["clientId"] = responsePayload["client_id"]; tenantBasedClientAppCredentials["clientSecret"] = responsePayload["client_secret"]; - publicMethods. + privateMethods. setCachedTenantBasedClientAppCredentials(tenantDomain, tenantBasedClientAppCredentials); return tenantBasedClientAppCredentials; } else { @@ -260,7 +258,7 @@ var util = function () { } }; - publicMethods.setCachedTenantBasedClientAppCredentials = function (tenantDomain, clientCredentials) { + privateMethods.setCachedTenantBasedClientAppCredentials = function (tenantDomain, clientCredentials) { var cachedTenantBasedClientAppCredentialsMap = application.get(constants["CACHED_CREDENTIALS"]); if (!cachedTenantBasedClientAppCredentialsMap) { cachedTenantBasedClientAppCredentialsMap = {}; @@ -271,7 +269,7 @@ var util = function () { } }; - publicMethods.getCachedTenantBasedClientAppCredentials = function (tenantDomain) { + privateMethods.getCachedTenantBasedClientAppCredentials = function (tenantDomain) { var cachedTenantBasedClientAppCredentialsMap = application.get(constants["CACHED_CREDENTIALS"]); if (!cachedTenantBasedClientAppCredentialsMap || !cachedTenantBasedClientAppCredentialsMap[tenantDomain]) { From 70074e2f3e77a71626c495b0faeaed94e0f0c62c Mon Sep 17 00:00:00 2001 From: dilanua Date: Fri, 29 Jul 2016 15:37:27 +0530 Subject: [PATCH 05/11] Refactoring OAuth utilities at UI Layer --- .../devicemgt/app/modules/token-handlers.js | 14 ++--- .../token-protected-service-invokers.js | 2 +- .../jaggeryapps/devicemgt/app/modules/util.js | 56 +++++++++---------- 3 files changed, 36 insertions(+), 36 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/token-handlers.js b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/token-handlers.js index 4aa17fb54b..5d1097a2ec 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/token-handlers.js +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/token-handlers.js @@ -132,22 +132,22 @@ var handlers = function () { } }; - publicMethods.refreshToken = function () { + publicMethods.refreshAccessToken = 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()"); + "session context - refreshAccessToken()"); } else { - var newAccessTokenPair = tokenUtil. + var newTokenPair = tokenUtil. getNewAccessTokenByRefreshToken(accessTokenPair["refreshToken"], 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()"); + if (!newTokenPair) { + log.error("{/app/modules/token-handlers.js} Error in refreshing access token. Unable to update " + + "session context with new access token pair - refreshAccessToken()"); } else { - session.put(constants["ACCESS_TOKEN_PAIR_IDENTIFIER"], stringify(newAccessTokenPair)); + session.put(constants["ACCESS_TOKEN_PAIR_IDENTIFIER"], stringify(newTokenPair)); } } }; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/token-protected-service-invokers.js b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/token-protected-service-invokers.js index f4688327c7..f1d4b33946 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/token-protected-service-invokers.js +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/token-protected-service-invokers.js @@ -103,7 +103,7 @@ var invokers = function () { if (xmlHttpRequest.status == 401 && (xmlHttpRequest.responseText == TOKEN_EXPIRED || xmlHttpRequest.responseText == TOKEN_INVALID ) && count < 5) { - tokenUtil.refreshToken(); + tokenUtil.refreshAccessToken(); return privateMethods.execute(httpMethod, requestPayload, endpoint, responseCallback, ++count); } else { return responseCallback(xmlHttpRequest); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/util.js b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/util.js index 2236c44ddd..c9328d8cbd 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/util.js +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/util.js @@ -58,28 +58,28 @@ var util = function () { xhr.setRequestHeader("Content-Type", "application/json"); xhr.send(stringify(requestPayload)); - var dynamicClientCredentials = {}; + var dynamicClientAppCredentials = {}; if (xhr["status"] == 201 && xhr["responseText"]) { var responsePayload = parse(xhr["responseText"]); - dynamicClientCredentials["clientId"] = responsePayload["client_id"]; - dynamicClientCredentials["clientSecret"] = responsePayload["client_secret"]; + dynamicClientAppCredentials["clientId"] = responsePayload["client_id"]; + dynamicClientAppCredentials["clientSecret"] = responsePayload["client_secret"]; } else if (xhr["status"] == 400) { log.error("{/app/modules/util.js - getDynamicClientAppCredentials()} " + "Bad request. Invalid data provided as dynamic client application properties."); - dynamicClientCredentials = null; + dynamicClientAppCredentials = null; } else { log.error("{/app/modules/util.js - getDynamicClientAppCredentials()} " + "Error in retrieving dynamic client credentials."); - dynamicClientCredentials = null; + dynamicClientAppCredentials = null; } // returning dynamic client credentials - return dynamicClientCredentials; + return dynamicClientAppCredentials; }; - publicMethods.getAccessTokenByPasswordGrantType = function (username, password, encodedClientCredentials, scopes) { - if (!username || !password || !encodedClientCredentials || !scopes) { + publicMethods.getAccessTokenByPasswordGrantType = function (username, password, encodedClientAppCredentials, scopes) { + if (!username || !password || !encodedClientAppCredentials || !scopes) { log.error("{/app/modules/util.js} Error in retrieving access token by password " + - "grant type. No username, password, encoded client credentials or scopes are " + + "grant type. No username, password, encoded client app credentials or scopes are " + "found - getAccessTokenByPasswordGrantType(a, b, c, d)"); return null; } else { @@ -91,7 +91,7 @@ var util = function () { var xhr = new XMLHttpRequest(); xhr.open("POST", requestURL, false); xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); - xhr.setRequestHeader("Authorization", "Basic " + encodedClientCredentials); + xhr.setRequestHeader("Authorization", "Basic " + encodedClientAppCredentials); xhr.send(requestPayload); if (xhr["status"] == 200 && xhr["responseText"]) { @@ -108,10 +108,10 @@ var util = function () { } }; - publicMethods.getAccessTokenBySAMLGrantType = function (assertion, encodedClientCredentials, scopes) { - if (!assertion || !encodedClientCredentials || !scopes) { + publicMethods.getAccessTokenBySAMLGrantType = function (assertion, encodedClientAppCredentials, scopes) { + if (!assertion || !encodedClientAppCredentials || !scopes) { log.error("{/app/modules/util.js} Error in retrieving access token by saml " + - "grant type. No assertion, encoded client credentials or scopes are " + + "grant type. No assertion, encoded client app credentials or scopes are " + "found - getAccessTokenBySAMLGrantType(x, y, z)"); return null; } else { @@ -143,7 +143,7 @@ var util = function () { var xhr = new XMLHttpRequest(); xhr.open("POST", requestURL, false); xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); - xhr.setRequestHeader("Authorization", "Basic " + encodedClientCredentials); + xhr.setRequestHeader("Authorization", "Basic " + encodedClientAppCredentials); xhr.send(requestPayload); if (xhr["status"] == 200 && xhr["responseText"]) { @@ -161,10 +161,10 @@ var util = function () { } }; - publicMethods.getNewAccessTokenByRefreshToken = function (refreshToken, encodedClientCredentials, scopes) { - if (!refreshToken || !encodedClientCredentials) { + publicMethods.getNewAccessTokenByRefreshToken = function (refreshToken, encodedClientAppCredentials, scopes) { + if (!refreshToken || !encodedClientAppCredentials) { log.error("{/app/modules/util.js} Error in retrieving new access token by current " + - "refresh token. No refresh token or encoded client credentials are " + + "refresh token. No refresh token or encoded client app credentials are " + "found - getNewAccessTokenByRefreshToken(x, y, z)"); return null; } else { @@ -177,7 +177,7 @@ var util = function () { var xhr = new XMLHttpRequest(); xhr.open("POST", requestURL, false); xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); - xhr.setRequestHeader("Authorization", "Basic " + encodedClientCredentials); + xhr.setRequestHeader("Authorization", "Basic " + encodedClientAppCredentials); xhr.send(requestPayload); if (xhr["status"] == 200 && xhr["responseText"]) { @@ -194,10 +194,10 @@ var util = function () { } }; - publicMethods.getAccessTokenByJWTGrantType = function (clientCredentials) { - if (!clientCredentials) { - log.error("{/app/modules/util.js} Error in retrieving new access token by current refresh " + - "token. No client credentials are found as input - getAccessTokenByJWTGrantType(x)"); + publicMethods.getAccessTokenByJWTGrantType = function (clientAppCredentials) { + if (!clientAppCredentials) { + log.error("{/app/modules/util.js} Error in retrieving new access token by current refresh token. " + + "No client app credentials are found as input - getAccessTokenByJWTGrantType(x)"); return null; } else { var JWTClientManagerServicePackagePath = @@ -205,15 +205,15 @@ var util = function () { var JWTClientManagerService = carbon.server.osgiService(JWTClientManagerServicePackagePath); var jwtClient = JWTClientManagerService.getJWTClient(); // returning access token by JWT grant type - return jwtClient.getAccessToken(clientCredentials["clientId"], clientCredentials["clientSecret"], + return jwtClient.getAccessToken(clientAppCredentials["clientId"], clientAppCredentials["clientSecret"], deviceMgtProps["oauthProvider"]["appRegistration"]["owner"], null)["accessToken"]; } }; publicMethods.getTenantBasedClientAppCredentials = function (username, jwtToken) { if (!username || !jwtToken) { - log.error("{/app/modules/util.js} Error in retrieving tenant based client application credentials. " + - "No username or jwt token is found as input - getTenantBasedClientAppCredentials(x, y)"); + log.error("{/app/modules/util.js} Error in retrieving tenant based client app " + + "credentials. No username or jwt token is found as input - getTenantBasedClientAppCredentials(x, y)"); return null; } else { var tenantDomain = carbon.server.tenantDomain({username: username}); @@ -258,14 +258,14 @@ var util = function () { } }; - privateMethods.setCachedTenantBasedClientAppCredentials = function (tenantDomain, clientCredentials) { + privateMethods.setCachedTenantBasedClientAppCredentials = function (tenantDomain, clientAppCredentials) { var cachedTenantBasedClientAppCredentialsMap = application.get(constants["CACHED_CREDENTIALS"]); if (!cachedTenantBasedClientAppCredentialsMap) { cachedTenantBasedClientAppCredentialsMap = {}; - cachedTenantBasedClientAppCredentialsMap[tenantDomain] = clientCredentials; + cachedTenantBasedClientAppCredentialsMap[tenantDomain] = clientAppCredentials; application.put(constants["CACHED_CREDENTIALS"], cachedTenantBasedClientAppCredentialsMap); } else if (!cachedTenantBasedClientAppCredentialsMap[tenantDomain]) { - cachedTenantBasedClientAppCredentialsMap[tenantDomain] = clientCredentials; + cachedTenantBasedClientAppCredentialsMap[tenantDomain] = clientAppCredentials; } }; From b86be8375c67b9a25465e92e890b634e93dc1963 Mon Sep 17 00:00:00 2001 From: dilanua Date: Fri, 29 Jul 2016 15:41:43 +0530 Subject: [PATCH 06/11] Refactoring OAuth utilities at UI Layer --- .../devicemgt/app/modules/token-handlers.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/token-handlers.js b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/token-handlers.js index 5d1097a2ec..097732c059 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/token-handlers.js +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/token-handlers.js @@ -32,30 +32,30 @@ var handlers = function () { var privateMethods = {}; var publicMethods = {}; - privateMethods.setUpEncodedTenantBasedClientCredentials = function (username) { + privateMethods.setUpEncodedTenantBasedClientAppCredentials = function (username) { if (!username) { 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)"); + "input - setUpEncodedTenantBasedClientAppCredentials(x)"); } else { var dynamicClientAppCredentials = tokenUtil.getDynamicClientAppCredentials(); if (!dynamicClientAppCredentials) { 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)"); + "dynamic client credentials - setUpEncodedTenantBasedClientAppCredentials(x)"); } else { var jwtToken = tokenUtil.getAccessTokenByJWTGrantType(dynamicClientAppCredentials); if (!jwtToken) { 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)"); + "a jwt token - setUpEncodedTenantBasedClientAppCredentials(x)"); } else { var tenantBasedClientCredentials = tokenUtil. getTenantBasedClientAppCredentials(username, jwtToken); if (!tenantBasedClientCredentials) { 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)"); + "to obtain such credentials - setUpEncodedTenantBasedClientAppCredentials(x)"); } else { var encodedTenantBasedClientCredentials = tokenUtil.encode(tenantBasedClientCredentials["clientId"] + ":" + @@ -74,7 +74,7 @@ var handlers = function () { "password grant type. Either username, password or both are missing as " + "input - setupAccessTokenPairByPasswordGrantType(x, y)"); } else { - privateMethods.setUpEncodedTenantBasedClientCredentials(username); + privateMethods.setUpEncodedTenantBasedClientAppCredentials(username); var encodedClientCredentials = session.get(constants["ENCODED_CLIENT_KEYS_IDENTIFIER"]); if (!encodedClientCredentials) { throw new Error("{/app/modules/token-handlers.js} Could not set up access token pair by " + @@ -109,7 +109,7 @@ var handlers = function () { "saml grant type. Either username, samlToken or both are missing as " + "input - setupAccessTokenPairByPasswordGrantType(x, y)"); } else { - privateMethods.setUpEncodedTenantBasedClientCredentials(username); + privateMethods.setUpEncodedTenantBasedClientAppCredentials(username); var encodedClientCredentials = session.get(constants["ENCODED_CLIENT_KEYS_IDENTIFIER"]); if (!encodedClientCredentials) { throw new Error("{/app/modules/token-handlers.js} Could not set up access token pair " + From 032b5d129ab3aa85f1127d0035d06c41f6ce2fc7 Mon Sep 17 00:00:00 2001 From: dilanua Date: Fri, 29 Jul 2016 16:48:20 +0530 Subject: [PATCH 07/11] Adding OAUTH module to UI --- .../devicemgt/api/data-tables-invoker-api.jag | 2 +- .../jaggeryapps/devicemgt/api/device-api.jag | 2 +- .../jaggeryapps/devicemgt/api/group-api.jag | 2 +- .../jaggeryapps/devicemgt/api/invoker-api.jag | 2 +- .../devicemgt/api/operation-api.jag | 2 +- .../jaggeryapps/devicemgt/api/user-api.jag | 4 +-- .../devicemgt/app/modules/device.js | 2 +- .../devicemgt/app/modules/group.js | 2 +- .../devicemgt/app/modules/login.js | 2 +- .../app/modules/{ => oauth}/token-handlers.js | 28 +++++++++---------- .../token-protected-service-invokers.js | 4 +-- .../devicemgt/app/modules/{ => oauth}/util.js | 28 +++++++++---------- .../devicemgt/app/modules/operation.js | 2 +- .../devicemgt/app/modules/policy.js | 2 +- .../jaggeryapps/devicemgt/app/modules/user.js | 2 +- 15 files changed, 43 insertions(+), 43 deletions(-) rename components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/{ => oauth}/token-handlers.js (83%) rename components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/{ => oauth}/token-protected-service-invokers.js (99%) rename components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/{ => oauth}/util.js (90%) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/api/data-tables-invoker-api.jag b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/api/data-tables-invoker-api.jag index 59c142ff2c..6777383200 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/api/data-tables-invoker-api.jag +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/api/data-tables-invoker-api.jag @@ -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/token-protected-service-invokers.js")["invokers"]; +var serviceInvokers = require("/app/modules/oauth/token-protected-service-invokers.js")["invokers"]; function appendQueryParam (url, queryParam , value) { if (url.indexOf("?") > 0) { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/api/device-api.jag b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/api/device-api.jag index a1d496f174..74ba1290d4 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/api/device-api.jag +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/api/device-api.jag @@ -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/token-protected-service-invokers.js")["invokers"]; +var serviceInvokers = require("/app/modules/oauth/token-protected-service-invokers.js")["invokers"]; var user = session.get(constants.USER_SESSION_KEY); var result; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/api/group-api.jag b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/api/group-api.jag index 46ac2913f5..cfce1f0409 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/api/group-api.jag +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/api/group-api.jag @@ -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/token-protected-service-invokers.js")["invokers"]; +var serviceInvokers = require("/app/modules/oauth/token-protected-service-invokers.js")["invokers"]; var user = session.get(constants.USER_SESSION_KEY); var result; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/api/invoker-api.jag b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/api/invoker-api.jag index 29da7a76b9..741bf1ed53 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/api/invoker-api.jag +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/api/invoker-api.jag @@ -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/token-protected-service-invokers.js")["invokers"]; +var serviceInvokers = require("/app/modules/oauth/token-protected-service-invokers.js")["invokers"]; if (uriMatcher.match("/{context}/api/invoker/execute/")) { var restAPIRequestDetails = request.getContent(); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/api/operation-api.jag b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/api/operation-api.jag index a08a662588..99fa61a9f9 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/api/operation-api.jag +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/api/operation-api.jag @@ -22,7 +22,7 @@ var uriMatcher = new URIMatcher(String(uri)); var log = new Log("api/operation-api.jag"); -var serviceInvokers = require("/app/modules/token-protected-service-invokers.js")["invokers"]; +var serviceInvokers = require("/app/modules/oauth/token-protected-service-invokers.js")["invokers"]; var devicemgtProps = require("/app/conf/reader/main.js")["conf"]; if (uriMatcher.match("/{context}/api/operation/paginate")) { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/api/user-api.jag b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/api/user-api.jag index a797c2f3fa..24aad8ece9 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/api/user-api.jag +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/api/user-api.jag @@ -27,8 +27,8 @@ 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/token-handlers.js")["handlers"]; -var util = require("/app/modules/util.js").util; +var apiWrapperUtil = require("/app/modules/oauth/token-handlers.js")["handlers"]; +var util = require("/app/modules/oauth/util.js").util; var responseProcessor = require('utils').response; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/device.js b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/device.js index d6f24bd913..085b9b7ea1 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/device.js +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/device.js @@ -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/token-protected-service-invokers.js")["invokers"]; + var serviceInvokers = require("/app/modules/oauth/token-protected-service-invokers.js")["invokers"]; var ArrayList = Packages.java.util.ArrayList; var Properties = Packages.java.util.Properties; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/group.js b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/group.js index 06b8dcbf3d..549d0d8787 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/group.js +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/group.js @@ -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/token-protected-service-invokers.js")["invokers"]; + var serviceInvokers = require("/app/modules/oauth/token-protected-service-invokers.js")["invokers"]; var groupServiceEndpoint = devicemgtProps["httpsURL"] + constants.ADMIN_SERVICE_CONTEXT + "/groups"; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/login.js b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/login.js index d282333644..9c11436e2d 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/login.js +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/login.js @@ -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/token-handlers.js")["handlers"]; + var apiWrapperUtil = require("/app/modules/oauth/token-handlers.js")["handlers"]; if (context.input.samlToken) { apiWrapperUtil.setupAccessTokenPairBySamlGrantType(context.input.username, context.input.samlToken); } else { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/token-handlers.js b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/oauth/token-handlers.js similarity index 83% rename from components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/token-handlers.js rename to components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/oauth/token-handlers.js index 097732c059..030b1cd11d 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/token-handlers.js +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/oauth/token-handlers.js @@ -23,9 +23,9 @@ * ----------------------------------------------------- */ 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 devicemgtProps = require("/app/conf/reader/main.js")["conf"]; @@ -34,26 +34,26 @@ var handlers = function () { privateMethods.setUpEncodedTenantBasedClientAppCredentials = function (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 " + "input - setUpEncodedTenantBasedClientAppCredentials(x)"); } else { var dynamicClientAppCredentials = tokenUtil.getDynamicClientAppCredentials(); 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 " + "dynamic client credentials - setUpEncodedTenantBasedClientAppCredentials(x)"); } else { var jwtToken = tokenUtil.getAccessTokenByJWTGrantType(dynamicClientAppCredentials); 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 " + "a jwt token - setUpEncodedTenantBasedClientAppCredentials(x)"); } else { var tenantBasedClientCredentials = tokenUtil. getTenantBasedClientAppCredentials(username, jwtToken); 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 " + "to obtain such credentials - setUpEncodedTenantBasedClientAppCredentials(x)"); } else { @@ -70,14 +70,14 @@ var handlers = function () { publicMethods.setupAccessTokenPairByPasswordGrantType = function (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 " + "input - setupAccessTokenPairByPasswordGrantType(x, y)"); } else { privateMethods.setUpEncodedTenantBasedClientAppCredentials(username); var encodedClientCredentials = session.get(constants["ENCODED_CLIENT_KEYS_IDENTIFIER"]); 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 " + "missing - setupAccessTokenPairByPasswordGrantType(x, y)"); } else { @@ -92,7 +92,7 @@ var handlers = function () { getAccessTokenByPasswordGrantType(username, encodeURIComponent(password), encodedClientCredentials, stringOfScopes); 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 " + "retrieval - setupAccessTokenPairByPasswordGrantType(x, y)"); } else { @@ -105,14 +105,14 @@ var handlers = function () { publicMethods.setupAccessTokenPairBySamlGrantType = function (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 " + "input - setupAccessTokenPairByPasswordGrantType(x, y)"); } else { privateMethods.setUpEncodedTenantBasedClientAppCredentials(username); var encodedClientCredentials = session.get(constants["ENCODED_CLIENT_KEYS_IDENTIFIER"]); 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 " + "missing - setupAccessTokenPairByPasswordGrantType(x, y)"); } else { @@ -121,7 +121,7 @@ var handlers = function () { accessTokenPair = tokenUtil. getAccessTokenBySAMLGrantType(samlToken, encodedClientCredentials, "PRODUCTION"); 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 " + "retrieval - setupAccessTokenPairByPasswordGrantType(x, y)"); } else { @@ -137,14 +137,14 @@ var handlers = function () { // 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 " + + 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 " + "session context - refreshAccessToken()"); } else { var newTokenPair = tokenUtil. getNewAccessTokenByRefreshToken(accessTokenPair["refreshToken"], encodedClientCredentials); 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()"); } else { session.put(constants["ACCESS_TOKEN_PAIR_IDENTIFIER"], stringify(newTokenPair)); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/token-protected-service-invokers.js b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/oauth/token-protected-service-invokers.js similarity index 99% rename from components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/token-protected-service-invokers.js rename to components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/oauth/token-protected-service-invokers.js index f1d4b33946..0ff97f851e 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/token-protected-service-invokers.js +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/oauth/token-protected-service-invokers.js @@ -24,7 +24,7 @@ * ---------------------------------------------------------------------------- */ var invokers = function () { - var log = new Log("/app/modules/token-protected-service-invokers.js"); + var log = new Log("/app/modules/oauth/token-protected-service-invokers.js"); var publicXMLHTTPInvokers = {}; var publicHTTPClientInvokers = {}; @@ -38,7 +38,7 @@ var invokers = 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/token-handlers.js")["handlers"]; + var tokenUtil = require("/app/modules/oauth/token-handlers.js")["handlers"]; /** * This method reads the token pair from the session and return the access token. diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/util.js b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/oauth/util.js similarity index 90% rename from components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/util.js rename to components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/oauth/util.js index c9328d8cbd..3776857960 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/util.js +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/oauth/util.js @@ -17,7 +17,7 @@ */ var util = function () { - var log = new Log("/app/modules/util.js"); + var log = new Log("/app/modules/oauth/util.js"); var privateMethods = {}; var publicMethods = {}; @@ -64,11 +64,11 @@ var util = function () { dynamicClientAppCredentials["clientId"] = responsePayload["client_id"]; dynamicClientAppCredentials["clientSecret"] = responsePayload["client_secret"]; } else if (xhr["status"] == 400) { - log.error("{/app/modules/util.js - getDynamicClientAppCredentials()} " + + log.error("{/app/modules/oauth/util.js - getDynamicClientAppCredentials()} " + "Bad request. Invalid data provided as dynamic client application properties."); dynamicClientAppCredentials = null; } else { - log.error("{/app/modules/util.js - getDynamicClientAppCredentials()} " + + log.error("{/app/modules/oauth/util.js - getDynamicClientAppCredentials()} " + "Error in retrieving dynamic client credentials."); dynamicClientAppCredentials = null; } @@ -78,7 +78,7 @@ var util = function () { publicMethods.getAccessTokenByPasswordGrantType = function (username, password, encodedClientAppCredentials, scopes) { if (!username || !password || !encodedClientAppCredentials || !scopes) { - log.error("{/app/modules/util.js} Error in retrieving access token by password " + + log.error("{/app/modules/oauth/util.js} Error in retrieving access token by password " + "grant type. No username, password, encoded client app credentials or scopes are " + "found - getAccessTokenByPasswordGrantType(a, b, c, d)"); return null; @@ -101,7 +101,7 @@ var util = function () { tokenPair["refreshToken"] = responsePayload["refresh_token"]; return tokenPair; } else { - log.error("{/app/modules/util.js} Error in retrieving access token by password " + + log.error("{/app/modules/oauth/util.js} Error in retrieving access token by password " + "grant type - getAccessTokenByPasswordGrantType(a, b, c, d)"); return null; } @@ -110,7 +110,7 @@ var util = function () { publicMethods.getAccessTokenBySAMLGrantType = function (assertion, encodedClientAppCredentials, scopes) { if (!assertion || !encodedClientAppCredentials || !scopes) { - log.error("{/app/modules/util.js} Error in retrieving access token by saml " + + log.error("{/app/modules/oauth/util.js} Error in retrieving access token by saml " + "grant type. No assertion, encoded client app credentials or scopes are " + "found - getAccessTokenBySAMLGrantType(x, y, z)"); return null; @@ -127,7 +127,7 @@ var util = function () { var extractedAssertion; if (assertionStartIndex == -1 || assertionEndIndex == -1) { - log.error("{/app/modules/util.js} Error in retrieving access token by saml grant type. " + + log.error("{/app/modules/oauth/util.js} Error in retrieving access token by saml grant type. " + "Issue in assertion format - getAccessTokenBySAMLGrantType(x, y, z)"); return null; } else { @@ -153,7 +153,7 @@ var util = function () { tokenPair["refreshToken"] = responsePayload["refresh_token"]; return tokenPair; } else { - log.error("{/app/modules/util.js} Error in retrieving access token by password " + + log.error("{/app/modules/oauth/util.js} Error in retrieving access token by password " + "grant type - getAccessTokenBySAMLGrantType(x, y, z)"); return null; } @@ -163,7 +163,7 @@ var util = function () { publicMethods.getNewAccessTokenByRefreshToken = function (refreshToken, encodedClientAppCredentials, scopes) { if (!refreshToken || !encodedClientAppCredentials) { - log.error("{/app/modules/util.js} Error in retrieving new access token by current " + + log.error("{/app/modules/oauth/util.js} Error in retrieving new access token by current " + "refresh token. No refresh token or encoded client app credentials are " + "found - getNewAccessTokenByRefreshToken(x, y, z)"); return null; @@ -187,7 +187,7 @@ var util = function () { tokenPair["refreshToken"] = responsePayload["refresh_token"]; return tokenPair; } else { - log.error("{/app/modules/util.js} Error in retrieving new access token by " + + log.error("{/app/modules/oauth/util.js} Error in retrieving new access token by " + "current refresh token - getNewAccessTokenByRefreshToken(x, y, z)"); return null; } @@ -196,7 +196,7 @@ var util = function () { publicMethods.getAccessTokenByJWTGrantType = function (clientAppCredentials) { if (!clientAppCredentials) { - log.error("{/app/modules/util.js} Error in retrieving new access token by current refresh token. " + + log.error("{/app/modules/oauth/util.js} Error in retrieving new access token by current refresh token. " + "No client app credentials are found as input - getAccessTokenByJWTGrantType(x)"); return null; } else { @@ -212,13 +212,13 @@ var util = function () { publicMethods.getTenantBasedClientAppCredentials = function (username, jwtToken) { if (!username || !jwtToken) { - log.error("{/app/modules/util.js} Error in retrieving tenant based client app " + + log.error("{/app/modules/oauth/util.js} Error in retrieving tenant based client app " + "credentials. No username or jwt token is found as input - getTenantBasedClientAppCredentials(x, y)"); return null; } else { var tenantDomain = carbon.server.tenantDomain({username: username}); if (!tenantDomain) { - log.error("{/app/modules/util.js} Error in retrieving tenant based client application " + + log.error("{/app/modules/oauth/util.js} Error in retrieving tenant based client application " + "credentials. Unable to obtain a valid tenant domain for provided " + "username - getTenantBasedClientAppCredentials(x, y)"); return null; @@ -249,7 +249,7 @@ var util = function () { setCachedTenantBasedClientAppCredentials(tenantDomain, tenantBasedClientAppCredentials); return tenantBasedClientAppCredentials; } else { - log.error("{/app/modules/util.js} Error in retrieving tenant based client " + + log.error("{/app/modules/oauth/util.js} Error in retrieving tenant based client " + "application credentials from API Manager - getTenantBasedClientAppCredentials(x, y)"); return null; } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/operation.js b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/operation.js index dac693543b..3b1e11a9c6 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/operation.js +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/operation.js @@ -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/token-protected-service-invokers.js")["invokers"];; + var serviceInvokers = require("/app/modules/oauth/token-protected-service-invokers.js")["invokers"];; var publicMethods = {}; var privateMethods = {}; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/policy.js b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/policy.js index 60e53b3ae3..0fbe5a2116 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/policy.js +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/policy.js @@ -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/token-protected-service-invokers.js")["invokers"]; + var serviceInvokers = require("/app/modules/oauth/token-protected-service-invokers.js")["invokers"]; var publicMethods = {}; var privateMethods = {}; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/user.js b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/user.js index 0cf67c3213..2a28f5fddf 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/user.js +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/user.js @@ -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/token-protected-service-invokers.js")["invokers"]; + var serviceInvokers = require("/app/modules/oauth/token-protected-service-invokers.js")["invokers"]; /* Initializing user manager */ var carbon = require("carbon"); From 7017f6e32b9fab6ecc4bf984bfcfe3f74b36c406 Mon Sep 17 00:00:00 2001 From: dilanua Date: Sat, 30 Jul 2016 13:52:44 +0530 Subject: [PATCH 08/11] Updating OAuth module dependants --- .../main/resources/jaggeryapps/devicemgt/api/user-api.jag | 8 +++----- .../jaggeryapps/devicemgt/app/modules/operation.js | 2 +- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/api/user-api.jag b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/api/user-api.jag index 24aad8ece9..a9665ac04c 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/api/user-api.jag +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/api/user-api.jag @@ -46,10 +46,7 @@ if (uriMatcher.match("/{context}/api/user/authenticate")) { if (log.isDebugEnabled()) { log.debug("User Logged In : " + user); } - apiWrapperUtil.setupAccessTokenPair("password", { - "username": username, - "password": password - }); + apiWrapperUtil.setupAccessTokenPairByPasswordGrantType(username, password); }, function () { response = responseProcessor.buildSuccessResponse(response, 200, {'sessionId': session.getId()}); }); @@ -68,7 +65,8 @@ if (uriMatcher.match("/{context}/api/user/authenticate")) { if (log.isDebugEnabled()) { log.debug("User Logged In : " + user); } - apiWrapperUtil.setupAccessTokenPair("password", {"username": username, "password": password}); + + apiWrapperUtil.setupAccessTokenPairByPasswordGrantType(username, password); var permissions = userModule.getUIPermissions(); if (permissions.VIEW_DASHBOARD) { response.sendRedirect(constants.WEB_APP_CONTEXT); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/operation.js b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/operation.js index 3b1e11a9c6..98890b9a19 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/operation.js +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/operation.js @@ -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/oauth/token-protected-service-invokers.js")["invokers"];; + var serviceInvokers = require("/app/modules/oauth/token-protected-service-invokers.js")["invokers"]; var publicMethods = {}; var privateMethods = {}; From 020f35cb4e9d3abe0b70797ff9cf345ca5854918 Mon Sep 17 00:00:00 2001 From: dilanua Date: Sat, 30 Jul 2016 19:47:34 +0530 Subject: [PATCH 09/11] Updating OAuth UI Module --- .../jaggeryapps/devicemgt/api/user-api.jag | 4 +- .../devicemgt/app/modules/constants.js | 4 +- .../devicemgt/app/modules/login.js | 4 +- .../app/modules/oauth/token-handlers.js | 153 +++++++++--------- .../oauth/token-protected-service-invokers.js | 24 +-- 5 files changed, 95 insertions(+), 94 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/api/user-api.jag b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/api/user-api.jag index a9665ac04c..1d04e761af 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/api/user-api.jag +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/api/user-api.jag @@ -46,7 +46,7 @@ if (uriMatcher.match("/{context}/api/user/authenticate")) { if (log.isDebugEnabled()) { log.debug("User Logged In : " + user); } - apiWrapperUtil.setupAccessTokenPairByPasswordGrantType(username, password); + apiWrapperUtil.setupTokenPairByPasswordGrantType(username, password); }, function () { response = responseProcessor.buildSuccessResponse(response, 200, {'sessionId': session.getId()}); }); @@ -66,7 +66,7 @@ if (uriMatcher.match("/{context}/api/user/authenticate")) { log.debug("User Logged In : " + user); } - apiWrapperUtil.setupAccessTokenPairByPasswordGrantType(username, password); + apiWrapperUtil.setupTokenPairByPasswordGrantType(username, password); var permissions = userModule.getUIPermissions(); if (permissions.VIEW_DASHBOARD) { response.sendRedirect(constants.WEB_APP_CONTEXT); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/constants.js b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/constants.js index 890a7e500e..f1ebedd948 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/constants.js +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/constants.js @@ -59,8 +59,8 @@ var USER_STORE_CONFIG_ADMIN_SERVICE_END_POINT = var SOAP_VERSION = 1.2; var WEB_SERVICE_ADDRESSING_VERSION = 1.0; -var ACCESS_TOKEN_PAIR_IDENTIFIER = "accessTokenPair"; -var ENCODED_CLIENT_KEYS_IDENTIFIER = "encodedClientKey"; +var TOKEN_PAIR = "tokenPair"; +var ENCODED_TENANT_BASED_CLIENT_APP_CREDENTIALS = "encodedTenantBasedClientAppCredentials"; var CONTENT_TYPE_IDENTIFIER = "Content-Type"; var CONTENT_DISPOSITION_IDENTIFIER = "Content-Disposition"; var APPLICATION_JSON = "application/json"; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/login.js b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/login.js index 9c11436e2d..c383e1e817 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/login.js +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/login.js @@ -26,9 +26,9 @@ var onFail; var utility = require("/app/modules/utility.js").utility; var apiWrapperUtil = require("/app/modules/oauth/token-handlers.js")["handlers"]; if (context.input.samlToken) { - apiWrapperUtil.setupAccessTokenPairBySamlGrantType(context.input.username, context.input.samlToken); + apiWrapperUtil.setupTokenPairBySamlGrantType(context.input.username, context.input.samlToken); } else { - apiWrapperUtil.setupAccessTokenPairByPasswordGrantType(context.input.username, context.input.password); + apiWrapperUtil.setupTokenPairByPasswordGrantType(context.input.username, context.input.password); } var devicemgtProps = require("/app/conf/reader/main.js")["conf"]; var carbonServer = require("carbon").server; 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 030b1cd11d..867c1db8da 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 @@ -29,125 +29,126 @@ var handlers = function () { var constants = require("/app/modules/constants.js"); var devicemgtProps = require("/app/conf/reader/main.js")["conf"]; - var privateMethods = {}; var publicMethods = {}; + var privateMethods = {}; - privateMethods.setUpEncodedTenantBasedClientAppCredentials = function (username) { - if (!username) { - 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 " + - "input - setUpEncodedTenantBasedClientAppCredentials(x)"); - } else { - var dynamicClientAppCredentials = tokenUtil.getDynamicClientAppCredentials(); - if (!dynamicClientAppCredentials) { - 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 " + - "dynamic client credentials - setUpEncodedTenantBasedClientAppCredentials(x)"); - } else { - var jwtToken = tokenUtil.getAccessTokenByJWTGrantType(dynamicClientAppCredentials); - if (!jwtToken) { - 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 " + - "a jwt token - setUpEncodedTenantBasedClientAppCredentials(x)"); - } else { - var tenantBasedClientCredentials = tokenUtil. - getTenantBasedClientAppCredentials(username, jwtToken); - if (!tenantBasedClientCredentials) { - 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 such credentials - setUpEncodedTenantBasedClientAppCredentials(x)"); - } else { - var encodedTenantBasedClientCredentials = - tokenUtil.encode(tenantBasedClientCredentials["clientId"] + ":" + - tenantBasedClientCredentials["clientSecret"]); - // setting up encoded tenant based client credentials to session context. - session.put(constants["ENCODED_CLIENT_KEYS_IDENTIFIER"], encodedTenantBasedClientCredentials); - } - } - } - } - }; - - publicMethods.setupAccessTokenPairByPasswordGrantType = function (username, password) { + publicMethods.setupTokenPairByPasswordGrantType = function (username, password) { if (!username || !password) { 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 " + - "input - setupAccessTokenPairByPasswordGrantType(x, y)"); + "password grant type. Either username of logged in user, password or both are missing " + + "as input - setupTokenPairByPasswordGrantType(x, y)"); } else { privateMethods.setUpEncodedTenantBasedClientAppCredentials(username); - var encodedClientCredentials = session.get(constants["ENCODED_CLIENT_KEYS_IDENTIFIER"]); - if (!encodedClientCredentials) { + 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 " + "password grant type. Encoded client credentials are " + - "missing - setupAccessTokenPairByPasswordGrantType(x, y)"); + "missing - setupTokenPairByPasswordGrantType(x, y)"); } else { - var accessTokenPair; - // accessTokenPair will include current access token as well as current refresh token + var tokenPair; + // tokenPair will include current access token as well as current refresh token var arrayOfScopes = devicemgtProps["scopes"]; var stringOfScopes = ""; arrayOfScopes.forEach(function (entry) { stringOfScopes += entry + " "; }); - accessTokenPair = tokenUtil. + tokenPair = tokenUtil. getAccessTokenByPasswordGrantType(username, - encodeURIComponent(password), encodedClientCredentials, stringOfScopes); - if (!accessTokenPair) { - throw new Error("{/app/modules/oauth/token-handlers.js} Could not set up access " + + encodeURIComponent(password), encodedClientAppCredentials, stringOfScopes); + if (!tokenPair) { + throw new Error("{/app/modules/oauth/token-handlers.js} Could not set up " + "token pair by password grant type. Error in token " + - "retrieval - setupAccessTokenPairByPasswordGrantType(x, y)"); + "retrieval - setupTokenPairByPasswordGrantType(x, y)"); } else { // setting up access token pair into session context as a string - session.put(constants["ACCESS_TOKEN_PAIR_IDENTIFIER"], stringify(accessTokenPair)); + session.put(constants["TOKEN_PAIR"], stringify(tokenPair)); } } } }; - publicMethods.setupAccessTokenPairBySamlGrantType = function (username, samlToken) { + 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, samlToken or both are missing as " + - "input - setupAccessTokenPairByPasswordGrantType(x, y)"); + "saml grant type. Either username of logged in user, samlToken or both are missing " + + "as input - setupTokenPairByPasswordGrantType(x, y)"); } else { privateMethods.setUpEncodedTenantBasedClientAppCredentials(username); - var encodedClientCredentials = session.get(constants["ENCODED_CLIENT_KEYS_IDENTIFIER"]); - if (!encodedClientCredentials) { + 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 - setupAccessTokenPairByPasswordGrantType(x, y)"); + "missing - setupTokenPairByPasswordGrantType(x, y)"); } else { - var accessTokenPair; + var tokenPair; // accessTokenPair will include current access token as well as current refresh token - accessTokenPair = tokenUtil. - getAccessTokenBySAMLGrantType(samlToken, encodedClientCredentials, "PRODUCTION"); - if (!accessTokenPair) { - throw new Error("{/app/modules/oauth/token-handlers.js} Could not set up access token " + + tokenPair = tokenUtil. + getAccessTokenBySAMLGrantType(samlToken, encodedClientAppCredentials, "PRODUCTION"); + if (!tokenPair) { + throw new Error("{/app/modules/oauth/token-handlers.js} Could not set up token " + "pair by password grant type. Error in token " + - "retrieval - setupAccessTokenPairByPasswordGrantType(x, y)"); + "retrieval - setupTokenPairByPasswordGrantType(x, y)"); } else { // setting up access token pair into session context as a string - session.put(constants["ACCESS_TOKEN_PAIR_IDENTIFIER"], stringify(accessTokenPair)); + session.put(constants["TOKEN_PAIR"], stringify(tokenPair)); } } } }; - publicMethods.refreshAccessToken = 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/oauth/token-handlers.js} Error in refreshing tokens. Either the access " + - "token pair, encoded client credentials or both input are not found under " + - "session context - refreshAccessToken()"); + publicMethods.refreshTokenPair = function () { + var currentTokenPair = parse(session.get(constants["TOKEN_PAIR"])); + // currentTokenPair includes current access token as well as current refresh token + var encodedClientAppCredentials = session.get(constants["ENCODED_TENANT_BASED_CLIENT_APP_CREDENTIALS"]); + if (!currentTokenPair || !encodedClientAppCredentials) { + throw new Error("{/app/modules/oauth/token-handlers.js} Error in refreshing tokens. Either the " + + "token pair, encoded client app credentials or both input are not found under " + + "session context - refreshTokenPair()"); } else { var newTokenPair = tokenUtil. - getNewAccessTokenByRefreshToken(accessTokenPair["refreshToken"], encodedClientCredentials); + getNewAccessTokenByRefreshToken(currentTokenPair["refreshToken"], encodedClientAppCredentials); if (!newTokenPair) { - log.error("{/app/modules/oauth/token-handlers.js} Error in refreshing access token. Unable to update " + - "session context with new access token pair - refreshAccessToken()"); + log.error("{/app/modules/oauth/token-handlers.js} Error in refreshing token pair. " + + "Unable to update session context with new access token pair - refreshTokenPair()"); + } else { + session.put(constants["TOKEN_PAIR"], stringify(newTokenPair)); + } + } + }; + + privateMethods.setUpEncodedTenantBasedClientAppCredentials = function (username) { + if (!username) { + throw new Error("{/app/modules/oauth/token-handlers.js} Could not set up encoded tenant based " + + "client credentials to session context. No username of logged in user is found as " + + "input - setUpEncodedTenantBasedClientAppCredentials(x)"); + } else { + var dynamicClientAppCredentials = tokenUtil.getDynamicClientAppCredentials(); + if (!dynamicClientAppCredentials) { + 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 " + + "dynamic client credentials - setUpEncodedTenantBasedClientAppCredentials(x)"); } else { - session.put(constants["ACCESS_TOKEN_PAIR_IDENTIFIER"], stringify(newTokenPair)); + var jwtToken = tokenUtil.getAccessTokenByJWTGrantType(dynamicClientAppCredentials); + if (!jwtToken) { + 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 " + + "a jwt token - setUpEncodedTenantBasedClientAppCredentials(x)"); + } else { + var tenantBasedClientAppCredentials = tokenUtil. + getTenantBasedClientAppCredentials(username, jwtToken); + if (!tenantBasedClientAppCredentials) { + 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 such credentials - setUpEncodedTenantBasedClientAppCredentials(x)"); + } else { + var encodedTenantBasedClientAppCredentials = + tokenUtil.encode(tenantBasedClientAppCredentials["clientId"] + ":" + + tenantBasedClientAppCredentials["clientSecret"]); + // setting up encoded tenant based client credentials to session context. + session.put(constants["ENCODED_TENANT_BASED_CLIENT_APP_CREDENTIALS"], + encodedTenantBasedClientAppCredentials); + } + } } } }; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/oauth/token-protected-service-invokers.js b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/oauth/token-protected-service-invokers.js index 0ff97f851e..2b9a3d09c9 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/oauth/token-protected-service-invokers.js +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/oauth/token-protected-service-invokers.js @@ -42,10 +42,10 @@ var invokers = function () { /** * This method reads the token pair from the session and return the access token. - * If the token pair s not set in the session this will send a redirect to the login page. + * If the token pair is not set in the session, this will return null. */ privateMethods.getAccessToken = function () { - var tokenPair = parse(session.get(constants["ACCESS_TOKEN_PAIR_IDENTIFIER"])); + var tokenPair = parse(session.get(constants["TOKEN_PAIR"])); if (tokenPair) { return tokenPair["accessToken"]; } else { @@ -103,7 +103,7 @@ var invokers = function () { if (xmlHttpRequest.status == 401 && (xmlHttpRequest.responseText == TOKEN_EXPIRED || xmlHttpRequest.responseText == TOKEN_INVALID ) && count < 5) { - tokenUtil.refreshAccessToken(); + tokenUtil.refreshTokenPair(); return privateMethods.execute(httpMethod, requestPayload, endpoint, responseCallback, ++count); } else { return responseCallback(xmlHttpRequest); @@ -122,7 +122,7 @@ var invokers = function () { }; /** - * This method invokes return initiateXMLHttpRequest for get calls + * This method invokes return initiateXMLHttpRequest for get calls. * @param endpoint Backend REST API url. * @param responseCallback a function to be called with response retrieved. */ @@ -132,7 +132,7 @@ var invokers = function () { }; /** - * This method invokes return initiateXMLHttpRequest for post calls + * This method invokes return initiateXMLHttpRequest for post calls. * @param endpoint Backend REST API url. * @param requestPayload payload/data if exists which is needed to be send. * @param responseCallback a function to be called with response retrieved. @@ -142,7 +142,7 @@ var invokers = function () { }; /** - * This method invokes return initiateXMLHttpRequest for put calls + * This method invokes return initiateXMLHttpRequest for put calls. * @param endpoint Backend REST API url. * @param requestPayload payload/data if exists which is needed to be send. * @param responseCallback a function to be called with response retrieved. @@ -152,7 +152,7 @@ var invokers = function () { }; /** - * This method invokes return initiateXMLHttpRequest for delete calls + * This method invokes return initiateXMLHttpRequest for delete calls. * @param endpoint Backend REST API url. * @param responseCallback a function to be called with response retrieved. */ @@ -214,7 +214,7 @@ var invokers = function () { }; /** - * This method invokes return initiateWSRequest for soap calls + * This method invokes return initiateWSRequest for soap calls. * @param action describes particular soap action. * @param requestPayload SOAP request payload which is needed to be send. * @param endpoint service end point to be triggered. @@ -303,7 +303,7 @@ var invokers = function () { }; /** - * This method invokes return initiateHTTPClientRequest for get calls + * This method invokes return initiateHTTPClientRequest for get calls. * @param url target url. * @param successCallback a function to be called if the respond if successful. * @param errorCallback a function to be called if en error is reserved. @@ -315,7 +315,7 @@ var invokers = function () { }; /** - * This method invokes return initiateHTTPClientRequest for post calls + * This method invokes return initiateHTTPClientRequest for post calls. * @param url target url. * @param payload payload/data which need to be send. * @param successCallback a function to be called if the respond if successful. @@ -327,7 +327,7 @@ var invokers = function () { }; /** - * This method invokes return initiateHTTPClientRequest for put calls + * This method invokes return initiateHTTPClientRequest for put calls. * @param url target url. * @param payload payload/data which need to be send. * @param successCallback a function to be called if the respond if successful. @@ -339,7 +339,7 @@ var invokers = function () { }; /** - * This method invokes return initiateHTTPClientRequest for delete calls + * This method invokes return initiateHTTPClientRequest for delete calls. * @param url target url. * @param successCallback a function to be called if the respond if successful. * @param errorCallback a function to be called if en error is reserved. From bc4192280c57fd094f1eb38e5ec5551ce81cc5b8 Mon Sep 17 00:00:00 2001 From: dilanua Date: Sat, 30 Jul 2016 20:38:32 +0530 Subject: [PATCH 10/11] Updating OAuth UI Module --- .../jaggeryapps/devicemgt/api/user-api.jag | 2 +- .../oauth/{util.js => token-handler-utils.js} | 77 ++++++++++--------- .../app/modules/oauth/token-handlers.js | 10 +-- 3 files changed, 46 insertions(+), 43 deletions(-) rename components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/oauth/{util.js => token-handler-utils.js} (78%) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/api/user-api.jag b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/api/user-api.jag index 1d04e761af..b8f495aa7c 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/api/user-api.jag +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/api/user-api.jag @@ -28,7 +28,7 @@ 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/oauth/token-handlers.js")["handlers"]; -var util = require("/app/modules/oauth/util.js").util; +var util = require("/app/modules/oauth/token-handler-utils.js")["utils"]; var responseProcessor = require('utils').response; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/oauth/util.js b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/oauth/token-handler-utils.js similarity index 78% rename from components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/oauth/util.js rename to components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/oauth/token-handler-utils.js index 3776857960..1ed0291e37 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/oauth/util.js +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/oauth/token-handler-utils.js @@ -16,18 +16,18 @@ * under the License. */ -var util = function () { - var log = new Log("/app/modules/oauth/util.js"); +var utils = function () { + var log = new Log("/app/modules/oauth/token-handler-utils.js"); - var privateMethods = {}; - var publicMethods = {}; + var deviceMgtProps = require("/app/conf/reader/main.js")["conf"]; + var constants = require("/app/modules/constants.js"); + var carbon = require("carbon"); var Base64 = Packages.org.apache.commons.codec.binary.Base64; var String = Packages.java.lang.String; - var deviceMgtProps = require("/app/conf/reader/main.js")["conf"]; - var constants = require("/app/modules/constants.js"); - var carbon = require("carbon"); + var publicMethods = {}; + var privateMethods = {}; publicMethods.encode = function (payload) { return new String(Base64.encodeBase64(new String(payload).getBytes())); @@ -64,11 +64,11 @@ var util = function () { dynamicClientAppCredentials["clientId"] = responsePayload["client_id"]; dynamicClientAppCredentials["clientSecret"] = responsePayload["client_secret"]; } else if (xhr["status"] == 400) { - log.error("{/app/modules/oauth/util.js - getDynamicClientAppCredentials()} " + + log.error("{/app/modules/oauth/token-handler-utils.js - getDynamicClientAppCredentials()} " + "Bad request. Invalid data provided as dynamic client application properties."); dynamicClientAppCredentials = null; } else { - log.error("{/app/modules/oauth/util.js - getDynamicClientAppCredentials()} " + + log.error("{/app/modules/oauth/token-handler-utils.js - getDynamicClientAppCredentials()} " + "Error in retrieving dynamic client credentials."); dynamicClientAppCredentials = null; } @@ -76,11 +76,11 @@ var util = function () { return dynamicClientAppCredentials; }; - publicMethods.getAccessTokenByPasswordGrantType = function (username, password, encodedClientAppCredentials, scopes) { + publicMethods.getTokenPairByPasswordGrantType = function (username, password, encodedClientAppCredentials, scopes) { if (!username || !password || !encodedClientAppCredentials || !scopes) { - log.error("{/app/modules/oauth/util.js} Error in retrieving access token by password " + + log.error("{/app/modules/oauth/token-handler-utils.js} Error in retrieving access token by password " + "grant type. No username, password, encoded client app credentials or scopes are " + - "found - getAccessTokenByPasswordGrantType(a, b, c, d)"); + "found - getTokenPairByPasswordGrantType(a, b, c, d)"); return null; } else { // calling oauth provider token service endpoint @@ -101,18 +101,18 @@ var util = function () { tokenPair["refreshToken"] = responsePayload["refresh_token"]; return tokenPair; } else { - log.error("{/app/modules/oauth/util.js} Error in retrieving access token by password " + - "grant type - getAccessTokenByPasswordGrantType(a, b, c, d)"); + log.error("{/app/modules/oauth/token-handler-utils.js} Error in retrieving access token " + + "by password grant type - getTokenPairByPasswordGrantType(a, b, c, d)"); return null; } } }; - publicMethods.getAccessTokenBySAMLGrantType = function (assertion, encodedClientAppCredentials, scopes) { + publicMethods.getTokenPairBySAMLGrantType = function (assertion, encodedClientAppCredentials, scopes) { if (!assertion || !encodedClientAppCredentials || !scopes) { - log.error("{/app/modules/oauth/util.js} Error in retrieving access token by saml " + + log.error("{/app/modules/oauth/token-handler-utils.js} Error in retrieving access token by saml " + "grant type. No assertion, encoded client app credentials or scopes are " + - "found - getAccessTokenBySAMLGrantType(x, y, z)"); + "found - getTokenPairBySAMLGrantType(x, y, z)"); return null; } else { var assertionXML = publicMethods.decode(assertion); @@ -127,8 +127,8 @@ var util = function () { var extractedAssertion; if (assertionStartIndex == -1 || assertionEndIndex == -1) { - log.error("{/app/modules/oauth/util.js} Error in retrieving access token by saml grant type. " + - "Issue in assertion format - getAccessTokenBySAMLGrantType(x, y, z)"); + log.error("{/app/modules/oauth/token-handler-utils.js} Error in retrieving access " + + "token by saml grant type. Issue in assertion format - getTokenPairBySAMLGrantType(x, y, z)"); return null; } else { extractedAssertion = assertionXML. @@ -153,19 +153,19 @@ var util = function () { tokenPair["refreshToken"] = responsePayload["refresh_token"]; return tokenPair; } else { - log.error("{/app/modules/oauth/util.js} Error in retrieving access token by password " + - "grant type - getAccessTokenBySAMLGrantType(x, y, z)"); + log.error("{/app/modules/oauth/token-handler-utils.js} Error in retrieving access token " + + "by password grant type - getTokenPairBySAMLGrantType(x, y, z)"); return null; } } } }; - publicMethods.getNewAccessTokenByRefreshToken = function (refreshToken, encodedClientAppCredentials, scopes) { + publicMethods.getNewTokenPairByRefreshToken = function (refreshToken, encodedClientAppCredentials, scopes) { if (!refreshToken || !encodedClientAppCredentials) { - log.error("{/app/modules/oauth/util.js} Error in retrieving new access token by current " + - "refresh token. No refresh token or encoded client app credentials are " + - "found - getNewAccessTokenByRefreshToken(x, y, z)"); + log.error("{/app/modules/oauth/token-handler-utils.js} Error in retrieving new access token " + + "by current refresh token. No refresh token or encoded client app credentials are " + + "found - getNewTokenPairByRefreshToken(x, y, z)"); return null; } else { var requestURL = deviceMgtProps["oauthProvider"]["tokenServiceURL"]; @@ -187,8 +187,8 @@ var util = function () { tokenPair["refreshToken"] = responsePayload["refresh_token"]; return tokenPair; } else { - log.error("{/app/modules/oauth/util.js} Error in retrieving new access token by " + - "current refresh token - getNewAccessTokenByRefreshToken(x, y, z)"); + log.error("{/app/modules/oauth/token-handler-utils.js} Error in retrieving new access token by " + + "current refresh token - getNewTokenPairByRefreshToken(x, y, z)"); return null; } } @@ -196,8 +196,9 @@ var util = function () { publicMethods.getAccessTokenByJWTGrantType = function (clientAppCredentials) { if (!clientAppCredentials) { - log.error("{/app/modules/oauth/util.js} Error in retrieving new access token by current refresh token. " + - "No client app credentials are found as input - getAccessTokenByJWTGrantType(x)"); + log.error("{/app/modules/oauth/token-handler-utils.js} Error in retrieving new access token " + + "by current refresh token. No client app credentials are found " + + "as input - getAccessTokenByJWTGrantType(x)"); return null; } else { var JWTClientManagerServicePackagePath = @@ -212,15 +213,16 @@ var util = function () { publicMethods.getTenantBasedClientAppCredentials = function (username, jwtToken) { if (!username || !jwtToken) { - log.error("{/app/modules/oauth/util.js} Error in retrieving tenant based client app " + - "credentials. No username or jwt token is found as input - getTenantBasedClientAppCredentials(x, y)"); + log.error("{/app/modules/oauth/token-handler-utils.js} Error in retrieving tenant " + + "based client app credentials. No username or jwt token is found " + + "as input - getTenantBasedClientAppCredentials(x, y)"); return null; } else { var tenantDomain = carbon.server.tenantDomain({username: username}); if (!tenantDomain) { - log.error("{/app/modules/oauth/util.js} Error in retrieving tenant based client application " + - "credentials. Unable to obtain a valid tenant domain for provided " + - "username - getTenantBasedClientAppCredentials(x, y)"); + log.error("{/app/modules/oauth/token-handler-utils.js} Error in retrieving tenant " + + "based client application credentials. Unable to obtain a valid tenant domain for provided " + + "username - getTenantBasedClientAppCredentials(x, y)"); return null; } else { var cachedTenantBasedClientAppCredentials = privateMethods. @@ -232,7 +234,7 @@ var util = function () { var applicationName = "webapp_" + tenantDomain; var requestURL = deviceMgtProps["oauthProvider"]["appRegistration"] ["apiManagerClientAppRegistrationServiceURL"] + - "?tenantDomain=" + tenantDomain + "&applicationName=" + applicationName; + "?tenantDomain=" + tenantDomain + "&applicationName=" + applicationName; var xhr = new XMLHttpRequest(); xhr.open("POST", requestURL, false); @@ -249,8 +251,9 @@ var util = function () { setCachedTenantBasedClientAppCredentials(tenantDomain, tenantBasedClientAppCredentials); return tenantBasedClientAppCredentials; } else { - log.error("{/app/modules/oauth/util.js} Error in retrieving tenant based client " + - "application credentials from API Manager - getTenantBasedClientAppCredentials(x, y)"); + log.error("{/app/modules/oauth/token-handler-utils.js} Error in retrieving tenant " + + "based client application credentials from API " + + "Manager - getTenantBasedClientAppCredentials(x, y)"); return null; } } 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 867c1db8da..64b0c1894a 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 @@ -25,7 +25,7 @@ var handlers = function () { var log = new Log("/app/modules/oauth/token-handlers.js"); - var tokenUtil = require("/app/modules/oauth/util.js")["util"]; + var tokenUtil = require("/app/modules/oauth/token-handler-utils.js")["utils"]; var constants = require("/app/modules/constants.js"); var devicemgtProps = require("/app/conf/reader/main.js")["conf"]; @@ -53,8 +53,8 @@ var handlers = function () { stringOfScopes += entry + " "; }); tokenPair = tokenUtil. - getAccessTokenByPasswordGrantType(username, - encodeURIComponent(password), encodedClientAppCredentials, stringOfScopes); + getTokenPairByPasswordGrantType(username, + encodeURIComponent(password), encodedClientAppCredentials, stringOfScopes); if (!tokenPair) { throw new Error("{/app/modules/oauth/token-handlers.js} Could not set up " + "token pair by password grant type. Error in token " + @@ -83,7 +83,7 @@ var handlers = function () { var tokenPair; // accessTokenPair will include current access token as well as current refresh token tokenPair = tokenUtil. - getAccessTokenBySAMLGrantType(samlToken, encodedClientAppCredentials, "PRODUCTION"); + getTokenPairBySAMLGrantType(samlToken, encodedClientAppCredentials, "PRODUCTION"); if (!tokenPair) { throw new Error("{/app/modules/oauth/token-handlers.js} Could not set up token " + "pair by password grant type. Error in token " + @@ -106,7 +106,7 @@ var handlers = function () { "session context - refreshTokenPair()"); } else { var newTokenPair = tokenUtil. - getNewAccessTokenByRefreshToken(currentTokenPair["refreshToken"], encodedClientAppCredentials); + getNewTokenPairByRefreshToken(currentTokenPair["refreshToken"], encodedClientAppCredentials); if (!newTokenPair) { log.error("{/app/modules/oauth/token-handlers.js} Error in refreshing token pair. " + "Unable to update session context with new access token pair - refreshTokenPair()"); From 303f16e747b744f8a5f894a21c1cce166f36e63b Mon Sep 17 00:00:00 2001 From: dilanua Date: Sun, 31 Jul 2016 11:15:40 +0530 Subject: [PATCH 11/11] Deprecating obsolete UI functions - device.js --- .../devicemgt/app/modules/device.js | 404 +++++++++--------- 1 file changed, 202 insertions(+), 202 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/device.js b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/device.js index 085b9b7ea1..cb640d4537 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/device.js +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/device.js @@ -25,18 +25,18 @@ deviceModule = function () { var devicemgtProps = require("/app/conf/reader/main.js")["conf"]; var serviceInvokers = require("/app/modules/oauth/token-protected-service-invokers.js")["invokers"]; - var ArrayList = Packages.java.util.ArrayList; - var Properties = Packages.java.util.Properties; - var DeviceIdentifier = Packages.org.wso2.carbon.device.mgt.common.DeviceIdentifier; - var DeviceManagerUtil = Packages.org.wso2.carbon.device.mgt.core.util.DeviceManagerUtil; - var SimpleOperation = Packages.org.wso2.carbon.device.mgt.core.operation.mgt.SimpleOperation; - var ConfigOperation = Packages.org.wso2.carbon.device.mgt.core.operation.mgt.ConfigOperation; - var CommandOperation = Packages.org.wso2.carbon.device.mgt.core.operation.mgt.CommandOperation; +// var ArrayList = Packages.java.util.ArrayList; +// var Properties = Packages.java.util.Properties; +// var DeviceIdentifier = Packages.org.wso2.carbon.device.mgt.common.DeviceIdentifier; +// var DeviceManagerUtil = Packages.org.wso2.carbon.device.mgt.core.util.DeviceManagerUtil; +// var SimpleOperation = Packages.org.wso2.carbon.device.mgt.core.operation.mgt.SimpleOperation; +// var ConfigOperation = Packages.org.wso2.carbon.device.mgt.core.operation.mgt.ConfigOperation; +// var CommandOperation = Packages.org.wso2.carbon.device.mgt.core.operation.mgt.CommandOperation; var publicMethods = {}; var privateMethods = {}; - var deviceCloudService = devicemgtProps["httpsURL"] + "/common/device_manager"; +// var deviceCloudService = devicemgtProps["httpsURL"] + "/common/device_manager"; privateMethods.validateAndReturn = function (value) { return (value == undefined || value == null) ? constants.UNSPECIFIED : value; @@ -45,57 +45,57 @@ deviceModule = function () { /* @Deprecated */ - publicMethods.listDevices = function () { - var carbonUser = session.get(constants.USER_SESSION_KEY); - var utility = require('/app/modules/utility.js').utility; - if (!carbonUser) { - log.error("User object was not found in the session"); - throw constants.ERRORS.USER_NOT_FOUND; - } - try { - utility.startTenantFlow(carbonUser); - var deviceManagementService = utility.getDeviceManagementService(); - var devices = deviceManagementService.getAllDevices(); - var deviceList = []; - var i, device, propertiesList, deviceObject; - for (i = 0; i < devices.size(); i++) { - device = devices.get(i); - propertiesList = DeviceManagerUtil.convertDevicePropertiesToMap(device.getProperties()); - - deviceObject = {}; - deviceObject[constants.DEVICE_IDENTIFIER] = - privateMethods.validateAndReturn(device.getDeviceIdentifier()); - deviceObject[constants.DEVICE_NAME] = - privateMethods.validateAndReturn(device.getName()); - deviceObject[constants.DEVICE_OWNERSHIP] = - privateMethods.validateAndReturn(device.getEnrolmentInfo().getOwnership()); - deviceObject[constants.DEVICE_OWNER] = - privateMethods.validateAndReturn(device.getEnrolmentInfo().getOwner()); - deviceObject[constants.DEVICE_TYPE] = - privateMethods.validateAndReturn(device.getType()); - deviceObject[constants.DEVICE_PROPERTIES] = {}; - if (device.getType() == constants.PLATFORM_IOS) { - deviceObject[constants.DEVICE_PROPERTIES][constants.DEVICE_MODEL] = - privateMethods.validateAndReturn(propertiesList.get(constants.DEVICE_PRODUCT)); - deviceObject[constants.DEVICE_PROPERTIES][constants.DEVICE_VENDOR] = constants.VENDOR_APPLE; - } else { - deviceObject[constants.DEVICE_PROPERTIES][constants.DEVICE_MODEL] = - privateMethods.validateAndReturn(propertiesList.get(constants.DEVICE_MODEL)); - deviceObject[constants.DEVICE_PROPERTIES][constants.DEVICE_VENDOR] = - privateMethods.validateAndReturn(propertiesList.get(constants.DEVICE_VENDOR)); - } - deviceObject[constants.DEVICE_PROPERTIES][constants.DEVICE_OS_VERSION] = - privateMethods.validateAndReturn(propertiesList.get(constants.DEVICE_OS_VERSION)); - - deviceList.push(deviceObject); - } - return deviceList; - } catch (e) { - throw e; - } finally { - utility.endTenantFlow(); - } - }; +// publicMethods.listDevices = function () { +// var carbonUser = session.get(constants.USER_SESSION_KEY); +// var utility = require('/app/modules/utility.js').utility; +// if (!carbonUser) { +// log.error("User object was not found in the session"); +// throw constants.ERRORS.USER_NOT_FOUND; +// } +// try { +// utility.startTenantFlow(carbonUser); +// var deviceManagementService = utility.getDeviceManagementService(); +// var devices = deviceManagementService.getAllDevices(); +// var deviceList = []; +// var i, device, propertiesList, deviceObject; +// for (i = 0; i < devices.size(); i++) { +// device = devices.get(i); +// propertiesList = DeviceManagerUtil.convertDevicePropertiesToMap(device.getProperties()); +// +// deviceObject = {}; +// deviceObject[constants.DEVICE_IDENTIFIER] = +// privateMethods.validateAndReturn(device.getDeviceIdentifier()); +// deviceObject[constants.DEVICE_NAME] = +// privateMethods.validateAndReturn(device.getName()); +// deviceObject[constants.DEVICE_OWNERSHIP] = +// privateMethods.validateAndReturn(device.getEnrolmentInfo().getOwnership()); +// deviceObject[constants.DEVICE_OWNER] = +// privateMethods.validateAndReturn(device.getEnrolmentInfo().getOwner()); +// deviceObject[constants.DEVICE_TYPE] = +// privateMethods.validateAndReturn(device.getType()); +// deviceObject[constants.DEVICE_PROPERTIES] = {}; +// if (device.getType() == constants.PLATFORM_IOS) { +// deviceObject[constants.DEVICE_PROPERTIES][constants.DEVICE_MODEL] = +// privateMethods.validateAndReturn(propertiesList.get(constants.DEVICE_PRODUCT)); +// deviceObject[constants.DEVICE_PROPERTIES][constants.DEVICE_VENDOR] = constants.VENDOR_APPLE; +// } else { +// deviceObject[constants.DEVICE_PROPERTIES][constants.DEVICE_MODEL] = +// privateMethods.validateAndReturn(propertiesList.get(constants.DEVICE_MODEL)); +// deviceObject[constants.DEVICE_PROPERTIES][constants.DEVICE_VENDOR] = +// privateMethods.validateAndReturn(propertiesList.get(constants.DEVICE_VENDOR)); +// } +// deviceObject[constants.DEVICE_PROPERTIES][constants.DEVICE_OS_VERSION] = +// privateMethods.validateAndReturn(propertiesList.get(constants.DEVICE_OS_VERSION)); +// +// deviceList.push(deviceObject); +// } +// return deviceList; +// } catch (e) { +// throw e; +// } finally { +// utility.endTenantFlow(); +// } +// }; /* @Deprecated @@ -103,105 +103,105 @@ deviceModule = function () { /* Get the supported features by the device type */ - publicMethods.getFeatures = function (deviceType) { - var carbonUser = session.get(constants.USER_SESSION_KEY); - var utility = require('/app/modules/utility.js').utility; - if (!carbonUser) { - log.error("User object was not found in the session"); - throw constants.ERRORS.USER_NOT_FOUND; - } - try { - utility.startTenantFlow(carbonUser); - var deviceManagementService = utility.getDeviceManagementService(); - var features = deviceManagementService.getFeatureManager(deviceType).getFeatures(); - var featuresConverted = {}; - if (features) { - var i, feature, featureObject; - for (i = 0; i < features.size(); i++) { - feature = features.get(i); - featureObject = {}; - featureObject[constants.FEATURE_NAME] = feature.getName(); - featureObject[constants.FEATURE_DESCRIPTION] = feature.getDescription(); - featuresConverted[feature.getName()] = featureObject; - } - } - return featuresConverted; - } catch (e) { - throw e; - } finally { - utility.endTenantFlow(); - } - }; +// publicMethods.getFeatures = function (deviceType) { +// var carbonUser = session.get(constants.USER_SESSION_KEY); +// var utility = require('/app/modules/utility.js').utility; +// if (!carbonUser) { +// log.error("User object was not found in the session"); +// throw constants.ERRORS.USER_NOT_FOUND; +// } +// try { +// utility.startTenantFlow(carbonUser); +// var deviceManagementService = utility.getDeviceManagementService(); +// var features = deviceManagementService.getFeatureManager(deviceType).getFeatures(); +// var featuresConverted = {}; +// if (features) { +// var i, feature, featureObject; +// for (i = 0; i < features.size(); i++) { +// feature = features.get(i); +// featureObject = {}; +// featureObject[constants.FEATURE_NAME] = feature.getName(); +// featureObject[constants.FEATURE_DESCRIPTION] = feature.getDescription(); +// featuresConverted[feature.getName()] = featureObject; +// } +// } +// return featuresConverted; +// } catch (e) { +// throw e; +// } finally { +// utility.endTenantFlow(); +// } +// }; /* @Deprecated */ - publicMethods.performOperation = function (devices, operation) { - var carbonUser = session.get(constants.USER_SESSION_KEY); - var utility = require('/app/modules/utility.js').utility; - if (!carbonUser) { - log.error("User object was not found in the session"); - throw constants.ERRORS.USER_NOT_FOUND; - } - try { - utility.startTenantFlow(carbonUser); - var deviceManagementService = utility.getDeviceManagementService(); - var operationInstance; - if (operation.type == "COMMAND") { - operationInstance = new CommandOperation(); - } else if (operation.type == "CONFIG") { - operationInstance = new ConfigOperation(); - } else { - operationInstance = new SimpleOperation(); - } - operationInstance.setCode(operation.featureName); - var props = new Properties(); - var i, object; - for (i = 0; i < operation.properties.length; i++) { - object = properties[i]; - props.setProperty(object.key, object.value); - } - operationInstance.setProperties(props); - var deviceList = new ArrayList(); - var j, device, deviceIdentifier; - for (j = 0; j < devices.length; i++) { - device = devices[j]; - deviceIdentifier = new DeviceIdentifier(); - deviceIdentifier.setId(device.id); - deviceIdentifier.setType(device.type); - deviceList.add(deviceIdentifier); - } - deviceManagementService.addOperation(operationInstance, deviceList); - } catch (e) { - throw e; - } finally { - utility.endTenantFlow(); - } - }; +// publicMethods.performOperation = function (devices, operation) { +// var carbonUser = session.get(constants.USER_SESSION_KEY); +// var utility = require('/app/modules/utility.js').utility; +// if (!carbonUser) { +// log.error("User object was not found in the session"); +// throw constants.ERRORS.USER_NOT_FOUND; +// } +// try { +// utility.startTenantFlow(carbonUser); +// var deviceManagementService = utility.getDeviceManagementService(); +// var operationInstance; +// if (operation.type == "COMMAND") { +// operationInstance = new CommandOperation(); +// } else if (operation.type == "CONFIG") { +// operationInstance = new ConfigOperation(); +// } else { +// operationInstance = new SimpleOperation(); +// } +// operationInstance.setCode(operation.featureName); +// var props = new Properties(); +// var i, object; +// for (i = 0; i < operation.properties.length; i++) { +// object = properties[i]; +// props.setProperty(object.key, object.value); +// } +// operationInstance.setProperties(props); +// var deviceList = new ArrayList(); +// var j, device, deviceIdentifier; +// for (j = 0; j < devices.length; i++) { +// device = devices[j]; +// deviceIdentifier = new DeviceIdentifier(); +// deviceIdentifier.setId(device.id); +// deviceIdentifier.setType(device.type); +// deviceList.add(deviceIdentifier); +// } +// deviceManagementService.addOperation(operationInstance, deviceList); +// } catch (e) { +// throw e; +// } finally { +// utility.endTenantFlow(); +// } +// }; /* @Deprecated */ - privateMethods.getDevice = function (type, deviceId) { - var carbonUser = session.get(constants.USER_SESSION_KEY); - var utility = require('/app/modules/utility.js').utility; - if (!carbonUser) { - log.error("User object was not found in the session"); - throw constants.ERRORS.USER_NOT_FOUND; - } - try { - utility.startTenantFlow(carbonUser); - var deviceManagementService = utility.getDeviceManagementService(); - var deviceIdentifier = new DeviceIdentifier(); - deviceIdentifier.setType(type); - deviceIdentifier.setId(deviceId); - return deviceManagementService.getDevice(deviceIdentifier); - } catch (e) { - throw e; - } finally { - utility.endTenantFlow(); - } - }; +// privateMethods.getDevice = function (type, deviceId) { +// var carbonUser = session.get(constants.USER_SESSION_KEY); +// var utility = require('/app/modules/utility.js').utility; +// if (!carbonUser) { +// log.error("User object was not found in the session"); +// throw constants.ERRORS.USER_NOT_FOUND; +// } +// try { +// utility.startTenantFlow(carbonUser); +// var deviceManagementService = utility.getDeviceManagementService(); +// var deviceIdentifier = new DeviceIdentifier(); +// deviceIdentifier.setType(type); +// deviceIdentifier.setId(deviceId); +// return deviceManagementService.getDevice(deviceIdentifier); +// } catch (e) { +// throw e; +// } finally { +// utility.endTenantFlow(); +// } +// }; /* @Updated @@ -218,37 +218,37 @@ deviceModule = function () { var url = devicemgtProps["httpsURL"] + constants.ADMIN_SERVICE_CONTEXT + "/devices/view?type=" + deviceType + "&id=" + deviceId; return serviceInvokers.XMLHttp.get( - url, function (responsePayload) { - var device = responsePayload.responseContent; - if (device) { - var propertiesList = device["properties"]; - var properties = {}; - if (propertiesList){ - for (var i = 0; i < propertiesList.length; i++) { - properties[propertiesList[i]["name"]] = propertiesList[i]["value"]; - } - } - var deviceObject = {}; - deviceObject[constants["DEVICE_IDENTIFIER"]] = device["deviceIdentifier"]; - deviceObject[constants["DEVICE_NAME"]] = device["name"]; - deviceObject[constants["DEVICE_OWNERSHIP"]] = device["enrolmentInfo"]["ownership"]; - deviceObject[constants["DEVICE_OWNER"]] = device["enrolmentInfo"]["owner"]; - deviceObject[constants["DEVICE_STATUS"]] = device["enrolmentInfo"]["status"]; - deviceObject[constants["DEVICE_TYPE"]] = device["type"]; - if (device["type"] == constants["PLATFORM_IOS"]) { - properties[constants["DEVICE_MODEL"]] = properties[constants["DEVICE_PRODUCT"]]; - delete properties[constants["DEVICE_PRODUCT"]]; - properties[constants["DEVICE_VENDOR"]] = constants["VENDOR_APPLE"]; + url, function (responsePayload) { + var device = responsePayload.responseContent; + if (device) { + var propertiesList = device["properties"]; + var properties = {}; + if (propertiesList){ + for (var i = 0; i < propertiesList.length; i++) { + properties[propertiesList[i]["name"]] = propertiesList[i]["value"]; } - deviceObject[constants["DEVICE_PROPERTIES"]] = properties; - return deviceObject; } - }, - function (responsePayload) { - var response = {}; - response["status"] = "error"; - return response; + var deviceObject = {}; + deviceObject[constants["DEVICE_IDENTIFIER"]] = device["deviceIdentifier"]; + deviceObject[constants["DEVICE_NAME"]] = device["name"]; + deviceObject[constants["DEVICE_OWNERSHIP"]] = device["enrolmentInfo"]["ownership"]; + deviceObject[constants["DEVICE_OWNER"]] = device["enrolmentInfo"]["owner"]; + deviceObject[constants["DEVICE_STATUS"]] = device["enrolmentInfo"]["status"]; + deviceObject[constants["DEVICE_TYPE"]] = device["type"]; + if (device["type"] == constants["PLATFORM_IOS"]) { + properties[constants["DEVICE_MODEL"]] = properties[constants["DEVICE_PRODUCT"]]; + delete properties[constants["DEVICE_PRODUCT"]]; + properties[constants["DEVICE_VENDOR"]] = constants["VENDOR_APPLE"]; + } + deviceObject[constants["DEVICE_PROPERTIES"]] = properties; + return deviceObject; } + }, + function (responsePayload) { + var response = {}; + response["status"] = "error"; + return response; + } ); } catch (e) { throw e; @@ -268,19 +268,19 @@ deviceModule = function () { url = devicemgtProps["httpsURL"] + constants.ADMIN_SERVICE_CONTEXT + "/devices/count"; } else if (uiPermissions.LIST_OWN_DEVICES) { url = devicemgtProps["httpsURL"] + constants.ADMIN_SERVICE_CONTEXT + "/devices/user/" + carbonUser.username - + "/count"; + + "/count"; } else { log.error("Access denied for user: " + carbonUser.username); return -1; } return serviceInvokers.XMLHttp.get( - url, function (responsePayload) { - return responsePayload; - }, - function (responsePayload) { - log.error(responsePayload); - return -1; - } + url, function (responsePayload) { + return responsePayload; + }, + function (responsePayload) { + log.error(responsePayload); + return -1; + } ); } else { log.error("User object was not found in the session"); @@ -291,13 +291,13 @@ deviceModule = function () { publicMethods.getDeviceTypes = function () { var url = devicemgtProps["httpsURL"] + constants.ADMIN_SERVICE_CONTEXT + "/devices/types"; return serviceInvokers.XMLHttp.get( - url, function (responsePayload) { - return responsePayload; - }, - function (responsePayload) { - log.error(responsePayload); - return -1; - } + url, function (responsePayload) { + return responsePayload; + }, + function (responsePayload) { + log.error(responsePayload); + return -1; + } ); }; @@ -328,16 +328,16 @@ deviceModule = function () { publicMethods.getDevices = function (userName) { var url = devicemgtProps["httpsURL"] + constants.ADMIN_SERVICE_CONTEXT + "/devices/user/" + userName; return serviceInvokers.XMLHttp.get( - url, function (responsePayload) { - for (var i = 0; i < responsePayload.length; i++) { - responsePayload[i].thumb = utility.getDeviceThumb(responsePayload[i].type); - } - return responsePayload; - }, - function (responsePayload) { - log.error(responsePayload); - return -1; + url, function (responsePayload) { + for (var i = 0; i < responsePayload.length; i++) { + responsePayload[i].thumb = utility.getDeviceThumb(responsePayload[i].type); } + return responsePayload; + }, + function (responsePayload) { + log.error(responsePayload); + return -1; + } ); }; return publicMethods;