1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-17 00:33:44 +00:00

refresh notification token if needed. authed and unlocked required

This commit is contained in:
Kyle Spearrin
2018-08-22 21:46:34 -04:00
parent d37fa836da
commit a360cd8e61
4 changed files with 35 additions and 27 deletions

View File

@@ -776,6 +776,15 @@ export class ApiService implements ApiServiceAbstraction {
// Helpers
async getActiveBearerToken(): Promise<string> {
let accessToken = await this.tokenService.getToken();
if (this.tokenService.tokenNeedsRefresh()) {
const tokenResponse = await this.doRefreshToken();
accessToken = tokenResponse.accessToken;
}
return accessToken;
}
fetch(request: Request): Promise<Response> {
if (request.method === 'GET') {
request.headers.set('Cache-Control', 'no-cache');
@@ -797,8 +806,8 @@ export class ApiService implements ApiServiceAbstraction {
};
if (authed) {
const authHeader = await this.handleTokenState();
headers.set('Authorization', authHeader);
const authHeader = await this.getActiveBearerToken();
headers.set('Authorization', 'Bearer ' + authHeader);
}
if (body != null) {
if (typeof body === 'string') {
@@ -844,16 +853,6 @@ export class ApiService implements ApiServiceAbstraction {
return new ErrorResponse(responseJson, response.status, tokenError);
}
private async handleTokenState(): Promise<string> {
let accessToken = await this.tokenService.getToken();
if (this.tokenService.tokenNeedsRefresh()) {
const tokenResponse = await this.doRefreshToken();
accessToken = tokenResponse.accessToken;
}
return 'Bearer ' + accessToken;
}
private async doRefreshToken(): Promise<IdentityTokenResponse> {
const refreshToken = await this.tokenService.getRefreshToken();
if (refreshToken == null || refreshToken === '') {