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

View File

@@ -75,6 +75,7 @@ import {
AuthManager, AuthManager,
RevisionClientInterface, RevisionClientInterface,
RevisionManager, RevisionManager,
ApiServiceEvent,
} from '@standardnotes/services' } from '@standardnotes/services'
import { BackupServiceInterface, FilesClientInterface } from '@standardnotes/files' import { BackupServiceInterface, FilesClientInterface } from '@standardnotes/files'
import { ComputePrivateUsername } from '@standardnotes/encryption' 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.integrityService, ExternalServices.SyncEvent.SyncRequestsIntegrityCheck)
this.internalEventBus.addEventHandler(this.syncService, ExternalServices.IntegrityEvent.IntegrityCheckCompleted) this.internalEventBus.addEventHandler(this.syncService, ExternalServices.IntegrityEvent.IntegrityCheckCompleted)
this.internalEventBus.addEventHandler(this.userService, AccountEvent.SignedInOrRegistered) this.internalEventBus.addEventHandler(this.userService, AccountEvent.SignedInOrRegistered)
this.internalEventBus.addEventHandler(this.sessionManager, ApiServiceEvent.SessionRefreshed)
} }
private clearInternalEventBus(): void { private clearInternalEventBus(): void {

View File

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

View File

@@ -22,6 +22,10 @@ import {
UNSUPPORTED_KEY_DERIVATION, UNSUPPORTED_KEY_DERIVATION,
UNSUPPORTED_PROTOCOL_VERSION, UNSUPPORTED_PROTOCOL_VERSION,
Challenge, Challenge,
InternalEventHandlerInterface,
InternalEventInterface,
ApiServiceEvent,
SessionRefreshedData,
} from '@standardnotes/services' } from '@standardnotes/services'
import { Base64String } from '@standardnotes/sncrypto-common' import { Base64String } from '@standardnotes/sncrypto-common'
import { ClientDisplayableError, SessionBody } from '@standardnotes/responses' 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 * 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. * 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 user?: Responses.User
private isSessionRenewChallengePresented = false 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 { override deinit(): void {
;(this.protocolService as unknown) = undefined ;(this.protocolService as unknown) = undefined
;(this.diskStorageService as unknown) = undefined ;(this.diskStorageService as unknown) = undefined