Example #1
0
        public void Validate_GivenAllPropertiesAreValid_ExpectValidationSuccess()
        {
            var cmd       = new EnrollAuthenticatorDeviceCommand("name", new AuthenticatorAttestationRawResponse(), new CredentialCreateOptions());
            var validator = new EnrollAuthenticatorDeviceCommandValidator();
            var result    = validator.Validate(cmd);

            Assert.True(result.IsValid);
        }
Example #2
0
        public void Constructor_GiveValidArguments_PropertiesAreSet()
        {
            var authenticatorAttestationRawResponse = new AuthenticatorAttestationRawResponse();
            var credentialCreateOptions             = new CredentialCreateOptions();
            var command = new EnrollAuthenticatorDeviceCommand("name", authenticatorAttestationRawResponse, credentialCreateOptions);

            Assert.Equal("name", command.Name);
            Assert.Equal(authenticatorAttestationRawResponse, command.AuthenticatorAttestationRawResponse);
            Assert.Equal(credentialCreateOptions, command.CredentialCreateOptions);
        }
        private async Task <Result <EnrollAuthenticatorDeviceCommandResult, ErrorData> > Process(
            EnrollAuthenticatorDeviceCommand request, CancellationToken cancellationToken)
        {
            var whenHappened     = this._clock.GetCurrentInstant().ToDateTimeUtc();
            var currentUserMaybe = this._currentAuthenticatedUserProvider.CurrentAuthenticatedUser;

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

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

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

            var user = userMaybe.Value;

            Fido2.CredentialMakeResult credentialMakeResult;
            try
            {
                Task <bool> IsCredentialIdUniqueToUser(IsCredentialIdUniqueToUserParams param)
                {
                    var count = user.AuthenticatorDevices.Count(x =>
                                                                x.CredentialId == param.CredentialId && !x.IsRevoked);

                    return(Task.FromResult(count == 0));
                }

                credentialMakeResult = await this._fido2.MakeNewCredentialAsync(
                    request.AuthenticatorAttestationRawResponse,
                    request.CredentialCreateOptions,
                    IsCredentialIdUniqueToUser);
            }
            catch (Fido2VerificationException)
            {
                return(Result.Fail <EnrollAuthenticatorDeviceCommandResult, ErrorData>(
                           new ErrorData(ErrorCodes.FidoVerificationFailed)));
            }

            var device = user.EnrollAuthenticatorDevice(
                Guid.NewGuid(),
                whenHappened,
                credentialMakeResult.Result.PublicKey,
                credentialMakeResult.Result.CredentialId,
                credentialMakeResult.Result.Aaguid,
                Convert.ToInt32(credentialMakeResult.Result.Counter),
                request.Name,
                credentialMakeResult.Result.CredType);

            return(Result.Ok <EnrollAuthenticatorDeviceCommandResult, ErrorData>(new EnrollAuthenticatorDeviceCommandResult(credentialMakeResult, device.Id)));
        }
Example #4
0
        public void Validate_GivenCredentialCreateOptionsIsNull_ExpectValidationFailure()
        {
            var cmd       = new EnrollAuthenticatorDeviceCommand("name", new AuthenticatorAttestationRawResponse(), null);
            var validator = new EnrollAuthenticatorDeviceCommandValidator();
            var result    = validator.Validate(cmd);

            Assert.False(result.IsValid);
            Assert.Contains(
                result.Errors,
                failure => failure.ErrorCode.Equals(ValidationCodes.FieldIsRequired) &&
                failure.PropertyName == "CredentialCreateOptions");
        }
        public async Task <Result <EnrollAuthenticatorDeviceCommandResult, ErrorData> > Handle(
            EnrollAuthenticatorDeviceCommand request, CancellationToken cancellationToken)
        {
            var result = await this.Process(request, cancellationToken);

            var dbResult = await this._userRepository.UnitOfWork.SaveEntitiesAsync(cancellationToken);

            if (!dbResult)
            {
                return(Result.Fail <EnrollAuthenticatorDeviceCommandResult, ErrorData>(new ErrorData(
                                                                                           ErrorCodes.SavingChanges, "Failed To Save Database")));
            }

            return(result);
        }