public async Task <IActionResult> Authentication( CancellationToken cancellationToken, [FromForm] AuthenticationBinding binding, [FromServices] Domain.Authentication.UserAuthenticationService authenticationService) { switch (binding.GrantType) { case GrantType.Password: try { var token = await authenticationService.AuthenticationByPassword(binding.UserName, binding.Password, cancellationToken); return(Ok(new TokenView(token.AccessToken, token.ExpiresIn, token.RefreshToken))); } catch (Domain.Authentication.UnauthorizedException) { return(BadRequest(new ErrorView(ErrorCode.UnauthorizedClient, "Email or password is incorrect"))); } case GrantType.RefreshToken: try { var token = await authenticationService.AuthenticationByRefreshToken(binding.RefreshToken, cancellationToken); return(Ok(new TokenView(token.AccessToken, token.ExpiresIn, token.RefreshToken))); } catch (Domain.Authentication.UnauthorizedException) { return(BadRequest(new ErrorView(ErrorCode.UnauthorizedClient, "Refresh token is incorrect"))); } default: return(BadRequest(new ErrorView(ErrorCode.UnsupportedGrantType, $"Unsupported grant type: {binding.GrantType}"))); } }
public async Task <IActionResult> Authentication(CancellationToken cancellationToken, [FromForm] AuthenticationBinding binding, [FromServices] UserAuthenticationService authenticationService) { const string passwordGrantType = "password"; const string refreshTokenGrantType = "refresh_token"; if (string.IsNullOrEmpty(binding.GrantType)) { return(BadRequest(new ErrorView(ErrorCode.InvalidRequest, "Field 'grant_type' is required"))); } switch (binding.GrantType) { case passwordGrantType: try { if (string.IsNullOrEmpty(binding.Username)) { return(BadRequest(new ErrorView(ErrorCode.InvalidRequest, $"Field 'username' is required for '{passwordGrantType}' grant type"))); } if (string.IsNullOrEmpty(binding.Password)) { BadRequest(new ErrorView(ErrorCode.InvalidRequest, $"Field 'password' is required for '{passwordGrantType}' grant type")); } var token = await authenticationService.AuthenticationByPassword(binding.Username, binding.Password, cancellationToken); return(Ok(new TokenView(token.AccessToken, token.ExpiresIn, token.RefreshToken))); } catch (InvalidCredentialsException) { return(BadRequest(new ErrorView(ErrorCode.UnauthorizedClient, "Invalid username or password"))); } case refreshTokenGrantType: try { if (string.IsNullOrEmpty(binding.RefreshToken)) { return(BadRequest(new ErrorView(ErrorCode.InvalidRequest, "Field 'refresh_token' is required for '{refreshTokenGrantType}' grant type"))); } var token = await authenticationService.AuthenticationByRefreshToken(binding.RefreshToken, cancellationToken); return(Ok(new TokenView(token.AccessToken, token.ExpiresIn, token.RefreshToken))); } catch (InvalidCredentialsException) { return(BadRequest(new ErrorView(ErrorCode.InvalidGrant, "Invalid refresh token"))); } default: return(BadRequest(new ErrorView(ErrorCode.UnsupportedGrantType, $"The authorization grant type '{binding.GrantType}' is not supported. Supported authorization grant types: '{passwordGrantType}', '{refreshTokenGrantType}'"))); } }