chore: store authenticator devices labels in preferences instead of server (#2252)
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
export interface ListAuthenticatorsResponseBody {
|
||||
authenticators: Array<{
|
||||
id: string
|
||||
name: string
|
||||
}>
|
||||
}
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
export interface VerifyAuthenticatorRegistrationResponseBody {
|
||||
success: boolean
|
||||
id: string
|
||||
}
|
||||
|
||||
@@ -44,6 +44,7 @@ export enum PrefKey {
|
||||
MomentsDefaultTagUuid = 'momentsDefaultTagUuid',
|
||||
SystemViewPreferences = 'systemViewPreferences',
|
||||
SuperNoteExportFormat = 'superNoteExportFormat',
|
||||
AuthenticatorNames = 'authenticatorNames',
|
||||
}
|
||||
|
||||
export enum NewNoteTitleFormat {
|
||||
@@ -111,4 +112,5 @@ export type PrefValue = {
|
||||
[PrefKey.MomentsDefaultTagUuid]: string | undefined
|
||||
[PrefKey.SystemViewPreferences]: Partial<Record<SystemViewId, TagPreferences>>
|
||||
[PrefKey.SuperNoteExportFormat]: 'json' | 'md' | 'html'
|
||||
[PrefKey.AuthenticatorNames]: string
|
||||
}
|
||||
|
||||
@@ -3,14 +3,17 @@
|
||||
import { AuthenticatorApiServiceInterface } from '@standardnotes/api'
|
||||
import { Username, Uuid } from '@standardnotes/domain-core'
|
||||
import { isErrorResponse } from '@standardnotes/responses'
|
||||
import { PrefKey } from '@standardnotes/models'
|
||||
|
||||
import { InternalEventBusInterface } from '../Internal/InternalEventBusInterface'
|
||||
import { AbstractService } from '../Service/AbstractService'
|
||||
import { AuthenticatorClientInterface } from './AuthenticatorClientInterface'
|
||||
import { PreferenceServiceInterface } from '../Preferences/PreferenceServiceInterface'
|
||||
|
||||
export class AuthenticatorManager extends AbstractService implements AuthenticatorClientInterface {
|
||||
constructor(
|
||||
private authenticatorApiService: AuthenticatorApiServiceInterface,
|
||||
private preferencesService: PreferenceServiceInterface,
|
||||
protected override internalEventBus: InternalEventBusInterface,
|
||||
) {
|
||||
super(internalEventBus)
|
||||
@@ -24,7 +27,18 @@ export class AuthenticatorManager extends AbstractService implements Authenticat
|
||||
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) {
|
||||
return []
|
||||
}
|
||||
@@ -37,6 +51,11 @@ export class AuthenticatorManager extends AbstractService implements Authenticat
|
||||
return false
|
||||
}
|
||||
|
||||
const authenticatorNames = this.getAuthenticatorNamesFromPreferences()
|
||||
authenticatorNames.delete(authenticatorId.value)
|
||||
|
||||
await this.preferencesService.setValue(PrefKey.AuthenticatorNames, JSON.stringify([...authenticatorNames]))
|
||||
|
||||
return true
|
||||
} catch (error) {
|
||||
return false
|
||||
@@ -73,7 +92,12 @@ export class AuthenticatorManager extends AbstractService implements Authenticat
|
||||
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) {
|
||||
return false
|
||||
}
|
||||
@@ -92,4 +116,18 @@ export class AuthenticatorManager extends AbstractService implements Authenticat
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1775,7 +1775,11 @@ export class SNApplication implements ApplicationInterface, AppGroupManagedAppli
|
||||
|
||||
const authenticatorApiService = new AuthenticatorApiService(authenticatorServer)
|
||||
|
||||
this.authenticatorManager = new AuthenticatorManager(authenticatorApiService, this.internalEventBus)
|
||||
this.authenticatorManager = new AuthenticatorManager(
|
||||
authenticatorApiService,
|
||||
this.preferencesService,
|
||||
this.internalEventBus,
|
||||
)
|
||||
}
|
||||
|
||||
private createAuthManager() {
|
||||
|
||||
Reference in New Issue
Block a user