public void ShouldReturnSuccessResponseIfOTPIsValidated() { var userId = Guid.NewGuid().ToString(); var generatedOtp = "213213"; var validateOTPRequest = new ValidateOTPRequest { UserId = userId, OTP = generatedOtp }; var movingFactor = 87302; _movingFactorAlgorithm.Expect(algorithm => algorithm.GetMovingFactorForValidation()).Return(new List <long> { movingFactor }); _otpAlgorithm.Expect( algorithm => algorithm.GenerateOTP(userId, _otpConfiguration.PrivateKey, movingFactor, _otpConfiguration.NumberOfDigitsInOTP)).Return(generatedOtp); var validateOTPResponse = _otpService.ValidateOtp(validateOTPRequest); Assert.That(validateOTPResponse, Is.Not.Null); Assert.That(validateOTPResponse.UserId, Is.EqualTo(userId)); Assert.That(validateOTPResponse.Success, Is.True); }
public void ShouldReturnErrorIfArgumentExceptionIsThrownByAlgorithmForValidateOTPRequest() { var userId = Guid.NewGuid().ToString(); var generatedOtp = "213213"; var validateOTPRequest = new ValidateOTPRequest { UserId = userId, OTP = generatedOtp }; var movingFactor = 87302; _movingFactorAlgorithm.Expect(algorithm => algorithm.GetMovingFactorForValidation()) .Return(new List <long> { movingFactor }); _otpAlgorithm.Expect(algorithm => algorithm.GenerateOTP(userId, _otpConfiguration.PrivateKey, movingFactor, _otpConfiguration.NumberOfDigitsInOTP)) .Throw(new ArgumentOutOfRangeException(nameof(userId))); var validateOTPResponse = _otpService.ValidateOtp(validateOTPRequest); Assert.That(validateOTPResponse, Is.Not.Null); Assert.That(validateOTPResponse.UserId, Is.Null); Assert.That(validateOTPResponse.Success, Is.False); Assert.That(validateOTPResponse.Error, Is.EqualTo(_genericError)); }
public static string DoOTPValidation(string requestStr) { Serializer ser = new Serializer(); string xmlOutputData = string.Empty; string responseCode = string.Empty; DateTime requestTime = DateTime.Now; ValidateOTPResponse objResp = new ValidateOTPResponse(); try { ValidateOTPRequest obj = ser.Deserialize <ValidateOTPRequest>(requestStr); if (obj != null) { bool isSaved = MandateRepo.ValidateOtp(obj.MandateCode, obj.OTP, Convert.ToDecimal(obj.Amount)); responseCode = isSaved ? ResponseCodeMap.Successful : ResponseCodeMap.InvalidXml; objResp = new ValidateOTPResponse { BankCode = obj.BankCode, BillerID = obj.BillerID, BillerName = obj.BillerName, BillerTransId = obj.BillerTransId, MandateCode = obj.MandateCode, TransType = obj.TransType, Amount = obj.Amount, ResponseCode = responseCode, HashValue = obj.HashValue }; } else { objResp = new ValidateOTPResponse { ResponseCode = ResponseCodeMap.InvalidXml }; } xmlOutputData = ser.Serialize <ValidateOTPResponse>(objResp); } catch (Exception e) { ExceptionLogRepo.SaveExceptionLog(e); xmlOutputData = ser.Serialize <ValidateOTPResponse>(new ValidateOTPResponse { ResponseCode = ResponseCodeMap.InvalidXml }); } DateTime responseTime = DateTime.Now; RequestResponseRepository.SaveRequestResponse("ASMX", requestStr, requestTime, "", xmlOutputData, responseTime); return(xmlOutputData); }
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() }; }
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() }); }
public Response Validate(ValidateOTPRequest request) { Response response = new Response(); try { response.Status = false; response.Description = "Invalid OTP / Email or Phone number combination"; using (IDbConnection conn = GetConnection()) { var result = conn.GetList <OTPDetails>("Where IsValidated = 0 and (Email = ?Email or PhoneNumber = ?PhoneNumber)", request).FirstOrDefault(); if (result != null) { if (DateTime.Now.Subtract(result.DateCreated).TotalMinutes > 3) { response.Status = false; response.Description = "OTP expired"; } else if (result.OTP == request.OTP) { response.Status = true; response.Description = "Successful"; } else { response.Status = false; response.Description = "Invalid OTP"; } result.IsValidated = true; conn.Update(result); } } } catch (Exception ex) { logger.Error(ex); response.Status = false; response.Description = ex.Message; } return(response); }
public void ShouldReturnSuccessResponseIfOTPIsValidated() { var userId = Guid.NewGuid().ToString(); var generatedOtp = "213213"; var validateOTPRequest = new ValidateOTPRequest { UserId = userId, OTP = generatedOtp }; var movingFactor = 87302; _movingFactorAlgorithm.Expect(algorithm => algorithm.GetMovingFactorForValidation()).Return(new List<long> { movingFactor }); _otpAlgorithm.Expect( algorithm => algorithm.GenerateOTP(userId, _otpConfiguration.PrivateKey, movingFactor, _otpConfiguration.NumberOfDigitsInOTP)).Return(generatedOtp); var validateOTPResponse = _otpService.ValidateOtp(validateOTPRequest); Assert.That(validateOTPResponse, Is.Not.Null); Assert.That(validateOTPResponse.UserId, Is.EqualTo(userId)); Assert.That(validateOTPResponse.Success, Is.True); }
public void ShouldReturnErrorIfArgumentExceptionIsThrownByAlgorithmForValidateOTPRequest() { var userId = Guid.NewGuid().ToString(); var generatedOtp = "213213"; var validateOTPRequest = new ValidateOTPRequest { UserId = userId, OTP = generatedOtp }; var movingFactor = 87302; _movingFactorAlgorithm.Expect(algorithm => algorithm.GetMovingFactorForValidation()) .Return(new List<long> {movingFactor}); _otpAlgorithm.Expect(algorithm => algorithm.GenerateOTP(userId, _otpConfiguration.PrivateKey, movingFactor, _otpConfiguration.NumberOfDigitsInOTP)) .Throw(new ArgumentOutOfRangeException(nameof(userId))); var validateOTPResponse = _otpService.ValidateOtp(validateOTPRequest); Assert.That(validateOTPResponse, Is.Not.Null); Assert.That(validateOTPResponse.UserId, Is.Null); Assert.That(validateOTPResponse.Success, Is.False); Assert.That(validateOTPResponse.Error, Is.EqualTo(_genericError)); }
public Response Validate([FromBody] ValidateOTPRequest value) { return(repo.Validate(value)); }