mirror of
https://github.com/bitwarden/browser.git
synced 2024-12-22 16:29:09 +01:00
replace jwthelper
This commit is contained in:
parent
e322c77725
commit
4c29b61189
@ -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');
|
||||
|
@ -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
|
||||
};
|
||||
}();
|
||||
|
@ -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) {
|
||||
|
Loading…
Reference in New Issue
Block a user