mirror of
https://repository.entgra.net/community/device-mgt-core.git
synced 2025-10-06 02:01:45 +00:00
Merge branch 'master' of https://github.com/wso2/carbon-device-mgt into scope-impl
This commit is contained in:
commit
34f1d9ee38
@ -22,7 +22,7 @@ var log = new Log("api/data-tables-invoker-api.jag");
|
||||
var uri = request.getRequestURI();
|
||||
var uriMatcher = new URIMatcher(String(uri));
|
||||
|
||||
var devicemgtProps = require("/app/conf/reader/main.js")["conf"];
|
||||
var devicemgtProps = require("/app/modules/conf-reader/main.js")["conf"];
|
||||
var serviceInvokers = require("/app/modules/oauth/token-protected-service-invokers.js")["invokers"];
|
||||
|
||||
function appendQueryParam (url, queryParam , value) {
|
||||
|
||||
@ -22,10 +22,10 @@ var uriMatcher = new URIMatcher(String(uri));
|
||||
|
||||
var log = new Log("api/device-api.jag");
|
||||
var constants = require("/app/modules/constants.js");
|
||||
var deviceModule = require("/app/modules/device.js").deviceModule;
|
||||
var deviceModule = require("/app/modules/business-controllers/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 devicemgtProps = require("/app/modules/conf-reader/main.js")["conf"];
|
||||
var userModule = require("/app/modules/business-controllers/user.js")["userModule"];
|
||||
var serviceInvokers = require("/app/modules/oauth/token-protected-service-invokers.js")["invokers"];
|
||||
|
||||
var user = session.get(constants.USER_SESSION_KEY);
|
||||
|
||||
@ -23,7 +23,7 @@ var uriMatcher = new URIMatcher(String(uri));
|
||||
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 devicemgtProps = require("/app/modules/conf-reader/main.js")["conf"];
|
||||
var serviceInvokers = require("/app/modules/oauth/token-protected-service-invokers.js")["invokers"];
|
||||
|
||||
var user = session.get(constants.USER_SESSION_KEY);
|
||||
|
||||
@ -23,7 +23,7 @@ var uri = request.getRequestURI();
|
||||
var uriMatcher = new URIMatcher(String(uri));
|
||||
|
||||
var constants = require("/app/modules/constants.js");
|
||||
var devicemgtProps = require("/app/conf/reader/main.js")["conf"];
|
||||
var devicemgtProps = require("/app/modules/conf-reader/main.js")["conf"];
|
||||
var serviceInvokers = require("/app/modules/oauth/token-protected-service-invokers.js")["invokers"];
|
||||
|
||||
if (uriMatcher.match("/{context}/api/invoker/execute/")) {
|
||||
|
||||
@ -23,7 +23,7 @@ var uriMatcher = new URIMatcher(String(uri));
|
||||
var log = new Log("api/operation-api.jag");
|
||||
|
||||
var serviceInvokers = require("/app/modules/oauth/token-protected-service-invokers.js")["invokers"];
|
||||
var devicemgtProps = require("/app/conf/reader/main.js")["conf"];
|
||||
var devicemgtProps = require("/app/modules/conf-reader/main.js")["conf"];
|
||||
|
||||
if (uriMatcher.match("/{context}/api/operation/paginate")) {
|
||||
var deviceType = request.getParameter("deviceType");
|
||||
|
||||
@ -27,7 +27,7 @@ var uriMatcher = new URIMatcher(String(uri));
|
||||
var log = new Log("api/policy-api.jag");
|
||||
|
||||
var constants = require("/modules/constants.js");
|
||||
var policyModule = require("/modules/policy.js").policyModule;
|
||||
var policyModule = require("/app/modules/business-controllers/group.js")["groupModule"];
|
||||
|
||||
var result;
|
||||
if (uriMatcher.match("/{context}/api/policies/update")) {
|
||||
|
||||
@ -23,9 +23,9 @@ var uriMatcher = new URIMatcher(String(uri));
|
||||
var log = new Log("api/user-api.jag");
|
||||
|
||||
var constants = require("/app/modules/constants.js");
|
||||
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 devicemgtProps = require("/app/modules/conf-reader/main.js")["conf"];
|
||||
var userModule = require("/app/modules/business-controllers/user.js")["userModule"];
|
||||
var deviceModule = require("/app/modules/business-controllers/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/token-handler-utils.js")["utils"];
|
||||
|
||||
@ -18,11 +18,11 @@
|
||||
|
||||
var deviceModule;
|
||||
deviceModule = function () {
|
||||
var log = new Log("/app/modules/device.js");
|
||||
var log = new Log("/app/modules/business-controllers/device.js");
|
||||
|
||||
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 devicemgtProps = require("/app/modules/conf-reader/main.js")["conf"];
|
||||
var serviceInvokers = require("/app/modules/oauth/token-protected-service-invokers.js")["invokers"];
|
||||
|
||||
// var ArrayList = Packages.java.util.ArrayList;
|
||||
@ -261,7 +261,7 @@ deviceModule = function () {
|
||||
publicMethods.getDevicesCount = function () {
|
||||
var carbonUser = session.get(constants.USER_SESSION_KEY);
|
||||
if (carbonUser) {
|
||||
var userModule = require("/app/modules/user.js").userModule;
|
||||
var userModule = require("/app/modules/business-controllers/user.js")["userModule"];
|
||||
var uiPermissions = userModule.getUIPermissions();
|
||||
var url;
|
||||
if (uiPermissions.LIST_DEVICES) {
|
||||
@ -18,11 +18,11 @@
|
||||
|
||||
var groupModule = {};
|
||||
(function (groupModule) {
|
||||
var log = new Log("/app/modules/group.js");
|
||||
var log = new Log("/app/modules/business-controllers/group.js");
|
||||
|
||||
var userModule = require("/app/modules/user.js").userModule;
|
||||
var userModule = require("/app/modules/business-controllers/user.js")["userModule"];
|
||||
var constants = require('/app/modules/constants.js');
|
||||
var devicemgtProps = require("/app/conf/reader/main.js")["conf"];
|
||||
var devicemgtProps = require("/app/modules/conf-reader/main.js")["conf"];
|
||||
var utility = require("/app/modules/utility.js").utility;
|
||||
var serviceInvokers = require("/app/modules/oauth/token-protected-service-invokers.js")["invokers"];
|
||||
|
||||
@ -0,0 +1,138 @@
|
||||
/*
|
||||
* 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 operationModule = function () {
|
||||
// var log = new Log("/app/modules/business-controllers/operation.js");
|
||||
// var utility = require('/app/modules/utility.js').utility;
|
||||
// var constants = require('/app/modules/constants.js');
|
||||
// var devicemgtProps = require("/app/modules/conf-reader/main.js")["conf"];
|
||||
// var serviceInvokers = require("/app/modules/oauth/token-protected-service-invokers.js")["invokers"];
|
||||
//
|
||||
// var publicMethods = {};
|
||||
// var privateMethods = {};
|
||||
//
|
||||
// /**
|
||||
// * This method reads the token from the Token client and return the access token.
|
||||
// * If the token pair s not set in the session this will send a redirect to the login page.
|
||||
// */
|
||||
// function getAccessToken(deviceType, owner, deviceId) {
|
||||
// var TokenClient = Packages.org.wso2.carbon.device.mgt.iot.apimgt.TokenClient;
|
||||
// var accessTokenClient = new TokenClient(deviceType);
|
||||
// var accessTokenInfo = accessTokenClient.getAccessToken(owner, deviceId);
|
||||
// return accessTokenInfo.getAccess_token();
|
||||
// }
|
||||
//
|
||||
// privateMethods.getOperationsFromFeatures = function (deviceType, operationType) {
|
||||
// var url = devicemgtProps["httpsURL"] + constants.ADMIN_SERVICE_CONTEXT + "/features/" + deviceType;
|
||||
// var featuresList = serviceInvokers.XMLHttp.get(url, function (responsePayload) {
|
||||
// var features = responsePayload;
|
||||
// var featureList = [];
|
||||
// var feature;
|
||||
// for (var i = 0; i < features.length; i++) {
|
||||
// feature = {};
|
||||
// var analyticStreams = utility.getDeviceTypeConfig(deviceType)["analyticStreams"];
|
||||
// if (analyticStreams) {
|
||||
// for (var stream in analyticStreams) {
|
||||
// if (analyticStreams[stream].name == features[i].name) {
|
||||
// feature.ui_unit = analyticStreams[stream].ui_unit;
|
||||
// break;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// feature["operation"] = features[i].code;
|
||||
// feature["name"] = features[i].name;
|
||||
// feature["description"] = features[i].description;
|
||||
// feature["deviceType"] = deviceType;
|
||||
// feature["params"] = [];
|
||||
// var metaData = features[i].metadataEntries;
|
||||
// if (metaData) {
|
||||
// for (var j = 0; j < metaData.length; j++) {
|
||||
// feature["params"].push(metaData[j].value);
|
||||
// }
|
||||
// featureList.push(feature);
|
||||
// }
|
||||
// }
|
||||
// return featureList;
|
||||
// }, function (responsePayload) {
|
||||
// var response = {};
|
||||
// response["status"] = "error";
|
||||
// return response;
|
||||
// }
|
||||
// );
|
||||
// return featuresList;
|
||||
// };
|
||||
//
|
||||
// publicMethods.getControlOperations = function (deviceType) {
|
||||
// var operations = privateMethods.getOperationsFromFeatures(deviceType, "operation");
|
||||
// for (var op in operations) {
|
||||
// var iconPath = utility.getOperationIcon(deviceType, operations[op].operation);
|
||||
// if (iconPath) {
|
||||
// operations[op]["icon"] = iconPath;
|
||||
// }
|
||||
// }
|
||||
// return operations;
|
||||
// };
|
||||
//
|
||||
// publicMethods.getMonitorOperations = function (deviceType) {
|
||||
// return privateMethods.getOperationsFromFeatures(deviceType, "monitor");
|
||||
// };
|
||||
//
|
||||
// publicMethods.handlePOSTOperation = function (deviceType, operation, deviceId, params) {
|
||||
// var user = session.get(constants.USER_SESSION_KEY);
|
||||
// var endPoint = devicemgtProps["httpsURL"] + '/' + deviceType + "/controller/" + operation;
|
||||
// var header = '{"owner":"' + user.username + '","deviceId":"' + deviceId +
|
||||
// '","protocol":"mqtt", "sessionId":"' + session.getId() + '", "' +
|
||||
// constants.AUTHORIZATION_HEADER + '":"' + constants.BEARER_PREFIX +
|
||||
// getAccessToken(deviceType, user.username, deviceId) + '"}';
|
||||
// return post(endPoint, params, JSON.parse(header), "json");
|
||||
// };
|
||||
//
|
||||
// publicMethods.handleGETOperation = function (deviceType, operation, operationName, deviceId) {
|
||||
// var user = session.get(constants.USER_SESSION_KEY);
|
||||
// var endPoint = devicemgtProps["httpsURL"] + '/' + deviceType + "/controller/" + operation;
|
||||
// var header = '{"owner":"' + user.username + '","deviceId":"' + deviceId +
|
||||
// '","protocol":"mqtt", "' + constants.AUTHORIZATION_HEADER + '":"' +
|
||||
// constants.BEARER_PREFIX + getAccessToken(deviceType, user.username, deviceId) +
|
||||
// '"}';
|
||||
// var result = get(endPoint, {}, JSON.parse(header), "json");
|
||||
// if (result.data) {
|
||||
// var values = result.data.sensorValue.split(',');
|
||||
// if (operationName == 'gps') {
|
||||
// result.data.map = {
|
||||
// lat: parseFloat(values[0]),
|
||||
// lng: parseFloat(values[1])
|
||||
// }
|
||||
// } else {
|
||||
// var sqSum = 0;
|
||||
// for (var v in values) {
|
||||
// sqSum += Math.pow(values[v], 2);
|
||||
// }
|
||||
// result.data[operationName] = Math.sqrt(sqSum);
|
||||
// }
|
||||
// delete result.data['sensorValue'];
|
||||
// }
|
||||
// return result;
|
||||
// };
|
||||
//
|
||||
// return publicMethods;
|
||||
//}();
|
||||
@ -21,11 +21,11 @@
|
||||
*/
|
||||
var policyModule;
|
||||
policyModule = function () {
|
||||
var log = new Log("/app/modules/policy.js");
|
||||
var log = new Log("/app/modules/business-controllers/policy.js");
|
||||
|
||||
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 devicemgtProps = require("/app/modules/conf-reader/main.js")["conf"];
|
||||
var serviceInvokers = require("/app/modules/oauth/token-protected-service-invokers.js")["invokers"];
|
||||
|
||||
var publicMethods = {};
|
||||
@ -20,11 +20,11 @@
|
||||
* This module contains user and roles related functionality.
|
||||
*/
|
||||
var userModule = function () {
|
||||
var log = new Log("/app/modules/user.js");
|
||||
var log = new Log("/app/modules/business-controllers/user.js");
|
||||
|
||||
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 devicemgtProps = require("/app/modules/conf-reader/main.js")["conf"];
|
||||
var serviceInvokers = require("/app/modules/oauth/token-protected-service-invokers.js")["invokers"];
|
||||
|
||||
/* Initializing user manager */
|
||||
@ -17,16 +17,18 @@
|
||||
*/
|
||||
|
||||
var conf = function () {
|
||||
var conf = application.get("UI_CONF");
|
||||
var conf = application.get("CONF");
|
||||
if (!conf) {
|
||||
conf = require("/app/conf/config.json");
|
||||
var pinch = require("/app/conf/reader/pinch.min.js")["pinch"];
|
||||
var pinch = require("/app/modules/conf-reader/pinch.min.js")["pinch"];
|
||||
var server = require("carbon")["server"];
|
||||
pinch(conf, /^/,
|
||||
function (path, key, value) {
|
||||
if ((typeof value === "string") && value.indexOf("%https.ip%") > -1) {
|
||||
//noinspection JSUnresolvedFunction
|
||||
return value.replace("%https.ip%", server.address("https"));
|
||||
} else if ((typeof value === "string") && value.indexOf("%http.ip%") > -1) {
|
||||
//noinspection JSUnresolvedFunction
|
||||
return value.replace("%http.ip%", server.address("http"));
|
||||
} else if ((typeof value === "string") && value.indexOf("%date-year%") > -1) {
|
||||
var year = new Date().getFullYear();
|
||||
@ -35,7 +37,7 @@ var conf = function () {
|
||||
return value;
|
||||
}
|
||||
);
|
||||
application.put("UI_CONF", conf);
|
||||
application.put("CONF", conf);
|
||||
}
|
||||
return conf;
|
||||
}();
|
||||
@ -17,14 +17,14 @@
|
||||
*/
|
||||
|
||||
var carbonModule = require("carbon");
|
||||
var devicemgtProps = require("/app/conf/reader/main.js")["conf"];
|
||||
var devicemgtProps = require("/app/modules/conf-reader/main.js")["conf"];
|
||||
var carbonServer = new carbonModule.server.Server({
|
||||
tenanted: true,
|
||||
url: devicemgtProps["httpsURL"] + "/admin"
|
||||
});
|
||||
application.put("carbonServer", carbonServer);
|
||||
|
||||
var userModule = require("/app/modules/user.js")["userModule"];
|
||||
var userModule = require("/app/modules/business-controllers/user.js")["userModule"];
|
||||
var utility = require("/app/modules/utility.js")["utility"];
|
||||
|
||||
var permissions = {
|
||||
|
||||
@ -30,7 +30,7 @@ var onFail;
|
||||
} else {
|
||||
apiWrapperUtil.setupTokenPairByPasswordGrantType(context.input.username, context.input.password);
|
||||
}
|
||||
var devicemgtProps = require("/app/conf/reader/main.js")["conf"];
|
||||
var devicemgtProps = require("/app/modules/conf-reader/main.js")["conf"];
|
||||
var carbonServer = require("carbon").server;
|
||||
(new carbonServer.Server({url: devicemgtProps["adminService"]}))
|
||||
.login(context.input.username, context.input.password);
|
||||
|
||||
@ -19,25 +19,29 @@
|
||||
var utils = function () {
|
||||
var log = new Log("/app/modules/oauth/token-handler-utils.js");
|
||||
|
||||
var deviceMgtProps = require("/app/conf/reader/main.js")["conf"];
|
||||
var deviceMgtProps = require("/app/modules/conf-reader/main.js")["conf"];
|
||||
var constants = require("/app/modules/constants.js");
|
||||
var carbon = require("carbon");
|
||||
|
||||
//noinspection JSUnresolvedVariable
|
||||
var Base64 = Packages.org.apache.commons.codec.binary.Base64;
|
||||
//noinspection JSUnresolvedVariable
|
||||
var String = Packages.java.lang.String;
|
||||
|
||||
var publicMethods = {};
|
||||
var privateMethods = {};
|
||||
|
||||
publicMethods.encode = function (payload) {
|
||||
return new String(Base64.encodeBase64(new String(payload).getBytes()));
|
||||
publicMethods["encode"] = function (payload) {
|
||||
//noinspection JSUnresolvedFunction
|
||||
return String(Base64.encodeBase64(String(payload).getBytes()));
|
||||
};
|
||||
|
||||
publicMethods.decode = function (payload) {
|
||||
return new String(Base64.decodeBase64(new String(payload).getBytes()));
|
||||
publicMethods["decode"] = function (payload) {
|
||||
//noinspection JSUnresolvedFunction
|
||||
return String(Base64.decodeBase64(String(payload).getBytes()));
|
||||
};
|
||||
|
||||
publicMethods.getDynamicClientAppCredentials = function () {
|
||||
publicMethods["getDynamicClientAppCredentials"] = function () {
|
||||
// setting up dynamic client application properties
|
||||
var dcAppProperties = {
|
||||
"applicationType": deviceMgtProps["oauthProvider"]["appRegistration"]["appType"],
|
||||
@ -76,7 +80,79 @@ var utils = function () {
|
||||
return dynamicClientAppCredentials;
|
||||
};
|
||||
|
||||
publicMethods.getTokenPairByPasswordGrantType = function (username, password, encodedClientAppCredentials, scopes) {
|
||||
publicMethods["getTenantBasedClientAppCredentials"] = function (username, jwtToken) {
|
||||
if (!username || !jwtToken) {
|
||||
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 {
|
||||
//noinspection JSUnresolvedFunction, JSUnresolvedVariable
|
||||
var tenantDomain = carbon.server.tenantDomain({username: username});
|
||||
if (!tenantDomain) {
|
||||
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.
|
||||
getCachedTenantBasedClientAppCredentials(tenantDomain);
|
||||
if (cachedTenantBasedClientAppCredentials) {
|
||||
return cachedTenantBasedClientAppCredentials;
|
||||
} else {
|
||||
// register a tenant based client app at API Manager
|
||||
var applicationName = "webapp_" + tenantDomain;
|
||||
var requestURL = deviceMgtProps["oauthProvider"]["appRegistration"]
|
||||
["apiManagerClientAppRegistrationServiceURL"] +
|
||||
"?tenantDomain=" + tenantDomain + "&applicationName=" + applicationName;
|
||||
|
||||
var xhr = new XMLHttpRequest();
|
||||
xhr.open("POST", requestURL, false);
|
||||
xhr.setRequestHeader("Content-Type", "application/json");
|
||||
xhr.setRequestHeader("Authorization", "Bearer " + jwtToken);
|
||||
xhr.send();
|
||||
|
||||
if (xhr["status"] == 201 && xhr["responseText"]) {
|
||||
var responsePayload = parse(xhr["responseText"]);
|
||||
var tenantBasedClientAppCredentials = {};
|
||||
tenantBasedClientAppCredentials["clientId"] = responsePayload["client_id"];
|
||||
tenantBasedClientAppCredentials["clientSecret"] = responsePayload["client_secret"];
|
||||
privateMethods.
|
||||
setCachedTenantBasedClientAppCredentials(tenantDomain, tenantBasedClientAppCredentials);
|
||||
return tenantBasedClientAppCredentials;
|
||||
} else {
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
privateMethods["setCachedTenantBasedClientAppCredentials"] = function (tenantDomain, clientAppCredentials) {
|
||||
var cachedTenantBasedClientAppCredentialsMap = application.get(constants["CACHED_CREDENTIALS"]);
|
||||
if (!cachedTenantBasedClientAppCredentialsMap) {
|
||||
cachedTenantBasedClientAppCredentialsMap = {};
|
||||
cachedTenantBasedClientAppCredentialsMap[tenantDomain] = clientAppCredentials;
|
||||
application.put(constants["CACHED_CREDENTIALS"], cachedTenantBasedClientAppCredentialsMap);
|
||||
} else if (!cachedTenantBasedClientAppCredentialsMap[tenantDomain]) {
|
||||
cachedTenantBasedClientAppCredentialsMap[tenantDomain] = clientAppCredentials;
|
||||
}
|
||||
};
|
||||
|
||||
privateMethods["getCachedTenantBasedClientAppCredentials"] = function (tenantDomain) {
|
||||
var cachedTenantBasedClientAppCredentialsMap = application.get(constants["CACHED_CREDENTIALS"]);
|
||||
if (!cachedTenantBasedClientAppCredentialsMap ||
|
||||
!cachedTenantBasedClientAppCredentialsMap[tenantDomain]) {
|
||||
return null;
|
||||
} else {
|
||||
return cachedTenantBasedClientAppCredentialsMap[tenantDomain];
|
||||
}
|
||||
};
|
||||
|
||||
publicMethods["getTokenPairByPasswordGrantType"] = function (username, password, encodedClientAppCredentials, scopes) {
|
||||
if (!username || !password || !encodedClientAppCredentials || !scopes) {
|
||||
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 " +
|
||||
@ -108,7 +184,7 @@ var utils = function () {
|
||||
}
|
||||
};
|
||||
|
||||
publicMethods.getTokenPairBySAMLGrantType = function (assertion, encodedClientAppCredentials, scopes) {
|
||||
publicMethods["getTokenPairBySAMLGrantType"] = function (assertion, encodedClientAppCredentials, scopes) {
|
||||
if (!assertion || !encodedClientAppCredentials || !scopes) {
|
||||
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 " +
|
||||
@ -117,8 +193,8 @@ var utils = function () {
|
||||
} else {
|
||||
var assertionXML = publicMethods.decode(assertion);
|
||||
/*
|
||||
TODO: make assertion extraction with proper parsing. Since Jaggery XML parser seem
|
||||
to add formatting which causes signature verification to fail.
|
||||
TODO: make assertion extraction with proper parsing.
|
||||
Since Jaggery XML parser seem to add formatting which causes signature verification to fail.
|
||||
*/
|
||||
var assertionStartMarker = "<saml2:Assertion";
|
||||
var assertionEndMarker = "<\/saml2:Assertion>";
|
||||
@ -161,7 +237,7 @@ var utils = function () {
|
||||
}
|
||||
};
|
||||
|
||||
publicMethods.getNewTokenPairByRefreshToken = function (refreshToken, encodedClientAppCredentials, scopes) {
|
||||
publicMethods["getNewTokenPairByRefreshToken"] = function (refreshToken, encodedClientAppCredentials, scopes) {
|
||||
if (!refreshToken || !encodedClientAppCredentials) {
|
||||
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 " +
|
||||
@ -194,7 +270,7 @@ var utils = function () {
|
||||
}
|
||||
};
|
||||
|
||||
publicMethods.getAccessTokenByJWTGrantType = function (clientAppCredentials) {
|
||||
publicMethods["getAccessTokenByJWTGrantType"] = function (clientAppCredentials) {
|
||||
if (!clientAppCredentials) {
|
||||
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 " +
|
||||
@ -203,7 +279,9 @@ var utils = function () {
|
||||
} else {
|
||||
var JWTClientManagerServicePackagePath =
|
||||
"org.wso2.carbon.identity.jwt.client.extension.service.JWTClientManagerService";
|
||||
//noinspection JSUnresolvedFunction, JSUnresolvedVariable
|
||||
var JWTClientManagerService = carbon.server.osgiService(JWTClientManagerServicePackagePath);
|
||||
//noinspection JSUnresolvedFunction
|
||||
var jwtClient = JWTClientManagerService.getJWTClient();
|
||||
// returning access token by JWT grant type
|
||||
return jwtClient.getAccessToken(clientAppCredentials["clientId"], clientAppCredentials["clientSecret"],
|
||||
@ -211,76 +289,5 @@ var utils = function () {
|
||||
}
|
||||
};
|
||||
|
||||
publicMethods.getTenantBasedClientAppCredentials = function (username, jwtToken) {
|
||||
if (!username || !jwtToken) {
|
||||
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/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.
|
||||
getCachedTenantBasedClientAppCredentials(tenantDomain);
|
||||
if (cachedTenantBasedClientAppCredentials) {
|
||||
return cachedTenantBasedClientAppCredentials;
|
||||
} else {
|
||||
// register a tenant based client app at API Manager
|
||||
var applicationName = "webapp_" + tenantDomain;
|
||||
var requestURL = deviceMgtProps["oauthProvider"]["appRegistration"]
|
||||
["apiManagerClientAppRegistrationServiceURL"] +
|
||||
"?tenantDomain=" + tenantDomain + "&applicationName=" + applicationName;
|
||||
|
||||
var xhr = new XMLHttpRequest();
|
||||
xhr.open("POST", requestURL, false);
|
||||
xhr.setRequestHeader("Content-Type", "application/json");
|
||||
xhr.setRequestHeader("Authorization", "Bearer " + jwtToken);
|
||||
xhr.send();
|
||||
|
||||
if (xhr["status"] == 201 && xhr["responseText"]) {
|
||||
var responsePayload = parse(xhr["responseText"]);
|
||||
var tenantBasedClientAppCredentials = {};
|
||||
tenantBasedClientAppCredentials["clientId"] = responsePayload["client_id"];
|
||||
tenantBasedClientAppCredentials["clientSecret"] = responsePayload["client_secret"];
|
||||
privateMethods.
|
||||
setCachedTenantBasedClientAppCredentials(tenantDomain, tenantBasedClientAppCredentials);
|
||||
return tenantBasedClientAppCredentials;
|
||||
} else {
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
privateMethods.setCachedTenantBasedClientAppCredentials = function (tenantDomain, clientAppCredentials) {
|
||||
var cachedTenantBasedClientAppCredentialsMap = application.get(constants["CACHED_CREDENTIALS"]);
|
||||
if (!cachedTenantBasedClientAppCredentialsMap) {
|
||||
cachedTenantBasedClientAppCredentialsMap = {};
|
||||
cachedTenantBasedClientAppCredentialsMap[tenantDomain] = clientAppCredentials;
|
||||
application.put(constants["CACHED_CREDENTIALS"], cachedTenantBasedClientAppCredentialsMap);
|
||||
} else if (!cachedTenantBasedClientAppCredentialsMap[tenantDomain]) {
|
||||
cachedTenantBasedClientAppCredentialsMap[tenantDomain] = clientAppCredentials;
|
||||
}
|
||||
};
|
||||
|
||||
privateMethods.getCachedTenantBasedClientAppCredentials = function (tenantDomain) {
|
||||
var cachedTenantBasedClientAppCredentialsMap = application.get(constants["CACHED_CREDENTIALS"]);
|
||||
if (!cachedTenantBasedClientAppCredentialsMap ||
|
||||
!cachedTenantBasedClientAppCredentialsMap[tenantDomain]) {
|
||||
return null;
|
||||
} else {
|
||||
return cachedTenantBasedClientAppCredentialsMap[tenantDomain];
|
||||
}
|
||||
};
|
||||
|
||||
return publicMethods;
|
||||
}();
|
||||
|
||||
@ -27,12 +27,12 @@ var handlers = function () {
|
||||
|
||||
var tokenUtil = require("/app/modules/oauth/token-handler-utils.js")["utils"];
|
||||
var constants = require("/app/modules/constants.js");
|
||||
var devicemgtProps = require("/app/conf/reader/main.js")["conf"];
|
||||
var devicemgtProps = require("/app/modules/conf-reader/main.js")["conf"];
|
||||
|
||||
var publicMethods = {};
|
||||
var privateMethods = {};
|
||||
|
||||
publicMethods.setupTokenPairByPasswordGrantType = 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 of logged in user, password or both are missing " +
|
||||
@ -67,7 +67,7 @@ var handlers = function () {
|
||||
}
|
||||
};
|
||||
|
||||
publicMethods.setupTokenPairBySamlGrantType = 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 of logged in user, samlToken or both are missing " +
|
||||
@ -96,7 +96,7 @@ var handlers = function () {
|
||||
}
|
||||
};
|
||||
|
||||
publicMethods.refreshTokenPair = function () {
|
||||
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"]);
|
||||
@ -116,7 +116,7 @@ var handlers = function () {
|
||||
}
|
||||
};
|
||||
|
||||
privateMethods.setUpEncodedTenantBasedClientAppCredentials = function (username) {
|
||||
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 " +
|
||||
|
||||
@ -35,9 +35,9 @@ var invokers = function () {
|
||||
var TOKEN_EXPIRED = "Access token expired";
|
||||
var TOKEN_INVALID = "Invalid input. Access token validation failed";
|
||||
|
||||
var devicemgtProps = require("/app/conf/reader/main.js")["conf"];
|
||||
var devicemgtProps = require("/app/modules/conf-reader/main.js")["conf"];
|
||||
var constants = require("/app/modules/constants.js");
|
||||
var userModule = require("/app/modules/user.js")["userModule"];
|
||||
var userModule = require("/app/modules/business-controllers/user.js")["userModule"];
|
||||
var tokenUtil = require("/app/modules/oauth/token-handlers.js")["handlers"];
|
||||
|
||||
/**
|
||||
@ -67,7 +67,7 @@ var invokers = function () {
|
||||
* @param responseCallback a function to be called with response retrieved.
|
||||
* @param count a counter which hold the number of recursive execution
|
||||
*/
|
||||
privateMethods.execute = function (httpMethod, requestPayload, endpoint, responseCallback, count) {
|
||||
privateMethods["execute"] = function (httpMethod, requestPayload, endpoint, responseCallback, count) {
|
||||
var xmlHttpRequest = new XMLHttpRequest();
|
||||
|
||||
xmlHttpRequest.open(httpMethod, endpoint);
|
||||
@ -81,8 +81,8 @@ var invokers = function () {
|
||||
response.sendRedirect(devicemgtProps["appContext"] + "login");
|
||||
});
|
||||
} else {
|
||||
xmlHttpRequest.
|
||||
setRequestHeader(constants["AUTHORIZATION_HEADER"], constants["BEARER_PREFIX"] + accessToken);
|
||||
xmlHttpRequest.setRequestHeader(constants["AUTHORIZATION_HEADER"],
|
||||
constants["BEARER_PREFIX"] + accessToken);
|
||||
}
|
||||
}
|
||||
|
||||
@ -95,11 +95,10 @@ var invokers = function () {
|
||||
log.debug("Service Invoker-URL: " + endpoint);
|
||||
log.debug("Service Invoker-Method: " + httpMethod);
|
||||
|
||||
log.info("Request : " + httpMethod + " " + endpoint);
|
||||
log.info("Request payload if any : " + stringify(requestPayload));
|
||||
log.info("Response status : " + xmlHttpRequest.status);
|
||||
log.info("Response payload if any : " + xmlHttpRequest.responseText);
|
||||
//log.info("Response headers : " + xmlHttpRequest.getAllResponseHeaders());
|
||||
// log.info("Request : " + httpMethod + " " + endpoint);
|
||||
// log.info("Request payload if any : " + stringify(requestPayload));
|
||||
// log.info("Response status : " + xmlHttpRequest.status);
|
||||
// log.info("Response payload if any : " + xmlHttpRequest.responseText);
|
||||
|
||||
if (xmlHttpRequest.status == 401 && (xmlHttpRequest.responseText == TOKEN_EXPIRED ||
|
||||
xmlHttpRequest.responseText == TOKEN_INVALID ) && count < 5) {
|
||||
@ -117,7 +116,7 @@ var invokers = function () {
|
||||
* @param endpoint Backend REST API url.
|
||||
* @param responseCallback a function to be called with response retrieved.
|
||||
*/
|
||||
privateMethods.initiateXMLHTTPRequest = function (httpMethod, requestPayload, endpoint, responseCallback) {
|
||||
privateMethods["initiateXMLHTTPRequest"] = function (httpMethod, requestPayload, endpoint, responseCallback) {
|
||||
return privateMethods.execute(httpMethod, requestPayload, endpoint, responseCallback, 0);
|
||||
};
|
||||
|
||||
@ -126,7 +125,7 @@ var invokers = function () {
|
||||
* @param endpoint Backend REST API url.
|
||||
* @param responseCallback a function to be called with response retrieved.
|
||||
*/
|
||||
publicXMLHTTPInvokers.get = function (endpoint, responseCallback) {
|
||||
publicXMLHTTPInvokers["get"] = function (endpoint, responseCallback) {
|
||||
var requestPayload = null;
|
||||
return privateMethods.initiateXMLHTTPRequest(constants["HTTP_GET"], requestPayload, endpoint, responseCallback);
|
||||
};
|
||||
@ -137,7 +136,7 @@ var invokers = function () {
|
||||
* @param requestPayload payload/data if exists which is needed to be send.
|
||||
* @param responseCallback a function to be called with response retrieved.
|
||||
*/
|
||||
publicXMLHTTPInvokers.post = function (endpoint, requestPayload, responseCallback) {
|
||||
publicXMLHTTPInvokers["post"] = function (endpoint, requestPayload, responseCallback) {
|
||||
return privateMethods.initiateXMLHTTPRequest(constants["HTTP_POST"], requestPayload, endpoint, responseCallback);
|
||||
};
|
||||
|
||||
@ -147,7 +146,7 @@ var invokers = function () {
|
||||
* @param requestPayload payload/data if exists which is needed to be send.
|
||||
* @param responseCallback a function to be called with response retrieved.
|
||||
*/
|
||||
publicXMLHTTPInvokers.put = function (endpoint, requestPayload, responseCallback) {
|
||||
publicXMLHTTPInvokers["put"] = function (endpoint, requestPayload, responseCallback) {
|
||||
return privateMethods.initiateXMLHTTPRequest(constants["HTTP_PUT"], requestPayload, endpoint, responseCallback);
|
||||
};
|
||||
|
||||
@ -156,7 +155,7 @@ var invokers = function () {
|
||||
* @param endpoint Backend REST API url.
|
||||
* @param responseCallback a function to be called with response retrieved.
|
||||
*/
|
||||
publicXMLHTTPInvokers.delete = function (endpoint, responseCallback) {
|
||||
publicXMLHTTPInvokers["delete"] = function (endpoint, responseCallback) {
|
||||
var requestPayload = null;
|
||||
return privateMethods.initiateXMLHTTPRequest(constants["HTTP_DELETE"], requestPayload, endpoint, responseCallback);
|
||||
};
|
||||
@ -176,8 +175,10 @@ var invokers = function () {
|
||||
* @param errorCallback a function to be called if en error is reserved.
|
||||
* @param soapVersion soapVersion which need to used.
|
||||
*/
|
||||
privateMethods.initiateWSRequest = function (action, endpoint, successCallback, errorCallback, soapVersion, payload) {
|
||||
privateMethods["initiateWSRequest"] = function (action, endpoint, successCallback,
|
||||
errorCallback, soapVersion, payload) {
|
||||
var ws = require("ws");
|
||||
//noinspection JSUnresolvedFunction
|
||||
var wsRequest = new ws.WSRequest();
|
||||
var options = [];
|
||||
if (devicemgtProps["isOAuthEnabled"]) {
|
||||
@ -222,8 +223,10 @@ var invokers = function () {
|
||||
* @param errorCallback a function to be called if en error is reserved.
|
||||
* @param soapVersion soapVersion which need to used.
|
||||
*/
|
||||
publicWSInvokers.soapRequest = function (action, requestPayload, endpoint, successCallback, errorCallback, soapVersion) {
|
||||
return privateMethods.initiateWSRequest(action, endpoint, successCallback, errorCallback, soapVersion, requestPayload);
|
||||
publicWSInvokers["soapRequest"] = function (action, requestPayload, endpoint,
|
||||
successCallback, errorCallback, soapVersion) {
|
||||
return privateMethods.initiateWSRequest(action, endpoint, successCallback,
|
||||
errorCallback, soapVersion, requestPayload);
|
||||
};
|
||||
|
||||
/**
|
||||
@ -240,37 +243,46 @@ var invokers = function () {
|
||||
* @param successCallback a function to be called if the respond if successful.
|
||||
* @param errorCallback a function to be called if en error is reserved.
|
||||
*/
|
||||
privateMethods.initiateHTTPClientRequest = function (method, url, successCallback, errorCallback, payload) {
|
||||
privateMethods["initiateHTTPClientRequest"] = function (method, url, successCallback, errorCallback, payload) {
|
||||
//noinspection JSUnresolvedVariable
|
||||
var HttpClient = Packages.org.apache.commons.httpclient.HttpClient;
|
||||
var httpMethodObject;
|
||||
switch (method) {
|
||||
case constants["HTTP_GET"]:
|
||||
//noinspection JSUnresolvedVariable
|
||||
var GetMethod = Packages.org.apache.commons.httpclient.methods.GetMethod;
|
||||
httpMethodObject = new GetMethod(url);
|
||||
break;
|
||||
case constants["HTTP_POST"]:
|
||||
//noinspection JSUnresolvedVariable
|
||||
var PostMethod = Packages.org.apache.commons.httpclient.methods.PostMethod;
|
||||
httpMethodObject = new PostMethod(url);
|
||||
break;
|
||||
case constants["HTTP_PUT"]:
|
||||
//noinspection JSUnresolvedVariable
|
||||
var PutMethod = Packages.org.apache.commons.httpclient.methods.PutMethod;
|
||||
httpMethodObject = new PutMethod(url);
|
||||
break;
|
||||
case constants["HTTP_DELETE"]:
|
||||
//noinspection JSUnresolvedVariable
|
||||
var DeleteMethod = Packages.org.apache.commons.httpclient.methods.DeleteMethod;
|
||||
httpMethodObject = new DeleteMethod(url);
|
||||
break;
|
||||
default:
|
||||
//noinspection JSUnresolvedFunction
|
||||
throw new IllegalArgumentException("Invalid HTTP request method: " + method);
|
||||
}
|
||||
//noinspection JSUnresolvedVariable
|
||||
var Header = Packages.org.apache.commons.httpclient.Header;
|
||||
var header = new Header();
|
||||
header.setName(constants["CONTENT_TYPE_IDENTIFIER"]);
|
||||
header.setValue(constants["APPLICATION_JSON"]);
|
||||
//noinspection JSUnresolvedFunction
|
||||
httpMethodObject.addRequestHeader(header);
|
||||
header = new Header();
|
||||
header.setName(constants["ACCEPT_IDENTIFIER"]);
|
||||
header.setValue(constants["APPLICATION_JSON"]);
|
||||
//noinspection JSUnresolvedFunction
|
||||
httpMethodObject.addRequestHeader(header);
|
||||
|
||||
if (devicemgtProps["isOAuthEnabled"]) {
|
||||
@ -279,25 +291,33 @@ var invokers = function () {
|
||||
header = new Header();
|
||||
header.setName(constants["AUTHORIZATION_HEADER"]);
|
||||
header.setValue(constants["BEARER_PREFIX"] + accessToken);
|
||||
//noinspection JSUnresolvedFunction
|
||||
httpMethodObject.addRequestHeader(header);
|
||||
} else {
|
||||
response.sendRedirect(devicemgtProps["appContext"] + "login");
|
||||
}
|
||||
}
|
||||
//noinspection JSUnresolvedFunction
|
||||
var stringRequestEntity = new StringRequestEntity(stringify(payload));
|
||||
//noinspection JSUnresolvedFunction
|
||||
httpMethodObject.setRequestEntity(stringRequestEntity);
|
||||
var client = new HttpClient();
|
||||
try {
|
||||
//noinspection JSUnresolvedFunction
|
||||
client.executeMethod(httpMethodObject);
|
||||
//noinspection JSUnresolvedFunction
|
||||
var status = httpMethodObject.getStatusCode();
|
||||
if (status == 200) {
|
||||
//noinspection JSUnresolvedFunction
|
||||
return successCallback(httpMethodObject.getResponseBody());
|
||||
} else {
|
||||
//noinspection JSUnresolvedFunction
|
||||
return errorCallback(httpMethodObject.getResponseBody());
|
||||
}
|
||||
} catch (e) {
|
||||
return errorCallback(response);
|
||||
} finally {
|
||||
//noinspection JSUnresolvedFunction
|
||||
method.releaseConnection();
|
||||
}
|
||||
};
|
||||
@ -308,7 +328,7 @@ var invokers = function () {
|
||||
* @param successCallback a function to be called if the respond if successful.
|
||||
* @param errorCallback a function to be called if en error is reserved.
|
||||
*/
|
||||
publicHTTPClientInvokers.get = function (url, successCallback, errorCallback) {
|
||||
publicHTTPClientInvokers["get"] = function (url, successCallback, errorCallback) {
|
||||
var requestPayload = null;
|
||||
return privateMethods.
|
||||
initiateHTTPClientRequest(constants["HTTP_GET"], url, successCallback, errorCallback, requestPayload);
|
||||
@ -321,7 +341,7 @@ var invokers = function () {
|
||||
* @param successCallback a function to be called if the respond if successful.
|
||||
* @param errorCallback a function to be called if en error is reserved.
|
||||
*/
|
||||
publicHTTPClientInvokers.post = function (url, payload, successCallback, errorCallback) {
|
||||
publicHTTPClientInvokers["post"] = function (url, payload, successCallback, errorCallback) {
|
||||
return privateMethods.
|
||||
initiateHTTPClientRequest(constants["HTTP_POST"], url, successCallback, errorCallback, payload);
|
||||
};
|
||||
@ -333,7 +353,7 @@ var invokers = function () {
|
||||
* @param successCallback a function to be called if the respond if successful.
|
||||
* @param errorCallback a function to be called if en error is reserved.
|
||||
*/
|
||||
publicHTTPClientInvokers.put = function (url, payload, successCallback, errorCallback) {
|
||||
publicHTTPClientInvokers["put"] = function (url, payload, successCallback, errorCallback) {
|
||||
return privateMethods.
|
||||
initiateHTTPClientRequest(constants["HTTP_PUT"], url, successCallback, errorCallback, payload);
|
||||
};
|
||||
@ -344,7 +364,7 @@ var invokers = function () {
|
||||
* @param successCallback a function to be called if the respond if successful.
|
||||
* @param errorCallback a function to be called if en error is reserved.
|
||||
*/
|
||||
publicHTTPClientInvokers.delete = function (url, successCallback, errorCallback) {
|
||||
publicHTTPClientInvokers["delete"] = function (url, successCallback, errorCallback) {
|
||||
var requestPayload = null;
|
||||
return privateMethods.
|
||||
initiateHTTPClientRequest(constants["HTTP_DELETE"], url, successCallback, errorCallback, requestPayload);
|
||||
|
||||
@ -1,134 +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.
|
||||
*/
|
||||
|
||||
var operationModule = function () {
|
||||
var log = new Log("/app/modules/operation.js");
|
||||
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 publicMethods = {};
|
||||
var privateMethods = {};
|
||||
|
||||
/**
|
||||
* This method reads the token from the Token client and return the access token.
|
||||
* If the token pair s not set in the session this will send a redirect to the login page.
|
||||
*/
|
||||
function getAccessToken(deviceType, owner, deviceId) {
|
||||
var TokenClient = Packages.org.wso2.carbon.device.mgt.iot.apimgt.TokenClient;
|
||||
var accessTokenClient = new TokenClient(deviceType);
|
||||
var accessTokenInfo = accessTokenClient.getAccessToken(owner, deviceId);
|
||||
return accessTokenInfo.getAccess_token();
|
||||
}
|
||||
|
||||
privateMethods.getOperationsFromFeatures = function (deviceType, operationType) {
|
||||
var url = devicemgtProps["httpsURL"] + constants.ADMIN_SERVICE_CONTEXT + "/features/" + deviceType;
|
||||
var featuresList = serviceInvokers.XMLHttp.get(url, function (responsePayload) {
|
||||
var features = responsePayload;
|
||||
var featureList = [];
|
||||
var feature;
|
||||
for (var i = 0; i < features.length; i++) {
|
||||
feature = {};
|
||||
var analyticStreams = utility.getDeviceTypeConfig(deviceType)["analyticStreams"];
|
||||
if (analyticStreams) {
|
||||
for (var stream in analyticStreams) {
|
||||
if (analyticStreams[stream].name == features[i].name) {
|
||||
feature.ui_unit = analyticStreams[stream].ui_unit;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
feature["operation"] = features[i].code;
|
||||
feature["name"] = features[i].name;
|
||||
feature["description"] = features[i].description;
|
||||
feature["deviceType"] = deviceType;
|
||||
feature["params"] = [];
|
||||
var metaData = features[i].metadataEntries;
|
||||
if (metaData) {
|
||||
for (var j = 0; j < metaData.length; j++) {
|
||||
feature["params"].push(metaData[j].value);
|
||||
}
|
||||
featureList.push(feature);
|
||||
}
|
||||
}
|
||||
return featureList;
|
||||
}, function (responsePayload) {
|
||||
var response = {};
|
||||
response["status"] = "error";
|
||||
return response;
|
||||
}
|
||||
);
|
||||
return featuresList;
|
||||
};
|
||||
|
||||
publicMethods.getControlOperations = function (deviceType) {
|
||||
var operations = privateMethods.getOperationsFromFeatures(deviceType, "operation");
|
||||
for (var op in operations) {
|
||||
var iconPath = utility.getOperationIcon(deviceType, operations[op].operation);
|
||||
if (iconPath) {
|
||||
operations[op]["icon"] = iconPath;
|
||||
}
|
||||
}
|
||||
return operations;
|
||||
};
|
||||
|
||||
publicMethods.getMonitorOperations = function (deviceType) {
|
||||
return privateMethods.getOperationsFromFeatures(deviceType, "monitor");
|
||||
};
|
||||
|
||||
publicMethods.handlePOSTOperation = function (deviceType, operation, deviceId, params) {
|
||||
var user = session.get(constants.USER_SESSION_KEY);
|
||||
var endPoint = devicemgtProps["httpsURL"] + '/' + deviceType + "/controller/" + operation;
|
||||
var header = '{"owner":"' + user.username + '","deviceId":"' + deviceId +
|
||||
'","protocol":"mqtt", "sessionId":"' + session.getId() + '", "' +
|
||||
constants.AUTHORIZATION_HEADER + '":"' + constants.BEARER_PREFIX +
|
||||
getAccessToken(deviceType, user.username, deviceId) + '"}';
|
||||
return post(endPoint, params, JSON.parse(header), "json");
|
||||
};
|
||||
|
||||
publicMethods.handleGETOperation = function (deviceType, operation, operationName, deviceId) {
|
||||
var user = session.get(constants.USER_SESSION_KEY);
|
||||
var endPoint = devicemgtProps["httpsURL"] + '/' + deviceType + "/controller/" + operation;
|
||||
var header = '{"owner":"' + user.username + '","deviceId":"' + deviceId +
|
||||
'","protocol":"mqtt", "' + constants.AUTHORIZATION_HEADER + '":"' +
|
||||
constants.BEARER_PREFIX + getAccessToken(deviceType, user.username, deviceId) +
|
||||
'"}';
|
||||
var result = get(endPoint, {}, JSON.parse(header), "json");
|
||||
if (result.data) {
|
||||
var values = result.data.sensorValue.split(',');
|
||||
if (operationName == 'gps') {
|
||||
result.data.map = {
|
||||
lat: parseFloat(values[0]),
|
||||
lng: parseFloat(values[1])
|
||||
}
|
||||
} else {
|
||||
var sqSum = 0;
|
||||
for (var v in values) {
|
||||
sqSum += Math.pow(values[v], 2);
|
||||
}
|
||||
result.data[operationName] = Math.sqrt(sqSum);
|
||||
}
|
||||
delete result.data['sensorValue'];
|
||||
}
|
||||
return result;
|
||||
};
|
||||
|
||||
return publicMethods;
|
||||
}();
|
||||
@ -20,7 +20,7 @@ var utility;
|
||||
utility = function () {
|
||||
|
||||
var constants = require('/app/modules/constants.js');
|
||||
var devicemgtProps = require("/app/conf/reader/main.js")["conf"];
|
||||
var devicemgtProps = require("/app/modules/conf-reader/main.js")["conf"];
|
||||
var log = new Log("/app/modules/utility.js");
|
||||
var JavaClass = Packages.java.lang.Class;
|
||||
var PrivilegedCarbonContext = Packages.org.wso2.carbon.context.PrivilegedCarbonContext;
|
||||
|
||||
@ -19,9 +19,9 @@
|
||||
function onRequest(context) {
|
||||
var constants = require("/app/modules/constants.js");
|
||||
var user = session.get(constants.USER_SESSION_KEY);
|
||||
var userModule = require("/app/modules/user.js").userModule;
|
||||
var userModule = require("/app/modules/business-controllers/user.js")["userModule"];
|
||||
var permissions = userModule.getUIPermissions();
|
||||
var devicemgtProps = require("/app/conf/reader/main.js")["conf"];
|
||||
var devicemgtProps = require("/app/modules/conf-reader/main.js")["conf"];
|
||||
|
||||
if (!permissions.VIEW_DASHBOARD) {
|
||||
response.sendRedirect(constants.WEB_APP_CONTEXT + "/devices");
|
||||
@ -31,9 +31,9 @@ function onRequest(context) {
|
||||
var page = {};
|
||||
page.permissions = permissions;
|
||||
page.enrollmentURL = devicemgtProps.enrollmentURL;
|
||||
var deviceModule = require("/app/modules/device.js").deviceModule;
|
||||
var groupModule = require("/app/modules/group.js").groupModule;
|
||||
var policyModule = require("/app/modules/policy.js").policyModule;
|
||||
var deviceModule = require("/app/modules/business-controllers/device.js")["deviceModule"];
|
||||
var groupModule = require("/app/modules/business-controllers/group.js")["groupModule"];
|
||||
var policyModule = require("/app/modules/business-controllers/policy.js")["policyModule"];
|
||||
|
||||
page.device_count = deviceModule.getDevicesCount();
|
||||
page.group_count = groupModule.getGroupCount();
|
||||
|
||||
@ -18,8 +18,8 @@
|
||||
|
||||
function onRequest(context) {
|
||||
var constants = require("/app/modules/constants.js");
|
||||
var userModule = require("/app/modules/user.js").userModule;
|
||||
var deviceModule = require("/app/modules/device.js").deviceModule;
|
||||
var userModule = require("/app/modules/business-controllers/user.js")["userModule"];
|
||||
var deviceModule = require("/app/modules/business-controllers/device.js")["deviceModule"];
|
||||
|
||||
var groupName = request.getParameter("groupName");
|
||||
var groupOwner = request.getParameter("groupOwner");
|
||||
@ -42,7 +42,7 @@ function onRequest(context) {
|
||||
page.currentUser = currentUser;
|
||||
var deviceCount = 0;
|
||||
if (groupName && groupOwner) {
|
||||
var groupModule = require("/app/modules/group.js").groupModule;
|
||||
var groupModule = require("/app/modules/business-controllers/group.js")["groupModule"];
|
||||
deviceCount = groupModule.getGroupDeviceCount(groupName, groupOwner);
|
||||
} else {
|
||||
deviceCount = deviceModule.getDevicesCount();
|
||||
|
||||
@ -18,7 +18,7 @@
|
||||
|
||||
function onRequest(context) {
|
||||
var utility = require("/app/modules/utility.js").utility;
|
||||
var groupModule = require("/app/modules/group.js").groupModule;
|
||||
var groupModule = require("/app/modules/business-controllers/group.js")["groupModule"];
|
||||
var groupName = context.uriParams.name;
|
||||
var groupOwner = context.uriParams.owner;
|
||||
var devices = groupModule.getGroupDevices(groupName, groupOwner).data;
|
||||
|
||||
@ -23,7 +23,7 @@
|
||||
* @returns {*} A context object that returns the dynamic state of this page to be presented
|
||||
*/
|
||||
function onRequest(context) {
|
||||
var devicemgtProps = require("/app/conf/reader/main.js")["conf"];
|
||||
var devicemgtProps = require("/app/modules/conf-reader/main.js")["conf"];
|
||||
var page = {};
|
||||
page["groupNameJSRegEx"] = devicemgtProps.groupValidationConfig.groupNameJSRegEx;
|
||||
page["groupNameRegExViolationErrorMsg"] = devicemgtProps.groupValidationConfig.groupNameRegExViolationErrorMsg;
|
||||
|
||||
@ -17,8 +17,8 @@
|
||||
*/
|
||||
|
||||
function onRequest(context) {
|
||||
var groupModule = require("/app/modules/group.js").groupModule;
|
||||
var userModule = require("/app/modules/user.js").userModule;
|
||||
var groupModule = require("/app/modules/business-controllers/group.js")["groupModule"];
|
||||
var userModule = require("/app/modules/business-controllers/user.js")["userModule"];
|
||||
var constants = require("/app/modules/constants.js");
|
||||
var currentUser = session.get(constants.USER_SESSION_KEY);
|
||||
var page = {};
|
||||
|
||||
@ -28,8 +28,8 @@ function onRequest(context) {
|
||||
}
|
||||
});
|
||||
var page = {};
|
||||
var policyModule = require("/app/modules/policy.js")["policyModule"];
|
||||
var userModule = require("/app/modules/user.js")["userModule"];
|
||||
var policyModule = require("/app/modules/business-controllers/group.js")["groupModule"];
|
||||
var userModule = require("/app/modules/business-controllers/user.js")["userModule"];
|
||||
var response = policyModule.getAllPolicies();
|
||||
if (response["status"] == "success") {
|
||||
var policyListToView = response["content"];
|
||||
|
||||
@ -25,8 +25,8 @@
|
||||
function onRequest(context) {
|
||||
//var log = new Log("/app/pages/cdmf.page.user.create server-side js");
|
||||
|
||||
var userModule = require("/app/modules/user.js")["userModule"];
|
||||
var devicemgtProps = require("/app/conf/reader/main.js")["conf"];
|
||||
var userModule = require("/app/modules/business-controllers/user.js")["userModule"];
|
||||
var devicemgtProps = require("/app/modules/conf-reader/main.js")["conf"];
|
||||
|
||||
var page = {};
|
||||
var response = userModule.getRolesByUserStore();
|
||||
|
||||
@ -17,10 +17,10 @@
|
||||
*/
|
||||
|
||||
function onRequest(context) {
|
||||
var userModule = require("/app/modules/user.js").userModule;
|
||||
var userModule = require("/app/modules/business-controllers/user.js")["userModule"];
|
||||
var userName = request.getParameter("username");
|
||||
var user = userModule.getUser(userName)["content"];
|
||||
var devicemgtProps = require("/app/conf/reader/main.js")["conf"];
|
||||
var devicemgtProps = require("/app/modules/conf-reader/main.js")["conf"];
|
||||
if (user) {
|
||||
var title;
|
||||
if (user.firstname || user.lastname) {
|
||||
|
||||
@ -17,7 +17,7 @@
|
||||
*/
|
||||
|
||||
function onRequest(context) {
|
||||
var userModule = require("/app/modules/user.js").userModule;
|
||||
var userModule = require("/app/modules/business-controllers/user.js")["userModule"];
|
||||
var username = request.getParameter("username");
|
||||
var user = userModule.getUser(username)["content"];
|
||||
var userModule = require("/app/modules/user.js")["userModule"];
|
||||
@ -38,7 +38,7 @@ function onRequest(context) {
|
||||
if (response["status"] == "success") {
|
||||
userRoles = response["content"];
|
||||
}
|
||||
var deviceModule = require("/app/modules/device.js").deviceModule;
|
||||
var deviceModule = require("/app/modules/business-controllers/device.js")["deviceModule"];
|
||||
devices = deviceModule.getDevices(userName);
|
||||
}
|
||||
return {"user": user, "userRoles": userRoles, "devices": devices};
|
||||
|
||||
@ -29,7 +29,7 @@ function onRequest(context) {
|
||||
|
||||
var page = {};
|
||||
var userModule = require("/app/modules/user.js")["userModule"];
|
||||
var deviceMgtProps = require("/app/conf/reader/main.js")["conf"];
|
||||
var deviceMgtProps = require("/app/modules/conf-reader/main.js")["conf"];
|
||||
|
||||
page["adminUser"] = deviceMgtProps["adminUser"];
|
||||
page["permissions"] = userModule.getUIPermissions();
|
||||
|
||||
@ -27,10 +27,10 @@ function onRequest (context) {
|
||||
}
|
||||
|
||||
//For QR Code
|
||||
var userModule = require("/app/modules/user.js").userModule;
|
||||
var userModule = require("/app/modules/business-controllers/user.js")["userModule"];
|
||||
var constants = require("/app/modules/constants.js");
|
||||
var permissions = userModule.getUIPermissions();
|
||||
var deviceMgtProps = require("/app/conf/reader/main.js")["conf"];
|
||||
var deviceMgtProps = require("/app/modules/conf-reader/main.js")["conf"];
|
||||
context.permissions = permissions;
|
||||
context["enrollmentURL"] = deviceMgtProps.enrollmentURL;
|
||||
|
||||
|
||||
@ -23,7 +23,7 @@ function onRequest(context) {
|
||||
var DTYPE_CONF_DEVICE_TYPE_LABEL_KEY = "label";
|
||||
|
||||
var viewModel = {};
|
||||
var deviceModule = require("/app/modules/device.js").deviceModule;
|
||||
var deviceModule = require("/app/modules/business-controllers/device.js")["deviceModule"];
|
||||
var utility = require("/app/modules/utility.js").utility;
|
||||
var deviceTypes = deviceModule.getDeviceTypes();
|
||||
|
||||
|
||||
@ -22,7 +22,7 @@ function onRequest(context) {
|
||||
var deviceId = request.getParameter("id");
|
||||
|
||||
if (deviceType != null && deviceType != undefined && deviceId != null && deviceId != undefined) {
|
||||
var deviceModule = require("/app/modules/device.js").deviceModule;
|
||||
var deviceModule = require("/app/modules/business-controllers/device.js")["deviceModule"];
|
||||
var device = deviceModule.viewDevice(deviceType, deviceId);
|
||||
|
||||
if (device) {
|
||||
|
||||
@ -17,7 +17,7 @@
|
||||
*/
|
||||
|
||||
function onRequest(context){
|
||||
var userModule = require("/app/modules/user.js").userModule;
|
||||
var userModule = require("/app/modules/business-controllers/user.js")["userModule"];
|
||||
var constants = require("/app/modules/constants.js");
|
||||
var permissions = [];
|
||||
if(userModule.isAuthorized("/permission/admin/device-mgt/emm-admin/notifications/list")){
|
||||
|
||||
@ -46,7 +46,7 @@ $(document).ready(function () {
|
||||
$("#ast-container").on("click", ".new-notification", function(e){
|
||||
var notificationId = $(this).data("id");
|
||||
var redirectUrl = $(this).data("url");
|
||||
var getNotificationsAPI = "/devicemgt_admin/notifications/"+notificationId+"/CHECKED";
|
||||
var getNotificationsAPI = "/api/device-mgt/v1.0/notifications/"+notificationId+"/CHECKED";
|
||||
var errorMsgWrapper = "#error-msg";
|
||||
var errorMsg = "#error-msg span";
|
||||
invokerUtil.put(
|
||||
@ -87,7 +87,7 @@ function loadNotifications(){
|
||||
var deviceListingSrc = deviceListing.attr("src");
|
||||
var currentUser = deviceListing.data("currentUser");
|
||||
$.template("notification-listing", deviceListingSrc, function (template) {
|
||||
var serviceURL = "/devicemgt_admin/notifications";
|
||||
var serviceURL = "/api/device-mgt/v1.0/notifications";
|
||||
var successCallback = function (data) {
|
||||
var viewModel = {};
|
||||
data = JSON.parse(data);
|
||||
|
||||
@ -17,8 +17,8 @@
|
||||
*/
|
||||
|
||||
function onRequest(context) {
|
||||
var utility = require("/app/modules/device.js").utility;
|
||||
var deviceModule = require("/app/modules/device.js").deviceModule;
|
||||
var utility = require("/app/modules/utility.js").utility;
|
||||
var deviceModule = require("/app/modules/business-controllers/device.js")["deviceModule"];
|
||||
//get all device types
|
||||
var data = deviceModule.getDeviceTypes();
|
||||
var deviceTypesArray = [];
|
||||
|
||||
@ -21,7 +21,7 @@ function onRequest(context) {
|
||||
var DTYPE_CONF_DEVICE_TYPE_LABEL_KEY = "label";
|
||||
|
||||
var utility = require("/app/modules/utility.js").utility;
|
||||
var userModule = require("/app/modules/user.js")["userModule"];
|
||||
var userModule = require("/app/modules/business-controllers/user.js")["userModule"];
|
||||
|
||||
var types = {};
|
||||
types["types"] = [];
|
||||
|
||||
@ -27,7 +27,7 @@ function onRequest(context) {
|
||||
return options.fn(this);
|
||||
}
|
||||
});
|
||||
var policyModule = require("/app/modules/policy.js")["policyModule"];
|
||||
var policyModule = require("/app/modules/business-controllers/group.js")["groupModule"];
|
||||
var response = policyModule.getAllPolicies();
|
||||
if (response["status"] == "success") {
|
||||
var policyListToView = response["content"];
|
||||
|
||||
@ -23,8 +23,8 @@
|
||||
* @returns {*} A context object that returns the dynamic state of this page to be presented
|
||||
*/
|
||||
function onRequest(context) {
|
||||
var userModule = require("/app/modules/user.js")["userModule"];
|
||||
var deviceMgtProps = require("/app/conf/reader/main.js")["conf"];
|
||||
var userModule = require("/app/modules/business-controllers/user.js")["userModule"];
|
||||
var deviceMgtProps = require("/app/modules/conf-reader/main.js")["conf"];
|
||||
var response = userModule.getRoles();
|
||||
if (response["status"] == "success") {
|
||||
context["roles"] = response["content"];
|
||||
|
||||
@ -23,7 +23,7 @@
|
||||
* @returns {*} A context object that returns the dynamic state of this page to be presented
|
||||
*/
|
||||
function onRequest(context) {
|
||||
var userModule = require("/app/modules/user.js")["userModule"];
|
||||
var userModule = require("/app/modules/business-controllers/user.js")["userModule"];
|
||||
var uri = request.getRequestURI();
|
||||
var uriMatcher = new URIMatcher(String(uri));
|
||||
var isMatched = uriMatcher.match("/{context}/roles/edit-role-permission/{rolename}");
|
||||
|
||||
@ -23,8 +23,8 @@
|
||||
* @returns {*} A context object that returns the dynamic state of this page to be presented
|
||||
*/
|
||||
function onRequest(context) {
|
||||
var userModule = require("/app/modules/user.js")["userModule"];
|
||||
var deviceMgtProps = require("/app/conf/reader/main.js")["conf"];
|
||||
var userModule = require("/app/modules/business-controllers/user.js")["userModule"];
|
||||
var deviceMgtProps = require("/app/modules/conf-reader/main.js")["conf"];
|
||||
var uri = request.getRequestURI();
|
||||
var uriMatcher = new URIMatcher(String(uri));
|
||||
var isMatched = uriMatcher.match("/{context}/role/edit/{rolename}");
|
||||
|
||||
@ -17,7 +17,7 @@
|
||||
*/
|
||||
|
||||
function onRequest(context) {
|
||||
var userModule = require("/app/modules/user.js")["userModule"];
|
||||
var userModule = require("/app/modules/business-controllers/user.js")["userModule"];
|
||||
var response = userModule.getUsers();
|
||||
var users = {};
|
||||
context["permissions"] = userModule.getUIPermissions();
|
||||
@ -30,7 +30,7 @@ function onRequest(context) {
|
||||
if (userModule.isAuthorized("/permission/admin/device-mgt/roles/remove")) {
|
||||
context["removePermitted"] = true;
|
||||
}
|
||||
var deviceMgtProps = require("/app/conf/reader/main.js")["conf"];
|
||||
var deviceMgtProps = require("/app/modules/conf-reader/main.js")["conf"];
|
||||
context["appContext"] = deviceMgtProps.appContext;
|
||||
context["adminRole"] = deviceMgtProps.adminRole;
|
||||
return context;
|
||||
|
||||
@ -27,8 +27,8 @@ function onRequest(context) {
|
||||
return options.fn(this);
|
||||
}
|
||||
});
|
||||
var userModule = require("/app/modules/user.js")["userModule"];
|
||||
var mdmProps = require('/app/conf/reader/main.js')["conf"];
|
||||
var userModule = require("/app/modules/business-controllers/user.js")["userModule"];
|
||||
var mdmProps = require("/app/modules/conf-reader/main.js")["conf"];
|
||||
var constants = require("/app/modules/constants.js");
|
||||
var uiPermissions = userModule.getUIPermissions();
|
||||
context["permissions"] = uiPermissions;
|
||||
|
||||
Loading…
Reference in New Issue
Block a user