UNPKG

6.17 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("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","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,null)}(this,function(t,n,o,r,i,a,u,s,c,p){"use strict";var l=function e(t){this.endpoint="https://account.kemnaker.go.id",Object.assign(this,t)};var f=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}(),h=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(f,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(){return this.signature=null,this.user=null,Promise.all([this.storage.remove("signature"),this.storage.remove("user")])},t.prototype.getState=function(){return this.storage.get("state")},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}(),g=function d(){},m=function(){function t(t){this.service=t}return t.prototype.canActivate=function(){var r=this;return this.service.getUser().pipe(s.first(),s.map(function(t){if(null===t){var e=document.location.protocol+"//"+document.location.hostname;document.location.port&&(e+=":"+document.location.port),window.location.href=r.service.login("basic email",e+"/auth")}return null!==t}))},t.prototype.canActivateChild=function(){return this.canActivate()},t.decorators=[{type:n.Injectable}],t.ctorParameters=function(){return[{type:h}]},t}(),y=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:h}]},t}();function v(t,e,r,n){return new h(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 b=new n.InjectionToken("NACO_CONFIG"),j=function(){function t(t,e,r){var n=this;this.route=t,this.router=e,this.naco=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.router.navigate(["/"])})},t.decorators=[{type:n.Component,args:[{selector:"naker-auth-page",template:"{{ message }}"}]}],t.ctorParameters=function(){return[{type:r.ActivatedRoute},{type:r.Router},{type:h}]},t}(),S=[{path:"auth",component:j}],R=r.RouterModule.forChild(S),P=function(){function t(){}return t.decorators=[{type:n.NgModule,args:[{imports:[R],declarations:[j]}]}],t}(),x=function(){function e(){}return e.forRoot=function(t){return{ngModule:e,providers:[{provide:b,useValue:t},{provide:h,useFactory:v,deps:[b,r.Router,i.Storage,c.HttpClient]},m]}},e.decorators=[{type:n.NgModule,args:[{imports:[i.UbudStorageModule,P]}]}],e}();t.NacoService=h,t.User=f,t.Signature=g,t.Config=l,t.AuthenticatedGuard=m,t.AuthInterceptor=y,t.NacoModule=x,t.ɵe=b,t.ɵd=v,t.ɵa=P,t.ɵc=j,t.ɵb=R,Object.defineProperty(t,"__esModule",{value:!0})});
2//# sourceMappingURL=naker-naco.umd.min.js.map
\No newline at end of file