adal-angular4
Version:
Angular 4+ ADAL Wrapper
16 lines (14 loc) • 8.07 kB
JavaScript
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("@angular/core"),require("rxjs"),require("rxjs/operators"),require("adal-angular")):"function"==typeof define&&define.amd?define("adal-angular4",["exports","@angular/core","rxjs","rxjs/operators","adal-angular"],t):t((e=e||self)["adal-angular4"]={},e.ng.core,e.rxjs,e.rxjs.operators,e["adal-angular"])}(this,(function(e,t,n,o,r){"use strict";
/*! *****************************************************************************
Copyright (c) Microsoft Corporation. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use
this file except in compliance with the License. You may obtain a copy of the
License at http://www.apache.org/licenses/LICENSE-2.0
THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
MERCHANTABLITY OR NON-INFRINGEMENT.
See the Apache Version 2.0 License for specific language governing permissions
and limitations under the License.
***************************************************************************** */function i(e,t,n,o){var r,i=arguments.length,s=i<3?t:null===o?o=Object.getOwnPropertyDescriptor(t,n):o;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)s=Reflect.decorate(e,t,n,o);else for(var c=e.length-1;c>=0;c--)(r=e[c])&&(s=(i<3?r(s):i>3?r(t,n,s):r(t,n))||s);return i>3&&s&&Object.defineProperty(t,n,s),s}var s=function(){function e(e){this.ngZone=e,this.context=null,this.loginRefreshTimer=null,this.user={authenticated:!1,userName:"",error:"",token:"",profile:{},loginCached:!1}}return e.prototype.init=function(e){if(!e)throw new Error("You must set config, when calling init.");var t=window.location.hash,n=window.location.href;t&&(n=n.replace(t,"")),e.redirectUri=e.redirectUri||n,e.postLogoutRedirectUri=e.postLogoutRedirectUri||n,this.context=r.inject(e),this.updateDataFromCache(),!this.user.loginCached||this.user.authenticated||window.self!==window.top||this.isInCallbackRedirectMode?this.user.loginCached&&this.user.authenticated&&!this.loginRefreshTimer&&window.self===window.top&&this.setupLoginTokenRefreshTimer():this.refreshLoginToken()},Object.defineProperty(e.prototype,"config",{get:function(){return this.context.config},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"userInfo",{get:function(){return this.user},enumerable:!0,configurable:!0}),e.prototype.login=function(){this.context.login()},e.prototype.loginInProgress=function(){return this.context.loginInProgress()},e.prototype.logOut=function(){this.context.logOut()},e.prototype.handleWindowCallback=function(e){void 0===e&&(e=!0);var t=window.location.hash;if(this.context.isCallback(t)){this.context._openedWindows.length>0&&this.context._openedWindows[this.context._openedWindows.length-1].opener&&this.context._openedWindows[this.context._openedWindows.length-1].opener._adalInstance?(this.context=this.context._openedWindows[this.context._openedWindows.length-1].opener._adalInstance,!0):window.parent&&window.parent._adalInstance&&(this.context=window.parent._adalInstance);var n=this.context.getRequestInfo(t);this.context.saveTokenFromHash(n);var o=this.context._callBackMappedToRenewStates[n.stateResponse]||this.context.callback;n.requestType===this.context.REQUEST_TYPE.LOGIN&&(this.updateDataFromCache(),this.setupLoginTokenRefreshTimer()),n.stateMatch&&"function"==typeof o&&n.requestType===this.context.REQUEST_TYPE.RENEW_TOKEN&&(n.parameters.access_token?o(this.context._getItem(this.context.CONSTANTS.STORAGE.ERROR_DESCRIPTION),n.parameters.access_token):n.parameters.id_token?o(this.context._getItem(this.context.CONSTANTS.STORAGE.ERROR_DESCRIPTION),n.parameters.id_token):n.parameters.error&&(o(this.context._getItem(this.context.CONSTANTS.STORAGE.ERROR_DESCRIPTION),null),this.context._renewFailed=!0))}e&&window.location.hash&&(window.history.replaceState?window.history.replaceState("","/",window.location.pathname):window.location.hash="")},e.prototype.getCachedToken=function(e){return this.context.getCachedToken(e)},e.prototype.acquireToken=function(e){var t=this;return n.bindCallback((function(n){t.context.acquireToken(e,(function(o,r){o?(t.context.error("Error when acquiring token for resource: "+e,o),n(null,o)):n(r,null)}))}))().pipe(o.map((function(e){if(!e[0]&&e[1])throw e[1];return e[0]})))},e.prototype.getUser=function(){var e=this;return n.bindCallback((function(t){e.context.getUser((function(n,o){n?(e.context.error("Error when getting user",n),t(null)):t(o||null)}))}))()},e.prototype.clearCache=function(){this.context.clearCache()},e.prototype.clearCacheForResource=function(e){this.context.clearCacheForResource(e)},e.prototype.info=function(e){this.context.info(e)},e.prototype.verbose=function(e){this.context.verbose(e)},e.prototype.getResourceForEndpoint=function(e){return this.context.getResourceForEndpoint(e)},e.prototype.refreshDataFromCache=function(){this.updateDataFromCache()},e.prototype.updateDataFromCache=function(){var e=this.context.getCachedToken(this.context.config.loginResource);this.user.authenticated=null!==e&&e.length>0;var t=this.context.getCachedUser();t?(this.user.userName=t.userName,this.user.profile=t.profile,this.user.token=e,this.user.error=this.context.getLoginError(),this.user.loginCached=!0):(this.user.userName="",this.user.profile={},this.user.token="",this.user.error=this.context.getLoginError(),this.user.loginCached=!1)},e.prototype.refreshLoginToken=function(){var e=this;if(!this.user.loginCached)throw new Error("User not logged in");this.acquireToken(this.context.config.loginResource).subscribe((function(t){e.user.token=t,!1===e.user.authenticated?(e.user.authenticated=!0,e.user.error="",window.location.reload()):e.setupLoginTokenRefreshTimer()}),(function(t){e.user.authenticated=!1,e.user.error=e.context.getLoginError()}))},e.prototype.now=function(){return Math.round((new Date).getTime()/1e3)},Object.defineProperty(e.prototype,"isInCallbackRedirectMode",{get:function(){return-1!==window.location.href.indexOf("#access_token")||-1!==window.location.href.indexOf("#id_token")},enumerable:!0,configurable:!0}),e.prototype.setupLoginTokenRefreshTimer=function(){var e=this,t=this.context._getItem(this.context.CONSTANTS.STORAGE.EXPIRATION_KEY+this.context.config.loginResource),o=t-this.now()-(this.context.config.expireOffsetSeconds||300)>0?t-this.now()-(this.context.config.expireOffsetSeconds||300):1;this.loginRefreshTimer&&this.loginRefreshTimer.unsubscribe(),this.ngZone.runOutsideAngular((function(){e.loginRefreshTimer=n.timer(1e3*o).subscribe((function(t){e.refreshLoginToken()}))}))},e.ctorParameters=function(){return[{type:t.NgZone}]},e.ɵprov=t["ɵɵdefineInjectable"]({factory:function(){return new e(t["ɵɵinject"](t.NgZone))},token:e,providedIn:"root"}),e=i([t.Injectable({providedIn:"root"})],e)}(),c=function(){function e(e){this.service=e}return e.prototype.canActivate=function(e,t){return this.service.userInfo.authenticated},e.ctorParameters=function(){return[{type:s}]},e.ɵprov=t["ɵɵdefineInjectable"]({factory:function(){return new e(t["ɵɵinject"](s))},token:e,providedIn:"root"}),e=i([t.Injectable({providedIn:"root"})],e)}(),a=function(){function e(e){this.service=e}return e.prototype.intercept=function(e,t){var n=this.service.getResourceForEndpoint(e.url),r=e.headers.get("skip-adal");if(!n||r)return t.handle(e);if(!this.service.userInfo.authenticated)throw new Error("Cannot send request to registered endpoint if the user is not authenticated.");return this.service.acquireToken(n).pipe(o.mergeMap((function(n){var o=e.clone({headers:e.headers.set("Authorization","Bearer "+n)});return t.handle(o)})))},e.ctorParameters=function(){return[{type:s}]},e=i([t.Injectable()],e)}();e.AdalGuard=c,e.AdalInterceptor=a,e.AdalService=s,Object.defineProperty(e,"__esModule",{value:!0})}));
//# sourceMappingURL=adal-angular4.umd.min.js.map