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() }); }
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)); }
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 Response Generate(GenerateOTPRequest request) { Response response = new Response(); try { if (string.IsNullOrEmpty(request.Email) && string.IsNullOrEmpty(request.PhoneNumber)) { response.Status = false; response.Description = "Kindly provide Email or Phone Number"; return(response); } Random generator = new Random(); String r = generator.Next(0, 999999).ToString("D6"); string sql = "update OTPDetails set IsValidated = 1 where IsValidated = 1 and (Email = ?Email or PhoneNumber = ?PhoneNumber)"; OTPDetails context = new OTPDetails() { OTP = r, Email = request.Email, PhoneNumber = request.PhoneNumber, IsValidated = false }; using (IDbConnection conn = GetConnection()) { conn.Execute(sql, request); conn.Insert(context); response.Status = true; response.Description = "Successful"; if (!string.IsNullOrEmpty(request.Email)) { EmailRequest email = new EmailRequest() { Body = $"Your OTP is {r}", Subject = "Homes Direct Verification", To = request.Email }; Helper.SendEmail(email); } } } catch (Exception ex) { logger.Error(ex); response.Status = false; response.Description = ex.Message; } return(response); }
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)); }
public Response Generate([FromBody] GenerateOTPRequest value) { return(repo.Generate(value)); }
public static string DoOTPGeneration(string requestStr) { Serializer ser = new Serializer(); string xmlOutputData = string.Empty; string responseCode = string.Empty; DateTime requestTime = DateTime.Now; GenerateOTPResponse objResp = new GenerateOTPResponse(); try { GenerateOTPRequest obj = ser.Deserialize <GenerateOTPRequest>(requestStr); if (obj != null) { var mandate = MandateRepo.GetMandateByCode(obj.MandateCode); if (mandate != null) { CentralPayOtp otp = new CentralPayOtp() { DateGenerated = DateTime.Now, IsUsed = false, MandateCodeId = mandate.Id, otp = Utils.GenerateNewOtp(), Amount = Convert.ToDecimal(obj.Amount), ReferenceNumber = Guid.NewGuid().ToString() }; bool isSaved = MandateRepo.SaveCentralPayOtp(otp); responseCode = isSaved ? ResponseCodeMap.Successful : ResponseCodeMap.UnknownError; objResp = new GenerateOTPResponse { BankCode = obj.BankCode, BillerID = obj.BillerID, BillerName = obj.BillerName, BillerTransId = obj.BillerTransId, MandateCode = obj.MandateCode, TransType = obj.TransType, Amount = obj.Amount, ResponseCode = responseCode, ReferenceNumber = Utils.GenerateReferenceNumber() }; } else { objResp = new GenerateOTPResponse { ResponseCode = ResponseCodeMap.USerNotSetUp }; } } else { objResp = new GenerateOTPResponse { ResponseCode = ResponseCodeMap.InvalidXml }; } xmlOutputData = ser.Serialize <GenerateOTPResponse>(objResp); } catch (Exception e) { ExceptionLogRepo.SaveExceptionLog(e); xmlOutputData = ser.Serialize <GenerateOTPResponse>(new GenerateOTPResponse { ResponseCode = ResponseCodeMap.InvalidXml }); } DateTime responseTime = DateTime.Now; RequestResponseRepository.SaveRequestResponse("ASMX", requestStr, requestTime, "", xmlOutputData, responseTime); return(xmlOutputData); }