public void Setup()
        {
            _otpAlgorithm          = MockRepository.GenerateMock <IOTPAlgorithm>();
            _movingFactorAlgorithm = MockRepository.GenerateMock <IMovingFactorAlgorithm>();
            _errorFactory          = MockRepository.GenerateMock <IErrorFactory>();
            _otpConfiguration      = new OTPConfiguration
            {
                OTPExpiryInSeconds  = 31,
                NumberOfDigitsInOTP = 6,
                PrivateKey          = "as9121jd623ms23h232k3"
            };
            _otpService          = new OTPService(_otpAlgorithm, _movingFactorAlgorithm, _errorFactory, _otpConfiguration);
            _invalidRequestError = new OTPError
            {
                Code        = "InvalidRequest",
                Description = "Please check your request and try again."
            };
            _errorFactory.Stub(factory => factory.GetInvalidRequestError()).Return(_invalidRequestError);

            _genericError = new OTPError
            {
                Code        = "InternalError",
                Description = "Something went wrong, please try again later."
            };
            _errorFactory.Stub(factory => factory.GetErrorForException(null)).IgnoreArguments().Return(_genericError);
        }
        public void Setup()
        {
            _otpAlgorithm = MockRepository.GenerateMock<IOTPAlgorithm>();
            _movingFactorAlgorithm = MockRepository.GenerateMock<IMovingFactorAlgorithm>();
            _errorFactory = MockRepository.GenerateMock<IErrorFactory>();
            _otpConfiguration = new OTPConfiguration
            {
                OTPExpiryInSeconds = 31,
                NumberOfDigitsInOTP = 6,
                PrivateKey = "as9121jd623ms23h232k3"
            };
            _otpService = new OTPService(_otpAlgorithm, _movingFactorAlgorithm, _errorFactory, _otpConfiguration);
            _invalidRequestError = new OTPError
            {
                Code = "InvalidRequest",
                Description = "Please check your request and try again."
            };
            _errorFactory.Stub(factory => factory.GetInvalidRequestError()).Return(_invalidRequestError);

            _genericError = new OTPError
            {
                Code = "InternalError",
                Description = "Something went wrong, please try again later."
            };
            _errorFactory.Stub(factory => factory.GetErrorForException(null)).IgnoreArguments().Return(_genericError);
        }
        public void ShouldNotValidateOutsideExpiryLimit()
        {
            var otpConfiguration = new OTPConfiguration
            {
                OTPExpiryInSeconds  = 2,
                NumberOfDigitsInOTP = 6,
                PrivateKey          = "as9121jd623ms23h232k3"
            };

            _otpService = new OTPService(new HmacBasedOTPAlgorithm(),
                                         new ExpiryBasedMovingFactorAlgorithm(otpConfiguration), new ErrorFactory(),
                                         otpConfiguration);

            string userId = "2j32jk432m23482394jkddsd";
            var    generateOTPResponse = _otpService.GenerateOtp(new GenerateOTPRequest
            {
                UserId = userId
            });

            Assert.That(generateOTPResponse, Is.Not.Null);
            Assert.That(generateOTPResponse.UserId, Is.EqualTo(userId));
            Assert.That(generateOTPResponse.OTP, Is.Not.Empty);

            Thread.Sleep(5000);

            var validateOTPResponse = _otpService.ValidateOtp(new ValidateOTPRequest
            {
                OTP    = generateOTPResponse.OTP,
                UserId = userId
            });

            Assert.That(validateOTPResponse, Is.Not.Null);
            Assert.That(validateOTPResponse.UserId, Is.EqualTo(userId));
            Assert.That(validateOTPResponse.Success, Is.False);
        }
        public void Setup()
        {
            _otpConfiguration = new OTPConfiguration
            {
                OTPExpiryInSeconds  = 30,
                NumberOfDigitsInOTP = 6,
                PrivateKey          = "as9121jd623ms23h232k3"
            };

            _currentTimeFunction = MockRepository.GenerateMock <Func <DateTime> >();
            _expiryBasedMovingFactorAlgorithm = new ExpiryBasedMovingFactorAlgorithm(_otpConfiguration,
                                                                                     _currentTimeFunction);
        }
        public void Setup()
        {
            var otpConfiguration = new OTPConfiguration
            {
                OTPExpiryInSeconds  = 31,
                NumberOfDigitsInOTP = 6,
                PrivateKey          = "as9121jd623ms23h232k3"
            };

            _otpService = new OTPService(new HmacBasedOTPAlgorithm(),
                                         new ExpiryBasedMovingFactorAlgorithm(otpConfiguration), new ErrorFactory(),
                                         otpConfiguration);
        }
        public void Setup()
        {
            _otpConfiguration = new OTPConfiguration
            {
                OTPExpiryInSeconds = 30,
                NumberOfDigitsInOTP = 6,
                PrivateKey = "as9121jd623ms23h232k3"
            };

            _currentTimeFunction = MockRepository.GenerateMock<Func<DateTime>>();
            _expiryBasedMovingFactorAlgorithm = new ExpiryBasedMovingFactorAlgorithm(_otpConfiguration,
                _currentTimeFunction);
        }
        protected override void ConfigureApplicationContainer(TinyIoCContainer container)
        {
            base.ConfigureApplicationContainer(container);
            var otpConfiguration = new OTPConfiguration()
            {
                NumberOfDigitsInOTP = Convert.ToInt32(ConfigurationManager.AppSettings["NumberOfDigitsInOTP"]),
                OTPExpiryInSeconds  = Convert.ToInt64(ConfigurationManager.AppSettings["OTPExpiryInSeconds"])
            };

            container.Register(otpConfiguration);
            container.Register <IOTPService, OTPService>();
            container.Register <IOTPAlgorithm, HmacBasedOTPAlgorithm>();
            container.Register <IErrorFactory, ErrorFactory>();
            container.Register <IMovingFactorAlgorithm>(new ExpiryBasedMovingFactorAlgorithm(otpConfiguration));
        }
 public ExpiryBasedMovingFactorAlgorithm(OTPConfiguration otpConfiguration,
                                         Func <DateTime> currentTimeFunction = null)
 {
     _currentTimeFunction = currentTimeFunction ?? (() => DateTime.Now);
     _otpConfiguration    = otpConfiguration;
 }
 public ExpiryBasedMovingFactorAlgorithm(OTPConfiguration otpConfiguration,
     Func<DateTime> currentTimeFunction = null)
 {
     _currentTimeFunction = currentTimeFunction ?? (() => DateTime.Now);
     _otpConfiguration = otpConfiguration;
 }