All files / universal.klown/gpii/node_modules/gpii-oauth2/gpii-oauth2-utilities/src OAuth2Utilities.js

93.02% Statements 40/43
100% Branches 24/24
72.73% Functions 8/11
93.02% Lines 40/43

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 95 96 97 98 99 100 101 102 103 104 105 106 107                            7x   7x 7x   7x 41x 33x 33x 27x     14x     7x 23x 23x   23x 2x   21x   23x     7x   14x 2x   12x 6x   6x 6x         7x 20x 20x 20x     7x                         7x 23x                 7x 48x 4x   44x                 7x 62x 27x     35x 35x 35x    
/*!
Copyright 2014-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
*/
"use strict";
 
var fluid = fluid || require("infusion");
 
var gpii = fluid.registerNamespace("gpii");
fluid.registerNamespace("gpii.oauth2");
 
gpii.oauth2.parseBearerAuthorizationHeader = function (req) {
    if (req.headers && req.headers.authorization) {
        var parts = req.headers.authorization.split(/\s+/);
        if (parts.length === 2 && parts[0].toLowerCase() === "bearer") {
            return parts[1];
        }
    }
    return undefined;
};
 
gpii.oauth2.getAuthorization = function (req, authGrantFinder) {
    var promiseTogo = fluid.promise();
    var accessToken = gpii.oauth2.parseBearerAuthorizationHeader(req);
 
    if (!accessToken) {
        promiseTogo.reject(gpii.oauth2.errors.unauthorized);
    } else {
        promiseTogo = authGrantFinder.getGrantForAccessToken(accessToken);
    }
    return promiseTogo;
};
 
gpii.oauth2.walkMiddleware = function (middleware, i, req, res, next) {
    // TODO best way to check if middleware is a single function?
    if (typeof middleware === "function") {
        return middleware(req, res, next);
    }
    if (i >= middleware.length) {
        return next();
    } else {
        return middleware[i](req, res, function () {
            return gpii.oauth2.walkMiddleware(middleware, i + 1, req, res, next);
        });
    }
};
 
gpii.oauth2.composeError = function (error, termMap) {
    var err = fluid.copy(error);
    err.message = fluid.stringTemplate(err.message, termMap);
    return err;
};
 
gpii.oauth2.mapPromiseToResponse = function (promise, response) {
    promise.then(function () {
        response.sendStatus(200);
    }, function (err) {
        response.sendStatus(err.statusCode);
    });
};
 
/**
 * Returns the current time in a human readable string that also naturally sort in chronological order.
 * See http://www.ecma-international.org/ecma-262/5.1/#sec-15.9.5.43
 * @return {String} The current time in ISO string format.
 */
gpii.oauth2.getCurrentTimestamp = function () {
    return new Date().toISOString();
};
 
/**
 * Calculate the timestamp of currentTime + expiresIn.
 * @param timestampStarts {Date} A start timestamp in the format returned by Date()
 * @param expiresIn {Number} The number of seconds that the expiration will occur.
 * @return {String} A date in simpilified ISO string format.
 */
gpii.oauth2.getTimestampExpires = function (timestampStarts, expiresIn) {
    if (!timestampStarts) {
        return undefined;
    }
    return new Date(timestampStarts.getTime() + expiresIn * 1000).toISOString();
};
 
/**
 * Compare the current time with the expiresIn time and return the number of seconds that the expiration will occur.
 * @param timestampStarts {Date} A start timestamp in the format returned by Date()
 * @param timestampExpires {String} A string in the format returned by Date().toISOString()
 * @return {Number} The number of seconds that the expiration will occur. Return 0 if the given timestampExpires < the current timestamp.
 */
gpii.oauth2.getExpiresIn = function (timestampStarts, timestampExpires) {
    if (!timestampStarts || !timestampExpires) {
        return undefined;
    }
 
    var startsTimeInMsec = timestampStarts.getTime();
    var expiresTimeInMsec = new Date(timestampExpires).getTime();
    return expiresTimeInMsec > startsTimeInMsec ? Math.round((expiresTimeInMsec - startsTimeInMsec) / 1000) : 0;
};