You are here

function _restclient_prepare_authentication_oauth2_client in RESTClient 7.2

Prepare authentication using oauth2 client.

Parameters

array $variables [reference]: Array of URL variables

  • Incoming ['authentication']['oauth2_client'] - Array of values to pass through so oauth2_client creates the client. May contain just ['name'] to look up an existing client by name. See oauth2_client documentation.
  • Incoming ['authentication']['oauth_format'] - Optional format for the Authorization request header to accommodate different server implementations. The default format is 'Bearer :token' where :token is replaced with the OAuth token.
  • Outgoing ['headers']['Authorization'] - authorization header containing the oauth token

Return value

boolean TRUE if authentication is ready or not needed, FALSE if there is an error.

1 call to _restclient_prepare_authentication_oauth2_client()
_restclient_prepare_authentication in ./restclient.module
Prepare authentication for the request, if needed.

File

./restclient.module, line 903
Defines a standard REST interface to RESTful services

Code

function _restclient_prepare_authentication_oauth2_client(&$variables) {
  $error_message = '';

  // Check if restclient has oauth2_client turned off.
  if (!variable_get('restclient_oauth2_client', FALSE)) {
    $error_message = 'OAuth2 client authentication is required but restclient has oauth2_client turned off.';
  }
  else {
    if (!module_exists('oauth2_client')) {
      $error_message = 'OAuth2 client authentication is required but oauth2_client module is not enabled.';
    }
  }

  // Check if oauth2_client is specified.
  if (!isset($variables['authentication']['oauth2_client'])) {
    $error_message = "Authorization parameters for ['oauth2_client'] not found.";
  }
  if (!empty($error_message)) {
    watchdog('restclient', $error_message, NULL, WATCHDOG_ERROR);
    return FALSE;
  }

  // Load the client and get the access token.
  try {

    // Method 1 - Look up the client by name in oauth2_client.
    if (isset($variables['authentication']['oauth2_client']['name'])) {
      $oauth2_client = oauth2_client_load($variables['authentication']['oauth2_client']['name']);
    }
    else {

      // Method 2 - Pass the array through so oauth2_client creates the client.
      $client_id = 'default_client_id';
      if (isset($variables['authentication']['oauth2_client']['client_id'])) {
        $client_id = $variables['authentication']['oauth2_client']['client_id'];
      }
      $oauth2_client = new OAuth2\Client($variables['authentication']['oauth2_client'], $client_id);
    }
    $oauth_token = $oauth2_client
      ->getAccessToken();
    if (empty($oauth_token)) {
      $error_message = 'Retrieved OAuth2 token is empty';
    }
  } catch (Exception $e) {
    $error_message = 'Exception retrieving OAuth2 token: ' . $e
      ->getMessage();
  }
  if (!empty($error_message)) {
    watchdog('restclient', $error_message, NULL, WATCHDOG_ERROR);
    return FALSE;
  }

  // Use the oauth token to prepare the authorization header according to
  // the format specified in $variables['authentication']['oauth_format'].
  $oauth_format = 'Bearer :token';

  // Default
  if (isset($variables['authentication']['oauth_format'])) {
    $oauth_format = $variables['authentication']['oauth_format'];
  }
  $variables['headers']['Authorization'] = str_replace(':token', $oauth_token, $oauth_format);
  return TRUE;
}