@asgardeo/auth-angular
Version:
Asgardeo Auth Angular SDK for Angular Applications.
2 lines • 10.9 kB
JavaScript
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("@angular/core"),require("@asgardeo/auth-spa"),require("rxjs"),require("@angular/router"),require("rxjs/operators")):"function"==typeof define&&define.amd?define("@asgardeo/auth-angular",["exports","@angular/core","@asgardeo/auth-spa","rxjs","@angular/router","rxjs/operators"],e):e(((t="undefined"!=typeof globalThis?globalThis:t||self).asgardeo=t.asgardeo||{},t.asgardeo["auth-angular"]={}),t.ng.core,t.i2,t.rxjs,t.ng.router,t.rxjs.operators)}(this,(function(t,e,n,r,o,i){"use strict";function s(t){if(t&&t.__esModule)return t;var e=Object.create(null);return t&&Object.keys(t).forEach((function(n){if("default"!==n){var r=Object.getOwnPropertyDescriptor(t,n);Object.defineProperty(e,n,r.get?r:{enumerable:!0,get:function(){return t[n]}})}})),e.default=t,Object.freeze(e)}var a=s(e);function u(t,e,n,r){return new(n||(n=Promise))((function(o,i){function s(t){try{u(r.next(t))}catch(t){i(t)}}function a(t){try{u(r.throw(t))}catch(t){i(t)}}function u(t){var e;t.done?o(t.value):(e=t.value,e instanceof n?e:new n((function(t){t(e)}))).then(s,a)}u((r=r.apply(t,e||[])).next())}))}function c(t,e){var n,r,o,i,s={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]};return i={next:a(0),throw:a(1),return:a(2)},"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function a(i){return function(a){return function(i){if(n)throw new TypeError("Generator is already executing.");for(;s;)try{if(n=1,r&&(o=2&i[0]?r.return:i[0]?r.throw||((o=r.return)&&o.call(r),0):r.next)&&!(o=o.call(r,i[1])).done)return o;switch(r=0,o&&(i=[2&i[0],o.value]),i[0]){case 0:case 1:o=i;break;case 4:return s.label++,{value:i[1],done:!1};case 5:s.label++,r=i[1],i=[0];continue;case 7:i=s.ops.pop(),s.trys.pop();continue;default:if(!(o=s.trys,(o=o.length>0&&o[o.length-1])||6!==i[0]&&2!==i[0])){s=0;continue}if(3===i[0]&&(!o||i[1]>o[0]&&i[1]<o[3])){s.label=i[1];break}if(6===i[0]&&s.label<o[1]){s.label=o[1],o=i;break}if(o&&s.label<o[2]){s.label=o[2],s.ops.push(i);break}o[2]&&s.ops.pop(),s.trys.pop();continue}i=e.call(t,s)}catch(t){i=[6,t],r=0}finally{n=o=0}if(5&i[0])throw i[1];return{value:i[0]?i[1]:void 0,done:!0}}([i,a])}}}Object.create;Object.create;var h=new e.InjectionToken("Asgardeo.config.angular"),f=function(){function t(t){try{this.router=t.get(o.Router)}catch(t){console.warn("Router is Not Provided")}}return t.prototype.navigateByUrl=function(t){return this.router?this.router.navigateByUrl(t):Promise.resolve(!1)},t.prototype.setRedirectUrl=function(){sessionStorage.setItem("redirectUrl",this.getCurrentRoute())},t.prototype.getRedirectUrl=function(){return sessionStorage.getItem("redirectUrl")||"/"},t.prototype.getRouteWithoutParams=function(t){return new URL(t).pathname},t.prototype.getCurrentRoute=function(){return this.router?this.router.url.split("?")[0]:window.location.href.split("?")[0]},t.prototype.getCurrentUrl=function(){return this.router?this.router.url:window.location.href},t}();f.ɵprov=a.ɵɵdefineInjectable({factory:function(){return new f(a.ɵɵinject(a.INJECTOR))},token:f,providedIn:"root"}),f.decorators=[{type:e.Injectable,args:[{providedIn:"root"}]}],f.ctorParameters=function(){return[{type:e.Injector}]};var l=function(){function t(){this.DEFAULT_STATE={allowedScopes:"",displayName:"",email:"",isAuthenticated:!1,isLoading:!0,sub:"",username:""},this._state=new r.BehaviorSubject(this.DEFAULT_STATE),this.state$=this._state.asObservable()}return Object.defineProperty(t.prototype,"state",{get:function(){return this._state.getValue()},set:function(t){this._state.next(Object.assign(Object.assign({},this._state),t))},enumerable:!1,configurable:!0}),t.prototype.setIsLoading=function(t){this._state.next(Object.assign(Object.assign({},this.state),{isLoading:t}))},t.prototype.reset=function(){this._state.next(this.DEFAULT_STATE)},t}();l.ɵprov=a.ɵɵdefineInjectable({factory:function(){return new l},token:l,providedIn:"root"}),l.decorators=[{type:e.Injectable,args:[{providedIn:"root"}]}];var p=function(){function t(t,e,o){var s=this;this.authConfig=t,this.navigator=e,this.stateStore=o,this.state$=this.stateStore.state$,this.auth=n.AsgardeoSPAClient.getInstance(),this.subscriptionDestroyer$=new r.Subject,this.trySignInSilently=function(){return u(s,void 0,void 0,(function(){var t=this;return c(this,(function(e){return this.stateStore.setIsLoading(!0),[2,this.auth.trySignInSilently().then((function(e){return u(t,void 0,void 0,(function(){var t;return c(this,(function(n){switch(n.label){case 0:return e?[4,this.auth.isAuthenticated()]:[2,!1];case 1:return n.sent()&&(t=e,this.stateStore.state={allowedScopes:t.allowedScopes,displayName:t.displayName,email:t.email,isAuthenticated:!0,isLoading:!1,sub:t.sub,username:t.username}),[2,e]}}))}))})).catch((function(t){return Promise.reject(t)})).finally((function(){t.stateStore.setIsLoading(!1)}))]}))}))},this.config=t,this.auth=n.AsgardeoSPAClient.getInstance(),this.initializeHooks(),u(s,void 0,void 0,(function(){return c(this,(function(t){switch(t.label){case 0:return[4,this.auth.initialize(this.authConfig)];case 1:return t.sent(),[4,this.handleSingleLogoutOnRedirect()];case 2:return t.sent(),this.handleAutoLogin().pipe(i.takeUntil(this.subscriptionDestroyer$)).subscribe(),[2]}}))}))}return t.prototype.ngOnDestroy=function(){this.subscriptionDestroyer$.next(!0),this.subscriptionDestroyer$.unsubscribe()},t.prototype.initializeHooks=function(){this.auth.on(n.Hooks.SignOut,(function(){}))},t.prototype.signIn=function(t,e,n){var r=this;return this.stateStore.setIsLoading(!0),this.auth.signIn(t,e,n).then((function(t){return u(r,void 0,void 0,(function(){return c(this,(function(e){switch(e.label){case 0:return t?[4,this.auth.isAuthenticated()]:[2];case 1:return e.sent()&&(this.stateStore.state={allowedScopes:t.allowedScopes,displayName:t.displayName,email:t.email,isAuthenticated:!0,isLoading:!1,sub:t.sub,username:t.username}),[2,t]}}))}))})).catch((function(t){return Promise.reject(t)})).finally((function(){r.stateStore.setIsLoading(!1)}))},t.prototype.signInWithRedirect=function(){this.navigator.setRedirectUrl();var t=this.navigator.getRouteWithoutParams(this.authConfig.signInRedirectURL);return this.navigator.navigateByUrl(t)},t.prototype.signOut=function(){var t=this;return this.stateStore.setIsLoading(!0),this.auth.signOut().then((function(e){return t.stateStore.reset(),e})).catch((function(t){return Promise.reject(t)})).finally((function(){t.stateStore.setIsLoading(!1)}))},t.prototype.isAuthenticated=function(){return this.auth.isAuthenticated()},t.prototype.getBasicUserInfo=function(){return this.auth.getBasicUserInfo()},t.prototype.getAccessToken=function(){return this.auth.getAccessToken()},t.prototype.getIDToken=function(){return this.auth.getIDToken()},t.prototype.getDecodedIDToken=function(){return this.auth.getDecodedIDToken()},t.prototype.getOIDCServiceEndpoints=function(){return this.auth.getOIDCServiceEndpoints()},t.prototype.refreshAccessToken=function(){return this.auth.refreshAccessToken()},t.prototype.revokeAccessToken=function(){var t=this;return this.auth.revokeAccessToken().then((function(){return t.stateStore.reset(),!0})).catch((function(t){return Promise.reject(t)})).finally((function(){t.stateStore.setIsLoading(!1)}))},t.prototype.on=function(t,e,r){return t===n.Hooks.CustomGrant?this.auth.on(t,e,r):this.auth.on(t,e)},t.prototype.requestCustomGrant=function(t){return this.auth.requestCustomGrant(t)},t.prototype.httpRequest=function(t){return this.auth.httpRequest(t)},t.prototype.httpRequestAll=function(t){return this.auth.httpRequestAll(t)},t.prototype.handleAutoLogin=function(){return this.stateStore.setIsLoading(!0),!this.config.skipRedirectCallback&&n.SPAUtils.hasAuthSearchParamsInURL()?r.from(this.signIn()):r.from(this.trySignInSilently())},t.prototype.handleSingleLogoutOnRedirect=function(){return u(this,void 0,void 0,(function(){return c(this,(function(t){return n.SPAUtils.hasErrorInURL()?[2,this.signIn({callOnlyOnRedirect:!0})]:[2]}))}))},t}();p.ɵprov=a.ɵɵdefineInjectable({factory:function(){return new p(a.ɵɵinject(h),a.ɵɵinject(f),a.ɵɵinject(l))},token:p,providedIn:"root"}),p.decorators=[{type:e.Injectable,args:[{providedIn:"root"}]}],p.ctorParameters=function(){return[{type:void 0,decorators:[{type:e.Inject,args:[h]}]},{type:f},{type:l}]};var d=function(){function t(t,e){this.auth=t,this.navigator=e}return t.prototype.ngOnInit=function(){var t=this;this.auth.on(n.Hooks.SignIn,(function(){t.navigator.navigateByUrl(t.navigator.getRedirectUrl())})),n.SPAUtils.hasAuthSearchParamsInURL(this.navigator.getCurrentUrl())||this.auth.signIn()},t}();d.decorators=[{type:e.Component,args:[{selector:"lib-asgardeo-sign-in-redirect",template:""}]}],d.ctorParameters=function(){return[{type:p},{type:f}]};var g=function(){function t(t){this.auth=t}return t.prototype.canActivate=function(){return u(this,void 0,void 0,(function(){return c(this,(function(t){switch(t.label){case 0:return[4,this.auth.isAuthenticated()];case 1:return t.sent()?[2,!0]:[2,!1]}}))}))},t.prototype.canActivateChild=function(){return u(this,void 0,void 0,(function(){return c(this,(function(t){return[2,this.canActivate()]}))}))},t}();g.ɵprov=a.ɵɵdefineInjectable({factory:function(){return new g(a.ɵɵinject(p))},token:g,providedIn:"root"}),g.decorators=[{type:e.Injectable,args:[{providedIn:"root"}]}],g.ctorParameters=function(){return[{type:p}]};var y=function(){function t(){}return t.forRoot=function(e){return{ngModule:t,providers:[p,g,{provide:h,useValue:e}]}},t}();y.decorators=[{type:e.NgModule,args:[{declarations:[d]}]}];var v=function(){function t(t,e){this.auth=t,this.authConfig=e}return t.prototype.intercept=function(t,e){return this.canAttachToken(t,this.authConfig.resourceServerURLs)?r.from(this.auth.getAccessToken()).pipe(i.mergeMap((function(n){if(n){var r="Bearer "+n,o=t.headers.set("Authorization",r);t=t.clone({headers:o})}return e.handle(t)})),i.catchError((function(n){return console.error(n),e.handle(t)}))):e.handle(t)},t.prototype.canAttachToken=function(t,e){var n=!1;return e&&e.forEach((function(e){var r;(null===(r=t.url)||void 0===r?void 0:r.startsWith(e))&&(n=!0)})),n},t}();v.decorators=[{type:e.Injectable}],v.ctorParameters=function(){return[{type:p},{type:void 0,decorators:[{type:e.Inject,args:[h]}]}]},Object.defineProperty(t,"Hooks",{enumerable:!0,get:function(){return n.Hooks}}),Object.defineProperty(t,"ResponseMode",{enumerable:!0,get:function(){return n.ResponseMode}}),Object.defineProperty(t,"Storage",{enumerable:!0,get:function(){return n.Storage}}),t.AsgardeoAuthGuard=g,t.AsgardeoAuthInterceptor=v,t.AsgardeoAuthModule=y,t.AsgardeoAuthService=p,t.AsgardeoSignInRedirectComponent=d,t["ɵa"]=h,t["ɵc"]=f,t["ɵd"]=l,t["ɵe"]=p,Object.defineProperty(t,"__esModule",{value:!0})}));
//# sourceMappingURL=asgardeo-auth-angular.umd.min.js.map