diff --git a/src/abstractions/api.service.ts b/src/abstractions/api.service.ts index 3ee640286a8..3c8775eaf8b 100644 --- a/src/abstractions/api.service.ts +++ b/src/abstractions/api.service.ts @@ -234,5 +234,6 @@ export abstract class ApiService { getUserPublicKey: (id: string) => Promise; + getActiveBearerToken: () => Promise; fetch: (request: Request) => Promise; } diff --git a/src/abstractions/notifications.service.ts b/src/abstractions/notifications.service.ts index 1908c436bcb..e89896550d2 100644 --- a/src/abstractions/notifications.service.ts +++ b/src/abstractions/notifications.service.ts @@ -3,6 +3,6 @@ import { EnvironmentService } from './environment.service'; export abstract class NotificationsService { init: (environmentService: EnvironmentService) => Promise; updateConnection: () => Promise; - reconnectFromActivity: () => Promise; - disconnectFromInactivity: () => Promise; + reconnectFromActivity: () => Promise; + disconnectFromInactivity: () => Promise; } diff --git a/src/services/api.service.ts b/src/services/api.service.ts index 20d28c42415..8d3669213b0 100644 --- a/src/services/api.service.ts +++ b/src/services/api.service.ts @@ -776,6 +776,15 @@ export class ApiService implements ApiServiceAbstraction { // Helpers + async getActiveBearerToken(): Promise { + let accessToken = await this.tokenService.getToken(); + if (this.tokenService.tokenNeedsRefresh()) { + const tokenResponse = await this.doRefreshToken(); + accessToken = tokenResponse.accessToken; + } + return accessToken; + } + fetch(request: Request): Promise { 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 { - 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 { const refreshToken = await this.tokenService.getRefreshToken(); if (refreshToken == null || refreshToken === '') { diff --git a/src/services/notifications.service.ts b/src/services/notifications.service.ts index f9af37ed404..896e7b9d3ad 100644 --- a/src/services/notifications.service.ts +++ b/src/services/notifications.service.ts @@ -4,10 +4,10 @@ import { NotificationType } from '../enums/notificationType'; import { ApiService } from '../abstractions/api.service'; import { AppIdService } from '../abstractions/appId.service'; +import { CryptoService } from '../abstractions/crypto.service'; import { EnvironmentService } from '../abstractions/environment.service'; import { NotificationsService as NotificationsServiceAbstraction } from '../abstractions/notifications.service'; import { SyncService } from '../abstractions/sync.service'; -import { TokenService } from '../abstractions/token.service'; import { UserService } from '../abstractions/user.service'; import { @@ -24,9 +24,9 @@ export class NotificationsService implements NotificationsServiceAbstraction { private inactive = false; private reconnectTimer: any = null; - constructor(private userService: UserService, private tokenService: TokenService, - private syncService: SyncService, private appIdService: AppIdService, - private apiService: ApiService) { } + constructor(private userService: UserService, private syncService: SyncService, + private appIdService: AppIdService, private apiService: ApiService, + private cryptoService: CryptoService) { } async init(environmentService: EnvironmentService): Promise { this.inited = false; @@ -46,7 +46,7 @@ export class NotificationsService implements NotificationsServiceAbstraction { this.signalrConnection = new signalR.HubConnectionBuilder() .withUrl(this.url + '/hub', { - accessTokenFactory: () => this.tokenService.getToken(), + accessTokenFactory: () => this.apiService.getActiveBearerToken(), }) // .configureLogging(signalR.LogLevel.Information) .build(); @@ -58,14 +58,14 @@ export class NotificationsService implements NotificationsServiceAbstraction { this.reconnect(); }); this.inited = true; - if (await this.userService.isAuthenticated()) { + if (await this.isAuthedAndUnlocked()) { await this.connect(); } } async updateConnection(): Promise { try { - if (await this.userService.isAuthenticated()) { + if (await this.isAuthedAndUnlocked()) { await this.connect(); } else { await this.signalrConnection.stop(); @@ -76,16 +76,17 @@ export class NotificationsService implements NotificationsServiceAbstraction { } } - async reconnectFromActivity(): Promise { + async reconnectFromActivity(): Promise { this.inactive = false; if (!this.connected) { - if (await this.userService.isAuthenticated()) { - return this.reconnect().then(() => this.syncService.fullSync(false)); + if (await this.isAuthedAndUnlocked()) { + await this.reconnect(); + await this.syncService.fullSync(false); } } } - async disconnectFromInactivity(): Promise { + async disconnectFromInactivity(): Promise { this.inactive = true; if (this.connected) { await this.signalrConnection.stop(); @@ -145,8 +146,8 @@ export class NotificationsService implements NotificationsServiceAbstraction { if (this.connected || !this.inited || this.inactive) { return; } - const authed = await this.userService.isAuthenticated(); - if (!authed) { + const authedAndUnlocked = await this.isAuthedAndUnlocked(); + if (!authedAndUnlocked) { return; } @@ -158,4 +159,11 @@ export class NotificationsService implements NotificationsServiceAbstraction { this.reconnectTimer = setTimeout(() => this.reconnect(), 120000); } } + + private async isAuthedAndUnlocked() { + if (await this.userService.isAuthenticated()) { + return this.cryptoService.hasKey(); + } + return false; + } }