From 32c2f2aac4d969800bb7bf714438618a00ca90c7 Mon Sep 17 00:00:00 2001 From: Andreas Coroiu Date: Wed, 17 Jan 2024 14:01:45 +0100 Subject: [PATCH] [PM-5731] feat: add first test --- .../Services/Fido2AuthenticatorService.cs | 12 ++++---- .../Fido2/Fido2AuthenticatorException.cs | 16 ++++++++++ ...edentialProviderViewController.Passkeys.cs | 30 +++++++++---------- .../Services/Fido2AuthenticatorTests.cs | 27 ++++++++--------- 4 files changed, 51 insertions(+), 34 deletions(-) create mode 100644 src/Core/Utilities/Fido2/Fido2AuthenticatorException.cs diff --git a/src/Core/Services/Fido2AuthenticatorService.cs b/src/Core/Services/Fido2AuthenticatorService.cs index d0747f3fb..38659b1bd 100644 --- a/src/Core/Services/Fido2AuthenticatorService.cs +++ b/src/Core/Services/Fido2AuthenticatorService.cs @@ -7,12 +7,14 @@ namespace Bit.Core.Services { public Task GetAssertionAsync(Fido2AuthenticatorGetAssertionParams assertionParams) { + throw new NotAllowedError(); + // TODO: IMPLEMENT this - return Task.FromResult(new Fido2AuthenticatorGetAssertionResult - { - AuthenticatorData = new byte[32], - Signature = new byte[8] - }); + // return Task.FromResult(new Fido2AuthenticatorGetAssertionResult + // { + // AuthenticatorData = new byte[32], + // Signature = new byte[8] + // }); } } } diff --git a/src/Core/Utilities/Fido2/Fido2AuthenticatorException.cs b/src/Core/Utilities/Fido2/Fido2AuthenticatorException.cs new file mode 100644 index 000000000..f47625f3d --- /dev/null +++ b/src/Core/Utilities/Fido2/Fido2AuthenticatorException.cs @@ -0,0 +1,16 @@ +namespace Bit.Core.Utilities.Fido2 +{ + public class Fido2AuthenticatorException : Exception + { + public Fido2AuthenticatorException(string message) : base(message) + { + } + } + + public class NotAllowedError : Fido2AuthenticatorException + { + public NotAllowedError() : base("NotAllowedError") + { + } + } +} diff --git a/src/iOS.Autofill/CredentialProviderViewController.Passkeys.cs b/src/iOS.Autofill/CredentialProviderViewController.Passkeys.cs index 3e06749ac..4cbac99f1 100644 --- a/src/iOS.Autofill/CredentialProviderViewController.Passkeys.cs +++ b/src/iOS.Autofill/CredentialProviderViewController.Passkeys.cs @@ -33,22 +33,22 @@ namespace Bit.iOS.Autofill return; } - // TODO: Generate the credential Signature and Auth data accordingly - var fido2AssertionResult = await _fido2AuthService.Value.GetAssertionAsync(new Bit.Core.Utilities.Fido2.Fido2AuthenticatorGetAssertionParams - { - RpId = cipherView.Login.MainFido2Credential.RpId, - Counter = cipherView.Login.MainFido2Credential.Counter, - CredentialId = cipherView.Login.MainFido2Credential.CredentialId - }); + // // TODO: Generate the credential Signature and Auth data accordingly + // var fido2AssertionResult = await _fido2AuthService.Value.GetAssertionAsync(new Bit.Core.Utilities.Fido2.Fido2AuthenticatorGetAssertionParams + // { + // RpId = cipherView.Login.MainFido2Credential.RpId, + // Counter = cipherView.Login.MainFido2Credential.Counter, + // CredentialId = cipherView.Login.MainFido2Credential.CredentialId + // }); - CompleteAssertionRequest(new ASPasskeyAssertionCredential( - cipherView.Login.MainFido2Credential.UserHandle, - cipherView.Login.MainFido2Credential.RpId, - NSData.FromArray(fido2AssertionResult.Signature), - _context.PasskeyCredentialRequest?.ClientDataHash, - NSData.FromArray(fido2AssertionResult.AuthenticatorData), - cipherView.Login.MainFido2Credential.CredentialId - )); + // CompleteAssertionRequest(new ASPasskeyAssertionCredential( + // cipherView.Login.MainFido2Credential.UserHandle, + // cipherView.Login.MainFido2Credential.RpId, + // NSData.FromArray(fido2AssertionResult.Signature), + // _context.PasskeyCredentialRequest?.ClientDataHash, + // NSData.FromArray(fido2AssertionResult.AuthenticatorData), + // cipherView.Login.MainFido2Credential.CredentialId + // )); } public void CompleteAssertionRequest(ASPasskeyAssertionCredential assertionCredential) diff --git a/test/Core.Test/Services/Fido2AuthenticatorTests.cs b/test/Core.Test/Services/Fido2AuthenticatorTests.cs index f81cf3549..993cd914f 100644 --- a/test/Core.Test/Services/Fido2AuthenticatorTests.cs +++ b/test/Core.Test/Services/Fido2AuthenticatorTests.cs @@ -1,7 +1,9 @@ +using System.Threading.Tasks; using Bit.Core.Abstractions; using Bit.Core.Exceptions; using Bit.Core.Services; using Bit.Core.Test.AutoFixture; +using Bit.Core.Utilities.Fido2; using Bit.Test.Common.AutoFixture; using Bit.Test.Common.AutoFixture.Attributes; using NSubstitute; @@ -12,26 +14,23 @@ namespace Bit.Core.Test.Services { public class Fido2AuthenticatorTests { - [Theory] - public async Task GetAssertionAsync_Throws_InputIsMissingSupportedAlgorithm(Fido2AuthenticatorService sut) + // Spec: If credentialOptions is now empty, return an error code equivalent to "NotAllowedError" and terminate the operation. + [Theory, SutAutoData] + public async Task GetAssertionAsync_Throws_NoCredentialExists(Fido2AuthenticatorService sut) { - await Assert.ThrowsAsync(async () => await sut.GetAssertionAsync(new Fido2AuthenticatorGetAssertionParams())); + var assertionParams = CreateAssertionParams(); + var exception = await Assert.ThrowsAsync(() => sut.GetAssertionAsync(assertionParams)); } - // it("should throw error when input does not contain any supported algorithms", async () => { - // const result = async () => - // await authenticator.makeCredential(invalidParams.unsupportedAlgorithm, tab); - - // await expect(result).rejects.toThrowError(Fido2AuthenticatorErrorCode.NotSupported); - // }); - - private Fido2AuthenticatorGetAssertionParams GetAssertionParams() + private Fido2AuthenticatorGetAssertionParams CreateAssertionParams() { return new Fido2AuthenticatorGetAssertionParams { - RpId = "test", - Counter = 0, - CredentialId = new byte[32] + RpId = "bitwarden.com", + Hash = new byte[32], + AllowCredentialDescriptorList = new PublicKeyCredentialDescriptor[0], + RequireUserVerification = true, + Extensions = new object() }; } }