all files / src/provider/ ngJwtAuthServiceProvider.ts

95.65% Statements 44/46
66.67% Branches 6/9
100% Functions 13/13
100% Lines 43/43
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    31× 31× 31× 31× 31×                     52× 52×     52×                                                          
"use strict";
var __extends = (this && this.__extends) || function (d, b) {
    for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
    function __() { this.constructor = d; }
    d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
var ngJwtAuthService_1 = require("../service/ngJwtAuthService");
var _ = require("lodash");
var NgJwtAuthException = (function (_super) {
    __extends(NgJwtAuthException, _super);
    function NgJwtAuthException(message) {
        _super.call(this, message);
        this.message = message;
        this.name = 'NgJwtAuthException';
        this.message = message;
        this.stack = (new Error()).stack;
    }
    NgJwtAuthException.prototype.toString = function () {
        return this.name + ': ' + this.message;
    };
    return NgJwtAuthException;
}(Error));
exports.NgJwtAuthException = NgJwtAuthException;
var NgJwtAuthTokenExpiredException = (function (_super) {
    __extends(NgJwtAuthTokenExpiredException, _super);
    function NgJwtAuthTokenExpiredException() {
        _super.apply(this, arguments);
    }
    return NgJwtAuthTokenExpiredException;
}(NgJwtAuthException));
exports.NgJwtAuthTokenExpiredException = NgJwtAuthTokenExpiredException;
var NgJwtAuthCredentialsFailedException = (function (_super) {
    __extends(NgJwtAuthCredentialsFailedException, _super);
    function NgJwtAuthCredentialsFailedException() {
        _super.apply(this, arguments);
    }
    return NgJwtAuthCredentialsFailedException;
}(NgJwtAuthException));
exports.NgJwtAuthCredentialsFailedException = NgJwtAuthCredentialsFailedException;
var NgJwtAuthServiceProvider = (function () {
    /**
     * Initialise the service provider
     */
    function NgJwtAuthServiceProvider() {
        this.$get = ['$http', '$q', '$window', '$interval', 'base64', '$cookies', '$location', function NgJwtAuthServiceFactory($http, $q, $window, $interval, base64, $cookies, $location) {
                return new ngJwtAuthService_1.NgJwtAuthService(this.config, $http, $q, $window, $interval, base64, $cookies, $location);
            }];
        //initialise service config
        this.config = {
            tokenLocation: 'token',
            tokenUser: '#user',
            apiEndpoints: {
                base: '/api/auth',
                login: '/login',
                tokenExchange: '/token',
                loginAsUser: '/user',
                refresh: '/refresh',
            },
            storageKeyName: 'NgJwtAuthToken',
            refreshBeforeSeconds: 60 * 2,
            checkExpiryEverySeconds: 60,
            cookie: {
                enabled: false,
                name: 'ngJwtAuthToken',
                topLevelDomain: false,
            }
        };
    }
    /**
     * Set the configuration
     * @param config
     * @returns {NgJwtAuthServiceProvider}
     */
    NgJwtAuthServiceProvider.prototype.configure = function (config) {
        var mismatchedConfig = _.difference(_.keys(config), _.keys(this.config));
        if (mismatchedConfig.length > 0) {
            throw new NgJwtAuthException("Invalid properties [" + mismatchedConfig.join(',') + "] passed to config)");
        }
        this.config = _.defaultsDeep(config, this.config);
        return this;
    };
    return NgJwtAuthServiceProvider;
}());
exports.NgJwtAuthServiceProvider = NgJwtAuthServiceProvider;