예제 #1
0
        public async Task <SendResetPasswordResponse> Handle(SendResetPasswordRequest request,
                                                             CancellationToken cancellationToken)
        {
            if (!await captchaService.VerifyCaptcha(request.CaptchaResponse))
            {
                throw new CaptchaException();
            }

            var result = await userTokenGenerator.GenerateResetPasswordToken(request.Login);

            if (result == null)
            {
                return(new SendResetPasswordResponse());
            }

            var(encryptedToken, encryptedEmail) =
                (cryptoService.Encrypt(result.Token), cryptoService.Encrypt(result.Email));

            string callbackUrl =
                $"{Configuration.GetValue<string>(AppSettingsKeys.ServerAddress)}api/auth/resetPassword/verify?email={encryptedEmail}&token={encryptedToken}";

            var emailTemplate =
                (await emailTemplateGenerator.FindEmailTemplate(EmailTemplateDictionary.RegisterTemplate))
                .ReplaceParameters(new EmailTemplateParameter("{{username}}", result.Username),
                                   new EmailTemplateParameter("{{callbackUrl}}", callbackUrl));

            await emailSender.Send(EmailMessages.ActivationAccountEmail(result.Email, emailTemplate));

            return(new SendResetPasswordResponse());
        }
예제 #2
0
        public async Task <SignUpResponse> Handle(SignUpRequest request, CancellationToken cancellationToken)
        {
            if (!await captchaService.VerifyCaptcha(request.CaptchaResponse))
            {
                throw new CaptchaException();
            }

            if (await authValidationService.UsernameExists(request.Username) ||
                await authValidationService.EmailExists(request.Email))
            {
                throw new DuplicateException("Account already exists");
            }

            if (await serialService.SerialExists(request.Serial))
            {
                throw new DuplicateException("Serial already exists");
            }

            User referrer = default;

            if (!string.IsNullOrEmpty(request.Referrer))
            {
                referrer = await userService.FindUserByUsername(request.Referrer);
            }

            var response = await authService.SignUp(request.Username, request.Email, request.Password, request.Serial,
                                                    referrer == null? 0 : referrer.Id);

            var(encryptedToken, encryptedEmail) = (cryptoService.Encrypt(response.TokenCode),
                                                   cryptoService.Encrypt(response.User.Email));

            //TODO:Change it on ClientAddress
            string callbackUrl =
                $"{Configuration.GetValue<string>(AppSettingsKeys.ServerAddress)}api/auth/confirm?email={encryptedEmail}&token={encryptedToken}";

            var emailTemplate =
                (await emailTemplateGenerator.FindEmailTemplate(EmailTemplateDictionary.RegisterTemplate))
                .ReplaceParameters(new EmailTemplateParameter("{{username}}", response.User.Username),
                                   new EmailTemplateParameter("{{callbackUrl}}", callbackUrl));

            return(await emailSender.Send(EmailMessages.ActivationAccountEmail(response.User.Email, emailTemplate))
                ? (SignUpResponse) new SignUpResponse
            {
                TokenCode = encryptedToken, User = mapper.Map <UserAuthDto>(response.User)
            }

                   .LogInformation($"User {request.Username} with email {request.Email} signed up")
                : throw new ServiceException("Sending confirmation email failed"));
        }