feat(snjs): add authenticator use cases (#2145)

* feat(snjs): add authenticator use case

* feat(snjs): add use cases for listing, deleting and verifying authenticators

* fix(snjs): spec for deleting authenticator
This commit is contained in:
Karol Sójko
2023-01-11 11:30:42 +01:00
committed by GitHub
parent 8fe78ecf5f
commit 5864ea84e7
16 changed files with 531 additions and 29 deletions

View File

@@ -0,0 +1,70 @@
import { AuthenticatorClientInterface } from '@standardnotes/services'
import { VerifyAuthenticator } from './VerifyAuthenticator'
describe('VerifyAuthenticator', () => {
let authenticatorClient: AuthenticatorClientInterface
let authenticatorVerificationPromptFunction: (
authenticationOptions: Record<string, unknown>,
) => Promise<Record<string, unknown>>
const createUseCase = () => new VerifyAuthenticator(authenticatorClient, authenticatorVerificationPromptFunction)
beforeEach(() => {
authenticatorClient = {} as jest.Mocked<AuthenticatorClientInterface>
authenticatorClient.generateAuthenticationOptions = jest.fn().mockResolvedValue({ foo: 'bar' })
authenticatorClient.verifyAuthenticationResponse = jest.fn().mockResolvedValue(true)
authenticatorVerificationPromptFunction = jest.fn()
})
it('should return an error if authenticator client fails to generate authentication options', async () => {
authenticatorClient.generateAuthenticationOptions = jest.fn().mockResolvedValue(null)
const result = await createUseCase().execute({ userUuid: '00000000-0000-0000-0000-000000000000' })
expect(result.isFailed()).toBe(true)
expect(result.getError()).toBe('Could not generate authenticator authentication options')
})
it('should return an error if authenticator verification prompt function fails', async () => {
authenticatorVerificationPromptFunction = jest.fn().mockRejectedValue(new Error('error'))
const result = await createUseCase().execute({ userUuid: '00000000-0000-0000-0000-000000000000' })
expect(result.isFailed()).toBe(true)
expect(result.getError()).toBe('Could not generate authenticator authentication options: error')
})
it('should return an error if authenticator client fails to verify authentication response', async () => {
authenticatorClient.verifyAuthenticationResponse = jest.fn().mockResolvedValue(false)
const result = await createUseCase().execute({ userUuid: '00000000-0000-0000-0000-000000000000' })
expect(result.isFailed()).toBe(true)
expect(result.getError()).toBe('Could not generate authenticator authentication options')
})
it('should return ok if authenticator client succeeds to verify authentication response', async () => {
const result = await createUseCase().execute({ userUuid: '00000000-0000-0000-0000-000000000000' })
expect(result.isFailed()).toBe(false)
})
it('should return an error if user uuid is invalid', async () => {
const result = await createUseCase().execute({ userUuid: 'invalid' })
expect(result.isFailed()).toBe(true)
})
it('should return error if authenticatorVerificationPromptFunction is not provided', async () => {
const result = await new VerifyAuthenticator(authenticatorClient).execute({
userUuid: '00000000-0000-0000-0000-000000000000',
})
expect(result.isFailed()).toBe(true)
expect(result.getError()).toBe(
'Could not generate authenticator authentication options: No authenticator verification prompt function provided',
)
})
})

View File

@@ -0,0 +1,49 @@
import { AuthenticatorClientInterface } from '@standardnotes/services'
import { Result, UseCaseInterface, Uuid } from '@standardnotes/domain-core'
import { VerifyAuthenticatorDTO } from './VerifyAuthenticatorDTO'
export class VerifyAuthenticator implements UseCaseInterface<void> {
constructor(
private authenticatorClient: AuthenticatorClientInterface,
private authenticatorVerificationPromptFunction?: (
authenticationOptions: Record<string, unknown>,
) => Promise<Record<string, unknown>>,
) {}
async execute(dto: VerifyAuthenticatorDTO): Promise<Result<void>> {
if (!this.authenticatorVerificationPromptFunction) {
return Result.fail(
'Could not generate authenticator authentication options: No authenticator verification prompt function provided',
)
}
const userUuidOrError = Uuid.create(dto.userUuid)
if (userUuidOrError.isFailed()) {
return Result.fail(`Could not generate authenticator authentication options: ${userUuidOrError.getError()}`)
}
const userUuid = userUuidOrError.getValue()
const authenticationOptions = await this.authenticatorClient.generateAuthenticationOptions()
if (authenticationOptions === null) {
return Result.fail('Could not generate authenticator authentication options')
}
let authenticatorResponse
try {
authenticatorResponse = await this.authenticatorVerificationPromptFunction(authenticationOptions)
} catch (error) {
return Result.fail(`Could not generate authenticator authentication options: ${(error as Error).message}`)
}
const verificationResponse = await this.authenticatorClient.verifyAuthenticationResponse(
userUuid,
authenticatorResponse,
)
if (!verificationResponse) {
return Result.fail('Could not generate authenticator authentication options')
}
return Result.ok()
}
}

View File

@@ -0,0 +1,3 @@
export interface VerifyAuthenticatorDTO {
userUuid: string
}