angular-aap-auth
Version:
## This library is deprecated. Please, use [ng-ebi-authorization](https://www.npmjs.com/package/ng-ebi-authorization).
2 lines • 6.27 kB
JavaScript
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("@angular/core"),require("@auth0/angular-jwt"),require("rxjs"),require("rxjs/operators")):"function"==typeof define&&define.amd?define("angular-aap-auth",["exports","@angular/core","@auth0/angular-jwt","rxjs","rxjs/operators"],t):t(e["angular-aap-auth"]={},e.ng.core,e.angularJwt,e.RxJS,e.rxjs.operators)}(this,function(e,n,t,i,o){"use strict";var r=function(){function e(e){this._jwt=e}return e.prototype.getToken=function(){return this._jwt.tokenGetter()},e.prototype.getTokenExpirationDate=function(){try{return this._jwt.getTokenExpirationDate()}catch(e){return null}},e.prototype.isTokenValid=function(){try{return!this._jwt.isTokenExpired()}catch(e){return!1}},e.prototype.getClaim=function(e,t){try{var n=this._jwt.decodeToken()[e];return n===undefined?t:n}catch(o){return t}},e.decorators=[{type:n.Injectable}],e.ctorParameters=function(){return[{type:t.JwtHelperService}]},e}(),a=new n.InjectionToken("AAP_CONFIG");function s(){return localStorage.getItem("id_token")||""}function u(){return localStorage.removeItem("id_token")}function l(e){return localStorage.setItem("id_token",e)}var c={aapURL:"https://api.aai.ebi.ac.uk",tokenGetter:s,tokenRemover:u,tokenUpdater:l},p=function(){function e(e,t,n){var o=this;this._rendererFactory=e,this._tokenService=t,this.config=n,this._credentials=new i.BehaviorSubject(null),this._loginCallbacks=[],this._logoutCallbacks=[],this._timeoutID=null,this._commKeyName="AngularAapAuthUpdated",this._commKeyUpdater=function(){return localStorage.setItem(o._commKeyName,""+(new Date).getTime())},this._domain=encodeURIComponent(window.location.origin),this._appURL=n.aapURL.replace(/\/$/,""),this._storageUpdater=n.tokenUpdater,n.tokenRemover?this._storageRemover=n.tokenRemover:this._storageRemover=function(){return n.tokenUpdater(null)};var r=this._rendererFactory.createRenderer(null,null);this._listenLoginMessage(r),this._listenChangesFromOtherWindows(r),this._updateCredentials()}return e.prototype.isAuthenticated=function(){return this._credentials.asObservable().pipe(o.map(function(e){return!!e}))},e.prototype.credentials=function(){return this._credentials.asObservable()},e.prototype.realname=function(){return this._credentials.asObservable().pipe(o.map(function(e){return e?e.realname:null}))},e.prototype.username=function(){return this._credentials.asObservable().pipe(o.map(function(e){return e?e.username:null}))},e.prototype.token=function(){return this._credentials.asObservable().pipe(o.map(function(e){return e?e.token:null}))},e.prototype.windowOpen=function(e,t,n,o,r){if(void 0===t&&(t=650),void 0===n&&(n=1e3),void 0===o&&(o=-1),void 0===r&&(r=-1),r<0){var i=screen.width;t<i&&(r=Math.round(i/2-t/2))}if(o<0){var a=screen.height;n<a&&(o=Math.round(a/2-n/2))}var s=["width="+t,"height="+n,"left="+r,"top="+o,"personalbar=no","toolbar=no","scrollbars=yes","resizable=yes","directories=no","location=no","menubar=no","titlebar=no","toolbar=no"],u=window.open(this.getSSOURL(e),"Sign in to Elixir",s.join(","));u&&u.focus()},e.prototype.tabOpen=function(e){var t=window.open(this.getSSOURL(e),"Sign in to Elixir");t&&t.focus()},e.prototype.getSSOURL=function(t){var e="";return t&&(this._filterLoginOptions(t),e=Object.keys(t).map(function(e){return[e,t[e]]}).reduce(function(e,t){return e+"&"+t[0]+"="+t[1]},"")),this._appURL+"/sso?from="+this._domain+e},e.prototype._filterLoginOptions=function(e){if(-1<Object.keys(e).indexOf("ttl")){var t=+e.ttl;1440<t?(window.console.error("Login requested with an expiration longer than 1440 minutes! This is not allowed."),window.console.error("Expiration request reset to 1440 minutes."),e.ttl="1440"):60<t&&window.console.warn("Login requested with an expiration longer than 60 minutes!")}},e.prototype.logOut=function(){this._storageRemover(),this._updateCredentials(),this._commKeyUpdater()},e.prototype.addLogInEventListener=function(e){return this._loginCallbacks.push(e)},e.prototype.removeLogInEventListener=function(e){return delete this._loginCallbacks[e-1]},e.prototype.addLogOutEventListener=function(e){return this._logoutCallbacks.push(e)},e.prototype.removeLogOutEventListener=function(e){return delete this._logoutCallbacks[e-1]},e.prototype._listenLoginMessage=function(e){var t=this;e.listen("window","message",function(e){t.messageIsAcceptable(e)&&(t._storageUpdater(e.data),e.source.close(),t._updateCredentials(),t._commKeyUpdater())})},e.prototype._listenChangesFromOtherWindows=function(e){var t=this;e.listen("window","storage",function(e){e.key===t._commKeyName&&t._updateCredentials()})},e.prototype.messageIsAcceptable=function(e){return e.origin===this._appURL},e.prototype._updateCredentials=function(){var e=this,t=this._loggedIn();if(this._timeoutID&&window.clearTimeout(this._timeoutID),t){this._credentials.next({realname:this._getRealName(),username:this._getUserName(),token:this._getToken()}),this._loginCallbacks.map(function(e){return e&&e()});var n=+this._tokenService.getTokenExpirationDate()-+new Date;this._timeoutID=window.setTimeout(function(){return e.logOut()},n)}else this._storageRemover(),this._credentials.next(null),this._logoutCallbacks.map(function(e){return e&&e()})},e.prototype._loggedIn=function(){return this._tokenService.isTokenValid()},e.prototype._getToken=function(){return this._tokenService.getToken()},e.prototype._getUserName=function(){return this._tokenService.getClaim("email",null)},e.prototype._getRealName=function(){return this._tokenService.getClaim("name",null)},e.decorators=[{type:n.Injectable}],e.ctorParameters=function(){return[{type:n.RendererFactory2},{type:r},{type:undefined,decorators:[{type:n.Inject,args:[a]}]}]},e}(),d=function(){function t(e){if(e)throw new Error("AuthModule is already loaded. It should only be imported in your application's main module.")}return t.forRoot=function(e){return{ngModule:t,providers:[r,{provide:a,useValue:e||c},p]}},t.decorators=[{type:n.NgModule,args:[{}]}],t.ctorParameters=function(){return[{type:t,decorators:[{type:n.Optional},{type:n.SkipSelf}]}]},t}();e.AuthModule=d,e.AuthService=p,e.TokenService=r,e.ɵb=a,e.ɵf=c,e.ɵc=s,e.ɵd=u,e.ɵe=l,Object.defineProperty(e,"__esModule",{value:!0})});
//# sourceMappingURL=angular-aap-auth.umd.min.js.map