/*
* GPII Untrusted Settings Get Handler
*
* Copyright 2017 OCAD University
*
* Licensed under the New BSD license. You may not use this file except in
* compliance with this License.
*
* You may obtain a copy of the License at
* https://github.com/GPII/universal/blob/master/LICENSE.txt
*/
"use strict";
var fluid = require("infusion"),
gpii = fluid.registerNamespace("gpii");
// Get settings in the ontology of preferences from the online flowmanager.
// These settings are untransformed lifecycle instructions.
// See [an example of the return payload of this endpoint](https://github.com/GPII/gpii-payloads/blob/master/CloudBasedFlowManagerUntrustedSettings.md#user-content-return-payload).
fluid.defaults("gpii.flowManager.cloudBased.untrustedSettings.get.handler", {
gradeNames: ["kettle.request.http", "gpii.flowManager.matchMakingRequest"],
invokers: {
handleRequest: {
funcName: "gpii.flowManager.cloudBased.untrustedSettings.get.handleRequest",
args: [
"{request}",
"{request}.req.params.userToken",
"{request}.req.params.device",
"{gpii.flowManager.cloudBased.oauth2}.authGrantFinder"
]
},
matchToUntrustedSettings: {
funcName: "gpii.flowManager.cloudBased.matchToUntrustedSettings",
args: ["{arguments}.0", "{request}.events.onSuccess"]
}
},
listeners: {
onMatchDone: "{that}.matchToUntrustedSettings"
}
});
gpii.flowManager.cloudBased.untrustedSettings.get.handleRequest = function (request, userToken, deviceString, authGrantFinder) {
// Verify the access token
var authorizationPromise = gpii.oauth2.getAuthorization(request.req, authGrantFinder);
authorizationPromise.then(function (authorization) {
if (authorization && authorization.gpiiToken === userToken && authorization.allowUntrustedSettingsGet) {
gpii.flowManager.cloudBased.settings.handleRequest(request.events.onError, userToken, request, deviceString);
} else {
fluid.log("CloudBased flowManager: unauthorized GET request at /untrusted-settings due to one of these reasons: 1. authorization record is missing; 2. gpiiToken associated with the authorization does not match the in-used token " + userToken + "; 3. the access token is unauthorized for using GET method at /untrusted-settings endpoint.");
request.events.onError.fire(gpii.oauth2.errors.unauthorized);
return;
}
}, function (error) {
fluid.log("CloudBased flowManager: GET request at /untrusted-settings for the token (" + userToken + ") failed with error: ", error);
request.events.onError.fire(gpii.oauth2.errors.unauthorized);
return;
});
};
gpii.flowManager.cloudBased.matchToUntrustedSettings = function (finalPayload, event) {
var settings = fluid.filterKeys(finalPayload, [
"userToken", "activeContextName", "preferences",
"activeConfiguration", "solutionsRegistryEntries", "matchMakerOutput"
]);
fluid.log("cloudBased flowManager: /untrusted-settings endpoint sending settings ", settings);
event.fire(settings);
};
|