Class OAuth2ClientSupport

java.lang.Object
org.glassfish.jersey.client.oauth2.OAuth2ClientSupport

public final class OAuth2ClientSupport extends Object
Main class to build the Authorization Flow instances and client filter feature that can supports performing of authenticated OAuth requests.

Authorization flow

For more information about authorization flow, see OAuth2CodeGrantFlow.

Client feature

Use method feature(String) to build the feature. OAuth2 client filter feature registers the support for performing authenticated requests to the Service Provider. The feature uses an access token to initialize the internal filter which will add Authorization http header containing OAuth 2 authorization information (based on bearer tokens).

The internal filter can be controlled by properties put into the client request using ClientRequestContext.setProperty(String, Object) method. The property key is defined in this class as a static variables (OAUTH2_PROPERTY_ACCESS_TOKEN (see its javadoc for usage). Using the property a specific access token can be defined for each request.

Example of using specific access token for one request:
 final Response response = client.target("foo").request()
           .property(OAUTH2_PROPERTY_ACCESS_TOKEN, "6ab45ab465e46f54d771a").get();
 
Since:
2.3
  • Field Details

    • OAUTH2_PROPERTY_ACCESS_TOKEN

      public static final String OAUTH2_PROPERTY_ACCESS_TOKEN
      Key of the property that can be attached to the client request using ClientRequestContext.setProperty(String, Object) and that defines access token that should be used when generating OAuth Authorization http header. The property will override the setting of the internal filter for the current request only. This property can be used only when OAauth 2 filter feature is registered into the Client instance.

      The value of the property must be a String.

      See Also:
  • Constructor Details

    • OAuth2ClientSupport

      private OAuth2ClientSupport()
      Prevent instantiation.
  • Method Details

    • feature

      public static javax.ws.rs.core.Feature feature(String accessToken)
      Build the client filter feature from the accessToken that will add Authorization http header to the request with the OAuth authorization information.
      Parameters:
      accessToken - Access token to be used in the authorization header or null if no default access token should be defined. In this case the token will have to be set for each request using OAUTH2_PROPERTY_ACCESS_TOKEN property.
      Returns:
      Client feature.
    • authorizationCodeGrantFlowBuilder

      public static OAuth2CodeGrantFlow.Builder authorizationCodeGrantFlowBuilder(ClientIdentifier clientIdentifier, String authorizationUri, String accessTokenUri)
      Get the builder of the Authorization Code Grant Flow.
      Parameters:
      clientIdentifier - Client identifier (id of application that wants to be approved). Issued by the Service Provider.
      authorizationUri - The URI to which the user should be redirected to authorize our application. The URI points to the authorization server and is defined by the Service Provider.
      accessTokenUri - The access token URI on which the access token can be requested. The URI points to the authorization server and is defined by the Service Provider.
      Returns:
      builder of the Authorization Code Grant Flow.
    • googleFlowBuilder

      public static OAuth2FlowGoogleBuilder googleFlowBuilder(ClientIdentifier clientIdentifier, String redirectURI, String scope)
      Get a builder that can be directly used to perform Authorization Code Grant flow defined by Google.
      Parameters:
      clientIdentifier - Client identifier (id of application that wants to be approved). Issued by the Service Provider.
      redirectURI - URI to which the user (resource owner) should be redirected after he/she grants access to our application or null if the application does not support redirection (eg. is not a web server).
      scope - The api to which an access is requested (eg. Google tasks).
      Returns:
      Google builder instance.
    • facebookFlowBuilder

      public static OAuth2CodeGrantFlow.Builder facebookFlowBuilder(ClientIdentifier clientIdentifier, String redirectURI)
      Get a builder that can be directly used to perform Authorization Code Grant flow defined by Facebook.
      Parameters:
      clientIdentifier - Client identifier (id of application that wants to be approved). Issued by the Service Provider.
      redirectURI - URI to which the user (resource owner) should be redirected after he/she grants access to our application or null if the application does not support redirection (eg. is not a web server).

      Returns:
      Builder instance.