@auth0/angular-jwt
Version:
JSON Web Token helper library for Angular
2 lines • 5.07 kB
JavaScript
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("@angular/core"),require("@angular/common"),require("rxjs/operators"),require("rxjs"),require("@angular/common/http")):"function"==typeof define&&define.amd?define("@auth0/angular-jwt",["exports","@angular/core","@angular/common","rxjs/operators","rxjs","@angular/common/http"],t):t(((e=e||self).auth0=e.auth0||{},e.auth0["angular-jwt"]={}),e.ng.core,e.ng.common,e.rxjs.operators,e.rxjs,e.ng.common.http)}(this,(function(e,t,r,o,n,i){"use strict";var a=new t.InjectionToken("JWT_OPTIONS"),s=function(){function e(e){void 0===e&&(e=null),this.tokenGetter=e&&e.tokenGetter||function(){}}return e.prototype.urlBase64Decode=function(e){var t=e.replace(/-/g,"+").replace(/_/g,"/");switch(t.length%4){case 0:break;case 2:t+="==";break;case 3:t+="=";break;default:throw new Error("Illegal base64url string!")}return this.b64DecodeUnicode(t)},e.prototype.b64decode=function(e){var t="";if((e=String(e).replace(/=+$/,"")).length%4==1)throw new Error("'atob' failed: The string to be decoded is not correctly encoded.");for(var r=0,o=void 0,n=void 0,i=0;n=e.charAt(i++);~n&&(o=r%4?64*o+n:n,r++%4)?t+=String.fromCharCode(255&o>>(-2*r&6)):0)n="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=".indexOf(n);return t},e.prototype.b64DecodeUnicode=function(e){return decodeURIComponent(Array.prototype.map.call(this.b64decode(e),(function(e){return"%"+("00"+e.charCodeAt(0).toString(16)).slice(-2)})).join(""))},e.prototype.decodeToken=function(e){if(void 0===e&&(e=this.tokenGetter()),!e||""===e)return null;var t=e.split(".");if(3!==t.length)throw new Error("The inspected token doesn't appear to be a JWT. Check to make sure it has three parts and see https://jwt.io for more.");var r=this.urlBase64Decode(t[1]);if(!r)throw new Error("Cannot decode the token.");return JSON.parse(r)},e.prototype.getTokenExpirationDate=function(e){var t;if(void 0===e&&(e=this.tokenGetter()),!(t=this.decodeToken(e))||!t.hasOwnProperty("exp"))return null;var r=new Date(0);return r.setUTCSeconds(t.exp),r},e.prototype.isTokenExpired=function(e,t){if(void 0===e&&(e=this.tokenGetter()),!e||""===e)return!0;var r=this.getTokenExpirationDate(e);return t=t||0,null!==r&&!(r.valueOf()>(new Date).valueOf()+1e3*t)},e.prototype.getAuthScheme=function(e,t){return"function"==typeof e?e(t):e},e}();s.decorators=[{type:t.Injectable}],s.ctorParameters=function(){return[{type:void 0,decorators:[{type:t.Inject,args:[a]}]}]};var u=function(){function e(e,t,r){this.jwtHelper=t,this.document=r,this.standardPorts=["80","443"],this.tokenGetter=e.tokenGetter,this.headerName=e.headerName||"Authorization",this.authScheme=e.authScheme||""===e.authScheme?e.authScheme:"Bearer ",this.allowedDomains=e.allowedDomains||[],this.disallowedRoutes=e.disallowedRoutes||[],this.throwNoTokenError=e.throwNoTokenError||!1,this.skipWhenExpired=e.skipWhenExpired}return e.prototype.isAllowedDomain=function(e){var t=new URL(e.url,this.document.location.origin);if(t.host===this.document.location.host)return!0;var r=t.hostname+(t.port&&!this.standardPorts.includes(t.port)?":"+t.port:"");return this.allowedDomains.findIndex((function(e){return"string"==typeof e?e===r:e instanceof RegExp&&e.test(r)}))>-1},e.prototype.isDisallowedRoute=function(e){var t=this,r=new URL(e.url,this.document.location.origin);return this.disallowedRoutes.findIndex((function(o){if("string"==typeof o){var n=new URL(o,t.document.location.origin);return n.hostname===r.hostname&&n.pathname===r.pathname}return o instanceof RegExp&&o.test(e.url)}))>-1},e.prototype.handleInterception=function(e,t,r){var o,n=this.jwtHelper.getAuthScheme(this.authScheme,t),i=!1;if(!e&&this.throwNoTokenError)throw new Error("Could not get token from tokenGetter function.");return this.skipWhenExpired&&(i=!e||this.jwtHelper.isTokenExpired(e)),e&&i&&this.skipWhenExpired?t=t.clone():e&&(t=t.clone({setHeaders:(o={},o[this.headerName]=""+n+e,o)})),r.handle(t)},e.prototype.intercept=function(e,t){var r=this;if(!this.isAllowedDomain(e)||this.isDisallowedRoute(e))return t.handle(e);var i=this.tokenGetter(e);return i instanceof Promise?n.from(i).pipe(o.mergeMap((function(o){return r.handleInterception(o,e,t)}))):this.handleInterception(i,e,t)},e}();u.decorators=[{type:t.Injectable}],u.ctorParameters=function(){return[{type:void 0,decorators:[{type:t.Inject,args:[a]}]},{type:s},{type:Document,decorators:[{type:t.Inject,args:[r.DOCUMENT]}]}]};var c=function(){function e(e){if(e)throw new Error("JwtModule is already loaded. It should only be imported in your application's main module.")}return e.forRoot=function(t){return{ngModule:e,providers:[{provide:i.HTTP_INTERCEPTORS,useClass:u,multi:!0},t.jwtOptionsProvider||{provide:a,useValue:t.config},s]}},e}();c.decorators=[{type:t.NgModule}],c.ctorParameters=function(){return[{type:c,decorators:[{type:t.Optional},{type:t.SkipSelf}]}]},e.JWT_OPTIONS=a,e.JwtHelperService=s,e.JwtInterceptor=u,e.JwtModule=c,Object.defineProperty(e,"__esModule",{value:!0})}));
//# sourceMappingURL=auth0-angular-jwt.umd.min.js.map