public static async Task CreateNewResetPasswordAsync( string email, string requesterAddress, IRepositoryManager repositoryManager, SmtpSettings smtpSettings, EmailSettings emailSettings) { var(exists, user) = await CheckUserExistsWithEmailAsync(email, repositoryManager.UserRepository) .ConfigureAwait(false); if (exists) { try { var emailService = new EmailService(); var resetIdentifier = ModelHelpers.GenerateUniqueIdentifier(IdentifierConsts.UserIdentifierLength); var hashedResetIdentifier = HashingHelper.HashIdentifier(resetIdentifier); var encryptedUserId = EncryptionService.EncryptString(user.Id.ToString()); var encryptedIdentifier = EncryptionService.EncryptString(resetIdentifier); var encodedEncryptedIdentifier = System.Web.HttpUtility.UrlEncode(encryptedIdentifier); var passwordReset = new PasswordReset { Identifier = hashedResetIdentifier, UserId = encryptedUserId, ExpiryDate = DateTime.Now.AddHours(1), RequesterAddress = requesterAddress }; var verificationViewModel = new LinkEmailViewModel { FullName = $"{user.FirstName} {user.LastName}", UrlDomain = emailSettings.PrimaryRedirectDomain, Link = encodedEncryptedIdentifier }; await repositoryManager.PasswordResetRepository.AddAsync(passwordReset); var verificationMessage = emailService.CreateHtmlMessage( smtpSettings, $"{user.FirstName} {user.LastName}", user.Email, "Reset Your Password", EmailCreationHelper.CreatePasswordResetEmailString(verificationViewModel)); await emailService.SendEmailAsync(smtpSettings, verificationMessage); } catch (Exception) { throw; } } }
public static async Task CreateNewUserAsync( Domain.Entities.User user, string requesterAddress, IRepositoryManager repositoryManager, SmtpSettings smtpSettings, EmailSettings emailSettings) { var(emailExists, _) = await CheckUserExistsWithEmailAsync(user.Email, repositoryManager.UserRepository) .ConfigureAwait(false); var(usernameExists, _) = await CheckUserExistsWithUsernameAsync(user.Username, repositoryManager.UserRepository) .ConfigureAwait(false); if (emailExists) { throw (new EmailAlreadyRegisteredException( "A User with this email address already exists", "A User with this email address already exists" )); } if (usernameExists) { throw (new UsernameAlreadyRegisteredException( "A User with this username already exists", "A User with this username already exists" )); } user.DisplayName = $"{user.FirstName} {user.LastName}"; user.Password = HashingHelper.HashPassword(user.Password); user.UserSecret = EncryptionService.EncryptString(ModelHelpers.GenerateUniqueIdentifier(IdentifierConsts.UserIdentifierLength)); user.CreatedOn = DateTime.Now; try { await repositoryManager.UserRepository.AddAsync(user); await CreateNewVerficationAsync( user.Email, requesterAddress, repositoryManager, smtpSettings, emailSettings, isFirstContact : true).ConfigureAwait(false); } catch (Exception) { throw; } }
private static async Task <string> GenerateNewGroupIdentifierAsync(IGroupRepository groupRepository) { var newGroupIdentifier = ModelHelpers.GenerateUniqueIdentifier(IdentifierConsts.GroupIdentifierLength); var groupExistsWithIdentifier = await CheckGroupExistsByIdentifierAsync(newGroupIdentifier, groupRepository).ConfigureAwait(false); if (groupExistsWithIdentifier) { return(await GenerateNewGroupIdentifierAsync(groupRepository).ConfigureAwait(false)); } else { return(newGroupIdentifier); } }
private static async Task <string> GenerateNewGroupPlaylistIdentifierAsync( Guid groupId, IGroupPlaylistRepository groupPlaylistRepository) { var newGroupPlaylistIdentifier = ModelHelpers.GenerateUniqueIdentifier(IdentifierConsts.GroupIdentifierLength); var groupPlaylistIdentifierExistsForGroup = await CheckGroupPlaylistIdentifierExistsForGroupAsync( newGroupPlaylistIdentifier, groupId, groupPlaylistRepository).ConfigureAwait(false); if (groupPlaylistIdentifierExistsForGroup) { return(await GenerateNewGroupPlaylistIdentifierAsync(groupId, groupPlaylistRepository).ConfigureAwait(false)); } else { return(newGroupPlaylistIdentifier); } }
public static async Task ResetPasswordAsync( string passwordResetToken, string newPassword, string requesterAddress, IRepositoryManager repositoryManager) { var decryptedResetToken = DecryptionService.DecryptString(passwordResetToken); var hashedResetIdentifier = HashingHelper.HashIdentifier(decryptedResetToken); var(existsAndValid, passwordReset) = await CheckPasswordResetIdentifierExistsAndIsValidAsync( hashedResetIdentifier, repositoryManager.PasswordResetRepository).ConfigureAwait(false); if (existsAndValid) { var userId = Guid.Parse(DecryptionService.DecryptString(passwordReset.UserId)); var user = await repositoryManager.UserRepository.GetByIdAsync(userId); user.Password = HashingHelper.HashPassword(newPassword); user.UserSecret = EncryptionService.EncryptString(ModelHelpers.GenerateUniqueIdentifier(IdentifierConsts.UserIdentifierLength)); user.LastUpdatedOn = DateTime.Now; user.LastUpdatedBy = userId; passwordReset.Active = false; passwordReset.UsedOn = DateTime.Now; passwordReset.UsedByAddress = requesterAddress; passwordReset.LastUpdatedOn = DateTime.Now; await repositoryManager.UserRepository.UpdateAsync(user); await repositoryManager.PasswordResetRepository.UpdateAsync(passwordReset); } else { throw new InvalidTokenException(InvalidTokenType.TokenNotFound, "The Password Reset Token is invalid"); } }
public static async Task CreateNewVerficationAsync( string email, string requesterAddress, IRepositoryManager repositoryManager, SmtpSettings smtpSettings, EmailSettings emailSettings, bool isFirstContact = false ) { var(exists, user) = await CheckUserExistsWithEmailAsync(email, repositoryManager.UserRepository) .ConfigureAwait(false); if (exists) { if (user.IsVerified) { throw new UserAlreadyVerifiedException("User has already been verified", "User has already been verified"); } try { if (!isFirstContact) { await DeactivateExistingUserVerificationsAsync(user.Id, repositoryManager.UserVerificationRepository) .ConfigureAwait(false); } var emailService = new EmailService(); var verificationIdentifier = ModelHelpers.GenerateUniqueIdentifier(IdentifierConsts.UserIdentifierLength); var hashedVerificationIdentifier = HashingHelper.HashIdentifier(verificationIdentifier); var verification = new UserVerification { Identifier = hashedVerificationIdentifier, UserId = user.Id, ExpiryDate = DateTime.Now.AddDays(7), RequesterAddress = requesterAddress }; var verificationViewModel = new LinkEmailViewModel { FullName = $"{user.FirstName} {user.LastName}", UrlDomain = emailSettings.PrimaryRedirectDomain, Link = verificationIdentifier }; await repositoryManager.UserVerificationRepository.AddAsync(verification); var verificationMessage = emailService.CreateHtmlMessage( smtpSettings, $"{user.FirstName} {user.LastName}", user.Email, isFirstContact ? "Welcome to Shufl" : "Verify Your Account", isFirstContact ? EmailCreationHelper.CreateWelcomeVerificationEmailString(verificationViewModel) : EmailCreationHelper.CreateVerificationEmailString(verificationViewModel)); await emailService.SendEmailAsync(smtpSettings, verificationMessage); } catch (Exception) { throw; } } }