1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-20 02:03:39 +00:00

[Auto-Logout] Update Token Service (#94)

* Auto logout on restart

* Updated setTokens function

* Remove async deocrator from setTokens

Co-authored-by: Vincent Salucci <vsalucci@bitwarden.com>
This commit is contained in:
Vincent Salucci
2020-04-06 11:06:32 -05:00
committed by GitHub
parent 28e3fff739
commit 72e3893f8e
3 changed files with 42 additions and 4 deletions

View File

@@ -26,9 +26,15 @@ export class TokenService implements TokenServiceAbstraction {
]);
}
setToken(token: string): Promise<any> {
async setToken(token: string): Promise<any> {
this.token = token;
this.decodedToken = null;
if (await this.skipTokenStorage()) {
// if we have a vault timeout and the action is log out, don't store token
return;
}
return this.storageService.save(Keys.accessToken, token);
}
@@ -41,8 +47,14 @@ export class TokenService implements TokenServiceAbstraction {
return this.token;
}
setRefreshToken(refreshToken: string): Promise<any> {
async setRefreshToken(refreshToken: string): Promise<any> {
this.refreshToken = refreshToken;
if (await this.skipTokenStorage()) {
// if we have a vault timeout and the action is log out, don't store token
return;
}
return this.storageService.save(Keys.refreshToken, refreshToken);
}
@@ -55,6 +67,23 @@ export class TokenService implements TokenServiceAbstraction {
return this.refreshToken;
}
async toggleTokens(): Promise<any> {
const token = await this.getToken();
const refreshToken = await this.getRefreshToken();
const timeout = await this.storageService.get(ConstantsService.vaultTimeoutKey);
const action = await this.storageService.get(ConstantsService.vaultTimeoutActionKey);
if ((timeout != null || timeout === 0) && action === 'logOut') {
// if we have a vault timeout and the action is log out, reset tokens
await this.clearToken();
this.token = token;
this.refreshToken = refreshToken;
return;
}
await this.setToken(token);
await this.setRefreshToken(refreshToken);
}
setTwoFactorToken(token: string, email: string): Promise<any> {
return this.storageService.save(Keys.twoFactorTokenPrefix + email, token);
}
@@ -183,4 +212,10 @@ export class TokenService implements TokenServiceAbstraction {
return decoded.iss as string;
}
private async skipTokenStorage(): Promise<boolean> {
const timeout = await this.storageService.get<number>(ConstantsService.vaultTimeoutKey);
const action = await this.storageService.get<string>(ConstantsService.vaultTimeoutActionKey);
return timeout != null && action === 'logOut';
}
}