Class ThrowableAuthenticationResponseAdapter

java.lang.Object
io.micronaut.security.authentication.AuthenticationFailed
io.micronaut.security.oauth2.endpoint.token.response.ThrowableAuthenticationResponseAdapter
All Implemented Interfaces:
AuthenticationResponse, Serializable

@Internal public final class ThrowableAuthenticationResponseAdapter extends AuthenticationFailed
Adapts the error signal of a token request to an AuthenticationResponse: a failed authentication whose reason is derived from the token endpoint's rejection of the grant.

A 4xx response of the token endpoint is the authorization server rejecting the grant (RFC 6749 section 5.2, for example invalid_grant for wrong credentials), so it is an authentication failure. Not every 4xx response is a verdict on the credentials, though, and those which are not are propagated as errors, as a 5xx response, a connection failure or an unparseable token response are:

  • The transient responses 408 Request Timeout, 425 Too Early and 429 Too Many Requests, and a response whose error code is temporarily_unavailable or server_error whatever its status, since some providers and the proxies in front of them signal a transient condition with a 4xx status. Valid credentials are rejected while the provider is unavailable or throttles the application, so counting such a response as a failed login would feed account lockout logic.
  • A response whose error code is invalid_client, unauthorized_client, unsupported_grant_type or invalid_request, which per RFC 6749 section 5.2 rejects the client or the request rather than the resource owner's credentials, and the statuses 404, 405, 407 and 415, which answer a token request sent to the wrong URL, through a proxy demanding authentication, or in a form the endpoint does not accept. These are never caused by the user, so they are logged at WARN: answered as a failed login they would surface as a 401 for every user and feed the lockout counter of each of them, for example while the client secret is rotated.

isRejection(Throwable) tells a rejection of the credentials from the other failures, so that a password grant provider can resume the error signal of the token request with this adapter for a rejection and propagate any other failure. Errors of the token response validation or of the authentication mapper happen after the token request succeeded and are not adapted.

Only the HTTP status and the error code of the response are logged, at DEBUG for a rejection: the response body may carry an error_description written by the provider, and the request carries the user's password and the client secret. The body is only parsed when its content type is JSON or absent, so that a non-JSON rejection does not reach the HTTP client's decoder. The failure message of the adapter is the same for every rejection, so that the 401 body does not tell a wrong password from an account the provider locked or a verification it demands; the reason still tells them apart for the listeners of a LoginFailedEvent.

Since:
5.5.2
Author:
Sergio del Amo
See Also:
  • Constructor Details

    • ThrowableAuthenticationResponseAdapter

      public ThrowableAuthenticationResponseAdapter(@NonNull Throwable throwable, @NonNull String clientName)
      Parameters:
      throwable - The error signal of the token request, for which isRejection(Throwable) is true
      clientName - The name of the OAuth 2.0 client whose token endpoint was called
      Throws:
      IllegalArgumentException - If the throwable is not the token endpoint rejecting the grant
  • Method Details

    • isRejection

      public static boolean isRejection(@NonNull Throwable throwable)
      Parameters:
      throwable - The error signal of the token request
      Returns:
      Whether the throwable is the token endpoint rejecting the credentials: a 4xx response other than the transient ones, the ones rejecting the client or the request, and the ones answering a misdirected request
    • onTokenRequestError

      public static org.reactivestreams.Publisher<AuthenticationResponse> onTokenRequestError(Throwable throwable, OauthClientConfiguration clientConfiguration)
      Maps the error signal of the token request: the token endpoint rejecting the credentials is a failed authentication, any other failure is propagated.
      Parameters:
      throwable - The error signal of the token request
      clientConfiguration - The configuration of the client whose token request failed
      Returns:
      A failed authentication for a rejection, otherwise an error publisher carrying the throwable
    • createMessage

      protected String createMessage(AuthenticationFailureReason authenticationFailure)
      The message is the same for every rejection, so that the 401 body does not relay the kind of rejection.
      Overrides:
      createMessage in class AuthenticationFailed
      Parameters:
      authenticationFailure - the authentication failure
      Returns:
      the Title Case String