public async Task Handle_GivenNoUserAppearsToBeAuthenticate_ExpectFailedResult()
        {
            var userRepository = new Mock <IUserRepository>();
            var unitOfWork     = new Mock <IUnitOfWork>();

            unitOfWork.Setup(x => x.SaveEntitiesAsync(It.IsAny <CancellationToken>())).ReturnsAsync(() => true);
            userRepository.Setup(x => x.UnitOfWork).Returns(unitOfWork.Object);

            var currentAuthenticatedUserProvider = new Mock <ICurrentAuthenticatedUserProvider>();

            currentAuthenticatedUserProvider.Setup(x => x.CurrentAuthenticatedUser)
            .Returns(Maybe <ISystemUser> .Nothing);

            var fido2 = new Mock <IFido2>();

            var handler =
                new InitiateAuthenticatorDeviceEnrollmentCommandHandler(
                    currentAuthenticatedUserProvider.Object, userRepository.Object, fido2.Object);

            var cmd = new InitiateAuthenticatorDeviceEnrollmentCommand(AuthenticatorAttachment.CrossPlatform);

            var result = await handler.Handle(cmd, CancellationToken.None);

            Assert.True(result.IsFailure);
            Assert.Equal(ErrorCodes.UserNotFound, result.Error.Code);
        }
        public async Task <Result <InitiateAuthenticatorDeviceEnrollmentCommandResult, ErrorData> > Handle(
            InitiateAuthenticatorDeviceEnrollmentCommand request, CancellationToken cancellationToken)
        {
            var currentUserMaybe = this._currentAuthenticatedUserProvider.CurrentAuthenticatedUser;

            if (currentUserMaybe.HasNoValue)
            {
                return(Result.Fail <InitiateAuthenticatorDeviceEnrollmentCommandResult, ErrorData>(
                           new ErrorData(ErrorCodes.UserNotFound)));
            }

            var userMaybe =
                await this._userRepository.Find(currentUserMaybe.Value.UserId, cancellationToken);

            if (userMaybe.HasNoValue)
            {
                return(Result.Fail <InitiateAuthenticatorDeviceEnrollmentCommandResult, ErrorData>(
                           new ErrorData(ErrorCodes.UserNotFound)));
            }

            var user = userMaybe.Value;

            var fidoUser = new Fido2User
            {
                Name        = user.EmailAddress,
                DisplayName = user.EmailAddress,
                Id          = user.Id.ToByteArray(),
            };

            var publicKeyCredentialDescriptors =
                user.AuthenticatorDevices.Where(x => !x.IsRevoked).Select(x => new PublicKeyCredentialDescriptor(x.CredentialId)).ToList();

            var authenticatorSelection = new AuthenticatorSelection
            {
                RequireResidentKey      = false,
                UserVerification        = UserVerificationRequirement.Preferred,
                AuthenticatorAttachment = request.AuthenticatorAttachment,
            };

            var authenticationExtensionsClientInputs = new AuthenticationExtensionsClientInputs
            {
                Extensions            = true,
                UserVerificationIndex = true,
                Location = true,
                UserVerificationMethod = true,
                BiometricAuthenticatorPerformanceBounds = new AuthenticatorBiometricPerfBounds
                {
                    FAR = float.MaxValue,
                    FRR = float.MaxValue,
                },
            };

            var options = this._fido2.RequestNewCredential(fidoUser, publicKeyCredentialDescriptors,
                                                           authenticatorSelection, AttestationConveyancePreference.None, authenticationExtensionsClientInputs);

            return(Result.Ok <InitiateAuthenticatorDeviceEnrollmentCommandResult, ErrorData>(
                       new InitiateAuthenticatorDeviceEnrollmentCommandResult(options)));
        }
        public async Task Handle_GivenUser_ExpectSuccessfulResultWithNewCredentialsRequested()
        {
            var user = new Mock <IUser>();

            user.Setup(x => x.AuthenticatorDevices).Returns(new List <AuthenticatorDevice>
            {
                new AuthenticatorDevice(
                    TestVariables.AuthenticatorDeviceId,
                    DateTime.Now,
                    TestVariables.AuthenticatorDevicePublicKey,
                    TestVariables.AuthenticatorDeviceCredentialId,
                    TestVariables.AuthenticatorDeviceAaguid,
                    1,
                    "name",
                    "cred-type"),
            });
            var userRepository = new Mock <IUserRepository>();
            var unitOfWork     = new Mock <IUnitOfWork>();

            unitOfWork.Setup(x => x.SaveEntitiesAsync(It.IsAny <CancellationToken>())).ReturnsAsync(() => true);
            userRepository.Setup(x => x.UnitOfWork).Returns(unitOfWork.Object);
            userRepository.Setup(x => x.Find(It.IsAny <Guid>(), It.IsAny <CancellationToken>()))
            .ReturnsAsync(() => Maybe.From(user.Object));

            var systemUser = new Mock <ISystemUser>();
            var currentAuthenticatedUserProvider = new Mock <ICurrentAuthenticatedUserProvider>();

            currentAuthenticatedUserProvider.Setup(x => x.CurrentAuthenticatedUser)
            .Returns(Maybe.From(systemUser.Object));

            var fido2 = new Mock <IFido2>();

            var handler =
                new InitiateAuthenticatorDeviceEnrollmentCommandHandler(
                    currentAuthenticatedUserProvider.Object, userRepository.Object, fido2.Object);

            var cmd = new InitiateAuthenticatorDeviceEnrollmentCommand(AuthenticatorAttachment.CrossPlatform);

            var result = await handler.Handle(cmd, CancellationToken.None);

            Assert.True(result.IsSuccess);
            fido2.Verify(x => x.RequestNewCredential(
                             It.IsAny <Fido2User>(), It.IsAny <List <PublicKeyCredentialDescriptor> >(),
                             It.IsAny <AuthenticatorSelection>(), It.IsAny <AttestationConveyancePreference>(),
                             It.IsAny <AuthenticationExtensionsClientInputs>()));
        }
Exemple #4
0
        public void Constructor_GiveValidArguments_PropertiesAreSet()
        {
            var command = new InitiateAuthenticatorDeviceEnrollmentCommand(AuthenticatorAttachment.CrossPlatform);

            Assert.Equal(AuthenticatorAttachment.CrossPlatform, command.AuthenticatorAttachment);
        }