diff --git a/packages/api/src/Domain/Response/Authenticator/ListAuthenticatorsResponseBody.ts b/packages/api/src/Domain/Response/Authenticator/ListAuthenticatorsResponseBody.ts index a6c4f14517e..d6837ef2ac1 100644 --- a/packages/api/src/Domain/Response/Authenticator/ListAuthenticatorsResponseBody.ts +++ b/packages/api/src/Domain/Response/Authenticator/ListAuthenticatorsResponseBody.ts @@ -1,6 +1,5 @@ export interface ListAuthenticatorsResponseBody { authenticators: Array<{ id: string - name: string }> } diff --git a/packages/api/src/Domain/Response/Authenticator/VerifyAuthenticatorRegistrationResponseBody.ts b/packages/api/src/Domain/Response/Authenticator/VerifyAuthenticatorRegistrationResponseBody.ts index 0018bafb9e3..c7d48822770 100644 --- a/packages/api/src/Domain/Response/Authenticator/VerifyAuthenticatorRegistrationResponseBody.ts +++ b/packages/api/src/Domain/Response/Authenticator/VerifyAuthenticatorRegistrationResponseBody.ts @@ -1,3 +1,3 @@ export interface VerifyAuthenticatorRegistrationResponseBody { - success: boolean + id: string } diff --git a/packages/models/src/Domain/Syncable/UserPrefs/PrefKey.ts b/packages/models/src/Domain/Syncable/UserPrefs/PrefKey.ts index 06a31b055b1..15d78294782 100644 --- a/packages/models/src/Domain/Syncable/UserPrefs/PrefKey.ts +++ b/packages/models/src/Domain/Syncable/UserPrefs/PrefKey.ts @@ -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> [PrefKey.SuperNoteExportFormat]: 'json' | 'md' | 'html' + [PrefKey.AuthenticatorNames]: string } diff --git a/packages/services/src/Domain/Authenticator/AuthenticatorManager.ts b/packages/services/src/Domain/Authenticator/AuthenticatorManager.ts index 0ccff24790d..a16a45c856e 100644 --- a/packages/services/src/Domain/Authenticator/AuthenticatorManager.ts +++ b/packages/services/src/Domain/Authenticator/AuthenticatorManager.ts @@ -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 { + let authenticatorNames: Map = 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 + } } diff --git a/packages/snjs/lib/Application/Application.ts b/packages/snjs/lib/Application/Application.ts index 6cb91ffb040..a437fcac328 100644 --- a/packages/snjs/lib/Application/Application.ts +++ b/packages/snjs/lib/Application/Application.ts @@ -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() {