UNPKG

6.93 kBJavaScriptView Raw
1!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("@angular/core"),require("class-transformer"),require("@angular/router"),require("@ubud/storage"),require("rxjs"),require("rxjs/internal/observable/fromPromise"),require("rxjs/internal/operators"),require("@angular/common/http"),require("@angular/common"),require("rxjs-compat/observable/ErrorObservable")):"function"==typeof define&&define.amd?define("@naker/naco",["exports","@angular/core","class-transformer","@angular/router","@ubud/storage","rxjs","rxjs/internal/observable/fromPromise","rxjs/internal/operators","@angular/common/http","@angular/common","rxjs-compat/observable/ErrorObservable"],e):e((t.naker=t.naker||{},t.naker.naco={}),t.ng.core,null,t.ng.router,null,t.rxjs,t.rxjs["internal/observable/fromPromise"],t.rxjs["internal/operators"],t.ng.common.http,t.ng.common,null)}(this,function(t,n,o,r,i,a,u,s,c,e,p){"use strict";var l=function f(t){this.endpoint="https://account.kemnaker.go.id",Object.assign(this,t)};var h=function(){function t(){}return function e(t,e,r,n){var o,i=arguments.length,a=i<3?e:null===n?n=Object.getOwnPropertyDescriptor(e,r):n;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)a=Reflect.decorate(t,e,r,n);else for(var u=t.length-1;0<=u;u--)(o=t[u])&&(a=(i<3?o(a):3<i?o(e,r,a):o(e,r))||a);return 3<i&&a&&Object.defineProperty(e,r,a),a}([o.Type(n.forwardRef(function(){return Date})),function r(t,e){if("object"==typeof Reflect&&"function"==typeof Reflect.metadata)return Reflect.metadata(t,e)}("design:type",Date)],t.prototype,"updatedAt",void 0),t}(),g=function(){function t(t,e,r,n){this.config=t,this.router=e,this.storage=r,this.http=n,this.VERSION="v1",this.signature=null,this.user=null}return t.prototype.getUser=function(){var n=this;return null!==this.user?a.of(this.user):u.fromPromise(this.storage.get("user")).pipe(s.switchMap(function(t){if(null!==t)return a.of(t);var r=n.config.endpoint+"/api/"+n.VERSION+"/users/me";return u.fromPromise(n.getSignature()).pipe(s.switchMap(function(t){if(null===t)return a.of(null);var e={headers:{Authorization:t.type+" "+t.token}};return n.http.get(r,e).pipe(s.map(function(t){if(t.data)return t.data;throw new Error("There are no body to be transformed")}),s.map(function(t){var e=o.plainToClass(h,t);return n.storage.set("user",e),e}))}))}))},t.prototype.getSignature=function(){return null!==this.signature?Promise.resolve(this.signature):this.storage.get("signature")},t.prototype.setSignature=function(t){this.signature=t,this.storage.set("signature",t)},t.prototype.login=function(t,e){e||(e=this.router.url);var r=this.generateState();this.storage.set("state",r);var n=this.buildQueryString({response_type:"token",scopes:t,client:this.config.clientId,state:r,"continue":e});return this.config.endpoint+"/auth?"+n},t.prototype.logout=function(r){var n=this;return this.signature=null,this.user=null,new Promise(function(e){Promise.all([n.storage.remove("signature"),n.storage.remove("user")]).then(function(){var t=n.config.endpoint+"/auth/logout";r&&(t+="?continue="+r),e(t)})})},t.prototype.getState=function(){return this.storage.get("state")},t.prototype.buildNestedUri=function(t){var e=this,r="";return t.routeConfig&&t.routeConfig.path&&(r+="/"+t.routeConfig.path),t.children&&t.children.forEach(function(t){r+=e.buildNestedUri(t)}),r},t.prototype.generateState=function(){for(var t="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",e="",r=0;r<5;r++)e+=t.charAt(Math.floor(Math.random()*t.length));return e},t.prototype.buildQueryString=function(e){return Object.keys(e).map(function(t){return encodeURIComponent(t)+"="+encodeURIComponent(e[t])}).join("&")},t.decorators=[{type:n.Injectable}],t.ctorParameters=function(){return[{type:l},{type:r.Router},{type:i.Storage},{type:c.HttpClient}]},t}(),d=function m(){},y=function(){function t(t,e,r){this.naco=t,this.storage=e,this.platformLocation=r}return t.prototype.canActivate=function(i){var a=this;return this.naco.getUser().pipe(s.first(),s.map(function(t){if(null===t){var e=a.rtrim(a.platformLocation.location.href),r=a.naco.buildNestedUri(i.root),n=e.replace(r,"")+"/"+a.ltrim(r),o=a.platformLocation.location.origin;a.storage.set("naco_intended_url",n).then(function(){window.location.href=a.naco.login("basic email",o+"/auth")})}return!!t}))},t.prototype.canActivateChild=function(t){return this.canActivate(t)},t.prototype.ltrim=function(t){return t.replace(/^\/+/,"")},t.prototype.rtrim=function(t){return t.replace(/\/+$/,"")},t.decorators=[{type:n.Injectable}],t.ctorParameters=function(){return[{type:g},{type:i.Storage},{type:e.PlatformLocation}]},t}(),v=function(){function t(t){this.naco=t}return t.prototype.intercept=function(e,r){return u.fromPromise(this.naco.getSignature()).pipe(s.switchMap(function(t){return t?r.handle(e.clone({setHeaders:{Authorization:t.type+" "+t.token}})).pipe(s.catchError(function(t){return t.status,p.ErrorObservable.create(t)})):r.handle(e)}))},t.decorators=[{type:n.Injectable}],t.ctorParameters=function(){return[{type:g}]},t}();function b(t,e,r,n){return new g(new l(function(){return t=Object.assign||function t(t){for(var e,r=1,n=arguments.length;r<n;r++)for(var o in e=arguments[r])Object.prototype.hasOwnProperty.call(e,o)&&(t[o]=e[o]);return t},t.apply(this,arguments)}({},t)),e,r,n)}var j=new n.InjectionToken("NACO_CONFIG"),S=function(){function t(t,e,r){var n=this;this.route=t,this.naco=e,this.storage=r,this.message="Authenticating...",this.route.queryParams.subscribe(function(t){return n.handle(t)})}return t.prototype.handle=function(e){var r=this;e.state?this.naco.getState().then(function(t){t===e.state?r.authenticate(e):r.message="Invalid CSRF"}):this.authenticate(e)},t.prototype.authenticate=function(t){var e=this,r={expiresIn:t.expires_in,type:t.token_type,token:t.access_token};this.naco.setSignature(r),this.naco.getUser().subscribe(function(t){null===t?e.message="Unauthenticated!":e.storage.get("naco_intended_url").then(function(t){e.storage.remove("naco_intended_url").then(function(){window.location.href=t||"/"})})})},t.decorators=[{type:n.Component,args:[{selector:"naker-auth-page",template:"{{ message }}"}]}],t.ctorParameters=function(){return[{type:r.ActivatedRoute},{type:g},{type:i.Storage}]},t}(),P=[{path:"auth",component:S}],R=r.RouterModule.forChild(P),w=function(){function t(){}return t.decorators=[{type:n.NgModule,args:[{imports:[R],declarations:[S]}]}],t}(),x=function(){function e(){}return e.forRoot=function(t){return{ngModule:e,providers:[{provide:j,useValue:t},{provide:g,useFactory:b,deps:[j,r.Router,i.Storage,c.HttpClient]},y]}},e.decorators=[{type:n.NgModule,args:[{imports:[i.UbudStorageModule,w]}]}],e}();t.NacoService=g,t.User=h,t.Signature=d,t.Config=l,t.AuthenticatedGuard=y,t.AuthInterceptor=v,t.NacoModule=x,t.ɵe=j,t.ɵd=b,t.ɵa=w,t.ɵc=S,t.ɵb=R,Object.defineProperty(t,"__esModule",{value:!0})});
2//# sourceMappingURL=naker-naco.umd.min.js.map
\No newline at end of file