fix(snjs): simplify authenticator api for generating options (#2147)

This commit is contained in:
Karol Sójko
2023-01-11 14:44:31 +01:00
committed by GitHub
parent 2e7302e3c1
commit 5d20a53e6e
12 changed files with 18 additions and 76 deletions

View File

@@ -58,10 +58,7 @@ export class AuthenticatorApiService implements AuthenticatorApiServiceInterface
} }
} }
async generateRegistrationOptions( async generateRegistrationOptions(): Promise<GenerateAuthenticatorRegistrationOptionsResponse> {
userUuid: string,
username: string,
): Promise<GenerateAuthenticatorRegistrationOptionsResponse> {
if (this.operationsInProgress.get(AuthenticatorApiOperations.GenerateRegistrationOptions)) { if (this.operationsInProgress.get(AuthenticatorApiOperations.GenerateRegistrationOptions)) {
throw new ApiCallError(ErrorMessage.GenericInProgress) throw new ApiCallError(ErrorMessage.GenericInProgress)
} }
@@ -69,10 +66,7 @@ export class AuthenticatorApiService implements AuthenticatorApiServiceInterface
this.operationsInProgress.set(AuthenticatorApiOperations.GenerateRegistrationOptions, true) this.operationsInProgress.set(AuthenticatorApiOperations.GenerateRegistrationOptions, true)
try { try {
const response = await this.authenticatorServer.generateRegistrationOptions({ const response = await this.authenticatorServer.generateRegistrationOptions()
username,
userUuid,
})
return response return response
} catch (error) { } catch (error) {
@@ -116,7 +110,7 @@ export class AuthenticatorApiService implements AuthenticatorApiServiceInterface
this.operationsInProgress.set(AuthenticatorApiOperations.GenerateAuthenticationOptions, true) this.operationsInProgress.set(AuthenticatorApiOperations.GenerateAuthenticationOptions, true)
try { try {
const response = await this.authenticatorServer.generateAuthenticationOptions({}) const response = await this.authenticatorServer.generateAuthenticationOptions()
return response return response
} catch (error) { } catch (error) {

View File

@@ -10,10 +10,7 @@ import {
export interface AuthenticatorApiServiceInterface { export interface AuthenticatorApiServiceInterface {
list(): Promise<ListAuthenticatorsResponse> list(): Promise<ListAuthenticatorsResponse>
delete(authenticatorId: string): Promise<DeleteAuthenticatorResponse> delete(authenticatorId: string): Promise<DeleteAuthenticatorResponse>
generateRegistrationOptions( generateRegistrationOptions(): Promise<GenerateAuthenticatorRegistrationOptionsResponse>
userUuid: string,
username: string,
): Promise<GenerateAuthenticatorRegistrationOptionsResponse>
verifyRegistrationResponse( verifyRegistrationResponse(
userUuid: string, userUuid: string,
name: string, name: string,

View File

@@ -1,3 +0,0 @@
export interface GenerateAuthenticatorAuthenticationOptionsRequestParams {
[additionalParam: string]: unknown
}

View File

@@ -1,5 +0,0 @@
export interface GenerateAuthenticatorRegistrationOptionsRequestParams {
userUuid: string
username: string
[additionalParam: string]: unknown
}

View File

@@ -1,7 +1,5 @@
export * from './ApiEndpointParam' export * from './ApiEndpointParam'
export * from './Authenticator/DeleteAuthenticatorRequestParams' export * from './Authenticator/DeleteAuthenticatorRequestParams'
export * from './Authenticator/GenerateAuthenticatorAuthenticationOptionsRequestParams'
export * from './Authenticator/GenerateAuthenticatorRegistrationOptionsRequestParams'
export * from './Authenticator/ListAuthenticatorsRequestParams' export * from './Authenticator/ListAuthenticatorsRequestParams'
export * from './Authenticator/VerifyAuthenticatorAuthenticationResponseRequestParams' export * from './Authenticator/VerifyAuthenticatorAuthenticationResponseRequestParams'
export * from './Authenticator/VerifyAuthenticatorRegistrationResponseRequestParams' export * from './Authenticator/VerifyAuthenticatorRegistrationResponseRequestParams'

View File

@@ -2,9 +2,7 @@ import { HttpServiceInterface } from '../../Http/HttpServiceInterface'
import { import {
ListAuthenticatorsRequestParams, ListAuthenticatorsRequestParams,
DeleteAuthenticatorRequestParams, DeleteAuthenticatorRequestParams,
GenerateAuthenticatorRegistrationOptionsRequestParams,
VerifyAuthenticatorRegistrationResponseRequestParams, VerifyAuthenticatorRegistrationResponseRequestParams,
GenerateAuthenticatorAuthenticationOptionsRequestParams,
VerifyAuthenticatorAuthenticationResponseRequestParams, VerifyAuthenticatorAuthenticationResponseRequestParams,
} from '../../Request' } from '../../Request'
import { import {
@@ -33,10 +31,8 @@ export class AuthenticatorServer implements AuthenticatorServerInterface {
return response as DeleteAuthenticatorResponse return response as DeleteAuthenticatorResponse
} }
async generateRegistrationOptions( async generateRegistrationOptions(): Promise<GenerateAuthenticatorRegistrationOptionsResponse> {
params: GenerateAuthenticatorRegistrationOptionsRequestParams, const response = await this.httpService.get(Paths.v1.generateRegistrationOptions)
): Promise<GenerateAuthenticatorRegistrationOptionsResponse> {
const response = await this.httpService.get(Paths.v1.generateRegistrationOptions, params)
return response as GenerateAuthenticatorRegistrationOptionsResponse return response as GenerateAuthenticatorRegistrationOptionsResponse
} }
@@ -49,10 +45,8 @@ export class AuthenticatorServer implements AuthenticatorServerInterface {
return response as VerifyAuthenticatorRegistrationResponseResponse return response as VerifyAuthenticatorRegistrationResponseResponse
} }
async generateAuthenticationOptions( async generateAuthenticationOptions(): Promise<GenerateAuthenticatorAuthenticationOptionsResponse> {
params: GenerateAuthenticatorAuthenticationOptionsRequestParams, const response = await this.httpService.get(Paths.v1.generateAuthenticationOptions)
): Promise<GenerateAuthenticatorAuthenticationOptionsResponse> {
const response = await this.httpService.get(Paths.v1.generateAuthenticationOptions, params)
return response as GenerateAuthenticatorAuthenticationOptionsResponse return response as GenerateAuthenticatorAuthenticationOptionsResponse
} }

View File

@@ -1,9 +1,7 @@
import { import {
ListAuthenticatorsRequestParams, ListAuthenticatorsRequestParams,
DeleteAuthenticatorRequestParams, DeleteAuthenticatorRequestParams,
GenerateAuthenticatorRegistrationOptionsRequestParams,
VerifyAuthenticatorRegistrationResponseRequestParams, VerifyAuthenticatorRegistrationResponseRequestParams,
GenerateAuthenticatorAuthenticationOptionsRequestParams,
VerifyAuthenticatorAuthenticationResponseRequestParams, VerifyAuthenticatorAuthenticationResponseRequestParams,
} from '../../Request' } from '../../Request'
import { import {
@@ -18,15 +16,11 @@ import {
export interface AuthenticatorServerInterface { export interface AuthenticatorServerInterface {
list(params: ListAuthenticatorsRequestParams): Promise<ListAuthenticatorsResponse> list(params: ListAuthenticatorsRequestParams): Promise<ListAuthenticatorsResponse>
delete(params: DeleteAuthenticatorRequestParams): Promise<DeleteAuthenticatorResponse> delete(params: DeleteAuthenticatorRequestParams): Promise<DeleteAuthenticatorResponse>
generateRegistrationOptions( generateRegistrationOptions(): Promise<GenerateAuthenticatorRegistrationOptionsResponse>
params: GenerateAuthenticatorRegistrationOptionsRequestParams,
): Promise<GenerateAuthenticatorRegistrationOptionsResponse>
verifyRegistrationResponse( verifyRegistrationResponse(
params: VerifyAuthenticatorRegistrationResponseRequestParams, params: VerifyAuthenticatorRegistrationResponseRequestParams,
): Promise<VerifyAuthenticatorRegistrationResponseResponse> ): Promise<VerifyAuthenticatorRegistrationResponseResponse>
generateAuthenticationOptions( generateAuthenticationOptions(): Promise<GenerateAuthenticatorAuthenticationOptionsResponse>
params: GenerateAuthenticatorAuthenticationOptionsRequestParams,
): Promise<GenerateAuthenticatorAuthenticationOptionsResponse>
verifyAuthenticationResponse( verifyAuthenticationResponse(
params: VerifyAuthenticatorAuthenticationResponseRequestParams, params: VerifyAuthenticatorAuthenticationResponseRequestParams,
): Promise<VerifyAuthenticatorAuthenticationResponseResponse> ): Promise<VerifyAuthenticatorAuthenticationResponseResponse>

View File

@@ -1,9 +1,9 @@
import { Username, Uuid } from '@standardnotes/domain-core' import { Uuid } from '@standardnotes/domain-core'
export interface AuthenticatorClientInterface { export interface AuthenticatorClientInterface {
list(): Promise<Array<{ id: string; name: string }>> list(): Promise<Array<{ id: string; name: string }>>
delete(authenticatorId: Uuid): Promise<boolean> delete(authenticatorId: Uuid): Promise<boolean>
generateRegistrationOptions(userUuid: Uuid, username: Username): Promise<Record<string, unknown> | null> generateRegistrationOptions(): Promise<Record<string, unknown> | null>
verifyRegistrationResponse( verifyRegistrationResponse(
userUuid: Uuid, userUuid: Uuid,
name: string, name: string,

View File

@@ -1,7 +1,7 @@
/* istanbul ignore file */ /* istanbul ignore file */
import { AuthenticatorApiServiceInterface } from '@standardnotes/api' import { AuthenticatorApiServiceInterface } from '@standardnotes/api'
import { Username, Uuid } from '@standardnotes/domain-core' import { Uuid } from '@standardnotes/domain-core'
import { InternalEventBusInterface } from '../Internal/InternalEventBusInterface' import { InternalEventBusInterface } from '../Internal/InternalEventBusInterface'
import { AbstractService } from '../Service/AbstractService' import { AbstractService } from '../Service/AbstractService'
@@ -43,9 +43,9 @@ export class AuthenticatorManager extends AbstractService implements Authenticat
} }
} }
async generateRegistrationOptions(userUuid: Uuid, username: Username): Promise<Record<string, unknown> | null> { async generateRegistrationOptions(): Promise<Record<string, unknown> | null> {
try { try {
const result = await this.authenticatorApiService.generateRegistrationOptions(userUuid.value, username.value) const result = await this.authenticatorApiService.generateRegistrationOptions()
if (result.data.error) { if (result.data.error) {
return null return null

View File

@@ -23,7 +23,6 @@ describe('AddAuthenticator', () => {
const result = await useCase.execute({ const result = await useCase.execute({
userUuid: 'invalid', userUuid: 'invalid',
username: 'username',
authenticatorName: 'authenticatorName', authenticatorName: 'authenticatorName',
}) })
@@ -31,25 +30,11 @@ describe('AddAuthenticator', () => {
expect(result.getError()).toBe('Could not generate authenticator registration options: Given value is not a valid uuid: invalid') expect(result.getError()).toBe('Could not generate authenticator registration options: Given value is not a valid uuid: invalid')
}) })
it('should return error if username is invalid', async () => {
const useCase = createUseCase()
const result = await useCase.execute({
userUuid: '00000000-0000-0000-0000-000000000000',
username: '',
authenticatorName: 'authenticatorName',
})
expect(result.isFailed()).toBe(true)
expect(result.getError()).toBe('Could not generate authenticator registration options: Username cannot be empty')
})
it('should return error if authenticatorName is invalid', async () => { it('should return error if authenticatorName is invalid', async () => {
const useCase = createUseCase() const useCase = createUseCase()
const result = await useCase.execute({ const result = await useCase.execute({
userUuid: '00000000-0000-0000-0000-000000000000', userUuid: '00000000-0000-0000-0000-000000000000',
username: 'username',
authenticatorName: '', authenticatorName: '',
}) })
@@ -64,7 +49,6 @@ describe('AddAuthenticator', () => {
const result = await useCase.execute({ const result = await useCase.execute({
userUuid: '00000000-0000-0000-0000-000000000000', userUuid: '00000000-0000-0000-0000-000000000000',
username: 'username',
authenticatorName: 'authenticator', authenticatorName: 'authenticator',
}) })
@@ -79,7 +63,6 @@ describe('AddAuthenticator', () => {
const result = await useCase.execute({ const result = await useCase.execute({
userUuid: '00000000-0000-0000-0000-000000000000', userUuid: '00000000-0000-0000-0000-000000000000',
username: 'username',
authenticatorName: 'authenticator', authenticatorName: 'authenticator',
}) })
@@ -96,7 +79,6 @@ describe('AddAuthenticator', () => {
const result = await useCase.execute({ const result = await useCase.execute({
userUuid: '00000000-0000-0000-0000-000000000000', userUuid: '00000000-0000-0000-0000-000000000000',
username: 'username',
authenticatorName: 'authenticator', authenticatorName: 'authenticator',
}) })
@@ -111,7 +93,7 @@ describe('AddAuthenticator', () => {
const result = await useCase.execute({ const result = await useCase.execute({
userUuid: '00000000-0000-0000-0000-000000000000', userUuid: '00000000-0000-0000-0000-000000000000',
username: 'username',
authenticatorName: 'authenticator', authenticatorName: 'authenticator',
}) })
@@ -124,7 +106,6 @@ describe('AddAuthenticator', () => {
const result = await useCase.execute({ const result = await useCase.execute({
userUuid: '00000000-0000-0000-0000-000000000000', userUuid: '00000000-0000-0000-0000-000000000000',
username: 'username',
authenticatorName: 'authenticator', authenticatorName: 'authenticator',
}) })
@@ -136,7 +117,6 @@ describe('AddAuthenticator', () => {
const result = await useCase.execute({ const result = await useCase.execute({
userUuid: '00000000-0000-0000-0000-000000000000', userUuid: '00000000-0000-0000-0000-000000000000',
username: 'username',
authenticatorName: 'authenticator', authenticatorName: 'authenticator',
}) })

View File

@@ -1,5 +1,5 @@
import { AuthenticatorClientInterface } from '@standardnotes/services' import { AuthenticatorClientInterface } from '@standardnotes/services'
import { Result, UseCaseInterface, Username, Uuid, Validator } from '@standardnotes/domain-core' import { Result, UseCaseInterface, Uuid, Validator } from '@standardnotes/domain-core'
import { AddAuthenticatorDTO } from './AddAuthenticatorDTO' import { AddAuthenticatorDTO } from './AddAuthenticatorDTO'
@@ -24,12 +24,6 @@ export class AddAuthenticator implements UseCaseInterface<void> {
} }
const userUuid = userUuidOrError.getValue() const userUuid = userUuidOrError.getValue()
const usernameOrError = Username.create(dto.username)
if (usernameOrError.isFailed()) {
return Result.fail(`Could not generate authenticator registration options: ${usernameOrError.getError()}`)
}
const username = usernameOrError.getValue()
const authenticatorNameValidatorResult = Validator.isNotEmpty(dto.authenticatorName) const authenticatorNameValidatorResult = Validator.isNotEmpty(dto.authenticatorName)
if (authenticatorNameValidatorResult.isFailed()) { if (authenticatorNameValidatorResult.isFailed()) {
return Result.fail( return Result.fail(
@@ -37,7 +31,7 @@ export class AddAuthenticator implements UseCaseInterface<void> {
) )
} }
const registrationOptions = await this.authenticatorClient.generateRegistrationOptions(userUuid, username) const registrationOptions = await this.authenticatorClient.generateRegistrationOptions()
if (registrationOptions === null) { if (registrationOptions === null) {
return Result.fail('Could not generate authenticator registration options') return Result.fail('Could not generate authenticator registration options')
} }

View File

@@ -1,5 +1,4 @@
export interface AddAuthenticatorDTO { export interface AddAuthenticatorDTO {
userUuid: string userUuid: string
username: string
authenticatorName: string authenticatorName: string
} }