All files / universal/gpii/node_modules/accessRequester/src accessRequester.js

100% Statements 14/14
100% Branches 2/2
100% Functions 3/3
100% Lines 14/14

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94                            1x 1x 1x                             1x                                                                           1x 24x 24x 24x 23x     23x 23x   1x   24x               1x        
/*!
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"
});