public async Task <LoginOtp> UpdateOtpAsync(LoginOtp loginOtp) { string token = "otp." + loginOtp.Token; string userToken = "otp." + loginOtp.UserId; var result = await GetOtpAsync(loginOtp.UserId); if (result != null) { return(result); } //The time limit could be set via appsettings.json but as we want this to be fast, //we decided to setup the time limit at compile time rather than run time var created = await _database.StringSetAsync(token, JsonSerializer.Serialize(loginOtp), DateTime.Now.AddDays(1).Date.Subtract(DateTime.Now)); if (!created) { return(null); } created = await _database.StringSetAsync(userToken, JsonSerializer.Serialize(loginOtp), DateTime.Now.AddDays(1).Date.Subtract(DateTime.Now)); if (!created) { _ = await DeleteOtpAsync(token); return(null); } return(await GetOtpAsync(loginOtp.Token)); }
public bool Save(LoginOtp loginOtp) { var loginOtpEntity = Mapper.Map <LoginOtp, LoginOtpEntity>(loginOtp); using (IDataAccessAdapter myAdapter = PersistenceLayer.GetDataAccessAdapter()) { loginOtpEntity.IsNew = !myAdapter.FetchEntity(new LoginOtpEntity(loginOtp.UserLoginId));; if (!myAdapter.SaveEntity(loginOtpEntity, false)) { throw new PersistenceFailureException(); } return(true); } }
public bool GenerateOtp(long userId, string sourceUrl) { var otp = _randomStringGenerator.GetRandomNumericString(6); var userOtp = _loginOtpRepository.Get(userId); if (userOtp != null) { userOtp.Otp = otp; userOtp.AttemptCount = 0; userOtp.DateCreated = DateTime.Now; _loginOtpRepository.Save(userOtp); } else { userOtp = new LoginOtp { Otp = otp, DateCreated = DateTime.Now, UserLoginId = userId, AttemptCount = 0 }; _loginOtpRepository.Save(userOtp); } var loginSettings = _loginSettingRepository.Get(userId); var organizationRoleUserId = _sessionContext.UserSession.CurrentOrganizationRole.OrganizationRoleUserId; var sendOtpBySms = _configurationSettingRepository.GetConfigurationValue(ConfigurationSettingName.OtpNotificationMediumSms); if (Convert.ToBoolean(sendOtpBySms) && (loginSettings.AuthenticationModeId == (long)AuthenticationMode.Sms || loginSettings.AuthenticationModeId == (long)AuthenticationMode.BothSmsEmail)) { var smsNotification = _smsNotificationModelsFactory.GetUserLoginOtpModel(otp); _notifier.NotifyViaSmsImmediate(NotificationTypeAlias.LoginOtpSmsNotification, EmailTemplateAlias.LoginOtpSmsNotification, (UserLoginOtpModel)smsNotification, userId, organizationRoleUserId, sourceUrl, null, 0, null, true); } var sendOtpByEmail = _configurationSettingRepository.GetConfigurationValue(ConfigurationSettingName.OtpNotificationMediumEmail); if (Convert.ToBoolean(sendOtpByEmail) && (loginSettings.AuthenticationModeId == (long)AuthenticationMode.Email || loginSettings.AuthenticationModeId == (long)AuthenticationMode.BothSmsEmail)) { var otpExpireMinsStr = _configurationSettingRepository.GetConfigurationValue(ConfigurationSettingName.OtpExpirationMinutes); var emailNotification = _emailNotificationModelsFactory.GetLoginOtpEmailNotificationViewModel(userId, otp, otpExpireMinsStr); _notifier.NotifySubscribersViaEmail(NotificationTypeAlias.LoginOtpEmailNotification, EmailTemplateAlias.LoginOtpEmailNotification, emailNotification, userId, organizationRoleUserId, sourceUrl, null, 0, null); } return(true); }