public void ShouldGetMovingFactorForValidationWhichContainsOriginalMovingFactorIfTimeHasNotExpired()
        {
            var dateTime = new DateTime(2015, 08, 29, 09, 00, 29);
            var dateTimePlus10Seconds = dateTime.AddSeconds(10);

            _currentTimeFunction.Expect(func => func()).Return(dateTime).Repeat.Once();
            var firstMovingFactor = _expiryBasedMovingFactorAlgorithm.GetMovingFactor();

            _currentTimeFunction.Expect(func => func()).Return(dateTimePlus10Seconds).Repeat.Twice();
            var secondMovingFactor = _expiryBasedMovingFactorAlgorithm.GetMovingFactor();

            var movingFactorForValidation = _expiryBasedMovingFactorAlgorithm.GetMovingFactorForValidation();

            Assert.That(movingFactorForValidation, Is.Not.Null);
            Assert.That(movingFactorForValidation, Has.Member(firstMovingFactor));
            Assert.That(movingFactorForValidation, Has.Member(secondMovingFactor));
        }
コード例 #2
0
        public ValidateOTPResponse ValidateOtp(ValidateOTPRequest validateOtpRequest)
        {
            if (!IsNullOrEmpty(validateOtpRequest?.UserId) && !IsNullOrEmpty(validateOtpRequest.OTP))
            {
                try
                {
                    var movingFactorForValidation = _movingFactorAlgorithm.GetMovingFactorForValidation();

                    foreach (var movingFactor in movingFactorForValidation)
                    {
                        var internalOtp = _otpAlgorithm.GenerateOTP(validateOtpRequest.UserId,
                                                                    _otpConfiguration.PrivateKey, movingFactor,
                                                                    _otpConfiguration.NumberOfDigitsInOTP);

                        Console.WriteLine("Validation: OTP : {0} MovingFactor: {1}", internalOtp, movingFactor);

                        var isValidOTP = StringUtilities.StringEqualsInConstantTime(internalOtp, validateOtpRequest.OTP);
                        if (isValidOTP)
                        {
                            return(new ValidateOTPResponse
                            {
                                UserId = validateOtpRequest.UserId,
                                Success = true
                            });
                        }
                    }

                    return(new ValidateOTPResponse
                    {
                        UserId = validateOtpRequest.UserId,
                        Success = false
                    });
                }
                catch (ArgumentOutOfRangeException exception)
                {
                    return(new ValidateOTPResponse
                    {
                        Error = _errorFactory.GetErrorForException(exception)
                    });
                }
            }
            return(new ValidateOTPResponse
            {
                Error = _errorFactory.GetInvalidRequestError()
            });
        }