Class OAuthService

Direct Known Subclasses:
FacebookService, OidcService

public abstract class OAuthService extends Object
An OAuth authorization (and authentication) service provider.

This class implements an OAuth client (2.0 draft), which can be used to allow the user to authorize access to information provided by a third-party OAuth service provider. This allows, among other things, for a user to safely authenticate with your web application without needing to store or even handle his authorization credentials (such as a password), a pattern called "OAuth2 connect".

The OAuth protocol provides a standard for a user to authorize access to protected resources made available by a third party service. The process starts with the user authenticating on an "authorization page" and authorizing access. This results eventually in an access token for the web application. The actual use of this token, to obtain certain information (such as an authenticated identity) from the third party is however not standardized, and there are many other uses of OAuth besides authentication.

Because the focus of the Wt::Auth library is authentication, the OAuth class also contains the implementation for using the access token for authentication (OAuthProcess#getIdentity()).

Like all service classes, this class holds only configuration state. Thus, once configured, it can be safely shared between multiple sessions since its state (the configuration) is read-only.

The OAuth authorization protocol, including the subsequent use for authentication, consists of a number of consecutive steps, some of which require user interaction, and some which require the use of remote web services. The state machine for this process is implemented in an OAuthProcess. To use OAuth, you need to create such a process and listen for state changes.

Usage example:

 OAuthService oauth = ...;

 // Creates an icon which prompts for authentication using this OAuth service.
 WImage icon = new WImage("css/oauth-" + auth.getName() + ".png", icons);

 // Creates a new process for authentication, which is started by a click on the icon
 process = oauth.createProcess(oauth.getAuthenticationScope());

 // And capture the result in a method.
 process.authenticated().addListener(this, new Signal1.Listener<Identity>() {
 public void trigger(Identity id) {

  • Constructor Details

    • OAuthService

      public OAuthService(AuthService auth)

      Creates a new OAuth service.

  • Method Details

    • getBaseAuth

      public AuthService getBaseAuth()
      Returns the basic authentication service.
    • createProcess

      public abstract OAuthProcess createProcess(String scope)
      Creates a new authorization process.

      This creates a new authorization process for the indicated scope. Valid names for the scope are service provider dependent.

      See Also:
    • setPopupEnabled

      public void setPopupEnabled(boolean enable)
      Configure if a popup should be used for the login.

      When set to false, the session will be suspended when the login starts, allowing the state to be restored when it completes. A timeout can be configured in wt_config.xml (see oauth2-redirect-timeout).

      The default value is false. When JavaScript is not available, a redirect without popup is always used.

      Note: In JWt 4.8.0 the default value changed from true to false

    • isPopupEnabled

      public boolean isPopupEnabled()
      Returns if a popup is used for the login.
    • getName

      public abstract String getName()
      Returns the provider name.

      This is a short identifier.

      See Also:
    • getDescription

      public abstract WString getDescription()
      Returns the provider description.

      This returns a description useful for e.g. tool tips on a login icon.

      See Also:
    • getPopupWidth

      public abstract int getPopupWidth()
      Returns the desired width for the popup window.

      See Also:
    • getPopupHeight

      public abstract int getPopupHeight()
      Returns the desired height for the popup window.

      See Also:
    • getAuthenticationScope

      public abstract String getAuthenticationScope()
      Returns the scope needed for authentication.

      This returns the scope that is needed (and sufficient) for obtaining identity information, and thus to authenticate the user.

      See Also:
    • getRedirectEndpoint

      public abstract String getRedirectEndpoint()
      Returns the redirection endpoint URL.

      This is the local URL to which the browser is redirect from the service provider, after the authorization process. You need to configure this URL with the third party authentication service.

      A static resource will be deployed at this URL.

    • getRedirectEndpointPath

      public String getRedirectEndpointPath()
      Returns the deployment path of the redirection endpoint.

      This returns the path at which the static resource is deployed that corresponds to the getRedirectEndpoint().

      The default implementation will derive this path from the getRedirectEndpoint() URL.

    • getAuthorizationEndpoint

      public abstract String getAuthorizationEndpoint()
      Returns the authorization endpoint URL.

      This is a remote URL which hosts the OAuth authorization user interface. This URL is loaded in the popup window at the start of an authorization process.

    • getTokenEndpoint

      public abstract String getTokenEndpoint()
      Returns the token endpoint URL.

      This is a remote URL which hosts a web-service that generates access tokens.

    • getUserInfoEndpoint

      public String getUserInfoEndpoint()
    • getClientId

      public abstract String getClientId()
      Returns the client ID.

      This is the identification for this web application with the OAuth authorization server.

    • getClientSecret

      public abstract String getClientSecret()
      Returns the client secret.

      This is the secret credentials for this web application with the OAuth authorization server.

    • encodeState

      public String encodeState(String url)
      Derives a state value from the session ID.

      The state value protects the authorization protocol against misuse, and is used to connect an authorization code response with a particular request.

      In the default implementation the state is the sessionId, crytpographically signed. This signature is verified in decodeState().

    • decodeState

      public String decodeState(String state)
      Validates and decodes a state parameter.

      This function returns the sessionId that is encoded in the state, if the signature validates that it is an authentic state generated by encodeState().

    • getTokenRequestMethod

      public Method getTokenRequestMethod()
      Returns the HTTP method used for the token request.

      While the current OAuth 2.0 draft mandates the use of POST, some implementations (like Facebook) use URL-encoding and a GET request.

      The default implementation returns Http::Method::Post (corresponding to the current draft).

    • getClientSecretMethod

      public abstract ClientSecretMethod getClientSecretMethod()
      Returns the method to transfer the client secret.

      Some implementations (like Facebook) encode the secret in the GET request parameters, while this is explicitly not allowed in the OAuth 2.0 specification.

      The default implementation returns HttpAuthorizationBasic (the recommended method).

    • getGenerateRedirectEndpoint

      public String getGenerateRedirectEndpoint()
    • getRedirectInternalPath

      public String getRedirectInternalPath()
    • configureRedirectEndpoint

      public void configureRedirectEndpoint()
      Configures the static resource implementing the redirect endpoint.

      By default, this endpoint is configured whenever it's necessary, but one may also configure it in advance, for example in a multi-process deployment (FastCGI).

    • configurationProperty

      protected static String configurationProperty(String property)