UNPKG

adal-angular6-wrapper

Version:

Angular 6 ADAL Wrapper. This version is a fork of follwing library for some personal implementation. No Support is available for this version. Original: https://github.com/benbaran/adal-angular4

478 lines (471 loc) 14.9 kB
import { bindCallback, timer } from 'rxjs'; import { inject } from 'adal-angular'; import { Injectable } from '@angular/core'; import { map, mergeMap } from 'rxjs/operators'; /** * @fileoverview added by tsickle * @suppress {checkTypes,extraRequire,uselessCode} checked by tsc */ var AdalService = /** @class */ (function () { function AdalService() { this.context = /** @type {?} */ (null); this.loginRefreshTimer = /** @type {?} */ (null); this.user = { authenticated: false, userName: '', error: '', token: '', profile: {}, loginCached: false }; } /** * @param {?} configOptions * @return {?} */ AdalService.prototype.init = /** * @param {?} configOptions * @return {?} */ function (configOptions) { if (!configOptions) { throw new Error('You must set config, when calling init.'); } /** @type {?} */ var existingHash = window.location.hash; /** @type {?} */ var pathDefault = window.location.href; if (existingHash) { pathDefault = pathDefault.replace(existingHash, ''); } configOptions.redirectUri = configOptions.redirectUri || pathDefault; configOptions.postLogoutRedirectUri = configOptions.postLogoutRedirectUri || pathDefault; // create instance with given config this.context = inject(configOptions); window.AuthenticationContext = this.context.constructor; // loginresource is used to set authenticated status this.updateDataFromCache(); if (this.user.loginCached && !this.user.authenticated && window.self == window.top && !this.isInCallbackRedirectMode) { this.refreshLoginToken(); } else if (this.user.loginCached && this.user.authenticated && !this.loginRefreshTimer && window.self == window.top) { this.setupLoginTokenRefreshTimer(); } }; Object.defineProperty(AdalService.prototype, "config", { get: /** * @return {?} */ function () { return this.context.config; }, enumerable: true, configurable: true }); Object.defineProperty(AdalService.prototype, "userInfo", { get: /** * @return {?} */ function () { return this.user; }, enumerable: true, configurable: true }); /** * @return {?} */ AdalService.prototype.login = /** * @return {?} */ function () { this.context.login(); }; /** * @return {?} */ AdalService.prototype.loginInProgress = /** * @return {?} */ function () { return this.context.loginInProgress(); }; /** * @return {?} */ AdalService.prototype.logOut = /** * @return {?} */ function () { this.context.logOut(); }; /** * @param {?=} removeHash * @return {?} */ AdalService.prototype.handleWindowCallback = /** * @param {?=} removeHash * @return {?} */ function (removeHash) { if (removeHash === void 0) { removeHash = true; } /** @type {?} */ var hash = window.location.hash; if (this.context.isCallback(hash)) { /** @type {?} */ var requestInfo = this.context.getRequestInfo(hash); this.context.saveTokenFromHash(requestInfo); if (requestInfo.requestType === this.context.REQUEST_TYPE.LOGIN) { this.updateDataFromCache(); this.setupLoginTokenRefreshTimer(); } else if (requestInfo.requestType === this.context.REQUEST_TYPE.RENEW_TOKEN) { this.context.callback = window.parent.callBackMappedToRenewStates[requestInfo.stateResponse]; } if (requestInfo.stateMatch) { if (typeof this.context.callback === 'function') { if (requestInfo.requestType === this.context.REQUEST_TYPE.RENEW_TOKEN) { // Idtoken or Accestoken can be renewed if (requestInfo.parameters['access_token']) { this.context.callback(this.context._getItem(this.context.CONSTANTS.STORAGE.ERROR_DESCRIPTION), requestInfo.parameters['access_token']); } else if (requestInfo.parameters['id_token']) { this.context.callback(this.context._getItem(this.context.CONSTANTS.STORAGE.ERROR_DESCRIPTION), requestInfo.parameters['id_token']); } else if (requestInfo.parameters['error']) { this.context.callback(this.context._getItem(this.context.CONSTANTS.STORAGE.ERROR_DESCRIPTION), null); this.context._renewFailed = true; } } } } } // Remove hash from url if (removeHash) { if (window.location.hash) { if (window.history.replaceState) { window.history.replaceState('', '/', window.location.pathname); } else { window.location.hash = ''; } } } }; /** * @param {?} resource * @return {?} */ AdalService.prototype.getCachedToken = /** * @param {?} resource * @return {?} */ function (resource) { return this.context.getCachedToken(resource); }; /** * @param {?} resource * @return {?} */ AdalService.prototype.acquireToken = /** * @param {?} resource * @return {?} */ function (resource) { var _this = this; return bindCallback(function (callback) { _this.context.acquireToken(resource, function (error, tokenOut) { if (error) { _this.context.error('Error when acquiring token for resource: ' + resource, error); callback(null, error); } else { callback(tokenOut, null); } }); })() .pipe(map(function (result) { if (!result[0] && result[1]) { throw (result[1]); } return result[0]; })); }; /** * @return {?} */ AdalService.prototype.getUser = /** * @return {?} */ function () { var _this = this; return bindCallback(function (callback) { _this.context.getUser(function (error, user) { if (error) { _this.context.error('Error when getting user', error); callback(null); } else { callback(user || null); } }); })(); }; /** * @return {?} */ AdalService.prototype.clearCache = /** * @return {?} */ function () { this.context.clearCache(); }; /** * @param {?} resource * @return {?} */ AdalService.prototype.clearCacheForResource = /** * @param {?} resource * @return {?} */ function (resource) { this.context.clearCacheForResource(resource); }; /** * @param {?} message * @return {?} */ AdalService.prototype.info = /** * @param {?} message * @return {?} */ function (message) { this.context.info(message); }; /** * @param {?} message * @return {?} */ AdalService.prototype.verbose = /** * @param {?} message * @return {?} */ function (message) { this.context.verbose(message); }; /** * @param {?} url * @return {?} */ AdalService.prototype.getResourceForEndpoint = /** * @param {?} url * @return {?} */ function (url) { return this.context.getResourceForEndpoint(url); }; /** * @return {?} */ AdalService.prototype.refreshDataFromCache = /** * @return {?} */ function () { this.updateDataFromCache(); }; /** * @return {?} */ AdalService.prototype.updateDataFromCache = /** * @return {?} */ function () { /** @type {?} */ var token = this.context.getCachedToken(/** @type {?} */ (this.context.config.loginResource)); this.user.authenticated = token !== null && token.length > 0; /** @type {?} */ var user = this.context.getCachedUser(); if (user) { this.user.userName = user.userName; this.user.profile = user.profile; this.user.token = token; this.user.error = this.context.getLoginError(); this.user.loginCached = true; } else { this.user.userName = ''; this.user.profile = {}; this.user.token = ''; this.user.error = this.context.getLoginError(); this.user.loginCached = false; } }; /** * @return {?} */ AdalService.prototype.refreshLoginToken = /** * @return {?} */ function () { var _this = this; if (!this.user.loginCached) throw ("User not logged in"); this.acquireToken(/** @type {?} */ (this.context.config.loginResource)).subscribe(function (token) { _this.user.token = token; if (_this.user.authenticated == false) { _this.user.authenticated = true; _this.user.error = ''; window.location.reload(); } else { _this.setupLoginTokenRefreshTimer(); } }, function (error) { _this.user.authenticated = false; _this.user.error = _this.context.getLoginError(); }); }; /** * @return {?} */ AdalService.prototype.now = /** * @return {?} */ function () { return Math.round(new Date().getTime() / 1000.0); }; Object.defineProperty(AdalService.prototype, "isInCallbackRedirectMode", { get: /** * @return {?} */ function () { return window.location.href.indexOf("#access_token") !== -1 || window.location.href.indexOf("#id_token") !== -1; }, enumerable: true, configurable: true }); /** * @return {?} */ AdalService.prototype.setupLoginTokenRefreshTimer = /** * @return {?} */ function () { var _this = this; /** @type {?} */ var exp = this.context._getItem(this.context.CONSTANTS.STORAGE.EXPIRATION_KEY + /** @type {?} */ (this.context.config.loginResource)); /** @type {?} */ var timerDelay = exp - this.now() - (this.context.config.expireOffsetSeconds || 300) > 0 ? exp - this.now() - (this.context.config.expireOffsetSeconds || 300) : 1; if (this.loginRefreshTimer) this.loginRefreshTimer.unsubscribe(); this.loginRefreshTimer = timer(timerDelay * 1000).subscribe(function (x) { _this.refreshLoginToken(); }); }; AdalService.decorators = [ { type: Injectable } ]; /** @nocollapse */ AdalService.ctorParameters = function () { return []; }; return AdalService; }()); /** * @fileoverview added by tsickle * @suppress {checkTypes,extraRequire,uselessCode} checked by tsc */ var AdalGuard = /** @class */ (function () { function AdalGuard(adalService) { this.adalService = adalService; } /** * @param {?} route * @param {?} state * @return {?} */ AdalGuard.prototype.canActivate = /** * @param {?} route * @param {?} state * @return {?} */ function (route, state) { return this.adalService.userInfo.authenticated; }; /** * @param {?} childRoute * @param {?} state * @return {?} */ AdalGuard.prototype.canActivateChild = /** * @param {?} childRoute * @param {?} state * @return {?} */ function (childRoute, state) { return this.canActivate(childRoute, state); }; AdalGuard.decorators = [ { type: Injectable } ]; /** @nocollapse */ AdalGuard.ctorParameters = function () { return [ { type: AdalService } ]; }; return AdalGuard; }()); /** * @fileoverview added by tsickle * @suppress {checkTypes,extraRequire,uselessCode} checked by tsc */ var AdalInterceptor = /** @class */ (function () { function AdalInterceptor(adal) { this.adal = adal; } /** * @param {?} request * @param {?} next * @return {?} */ AdalInterceptor.prototype.intercept = /** * @param {?} request * @param {?} next * @return {?} */ function (request, next) { /** @type {?} */ var resource = this.adal.getResourceForEndpoint(request.url); if (!resource) { return next.handle(request); } // if the user is not authenticated then drop the request if (!this.adal.userInfo.authenticated) { throw new Error('Cannot send request to registered endpoint if the user is not authenticated.'); } // if the endpoint is registered then acquire and inject token return this.adal.acquireToken(resource) .pipe(mergeMap(function (token) { /** @type {?} */ var authorizedRequest = request.clone({ headers: request.headers.set('Authorization', 'Bearer ' + token), }); return next.handle(authorizedRequest); })); }; AdalInterceptor.decorators = [ { type: Injectable } ]; /** @nocollapse */ AdalInterceptor.ctorParameters = function () { return [ { type: AdalService } ]; }; return AdalInterceptor; }()); /** * @fileoverview added by tsickle * @suppress {checkTypes,extraRequire,uselessCode} checked by tsc */ /** * @fileoverview added by tsickle * @suppress {checkTypes,extraRequire,uselessCode} checked by tsc */ export { AdalService, AdalGuard, AdalInterceptor }; //# sourceMappingURL=adal-angular6-wrapper.js.map