chore: store authenticator devices labels in preferences instead of server (#2252)

This commit is contained in:
Karol Sójko
2023-03-09 11:05:15 +01:00
committed by GitHub
parent aeab3fed0d
commit 54f1e618de
5 changed files with 48 additions and 5 deletions

View File

@@ -1,6 +1,5 @@
export interface ListAuthenticatorsResponseBody { export interface ListAuthenticatorsResponseBody {
authenticators: Array<{ authenticators: Array<{
id: string id: string
name: string
}> }>
} }

View File

@@ -1,3 +1,3 @@
export interface VerifyAuthenticatorRegistrationResponseBody { export interface VerifyAuthenticatorRegistrationResponseBody {
success: boolean id: string
} }

View File

@@ -44,6 +44,7 @@ export enum PrefKey {
MomentsDefaultTagUuid = 'momentsDefaultTagUuid', MomentsDefaultTagUuid = 'momentsDefaultTagUuid',
SystemViewPreferences = 'systemViewPreferences', SystemViewPreferences = 'systemViewPreferences',
SuperNoteExportFormat = 'superNoteExportFormat', SuperNoteExportFormat = 'superNoteExportFormat',
AuthenticatorNames = 'authenticatorNames',
} }
export enum NewNoteTitleFormat { export enum NewNoteTitleFormat {
@@ -111,4 +112,5 @@ export type PrefValue = {
[PrefKey.MomentsDefaultTagUuid]: string | undefined [PrefKey.MomentsDefaultTagUuid]: string | undefined
[PrefKey.SystemViewPreferences]: Partial<Record<SystemViewId, TagPreferences>> [PrefKey.SystemViewPreferences]: Partial<Record<SystemViewId, TagPreferences>>
[PrefKey.SuperNoteExportFormat]: 'json' | 'md' | 'html' [PrefKey.SuperNoteExportFormat]: 'json' | 'md' | 'html'
[PrefKey.AuthenticatorNames]: string
} }

View File

@@ -3,14 +3,17 @@
import { AuthenticatorApiServiceInterface } from '@standardnotes/api' import { AuthenticatorApiServiceInterface } from '@standardnotes/api'
import { Username, Uuid } from '@standardnotes/domain-core' import { Username, Uuid } from '@standardnotes/domain-core'
import { isErrorResponse } from '@standardnotes/responses' import { isErrorResponse } from '@standardnotes/responses'
import { PrefKey } from '@standardnotes/models'
import { InternalEventBusInterface } from '../Internal/InternalEventBusInterface' import { InternalEventBusInterface } from '../Internal/InternalEventBusInterface'
import { AbstractService } from '../Service/AbstractService' import { AbstractService } from '../Service/AbstractService'
import { AuthenticatorClientInterface } from './AuthenticatorClientInterface' import { AuthenticatorClientInterface } from './AuthenticatorClientInterface'
import { PreferenceServiceInterface } from '../Preferences/PreferenceServiceInterface'
export class AuthenticatorManager extends AbstractService implements AuthenticatorClientInterface { export class AuthenticatorManager extends AbstractService implements AuthenticatorClientInterface {
constructor( constructor(
private authenticatorApiService: AuthenticatorApiServiceInterface, private authenticatorApiService: AuthenticatorApiServiceInterface,
private preferencesService: PreferenceServiceInterface,
protected override internalEventBus: InternalEventBusInterface, protected override internalEventBus: InternalEventBusInterface,
) { ) {
super(internalEventBus) super(internalEventBus)
@@ -24,7 +27,18 @@ export class AuthenticatorManager extends AbstractService implements Authenticat
return [] return []
} }
return result.data.authenticators const authenticatorNames = this.getAuthenticatorNamesFromPreferences()
const nameDecoratedAuthenticators: { id: string; name: string }[] = result.data.authenticators.map(
(authenticator: { id: string }) => ({
id: authenticator.id,
name: authenticatorNames.has(authenticator.id)
? (authenticatorNames.get(authenticator.id) as string)
: 'Security Key',
}),
)
return nameDecoratedAuthenticators
} catch (error) { } catch (error) {
return [] return []
} }
@@ -37,6 +51,11 @@ export class AuthenticatorManager extends AbstractService implements Authenticat
return false return false
} }
const authenticatorNames = this.getAuthenticatorNamesFromPreferences()
authenticatorNames.delete(authenticatorId.value)
await this.preferencesService.setValue(PrefKey.AuthenticatorNames, JSON.stringify([...authenticatorNames]))
return true return true
} catch (error) { } catch (error) {
return false return false
@@ -73,7 +92,12 @@ export class AuthenticatorManager extends AbstractService implements Authenticat
return false return false
} }
return result.data.success const authenticatorNames = this.getAuthenticatorNamesFromPreferences()
authenticatorNames.set(result.data.id, name)
await this.preferencesService.setValue(PrefKey.AuthenticatorNames, JSON.stringify([...authenticatorNames]))
return true
} catch (error) { } catch (error) {
return false return false
} }
@@ -92,4 +116,18 @@ export class AuthenticatorManager extends AbstractService implements Authenticat
return null return null
} }
} }
private getAuthenticatorNamesFromPreferences(): Map<string, string> {
let authenticatorNames: Map<string, string> = new Map()
const authenticatorNamesFromPreferences = this.preferencesService.getValue(PrefKey.AuthenticatorNames)
if (authenticatorNamesFromPreferences !== undefined) {
try {
authenticatorNames = new Map(JSON.parse(authenticatorNamesFromPreferences))
} catch (error) {
authenticatorNames = new Map()
}
}
return authenticatorNames
}
} }

View File

@@ -1775,7 +1775,11 @@ export class SNApplication implements ApplicationInterface, AppGroupManagedAppli
const authenticatorApiService = new AuthenticatorApiService(authenticatorServer) const authenticatorApiService = new AuthenticatorApiService(authenticatorServer)
this.authenticatorManager = new AuthenticatorManager(authenticatorApiService, this.internalEventBus) this.authenticatorManager = new AuthenticatorManager(
authenticatorApiService,
this.preferencesService,
this.internalEventBus,
)
} }
private createAuthManager() { private createAuthManager() {