diff --git a/src/popup/app/config.js b/src/popup/app/config.js index f55de6fa6b..2941081a18 100644 --- a/src/popup/app/config.js +++ b/src/popup/app/config.js @@ -85,12 +85,12 @@ } }); }) - .run(function ($rootScope, userService, loginService, jwtHelper, tokenService, $state) { + .run(function ($rootScope, userService, loginService, tokenService, $state) { $rootScope.$on('$stateChangeStart', function (event, toState, toParams) { tokenService.getToken(function (token) { userService.isAuthenticated(function (isAuthenticated) { if (!toState.data || !toState.data.authorize) { - if (isAuthenticated && !jwtHelper.isTokenExpired(token)) { + if (isAuthenticated && !tokenService.isTokenExpired(token)) { event.preventDefault(); $state.go('tabs.current'); } @@ -98,7 +98,7 @@ return; } - if (!isAuthenticated || jwtHelper.isTokenExpired(token)) { + if (!isAuthenticated || tokenService.isTokenExpired(token)) { event.preventDefault(); loginService.logOut(function () { $state.go('login'); diff --git a/src/services/tokenService.js b/src/services/tokenService.js index 597c38c1de..131225045a 100644 --- a/src/services/tokenService.js +++ b/src/services/tokenService.js @@ -46,4 +46,59 @@ callback(); }); }; + + // jwthelper methods + // ref https://github.com/auth0/angular-jwt/blob/master/src/angularJwt/services/jwt.js + + TokenService.prototype.decodeToken = function (token) { + var parts = token.split('.'); + + if (parts.length !== 3) { + throw new Error('JWT must have 3 parts'); + } + + var decoded = urlBase64Decode(parts[1]); + if (!decoded) { + throw new Error('Cannot decode the token'); + } + + return JSON.parse(decoded); + }; + + TokenService.prototype.getTokenExpirationDate = function (token) { + var decoded = this.decodeToken(token); + + if (typeof decoded.exp === "undefined") { + return null; + } + + var d = new Date(0); // The 0 here is the key, which sets the date to the epoch + d.setUTCSeconds(decoded.exp); + + return d; + }; + + TokenService.prototype.isTokenExpired = function (token, offsetSeconds) { + var d = this.getTokenExpirationDate(token); + offsetSeconds = offsetSeconds || 0; + if (d === null) { + return false; + } + + // Token expired? + return !(d.valueOf() > (new Date().valueOf() + (offsetSeconds * 1000))); + }; + + function urlBase64Decode(str) { + var output = str.replace(/-/g, '+').replace(/_/g, '/'); + switch (output.length % 4) { + case 0: { break; } + case 2: { output += '=='; break; } + case 3: { output += '='; break; } + default: { + throw 'Illegal base64url string!'; + } + } + return window.decodeURIComponent(escape(window.atob(output))); //polyfill https://github.com/davidchambers/Base64.js + }; }(); diff --git a/src/services/userService.js b/src/services/userService.js index 89b765be24..efea59df29 100644 --- a/src/services/userService.js +++ b/src/services/userService.js @@ -30,13 +30,13 @@ return; } - //var decodedToken = jwtHelper.decodeToken(token); - var twoFactor = false;// decodedToken.authmethod === "TwoFactor"; + var decodedToken = this.tokenService.decodeToken(token); + var twoFactor = decodedToken.authmethod === "TwoFactor"; _userProfile = { - //id: decodedToken.nameid, - //email: decodedToken.email, - //twoFactor: twoFactor + id: decodedToken.nameid, + email: decodedToken.email, + twoFactor: twoFactor }; if (!twoFactor && profile) {