ng-ebi-authorization
Version:
The ng-ebi-authorization is a simple authentication Angular library that relies on EBI's Authentication and Authorization Profile (AAP) infrastructure. After successful login, a JWT token is stored on the browser (via cookie, local or session storage).
2 lines • 8.02 kB
JavaScript
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("@auth0/angular-jwt"),require("@angular/core"),require("@angular/common/http"),require("rxjs"),require("rxjs/operators")):"function"==typeof define&&define.amd?define("ng-ebi-authorization",["exports","@auth0/angular-jwt","@angular/core","@angular/common/http","rxjs","rxjs/operators"],e):e(t["ng-ebi-authorization"]={},t.angularJwt,t.ng.core,t.ng.common.http,t.RxJS,t.rxjs.operators)}(this,function(t,e,n,r,a,i){"use strict";var o=function(){function t(t){this._jwt=t}return t.prototype.getToken=function(){return this._jwt.tokenGetter()},t.prototype.getTokenExpirationDate=function(){try{return this._jwt.getTokenExpirationDate()}catch(t){return null}},t.prototype.isTokenValid=function(){try{return!this._jwt.isTokenExpired()}catch(t){return!1}},t.prototype.getClaim=function(t,e){try{var n=this._jwt.decodeToken()[t];return n===undefined?e:n}catch(o){return e}},t.decorators=[{type:n.Injectable}],t.ctorParameters=function(){return[{type:e.JwtHelperService}]},t}(),s=new n.InjectionToken("AAP_CONFIG");function u(){return localStorage.getItem("id_token")||""}function p(){return localStorage.removeItem("id_token")}function c(t){return localStorage.setItem("id_token",t)}var h={aapURL:"https://api.aai.ebi.ac.uk",tokenGetter:u,tokenRemover:p,tokenUpdater:c},l=function(){return(l=Object.assign||function(t){for(var e,n=1,o=arguments.length;n<o;n++)for(var r in e=arguments[n])Object.prototype.hasOwnProperty.call(e,r)&&(t[r]=e[r]);return t}).apply(this,arguments)};var d=function(){function t(t,e,n,o){var r=this;this._rendererFactory=t,this._tokenService=e,this._http=n,this.config=o,this._user=new a.BehaviorSubject(null),this._currentState=null,this._loginCallbacks=[],this._logoutCallbacks=[],this._timeoutID=null,this._commKeyName="AngularAapAuthUpdated",this._commKeyUpdater=function(){return localStorage.setItem(r._commKeyName,""+(new Date).getTime())},this._domain=encodeURIComponent(window.location.origin),this._appURL=o.aapURL.replace(/\/$/,""),this._authURL=this._appURL+"/auth",this._tokenURL=this._appURL+"/token",this._storageUpdater=o.tokenUpdater,o.tokenRemover?this._storageRemover=o.tokenRemover:this._storageRemover=function(){return o.tokenUpdater(null)};var i=this._rendererFactory.createRenderer(null,null);this._unlistenEvents=[this._listenLoginMessage(i),this._listenChangesFromOtherWindows(i)],this._currentState=null,this._updateUser()}return t.prototype.ngOnDestroy=function(){this._unlistenEvents.forEach(function(t){return t()})},t.prototype.user=function(){return this._user.asObservable()},t.prototype.openLoginWindow=function(t,e,n,o,r){if(void 0===e&&(e=650),void 0===n&&(n=1e3),void 0===o&&(o=-1),void 0===r&&(r=-1),o<0){var i=screen.width;e<i&&(o=Math.round((i-e)/2))}if(r<0){var a=screen.height;n<a&&(r=Math.round((a-n)/2))}var s=["width="+e,"height="+n,"left="+o,"top="+r,"personalbar=no","toolbar=no","scrollbars=yes","resizable=yes","directories=no","location=no","menubar=no","titlebar=no","toolbar=no"],u=window.open(this.getSSOURL(t),"Sign in to Elixir",s.join(","));u&&u.focus()},t.prototype.windowOpen=function(t,e,n,o,r){void 0===e&&(e=650),void 0===n&&(n=1e3),void 0===o&&(o=-1),void 0===r&&(r=-1),this._deprecationWarning("windowOpen","openLoginWindow"),this.openLoginWindow(t,e,n,r,o)},t.prototype.openLoginTab=function(t){var e=window.open(this.getSSOURL(t),"Sign in to Elixir");e&&e.focus()},t.prototype.tabOpen=function(t){this._deprecationWarning("tabOpen","openLoginTab"),this.openLoginTab(t)},t.prototype.getSSOURL=function(t){var e=this._formatFragments(l({from:this._domain},t));return this._appURL+"/sso"+e},t.prototype.logOut=function(){this._storageRemover(),this._updateUser(),this._commKeyUpdater()},t.prototype.createAAPaccount=function(t){return this._http.post(this._authURL,t,{responseType:"text"})},t.prototype.loginAAP=function(t,e){var n=this,o=this._formatFragments(e);return this._http.get(""+this._authURL+o,{headers:this._createAuthHeader(t),responseType:"text"}).pipe(i.tap(function(t){n._storageRemover(),n._storageUpdater(t),n._updateUser(),n._commKeyUpdater()}),i.map(Boolean))},t.prototype.changePasswordAAP=function(t){var e=t.username,n=t.oldPassword,o=t.newPassword;return this._http.patch(this._authURL,{username:e,password:o},{headers:this._createAuthHeader({username:e,password:n})}).pipe(i.map(function(t){return!0}))},t.prototype.addLogInEventListener=function(t){return this._loginCallbacks.push(t)},t.prototype.removeLogInEventListener=function(t){return delete this._loginCallbacks[t-1]},t.prototype.addLogOutEventListener=function(t){return this._logoutCallbacks.push(t)},t.prototype.removeLogOutEventListener=function(t){return delete this._logoutCallbacks[t-1]},t.prototype.refresh=function(){var e=this;return this._http.get(this._tokenURL,{responseType:"text"}).pipe(i.tap(function(t){e._storageRemover(),e._storageUpdater(t),e._updateUser(),e._commKeyUpdater()}),i.map(Boolean))},t.prototype._formatFragments=function(t){return t?(this._filterLoginOptions(t),"?"+Object.entries(t).map(function(t){var e=function u(t,e){var n="function"==typeof Symbol&&t[Symbol.iterator];if(!n)return t;var o,r,i=n.call(t),a=[];try{for(;(void 0===e||0<e--)&&!(o=i.next()).done;)a.push(o.value)}catch(s){r={error:s}}finally{try{o&&!o.done&&(n=i["return"])&&n.call(i)}finally{if(r)throw r.error}}return a}(t,2);return e[0]+"="+e[1]}).join("&")):""},t.prototype._filterLoginOptions=function(t){if(-1<Object.keys(t).indexOf("ttl")){var e=+t.ttl;if(1440<e)throw new Error("Login requested with an expiration longer than 1440 minutes! This is not allowed.");60<e&&window.console.warn("Login requested with an expiration longer than 60 minutes!")}},t.prototype._createAuthHeader=function(t){var e=t.username,n=t.password,o=btoa(e+":"+n);return new r.HttpHeaders({Authorization:"Basic "+o})},t.prototype._listenLoginMessage=function(t){var e=this;return t.listen("window","message",function(t){e._messageIsAcceptable(t)&&(e._storageUpdater(t.data),t.source&&t.source.close(),e._updateUser(),e._commKeyUpdater())})},t.prototype._listenChangesFromOtherWindows=function(t){var e=this;return t.listen("window","storage",function(t){t.key===e._commKeyName&&e._updateUser()})},t.prototype._messageIsAcceptable=function(t){return t.origin===this._appURL},t.prototype._updateUser=function(){var t=this;if(this._timeoutID&&window.clearTimeout(this._timeoutID),this._tokenService.isTokenValid()){var e=this._tokenService.getToken();this._user.next({uid:this._getClaim("sub"),name:this._getClaim("name"),nickname:this._getClaim("nickname"),email:this._getClaim("email"),token:e}),null===this._currentState&&this._loginCallbacks.forEach(function(t){return t()});var n=+this._tokenService.getTokenExpirationDate()-+new Date;this._timeoutID=window.setTimeout(function(){return t.logOut()},n),this._currentState!==e&&(this._currentState=e)}else this._storageRemover(),null!==this._currentState&&(this._logoutCallbacks.forEach(function(t){return t()}),this._user.next(null)),this._currentState=null},t.prototype._getClaim=function(t){return this._tokenService.getClaim(t,"")},t.prototype._deprecationWarning=function(t,e){window.console.warn("Method '"+t+"' has been deprecated, please use '"+e+"' method instead")},t.decorators=[{type:n.Injectable}],t.ctorParameters=function(){return[{type:n.RendererFactory2},{type:o},{type:r.HttpClient},{type:undefined,decorators:[{type:n.Inject,args:[s]}]}]},t}(),_=function(){function e(t){if(t)throw new Error("AuthModule is already loaded. It should only be imported in your application's main module.")}return e.forRoot=function(t){return{ngModule:e,providers:[o,{provide:s,useValue:t||h},d]}},e.decorators=[{type:n.NgModule,args:[{}]}],e.ctorParameters=function(){return[{type:e,decorators:[{type:n.Optional},{type:n.SkipSelf}]}]},e}();t.AuthModule=_,t.AuthService=d,t.TokenService=o,t.ɵb=s,t.ɵf=h,t.ɵc=u,t.ɵd=p,t.ɵe=c,Object.defineProperty(t,"__esModule",{value:!0})});
//# sourceMappingURL=ng-ebi-authorization.umd.min.js.map