fix(auth): updating counter post authenticator verification

This commit is contained in:
Karol Sójko
2023-03-01 12:48:10 +01:00
parent ec035ba648
commit a9cc00a478
4 changed files with 17 additions and 5 deletions
@@ -7,6 +7,7 @@ export interface AuthenticatorRepositoryInterface {
findById(id: UniqueEntityId): Promise<Authenticator | null>
findByUserUuidAndCredentialId(userUuid: Uuid, credentialId: string): Promise<Authenticator | null>
save(authenticator: Authenticator): Promise<void>
updateCounter(id: UniqueEntityId, counter: number): Promise<void>
remove(authenticator: Authenticator): Promise<void>
removeByUserUuid(userUuid: Uuid): Promise<void>
}
@@ -36,7 +36,7 @@ describe('VerifyAuthenticatorAuthenticationResponse', () => {
authenticatorRepository = {} as jest.Mocked<AuthenticatorRepositoryInterface>
authenticatorRepository.findByUserUuidAndCredentialId = jest.fn().mockReturnValue(authenticator)
authenticatorRepository.save = jest.fn()
authenticatorRepository.updateCounter = jest.fn()
authenticatorChallengeRepository = {} as jest.Mocked<AuthenticatorChallengeRepositoryInterface>
authenticatorChallengeRepository.findByUserUuid = jest.fn().mockReturnValue({
@@ -221,6 +221,6 @@ describe('VerifyAuthenticatorAuthenticationResponse', () => {
})
expect(result.isFailed()).toBeFalsy()
expect(authenticatorRepository.save).toHaveBeenCalled()
expect(authenticatorRepository.updateCounter).toHaveBeenCalled()
})
})
@@ -60,9 +60,7 @@ export class VerifyAuthenticatorAuthenticationResponse implements UseCaseInterfa
return Result.fail(`Could not verify authenticator authentication response: ${(error as Error).message}`)
}
authenticator.props.counter = verification.authenticationInfo.newCounter as number
await this.authenticatorRepository.save(authenticator)
await this.authenticatorRepository.updateCounter(authenticator.id, verification.authenticationInfo.newCounter)
return Result.ok(true)
}
@@ -11,6 +11,19 @@ export class MySQLAuthenticatorRepository implements AuthenticatorRepositoryInte
private mapper: MapperInterface<Authenticator, TypeORMAuthenticator>,
) {}
async updateCounter(id: UniqueEntityId, counter: number): Promise<void> {
await this.ormRepository
.createQueryBuilder()
.update()
.set({
counter,
})
.where('uuid = :uuid', {
uuid: id.toString(),
})
.execute()
}
async removeByUserUuid(userUuid: Uuid): Promise<void> {
await this.ormRepository
.createQueryBuilder()