public async Task GetProfileDataAsync(ProfileDataRequestContext context) { var email = !string.IsNullOrWhiteSpace(context.Subject.Identity.Name) ? context.Subject.Identity.Name : context.Subject.FindFirst(x => x.Type.Equals(JwtClaimTypes.Email))?.Value; var getAccountResult = await _accountRepository.GetByEmailAsync(email); context.IssuedClaims = await _accountClaimsCreatorService.CreateAccountClaimsAsync(getAccountResult.Value); }
public async Task HandleAsync(RequestPasswordResetTokenCommand command, CancellationToken cancellationToken = default) { var getAccountResult = await _accountGetterService.GetByEmailAsync(command.Email); if (!getAccountResult.Success) { throw new ValidationException(getAccountResult.Errors); } var accountIsConfirmedVerificationResult = _accountVerificationService.VerifyAccountIsConfirmed(getAccountResult.Value.Confirmed); if (!accountIsConfirmedVerificationResult.Success) { throw new ValidationException(accountIsConfirmedVerificationResult.Errors); } var passwordIsSetVerificationResult = _accountVerificationService.VerifyPasswordIsSet(getAccountResult.Value.PasswordHash); if (!passwordIsSetVerificationResult.Success) { throw new ValidationException(passwordIsSetVerificationResult.Errors); } var correlationId = Guid.NewGuid(); var token = getAccountResult.Value.GenerateToken(TokenTypeEnumeration.PasswordReset, correlationId); await _communicationBus.DispatchDomainEventsAsync(getAccountResult.Value, cancellationToken); await _accountRepository.UpdateAsync(getAccountResult.Value); await _passwordResetTokenRequestService.PublishPasswordResetRequestedIntegrationEventAsync(getAccountResult.Value.Email, token.Value, correlationId); }
public async Task ValidateAsync(ResourceOwnerPasswordValidationContext context) { var invalidCredentialsResult = new GrantValidationResult(TokenRequestErrors.InvalidTarget, "Invalid credentials"); var getAccountResult = await _accountGetterService.GetByEmailAsync(context.UserName); if (!getAccountResult.Success) { context.Result = invalidCredentialsResult; return; } var accountCanBeAuthenticatedResult = _accountVerificationService.VerifyAccountCanBeAuthenticated(getAccountResult.Value, context.Password); if (!accountCanBeAuthenticatedResult.Success) { var error = accountCanBeAuthenticatedResult.Errors.Single(); context.Result = error.ErrorCode.Equals(AccountErrorCodeEnumeration.PasswordIsNotSet) || error.ErrorCode.Equals(AccountErrorCodeEnumeration.IncorrectPassword) ? invalidCredentialsResult : new GrantValidationResult(TokenRequestErrors.InvalidTarget, error.ErrorMessage); return; } var claims = await _accountClaimsCreatorService.CreateAccountClaimsAsync(getAccountResult.Value); context.Result = new GrantValidationResult(getAccountResult.Value.Id.ToString(), context.Request.GrantType, claims); }
public async Task <LocalLoginResultOutput> ExecuteAsync(string email, string password, bool rememberLogin, string returnUrl) { var authRequest = await _authorizationService.GetAuthorizationRequestAsync(returnUrl); var getAccountResult = await _accountGetterService.GetByEmailAsync(email); if (!getAccountResult.Success) { return(LocalLoginResultOutput.Fail(authRequest != null, getAccountResult.Errors)); } var accountCanBeAuthenticatedVerificationResult = _accountVerificationService.VerifyAccountCanBeAuthenticated(getAccountResult.Value, password); if (!accountCanBeAuthenticatedVerificationResult.Success) { return(LocalLoginResultOutput.Fail(authRequest != null, accountCanBeAuthenticatedVerificationResult.Errors)); } var claims = await _accountClaimsCreatorService.CreateAccountClaimsAsync(getAccountResult.Value); await _signInService.SignInAsync(getAccountResult.Value.Id, getAccountResult.Value.Email, rememberLogin, claims); return(LocalLoginResultOutput.Ok(authRequest != null, authRequest?.IsNativeClient)); }
public async Task HandleAsync(ConfirmAccountCommand command, CancellationToken cancellationToken = default) { var getAccountResult = await _accountGetterService.GetByEmailAsync(command.Email); if (!getAccountResult.Success) { throw new ValidationException(getAccountResult.Errors); } var accountIsNotConfirmedVerificationResult = _accountVerificationService.VerifyAccountIsNotConfirmed(getAccountResult.Value.Confirmed); if (!accountIsNotConfirmedVerificationResult.Success) { throw new ValidationException(accountIsNotConfirmedVerificationResult.Errors); } var accountConfirmationToken = getAccountResult.Value.Tokens.SingleOrDefault(x => Equals(x.Type, TokenTypeEnumeration.AccountConfirmation)); var confirmationCodeVerificationResult = _accountVerificationService.VerifyConfirmationCode(accountConfirmationToken, command.Code); if (!confirmationCodeVerificationResult.Success) { throw new ValidationException(confirmationCodeVerificationResult.Errors); } getAccountResult.Value.Confirm(Guid.NewGuid()); await _communicationBus.DispatchDomainEventsAsync(getAccountResult.Value, cancellationToken); await _accountRepository.UpdateAsync(getAccountResult.Value); }
public async Task <Account> ProvideAccountForExternalLoginAsync(string email, Guid correlationId) { var getAccountResult = await _accountGetterService.GetByEmailAsync(email); Account account; if (!getAccountResult.Success) { account = await _accountCreatorService.CreateAsync(Guid.NewGuid(), email, string.Empty, correlationId); } else { account = getAccountResult.Value; if (account.Confirmed) { return(account); } account.Confirm(correlationId); await _communicationBus.DispatchDomainEventsAsync(account); await _accountRepository.UpdateAsync(account); } return(account); }
public async Task GetByEmailAsync_Should_Return_GetResult_With_Account() { const string email = "*****@*****.**"; var account = Account.Builder() .SetId(Guid.NewGuid()) .SetEmail(email) .SetConfirmed(true) .SetPasswordHash("PasswordHash") .SetSecurityStamp(Guid.NewGuid()) .SetCreated(DateTimeOffset.UtcNow) .SetRoles(new List <Guid> { Guid.NewGuid() }) .Build(); var expectedResult = GetResult <Account> .Ok(account); _accountRepositoryMock.Setup(x => x.GetByEmailAsync(It.IsAny <string>())).ReturnsAsync(account); var result = await _accountGetterService.GetByEmailAsync(email); result.Should().BeEquivalentTo(expectedResult); }
public async Task <VerificationResult> VerifyEmailIsNotTakenAsync(string email) { var getAccountResult = await _accountGetterService.GetByEmailAsync(email); if (getAccountResult.Success) { var errors = new Collection <IError> { new Error(AccountErrorCodeEnumeration.EmailIsAlreadyTaken, AccountErrorMessage.EmailIsAlreadyTaken) }; return(VerificationResult.Fail(errors)); } return(VerificationResult.Ok()); }