forked from PatrickJS/angular-jwt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathangular-jwt.min.js
executable file
·1 lines (1 loc) · 1.44 KB
/
angular-jwt.min.js
1
!function(){angular.module("angular-jwt",["angular-jwt.interceptor","angular-jwt.jwt"]),angular.module("angular-jwt.interceptor",[]).provider("jwtInterceptor",function(){this.authHeader="Authorization",this.authPrefix="Bearer ",this.tokenGetter=function(){return null};var e=this;this.$get=["$q","$injector","$rootScope",function(t,r,n){return{request:function(n){if(n.skipAuthorization)return n;if(n.headers=n.headers||{},n.headers[e.authHeader])return n;var a=t.when(r.invoke(e.tokenGetter,this,{config:n}));return a.then(function(t){return t&&(n.headers[e.authHeader]=e.authPrefix+t),n})},responseError:function(e){return 401===e.status&&n.$broadcast("unauthenticated",e),t.reject(e)}}}]}),angular.module("angular-jwt.jwt",[]).service("jwtHelper",function(){this.urlBase64Decode=function(e){var t=e.replace("-","+").replace("_","/");switch(t.length%4){case 0:break;case 2:t+="==";break;case 3:t+="=";break;default:throw"Illegal base64url string!"}return decodeURIComponent(escape(window.atob(t)))},this.decodeToken=function(e){var t=e.split(".");if(3!==t.length)throw new Error("JWT must have 3 parts");var r=this.urlBase64Decode(t[1]);if(!r)throw new Error("Cannot decode the token");return JSON.parse(r)},this.getTokenExpirationDate=function(e){var t;if(t=this.decodeToken(e),!t.exp)return null;var r=new Date(0);return r.setUTCSeconds(t.exp),r},this.isTokenExpired=function(e){var t=this.getTokenExpirationDate(e);return t?!(t.valueOf()>(new Date).valueOf()):!1}})}();