Ejemplo n.º 1
0
        /// <summary>
        /// Resends an OTP message.
        /// If there is a current OTP, then the same OTP is resent.
        /// Otherwise a new OTP is generated and sent.
        /// </summary>
        /// <param name="userId">Id of the recipient user.</param>
        /// <param name="phoneNumber">Phone number to send OTP to.</param>
        /// <param name="userType">The user type.</param>
        public static void ResendOTP(int userId, string phoneNumber, string userType)
        {
            OneTimePwd otp       = null;
            string     otpString = string.Empty;

            using (OneTimePwdDao dao = new OneTimePwdDao())
            {
                ReapOldOTPs(dao);
                otp = dao.FindByUserId(userId); // Check if there is a current OTP for the user.
                if (otp != null)                // If there is, just reset the timestamp on the OTP
                {
                    otpString       = otp.Otg;
                    otp.CreatedDate = DateTime.Now;
                    dao.Update(otp);
                }
                else
                {
                    otpString = GenerateOTP();
                    otp       = new OneTimePwd {
                        Otg = otpString, UserID = userId, UserType = userType, CreatedDate = DateTime.Now
                    };
                    dao.Insert(otp);
                }
            }
            SendOTP(phoneNumber, otpString);
        }
Ejemplo n.º 2
0
 public static void RemoveOTP(int user_id, string otp_code)
 {
     using (OneTimePwdDao dao = new OneTimePwdDao())
     {
         ReapOldOTPs(dao);
         OneTimePwd otp = dao.FindByUserId(user_id);
         if (otp != null && otp.Otg == otp_code)
         {
             dao.DeleteOTP(otp);
         }
     }
 }
Ejemplo n.º 3
0
        public static bool SaveOTP(string otp, int userId, string userType)
        {
            OneTimePwd otpEntity = new OneTimePwd {
                Otg = otp, UserID = userId, UserType = userType, CreatedDate = DateTime.Now
            };

            using (OneTimePwdDao dao = new OneTimePwdDao())
            {
                dao.Insert(otpEntity);
            }
            return(true);
        }
Ejemplo n.º 4
0
 public static bool ValidateOTP(int user_id, string otp_code)
 {
     //if (otp_code == "1111")//TODO : comment out this block; only for testing
     //{
     //    return true;
     //}
     using (OneTimePwdDao dao = new OneTimePwdDao())
     {
         ReapOldOTPs(dao);
         OneTimePwd otp = dao.FindByUserId(user_id);
         if (otp != null && otp.Otg == otp_code)
         {
             return(true);
         }
     }
     return(false);
 }
Ejemplo n.º 5
0
        /// <summary>
        /// Remove all OTP records past the OTP lifespan.
        /// </summary>
        /// <param name="dao">The db context to use</param>
        private static void ReapOldOTPs(OneTimePwdDao dao)
        {
            DateTime timeLimit = DateTime.Now.AddSeconds(-OTP_LIFESPAN_SECS);

            dao.DeleteOlderOTP(timeLimit);
        }