/*!
GPII Access Requester
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 = fluid || require("infusion"),
gpii = fluid.registerNamespace("gpii"),
$ = fluid.registerNamespace("jQuery");
/*
* Access Requester provides getAccessToken(userToken) API that returns a promise object whose resolved value is the access token.
* When this API is called, the access requester does:
* 1. Retrieves client credentials;
* 2. Communicate with GPII Cloud end point via [resource owner GPII token grant](https://wiki.gpii.net/w/GPII_OAuth_2_Guide#Resource_Owner_GPII_Token_Grant);
* 3. Retrieves and returns a promise object whose resolved value contains the access token responded by the GPII Cloud /access_token endpoint;
*
* Access Requester requires these input options:
* @url {String} The GPII cloud http end point to request access tokens granted by OAuth2 resource owner GPII token grant.
* such as https://flowmanager.gpii.net/access_token
* @clientCredentialDataSourceGrade {String or Array of Strings} The grade name of the implementation of "clientCredentialDataSource" subcomponent
* that provides a get() API that returns a promise object whose resolved value is the client credential.
*/
fluid.defaults("gpii.accessRequester", {
gradeNames: ["fluid.component"],
// These options must be provided by integrators
url: "/access_token", // Must be provided by integrators. The API endpoint provided by the authorization server to request access tokens via Resource Owner GPII Token Grant
clientCredentialDataSourceGrade: null, // Must be provided by integrators
// End of integrators provided options
distributeOptions: {
source: "{that}.options.clientCredentialDataSourceGrade",
target: "{that > clientCredentialDataSource}.options.gradeNames"
},
components: {
clientCredentialDataSource: {
type: "fluid.component"
},
accessTokenDataSource: {
type: "kettle.dataSource.URL",
options: {
url: "{accessRequester}.options.url",
writable: true,
writeMethod: "POST",
dataSourceModel: {
grant_type: "password",
password: "dummy"
}
}
}
},
invokers: {
getAccessToken: {
funcName: "gpii.accessRequester.getAccessToken",
args: ["{that}.clientCredentialDataSource", "{that}.accessTokenDataSource", "{arguments}.0"]
// userToken
}
}
});
gpii.accessRequester.getAccessToken = function (clientCredentialDataSource, accessTokenDataSource, userToken) {
var promiseTogo = fluid.promise();
var clientCredentialPromise = clientCredentialDataSource.get();
clientCredentialPromise.then(function (clientCredential) {
var accessTokenRequestParams = $.extend({}, accessTokenDataSource.options.dataSourceModel, clientCredential, {
username: userToken
});
var accessTokenPromise = accessTokenDataSource.set(null, accessTokenRequestParams);
fluid.promise.follow(accessTokenPromise, promiseTogo);
}, function (err) {
promiseTogo.reject(err);
});
return promiseTogo;
};
/***********************************
Client Credential File Data Source
***********************************/
// The client credential is read from a file
fluid.defaults("gpii.accessRequester.clientCredentialDataSource.file", {
gradeNames: ["kettle.dataSource.file.moduleTerms"],
path: "%gpii-universal/testData/clientCredentials/pilot.json"
});
|