You are here

class HttpClientOAuth2 in Http Client 7.2

Hierarchy

Expanded class hierarchy of HttpClientOAuth2

File

includes/auth/oauth2/HttpClientOAuth2.inc, line 3

View source
class HttpClientOAuth2 implements HttpClientAuthentication {

  /**
   * The settings for the oauth2 client.
   *
   * An associative array that can have these items:
   *   - token_endpoint: something like https://server.org/oauth2/token
   *   - auth_flow: can be server-side|client-credentials|user-password)
   *   - client_id: the client ID as registered on the oauth2 server
   *   - client_secret: the client secret as registered on the oauth2 server
   *   - redirect_uri: something like https://client.org/oauth2/authorized
   *   - authorization_endpoint: like https://server.org/oauth2/authorize
   *   - username: username of resource owner on the oauth2 server
   *   - password: password of resource owner on the oauth2 server
   *   - scope: space separated list of scopes
   *   - query_auth: if TRUE, access_token will be set on the query,
   *                 otherwise on the headers
   *
   * See README.org for some examples.
   */
  private $settings;
  public function __construct($settings) {
    $this->settings = $settings;
  }

  /**
   * Gets an access_token from the oauth2 client and sets it to the $request.
   *
   * Used by the HttpClient to authenticate requests.
   *
   * @param HttpClientRequest $request
   * @return void
   */
  public function authenticate($request) {

    // Get an access_token.
    try {
      module_load_include('inc', 'oauth2_client', 'oauth2_client');
      $oauth2 = new OAuth2\Client($this->settings);
      $access_token = $oauth2
        ->getAccessToken();
    } catch (Exception $e) {
      drupal_set_message($e
        ->getMessage(), 'error');
    }

    // Set the access_token to the request.
    $this
      ->setAccessToken($request, $access_token);
  }

  /**
   * Set the access_token to the request.
   */
  protected function setAccessToken(&$request, $access_token) {
    $access_token_on_query = isset($this->settings['query_auth']) ? $this->settings['query_auth'] : FALSE;
    if ($access_token_on_query) {

      // Send the access token in the query string.
      $request->parameters['access_token'] = $access_token;
    }
    else {

      // Send the access token in the 'Authorization' header.
      $request
        ->setHeader('Authorization', 'Bearer ' . $access_token);
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
HttpClientOAuth2::$settings private property The settings for the oauth2 client.
HttpClientOAuth2::authenticate public function Gets an access_token from the oauth2 client and sets it to the $request. Overrides HttpClientAuthentication::authenticate
HttpClientOAuth2::setAccessToken protected function Set the access_token to the request.
HttpClientOAuth2::__construct public function