Class OAuth2Authenticator

  • All Implemented Interfaces:
    ClientRequestFilter

    public final class OAuth2Authenticator
    extends Object
    implements ClientRequestFilter
    OAuth 2.0 authentication filter for the JAX-RS Client API. This class should support any grant types which uses a token endpoint.
    Since:
    5.0
    Author:
    Kaz Nishimura
    See Also:
    RFC 6749
    • Constructor Detail

      • OAuth2Authenticator

        public OAuth2Authenticator​(URI baseUri,
                                   URI tokenEndpointUri)
        Initializes the object.
        Parameters:
        baseUri - a base URI
        tokenEndpointUri - a token endpoint URI
    • Method Detail

      • getClientId

        public String getClientId()
        Returns the client identifier.
        Returns:
        the client identifier
      • setClientId

        public void setClientId​(String newValue)
        Sets the client identifier.
        Parameters:
        newValue - a new value of the client identifier
      • setClientSecret

        public void setClientSecret​(String newValue)
        Sets the client secret.
        Parameters:
        newValue - a new value of the client secret.
      • getAccessToken

        public String getAccessToken()
        Returns the access token.
        Returns:
        the access token
      • setAccessToken

        public void setAccessToken​(String newValue)
        Sets the access token.
        Parameters:
        newValue - a new value of the access token
      • getRefreshToken

        public String getRefreshToken()
        Returns the refresh token.
        Returns:
        the refresh token
      • setRefreshToken

        public void setRefreshToken​(String newValue)
        Sets the refresh token.
        Parameters:
        newValue - a new value of the refresh token
      • getAccessTokenExpiry

        public Instant getAccessTokenExpiry()
        Returns the time when the access token expires.
        Returns:
        the time when the access token expires
      • setAccessTokenExpiry

        public void setAccessTokenExpiry​(Instant newValue)
        Sets the time when the access token expires.
        Parameters:
        newValue - new value of the time when the access token expires
      • fireTokenRefreshed

        protected void fireTokenRefreshed()
        Fires a token refreshed event.
      • fireTokenRefreshed

        protected void fireTokenRefreshed​(TokenRefreshEvent event)
        Fires a token refreshed event.
        Parameters:
        event - a token refreshed event.
      • addTokenRefreshListener

        public void addTokenRefreshListener​(TokenRefreshListener listener)
        Adds a token refresh listener.
        Parameters:
        listener - a token refresh listener
      • removeTokenRefreshListener

        public void removeTokenRefreshListener​(TokenRefreshListener listener)
        Removes a token refresh listener.
        Parameters:
        listener - a token refresh listener
      • requestAccessToken

        public void requestAccessToken​(Entity formEntity)
        Requests an access token by posting a form entity.
        Parameters:
        formEntity - a form entity
      • refreshAccessToken

        protected void refreshAccessToken()
        Refreshes the access token.
      • filter

        public void filter​(ClientRequestContext requestContext)
        Adds an Authorization HTTP header to a request when authentication is required. The header shall be added only if the request URI is below the base URI.
        Specified by:
        filter in interface ClientRequestFilter
        Parameters:
        requestContext - a request context