All files / universal/gpii/node_modules/gpii-oauth2/gpii-oauth2-authz-server/src AuthGrantFinder.js

100% Statements 13/13
100% Branches 8/8
100% Functions 3/3
100% Lines 13/13

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                                3x   3x       3x   3x                                   3x 29x 29x     29x 29x 23x   21x               29x     29x      
/*!
Copyright 2016-2017 OCAD university
 
Licensed under the New BSD license. You may not use this file except in
compliance with this License.
 
The research leading to these results has received funding from the European Union's
Seventh Framework Programme (FP7/2007-2013) under grant agreement no. 289016.
 
You may obtain a copy of the License at
https://github.com/GPII/universal/blob/master/LICENSE.txt
*/
 
/* eslint-env browser */
/* eslint strict: ["error", "function"] */
 
var fluid = fluid || require("infusion");
 
(function () {
 
    "use strict";
 
    var gpii = fluid.registerNamespace("gpii");
 
    fluid.defaults("gpii.oauth2.authGrantFinder", {
        gradeNames: ["fluid.component"],
        components: {
            authorizationService: {
                type: "gpii.oauth2.authorizationService"
            }
        },
        invokers: {
            getGrantForAccessToken: {
                funcName: "gpii.oauth2.authGrantFinder.getGrantForAccessToken",
                args: ["{that}.authorizationService", "{arguments}.0"]
                    // accessToken
            }
        }
    });
 
    // Return a promise object that contains the granted privilege for the access token.
    // This function looks up access tokens granted for GPII app installations to find the match.
    gpii.oauth2.authGrantFinder.getGrantForAccessToken = function (authorizationService, accessToken) {
        var promiseTogo = fluid.promise();
        var authorizationPromise = authorizationService.getAuthorizationByAccessToken(accessToken);
        var grant;
 
        authorizationPromise.then(function (authRecord) {
            if (authRecord) {
                if (authRecord.authorization.type === gpii.oauth2.docTypes.gpiiAppInstallationAuthorization &&
                    gpii.oauth2.getExpiresIn(new Date(), authRecord.authorization.timestampExpires) > 0) {
                    grant = {
                        accessToken: accessToken,
                        gpiiToken: authRecord.authorization.gpiiToken,
                        allowUntrustedSettingsGet: true,
                        allowUntrustedSettingsPut: true
                    };
                }
            }
            promiseTogo.resolve(grant);
        });
 
        return promiseTogo;
    };
})();