Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from Mar 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -1,6 +1,5 @@
export interface ListAuthenticatorsResponseBody {
authenticators: Array<{
id: string
name: string
}>
}
@@ -1,3 +1,3 @@
export interface VerifyAuthenticatorRegistrationResponseBody {
success: boolean
id: string
}
2 changes: 2 additions & 0 deletions packages/models/src/Domain/Syncable/UserPrefs/PrefKey.ts
Expand Up @@ -44,6 +44,7 @@ export enum PrefKey {
MomentsDefaultTagUuid = 'momentsDefaultTagUuid',
SystemViewPreferences = 'systemViewPreferences',
SuperNoteExportFormat = 'superNoteExportFormat',
AuthenticatorNames = 'authenticatorNames',
}

export enum NewNoteTitleFormat {
Expand Down Expand Up @@ -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
}
42 changes: 40 additions & 2 deletions packages/services/src/Domain/Authenticator/AuthenticatorManager.ts
Expand Up @@ -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)
Expand All @@ -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 []
}
Expand All @@ -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
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
}
6 changes: 5 additions & 1 deletion packages/snjs/lib/Application/Application.ts
Expand Up @@ -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() {
Expand Down