fix(snjs): notify http service of refresh session

This commit is contained in:
Karol Sójko
2023-01-18 14:38:26 +01:00
parent 76937ba68c
commit 271db1c3dc
4 changed files with 35 additions and 8 deletions

View File

@@ -1,12 +1,15 @@
import { AbstractService } from '../Service/AbstractService'
import { Uuid } from '@standardnotes/common'
import { Either, Uuid } from '@standardnotes/common'
import { Role } from '@standardnotes/auth'
import { FilesApiInterface } from '@standardnotes/files'
import { Session } from '@standardnotes/domain-core'
import { AbstractService } from '../Service/AbstractService'
/* istanbul ignore file */
export enum ApiServiceEvent {
MetaReceived = 'MetaReceived',
SessionRefreshed = 'SessionRefreshed',
}
export type MetaReceivedData = {
@@ -14,6 +17,10 @@ export type MetaReceivedData = {
userRoles: Role[]
}
export interface ApiServiceInterface
extends AbstractService<ApiServiceEvent.MetaReceived, MetaReceivedData>,
FilesApiInterface {}
export type SessionRefreshedData = {
session: Session
}
export type ApiServiceEventData = Either<MetaReceivedData, SessionRefreshedData>
export interface ApiServiceInterface extends AbstractService<ApiServiceEvent, ApiServiceEventData>, FilesApiInterface {}

View File

@@ -75,6 +75,7 @@ import {
AuthManager,
RevisionClientInterface,
RevisionManager,
ApiServiceEvent,
} from '@standardnotes/services'
import { BackupServiceInterface, FilesClientInterface } from '@standardnotes/files'
import { ComputePrivateUsername } from '@standardnotes/encryption'
@@ -1288,6 +1289,7 @@ export class SNApplication implements ApplicationInterface, AppGroupManagedAppli
this.internalEventBus.addEventHandler(this.integrityService, ExternalServices.SyncEvent.SyncRequestsIntegrityCheck)
this.internalEventBus.addEventHandler(this.syncService, ExternalServices.IntegrityEvent.IntegrityCheckCompleted)
this.internalEventBus.addEventHandler(this.userService, AccountEvent.SignedInOrRegistered)
this.internalEventBus.addEventHandler(this.sessionManager, ApiServiceEvent.SessionRefreshed)
}
private clearInternalEventBus(): void {

View File

@@ -10,7 +10,6 @@ import {
ItemsServerInterface,
StorageKey,
ApiServiceEvent,
MetaReceivedData,
DiagnosticInfo,
KeyValueStoreInterface,
API_MESSAGE_GENERIC_SYNC_FAIL,
@@ -31,6 +30,7 @@ import {
API_MESSAGE_INVALID_SESSION,
API_MESSAGE_LOGIN_IN_PROGRESS,
API_MESSAGE_TOKEN_REFRESH_IN_PROGRESS,
ApiServiceEventData,
} from '@standardnotes/services'
import { FilesApiInterface } from '@standardnotes/files'
import { ServerSyncPushContextualPayload, SNFeatureRepo, FileContent } from '@standardnotes/models'
@@ -56,7 +56,7 @@ const V0_API_VERSION = '20200115'
type InvalidSessionObserver = (revoked: boolean) => void
export class SNApiService
extends AbstractService<ApiServiceEvent.MetaReceived, MetaReceivedData>
extends AbstractService<ApiServiceEvent, ApiServiceEventData>
implements
ApiServiceInterface,
FilesApiInterface,
@@ -443,6 +443,11 @@ export class SNApiService
this.setSession(session)
this.processResponse(response)
await this.notifyEventSync(ApiServiceEvent.SessionRefreshed, {
session,
})
return response
})
.catch((errorResponse) => {

View File

@@ -22,6 +22,10 @@ import {
UNSUPPORTED_KEY_DERIVATION,
UNSUPPORTED_PROTOCOL_VERSION,
Challenge,
InternalEventHandlerInterface,
InternalEventInterface,
ApiServiceEvent,
SessionRefreshedData,
} from '@standardnotes/services'
import { Base64String } from '@standardnotes/sncrypto-common'
import { ClientDisplayableError, SessionBody } from '@standardnotes/responses'
@@ -67,7 +71,10 @@ export enum SessionEvent {
* server credentials, such as the session token. It also exposes methods for registering
* for a new account, signing into an existing one, or changing an account password.
*/
export class SNSessionManager extends AbstractService<SessionEvent> implements SessionsClientInterface {
export class SNSessionManager
extends AbstractService<SessionEvent>
implements SessionsClientInterface, InternalEventHandlerInterface
{
private user?: Responses.User
private isSessionRenewChallengePresented = false
@@ -94,6 +101,12 @@ export class SNSessionManager extends AbstractService<SessionEvent> implements S
})
}
async handleEvent(event: InternalEventInterface): Promise<void> {
if (event.type === ApiServiceEvent.SessionRefreshed) {
this.httpService.setSession((event.payload as SessionRefreshedData).session)
}
}
override deinit(): void {
;(this.protocolService as unknown) = undefined
;(this.diskStorageService as unknown) = undefined