public async Task <VerificationCodeResult> VerifyEmail([FromBody] VerificationCodeRequest request) { var result = new VerificationCodeResult(); if (request == null || !request.Key.IsValidPartitionOrRowKey()) { return(result); } var existingCode = await _verificationCodesRepository.GetCodeAsync(request.Key); if (existingCode != null && existingCode.Code == request.Code) { result.Code = existingCode; AccountExistsModel accountExistsModel = await _clientAccountClient.IsTraderWithEmailExistsAsync(existingCode.Email, null); result.IsEmailTaken = accountExistsModel.IsClientAccountExisting; if (result.IsEmailTaken) { await _verificationCodesRepository.DeleteCodesAsync(existingCode.Email); } } return(result); }
public async Task <VerificationCodeResult> RequestPhoneVerificationAsync(string customerId) { if (string.IsNullOrEmpty(customerId)) { throw new ArgumentNullException(nameof(customerId)); } if (!await _callRateLimiterService.IsAllowedToCallPhoneVerificationAsync(customerId)) { _log.Info( $"Customer with Id: {customerId} made too many phone verification request and was blocked for a period"); return(VerificationCodeResult.Failed(VerificationCodeError.ReachedMaximumRequestForPeriod)); } await _callRateLimiterService.RecordPhoneVerificationCallAsync(customerId); var customer = await _customerProfileClient.CustomerProfiles.GetByCustomerIdAsync(customerId, includeNotVerified : true); if (customer?.Profile == null) { return(VerificationCodeResult.Failed(VerificationCodeError.CustomerDoesNotExist)); } if (string.IsNullOrEmpty(customer.Profile.ShortPhoneNumber)) { return(VerificationCodeResult.Failed(VerificationCodeError.CustomerPhoneIsMissing)); } if (customer.Profile.IsPhoneVerified) { return(VerificationCodeResult.Failed(VerificationCodeError.AlreadyVerified)); } var verificationCodeValue = GeneratePhoneVerificationCode(); var phoneVerificationCodeEntity = await _phoneVerificationCodeRepository.CreateOrUpdateAsync(customerId, verificationCodeValue, _verificationCodeExpirationPeriod); await _phoneVerificationSmsPublisher.PublishAsync(new SmsEvent { CustomerId = customerId, MessageTemplateId = _phoneVerificationSmsTemplateId, TemplateParameters = new Dictionary <string, string> { { "VerificationCode", verificationCodeValue } }, Source = $"{AppEnvironment.Name} - {AppEnvironment.Version}" }); _log.Info(message: "Successfully generated phone verification SMS for customer", context: customerId); return(VerificationCodeResult.Succeeded(phoneVerificationCodeEntity.ExpireDate)); }
/// <inheritdoc /> public async Task <VerificationCodeResult> RequestVerificationAsync(string customerId) { if (string.IsNullOrEmpty(customerId)) { throw new ArgumentNullException(nameof(customerId)); } if (!await _callRateLimiterService.IsAllowedToCallEmailVerificationAsync(customerId)) { _log.Info($"Customer with Id: {customerId} made too many email verification request and was blocked"); return(VerificationCodeResult.Failed(VerificationCodeError.ReachedMaximumRequestForPeriod)); } await _callRateLimiterService.RecordEmailVerificationCallAsync(customerId); var customer = await _customerProfileClient.CustomerProfiles.GetByCustomerIdAsync(customerId, includeNotVerified : true); if (customer?.Profile == null) { return(VerificationCodeResult.Failed(VerificationCodeError.CustomerDoesNotExist)); } if (customer.Profile.IsEmailVerified) { return(VerificationCodeResult.Failed(VerificationCodeError.AlreadyVerified)); } var existingCode = await _emailVerificationCodeRepository.GetByCustomerAsync(customerId); if (existingCode?.IsVerified ?? false) { return(VerificationCodeResult.Failed(VerificationCodeError.AlreadyVerified)); } var verificationCodeValue = Guid.NewGuid().ToString("D"); var verificationCodeEntity = await _emailVerificationCodeRepository.CreateOrUpdateAsync( customerId, verificationCodeValue); await _emailEventPublisher.PublishAsync(new EmailMessageEvent { CustomerId = customerId, MessageTemplateId = _verificationEmailTemplateId, SubjectTemplateId = _verificationEmailSubjectTemplateId, TemplateParameters = new Dictionary <string, string> { { "EmailVerificationLink", string.Format( _verificationEmailVerificationLink, verificationCodeEntity.VerificationCode.ToBase64()) } }, Source = $"{AppEnvironment.Name} - {AppEnvironment.Version}" }); _log.Info(message: "Successfully generated Verification Email for customer", context: customerId); return(VerificationCodeResult.Succeeded(verificationCodeEntity.ExpireDate)); }