Esempio n. 1
0
 public GenerateOTPResponse GenerateOtp(GenerateOTPRequest generateOtpRequest)
 {
     if (!IsNullOrEmpty(generateOtpRequest?.UserId))
     {
         try
         {
             var movingFactor = _movingFactorAlgorithm.GetMovingFactor();
             var otp = _otpAlgorithm.GenerateOTP(generateOtpRequest.UserId, _otpConfiguration.PrivateKey,
                 movingFactor,
                 _otpConfiguration.NumberOfDigitsInOTP);
             Console.WriteLine("Generation: OTP : {0} MovingFactor: {1}", otp, movingFactor);
             return new GenerateOTPResponse
             {
                 UserId = generateOtpRequest.UserId,
                 OTP = otp
             };
         }
         catch (ArgumentOutOfRangeException exception)
         {
             return new GenerateOTPResponse
             {
                 Error = _errorFactory.GetErrorForException(exception)
             };
         }
     }
     return new GenerateOTPResponse
     {
         Error = _errorFactory.GetInvalidRequestError()
     };
 }
 private string GetValidOTP(string userId)
 {
     var otpService = new OTPService(new HmacBasedOTPAlgorithm(),
         new ExpiryBasedMovingFactorAlgorithm(new OTPConfiguration()), new ErrorFactory(), new OTPConfiguration());
     var generateOtpRequest = new GenerateOTPRequest {UserId = userId};
     GenerateOTPResponse generateOTPResponse = otpService.GenerateOtp(generateOtpRequest);
     return generateOTPResponse.OTP;
 }
        public void ShouldGenerateOTPForAGivenUserId()
        {
            var userId = Guid.NewGuid().ToString();
            var generatedOtp = "321382113asjd72131";

            var generateOtpRequest = new GenerateOTPRequest
            {
                UserId = userId
            };

            var movingFactor = 87302;
            _movingFactorAlgorithm.Expect(algorithm => algorithm.GetMovingFactor()).Return(movingFactor);
            _otpAlgorithm.Expect(
                algorithm =>
                    algorithm.GenerateOTP(userId, _otpConfiguration.PrivateKey, movingFactor,
                        _otpConfiguration.NumberOfDigitsInOTP)).Return(generatedOtp);

            var generateOTPResponse = _otpService.GenerateOtp(generateOtpRequest);
            Assert.That(generateOTPResponse, Is.Not.Null);
            Assert.That(generateOTPResponse.UserId, Is.EqualTo(userId));
            Assert.That(generateOTPResponse.OTP, Is.EqualTo(generatedOtp));
        }
        public void ShouldReturnErrorIfArgumentExceptionIsThrownByAlgorithmForGenerateOTPRequest()
        {
            var userId = Guid.NewGuid().ToString();

            var generateOtpRequest = new GenerateOTPRequest
            {
                UserId = userId
            };

            var movingFactor = 87302;
            _movingFactorAlgorithm.Expect(algorithm => algorithm.GetMovingFactor()).Return(movingFactor);
            _otpAlgorithm.Expect(algorithm =>
                algorithm.GenerateOTP(userId, _otpConfiguration.PrivateKey, movingFactor,
                    _otpConfiguration.NumberOfDigitsInOTP))
                .Throw(new ArgumentOutOfRangeException(nameof(userId)));

            var generateOTPResponse = _otpService.GenerateOtp(generateOtpRequest);
            Assert.That(generateOTPResponse, Is.Not.Null);
            Assert.That(generateOTPResponse.UserId, Is.Null);
            Assert.That(generateOTPResponse.OTP, Is.Null);
            Assert.That(generateOTPResponse.Error, Is.EqualTo(_genericError));
        }