diff --git a/libs/common/src/webauthn/services/fido2-authenticator.service.spec.ts b/libs/common/src/webauthn/services/fido2-authenticator.service.spec.ts index 9af5ca14c2..405f8885c1 100644 --- a/libs/common/src/webauthn/services/fido2-authenticator.service.spec.ts +++ b/libs/common/src/webauthn/services/fido2-authenticator.service.spec.ts @@ -202,6 +202,18 @@ describe("FidoAuthenticatorService", () => { await expect(result).rejects.toThrowError(Fido2AutenticatorErrorCode.NotAllowed); }); + + /** Spec: If any error occurred while creating the new credential object, return an error code equivalent to "UnknownError" and terminate the operation. */ + it("should throw unkown error if creation fails", async () => { + const encryptedCipher = Symbol(); + userInterface.confirmNewCredential.mockResolvedValue(true); + cipherService.encrypt.mockResolvedValue(encryptedCipher as unknown as Cipher); + cipherService.createWithServer.mockRejectedValue(new Error("Internal error")); + + const result = async () => await authenticator.makeCredential(params); + + await expect(result).rejects.toThrowError(Fido2AutenticatorErrorCode.Unknown); + }); }); describe("creation of non-discoverable credential", () => { @@ -270,6 +282,18 @@ describe("FidoAuthenticatorService", () => { await expect(result).rejects.toThrowError(Fido2AutenticatorErrorCode.NotAllowed); }); + + /** Spec: If any error occurred while creating the new credential object, return an error code equivalent to "UnknownError" and terminate the operation. */ + it("should throw unkown error if creation fails", async () => { + const encryptedCipher = Symbol(); + userInterface.confirmNewNonDiscoverableCredential.mockResolvedValue(existingCipherView.id); + cipherService.encrypt.mockResolvedValue(encryptedCipher as unknown as Cipher); + cipherService.updateWithServer.mockRejectedValue(new Error("Internal error")); + + const result = async () => await authenticator.makeCredential(params); + + await expect(result).rejects.toThrowError(Fido2AutenticatorErrorCode.Unknown); + }); }); }); }); diff --git a/libs/common/src/webauthn/services/fido2-authenticator.service.ts b/libs/common/src/webauthn/services/fido2-authenticator.service.ts index 0cadca3835..26a73e42bd 100644 --- a/libs/common/src/webauthn/services/fido2-authenticator.service.ts +++ b/libs/common/src/webauthn/services/fido2-authenticator.service.ts @@ -70,14 +70,18 @@ export class Fido2AuthenticatorService implements Fido2AuthenticatorServiceAbstr throw new Fido2AutenticatorError(Fido2AutenticatorErrorCode.NotAllowed); } - const keyPair = await this.createKeyPair(); + try { + const keyPair = await this.createKeyPair(); - const cipher = new CipherView(); - cipher.type = CipherType.Fido2Key; - cipher.name = params.rpEntity.name; - cipher.fido2Key = await this.createKeyView(params, keyPair.privateKey); - const encrypted = await this.cipherService.encrypt(cipher); - await this.cipherService.createWithServer(encrypted); + const cipher = new CipherView(); + cipher.type = CipherType.Fido2Key; + cipher.name = params.rpEntity.name; + cipher.fido2Key = await this.createKeyView(params, keyPair.privateKey); + const encrypted = await this.cipherService.encrypt(cipher); + await this.cipherService.createWithServer(encrypted); + } catch { + throw new Fido2AutenticatorError(Fido2AutenticatorErrorCode.Unknown); + } } else { const cipherId = await this.userInterface.confirmNewNonDiscoverableCredential({ credentialName: params.rpEntity.name, @@ -88,13 +92,17 @@ export class Fido2AuthenticatorService implements Fido2AuthenticatorServiceAbstr throw new Fido2AutenticatorError(Fido2AutenticatorErrorCode.NotAllowed); } - const keyPair = await this.createKeyPair(); + try { + const keyPair = await this.createKeyPair(); - const encrypted = await this.cipherService.get(cipherId); - const cipher = await encrypted.decrypt(); - cipher.fido2Key = await this.createKeyView(params, keyPair.privateKey); - const reencrypted = await this.cipherService.encrypt(cipher); - await this.cipherService.updateWithServer(reencrypted); + const encrypted = await this.cipherService.get(cipherId); + const cipher = await encrypted.decrypt(); + cipher.fido2Key = await this.createKeyView(params, keyPair.privateKey); + const reencrypted = await this.cipherService.encrypt(cipher); + await this.cipherService.updateWithServer(reencrypted); + } catch { + throw new Fido2AutenticatorError(Fido2AutenticatorErrorCode.Unknown); + } } }