Exemplo n.º 1
0
        public async Task <SignUpResponse> Handle(SignUpRequest request, CancellationToken cancellationToken)
        {
            if (await authValidationService.EmailExists(request.Email))
            {
                throw new DuplicateException("Email address already exists", ErrorCodes.EmailExists);
            }

            if (await authValidationService.UsernameExists(request.Username))
            {
                throw new DuplicateException("Username already exists", ErrorCodes.UsernameExists);
            }

            var user = await identityService.SignUp(request.Email, request.Username, request.Password);

            if (user != null)
            {
                await balanceService.CreateBalanceAccount(user.Id);

                var confirmAccountToken = await identityService.GenerateConfirmAccountToken(user);

                string callbackUrl = $"{Configuration.GetValue<string>(AppSettingsKeys.ClientAddress)}register/confirm?email={user.Email}&token={confirmAccountToken}";

                return(await emailSender.Send(EmailMessages.ActivationAccountEmail(user.Email, user.UserName, callbackUrl))
                    ? new SignUpResponse
                {
                    Token = confirmAccountToken, User = mapper.Map <UserAuthDto>(user)
                }

                    : throw new ServiceException("Account confirmation email sending failed"));
            }

            throw new AuthException("Error occurred during signing up");
        }
Exemplo n.º 2
0
        public async Task <SendChangeEmailEmailResponse> Handle(SendChangeEmailEmailRequest request,
                                                                CancellationToken cancellationToken)
        {
            if (await authValidationService.EmailExists(request.NewEmail))
            {
                throw new DuplicateException("Account with this email already exists");
            }

            var result = await userTokenGenerator.GenerateChangeEmailToken();

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

            string callbackUrl =
                $"{Configuration.GetValue<string>(AppSettingsKeys.ServerAddress)}api/account/changeEmail?email={encryptedEmail}&token={encryptedToken}&newEmail={encryptedNewEmail}";

            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 SendChangeEmailEmailResponse());
        }
Exemplo n.º 3
0
        public async Task <RegisterResponse> Handle(RegisterRequest request, CancellationToken cancellationToken)
        {
            if (await authValidationService.EmailExists(request.Email))
            {
                throw new DuplicateException("Email already exists", ErrorCodes.EmailExists);
            }

            if (await authValidationService.UsernameExists(request.Username))
            {
                throw new DuplicateException("Username already exists", ErrorCodes.UsernameExists);
            }

            var signUpResult = await authService.SignUp(request.Email, request.Password, request.Username);

            if (signUpResult != null)
            {
                var registerToken = signUpResult.User?.Tokens.SingleOrDefault(t => t.Code == signUpResult.Token) ??
                                    throw new TokenException("Register token was not created",
                                                             ErrorCodes.CannotGenerateToken);

                return(await emailSender.Send(EmailMessages.ActivationAccountEmail(signUpResult.User?.Email,
                                                                                   signUpResult.User?.Username,
                                                                                   $"{Configuration.GetValue<string>(AppSettingsKeys.ClientAddress)}account/confirm?userId={signUpResult.User?.Id}&token={registerToken.Code}"))
                    ? new RegisterResponse()
                    : throw new ServiceException("Some error occurred during sending an email message",
                                                 ErrorCodes.SendEmailFailed));
            }

            throw new AuthException("Error occurred during signing up");
        }
Exemplo n.º 4
0
        public async Task <ChangeEmailResponse> Handle(ChangeEmailRequest request, CancellationToken cancellationToken)
        {
            if (await authValidationService.EmailExists(request.NewEmail))
            {
                throw new DuplicateException("Email already exists", ErrorCodes.EmailExists);
            }

            return(await profileService.ChangeEmail(request.UserId, request.NewEmail, request.Token)
                ? new ChangeEmailResponse()
                : throw new ProfileUpdateException("Changing email failed"));
        }
Exemplo n.º 5
0
        public async Task <ChangeEmailResponse> Handle(ChangeEmailRequest request, CancellationToken cancellationToken)
        {
            if (await authValidationService.EmailExists(request.NewEmail))
            {
                throw new DuplicateException("Email address already exists", ErrorCodes.EmailExists);
            }

            var emailChanged = await accountManager.ChangeEmail(request.NewEmail, request.Token);

            return(emailChanged ? new ChangeEmailResponse()
                : throw new ProfileUpdateException("Error occurred during changing email address"));
        }
Exemplo n.º 6
0
        public async Task <SendChangeEmailCallbackResponse> Handle(SendChangeEmailCallbackRequest request, CancellationToken cancellationToken)
        {
            if (await authValidationService.EmailExists(request.NewEmail))
            {
                throw new DuplicateException("Email address already exists", ErrorCodes.EmailExists);
            }

            var changeEmailCallbackSent = await accountManager.SendChangeEmailCallback(request.NewEmail);

            return(changeEmailCallbackSent ? new SendChangeEmailCallbackResponse()
                : throw new CannotGenerateTokenException("Generating change email token failed"));
        }
Exemplo n.º 7
0
        public async Task <ChangeEmailResponse> Handle(ChangeEmailRequest request, CancellationToken cancellationToken)
        {
            var(token, email, newEmail) = (cryptoService.Decrypt(request.Token),
                                           cryptoService.Decrypt(request.Email), cryptoService.Decrypt(request.NewEmail));

            if (await authValidationService.EmailExists(newEmail))
            {
                throw new DuplicateException("Account with this email already exists");
            }

            return(await accountManager.ChangeEmail(newEmail, email, token)
                ? new ChangeEmailResponse()
                : throw new ChangeEmailException("Error occured during changing email"));
        }
Exemplo n.º 8
0
        public async Task <GetAuthValidationsResponse> Handle(GetAuthValidationsRequest request, CancellationToken cancellationToken)
        {
            bool isAvailable = request.AuthValidationType switch
            {
                AuthValidationType.Email => !await authValidationService.EmailExists(request.Content),
                AuthValidationType.Username => !await authValidationService.UsernameExists(request.Content),
                _ => false
            };

            return(new GetAuthValidationsResponse {
                IsAvailable = isAvailable
            });
        }
    }
Exemplo n.º 9
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"));
        }
        public async Task <GenerateChangeEmailTokenResponse> Handle(GenerateChangeEmailTokenRequest request, CancellationToken cancellationToken)
        {
            if (await authValidationService.EmailExists(request.NewEmail))
            {
                throw new DuplicateException("Email already exists", ErrorCodes.EmailExists);
            }

            var generateChangeEmailTokenResult = await profileService.GenerateChangeEmailToken(request.NewEmail) ??
                                                 throw new TokenException("Error occurred during generating change email token", ErrorCodes.CannotGenerateToken);

            return(await emailSender.Send(EmailMessages.EmailChangeEmail(request.NewEmail,
                                                                         $"{Configuration.GetValue<string>(AppSettingsKeys.ClientAddress)}profile/changeEmail?userId={generateChangeEmailTokenResult.UserId}&token={generateChangeEmailTokenResult.Token}&newEmail={generateChangeEmailTokenResult.NewEmail}"))
                    ? new GenerateChangeEmailTokenResponse()
                    : throw new ServiceException("Sending change email callback failed", ErrorCodes.SendEmailFailed));
        }
Exemplo n.º 11
0
        public async Task <IActionResult> OnGetAsync([FromQuery] string userId, string code, string newEmail)
        {
            if (await authValidationService.EmailExists(newEmail))
            {
                Alertify.Push("Email address already exists", AlertType.Error);
                return(Page());
            }

            var emailChanged = await profileService.ChangeEmail(userId, newEmail, code);

            if (emailChanged)
            {
                Alertify.Push("Email changed", AlertType.Primary);
            }
            else
            {
                Alertify.Push("Email changing failed", AlertType.Error);
            }

            return(RedirectToPage("/Profile", new { area = "Profile", emailChanged = emailChanged }));
        }
Exemplo n.º 12
0
        public async Task <IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            if (await authValidationService.EmailExists(UserRegister.Email))
            {
                ModelState.TryAddModelError(ErrorCodes.EmailExists, "Email address already exists");
                return(Page());
            }

            if (await authValidationService.UsernameExists(UserRegister.Username))
            {
                ModelState.TryAddModelError(ErrorCodes.UsernameExists, "Username already exists");
                return(Page());
            }

            var authResult = await authService.SignUp(UserRegister.Email, UserRegister.Password, UserRegister.Username);

            if (authResult != null)
            {
                var emailSent = await emailSender.Send(Constants.ActivationAccountEmail(UserRegister.Email,
                                                                                        $"{Configuration.GetValue<string>(AppSettingsKeys.ServerAddress)}Auth/ConfirmAccount?userId={authResult.User.Id}&code={authResult.Token}"));

                if (emailSent)
                {
                    Alertify.Push($"Activation email was sent to: {UserRegister.Email}", AlertType.Info);
                    return(Page());
                }

                Alertify.Push("There was a problem during sending activation email", AlertType.Error);
            }

            return(Page());
        }