示例#1
0
        public void Validate_GivenAllPropertiesAreValid_ExpectValidationSuccess()
        {
            var cmd       = new ValidateDeviceMfaAgainstCurrentUserCommand(new AuthenticatorAssertionRawResponse(), new AssertionOptions());
            var validator = new ValidateDeviceMfaAgainstCurrentUserCommandValidator();
            var result    = validator.Validate(cmd);

            Assert.True(result.IsValid);
        }
示例#2
0
        public void Constructor_GiveValidArguments_PropertiesAreSet()
        {
            var authenticatorAssertionRawResponse = new AuthenticatorAssertionRawResponse();
            var assertionOptions = new AssertionOptions();
            var command          =
                new ValidateDeviceMfaAgainstCurrentUserCommand(authenticatorAssertionRawResponse, assertionOptions);

            Assert.Equal(authenticatorAssertionRawResponse, command.AuthenticatorAssertionRawResponse);
            Assert.Equal(assertionOptions, command.AssertionOptions);
        }
示例#3
0
        public void Validate_GivenAssertionOptionsIsNull_ExpectValidationFailure()
        {
            var cmd       = new ValidateDeviceMfaAgainstCurrentUserCommand(new AuthenticatorAssertionRawResponse(), null);
            var validator = new ValidateDeviceMfaAgainstCurrentUserCommandValidator();
            var result    = validator.Validate(cmd);

            Assert.False(result.IsValid);
            Assert.Contains(
                result.Errors,
                failure => failure.ErrorCode.Equals(ValidationCodes.FieldIsRequired) &&
                failure.PropertyName == "AssertionOptions");
        }
示例#4
0
        public async Task <Result <ValidateDeviceMfaAgainstCurrentUserCommandResult, ErrorData> > Handle(
            ValidateDeviceMfaAgainstCurrentUserCommand request, CancellationToken cancellationToken)
        {
            var result = await this.Process(request, cancellationToken);

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

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

            return(result);
        }
示例#5
0
        private async Task <Result <ValidateDeviceMfaAgainstCurrentUserCommandResult, ErrorData> > Process(
            ValidateDeviceMfaAgainstCurrentUserCommand request, CancellationToken cancellationToken)
        {
            var currentUserMaybe = this._currentAuthenticatedUserProvider.CurrentAuthenticatedUser;

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

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

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

            var user = userMaybe.Value;
            var authenticatorDevice = user.AuthenticatorDevices.FirstOrDefault(x =>
                                                                               request.AuthenticatorAssertionRawResponse.Id.SequenceEqual(x.CredentialId));

            if (authenticatorDevice == null)
            {
                return(Result.Fail <ValidateDeviceMfaAgainstCurrentUserCommandResult, ErrorData>(
                           new ErrorData(ErrorCodes.DeviceNotFound)));
            }

            var res = await this._fido2.MakeAssertionAsync(
                request.AuthenticatorAssertionRawResponse,
                request.AssertionOptions, authenticatorDevice.PublicKey, (uint)authenticatorDevice.Counter,
                @params => Task.FromResult(true));

            authenticatorDevice.UpdateCounter((int)res.Counter, this._clock.GetCurrentInstant().ToDateTimeUtc());

            return(Result.Ok <ValidateDeviceMfaAgainstCurrentUserCommandResult, ErrorData>(
                       new ValidateDeviceMfaAgainstCurrentUserCommandResult(user.Id, res)));
        }