1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-11-25 12:15:18 +01:00

replace jwthelper

This commit is contained in:
Kyle Spearrin 2016-09-03 00:11:57 -04:00
parent e322c77725
commit 4c29b61189
3 changed files with 63 additions and 8 deletions

View File

@ -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');

View File

@ -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
};
}();

View File

@ -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) {