public async Task HandleAsync(SignIn command) { var user = await _repository.GetAsync(u => u.Username == command.Username); if (user is null) { throw new UserNotFoundException(command.Username); } var passwordHash = _passwordService.HashPassword(command.Password, user.Salt); if (passwordHash != user.Password) { throw new InvalidUserPasswordException(user.Username); } var token = _authTokensService.Create(user.Username); var refreshToken = await _refreshTokensService.CreateAsync(user.Username); token.RefreshToken = refreshToken; _cache.Set(token); }
public async Task <PostAuthorizeResponse> AuthorizeAsync( string country, string key, string password, string responseType, string redirectUri, string state, string ipAddress, string userAgent, List <string> scopes, string audience, CancellationToken ct) { var identityTypes = IdentityTypeExtensions.TypesWithPassword; var phoneIdentityType = new[] { IdentityType.PhoneAndPassword }; var identity = await _identitiesService.GetVerifiedByKeyAndTypesAsync(key, identityTypes, ct) ?? await _identitiesService.GetVerifiedByKeyAndTypesAsync(key.GetPhoneWithoutPrefixes(country), phoneIdentityType, ct); if (identity == null) { return(new PostAuthorizeResponse(redirectUri, true)); } var profile = await _profilesService.GetAsync(identity.ProfileId, ct); if (profile == null) { return(new PostAuthorizeResponse(redirectUri, true)); } var isPasswordCorrect = _identitiesService.IsPasswordCorrect(identity, password); if (!isPasswordCorrect) { return(new PostAuthorizeResponse(redirectUri, true)); } var claims = await _claimsService.GetByScopesAsync(scopes, profile, ct); switch (responseType) { case ResponseType.Code: { var code = _codesService.Create(profile, claims); var callbackUri = _callbackUriService.GetByCode(redirectUri, state, code); return(new PostAuthorizeResponse(callbackUri, false)); } case ResponseType.Token: { var accessToken = _accessTokensService.Create(audience, claims); var refreshToken = await _refreshTokensService.CreateAsync(claims, profile, ipAddress, userAgent, ct); var callbackUri = _callbackUriService.GetByTokens(redirectUri, state, accessToken, refreshToken); return(new PostAuthorizeResponse(callbackUri, false)); } default: throw new ArgumentOutOfRangeException(responseType); } }