You are here

function _restclient_prepare_authentication_hybridauth in RESTClient 7.2

Prepare authentication using hybridauth.

Parameters

array $variables [reference]: Array of URL variables

  • Incoming ['authentication']['hybridauth_client'] - Array of values to pass through so hybridauth creates the client. May contain just ['name'] to look up an existing client by name. See hybridauth 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_hybridauth()
_restclient_prepare_authentication in ./restclient.module
Prepare authentication for the request, if needed.

File

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

Code

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

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

  // Check if hybridauth is specified.
  if (!isset($variables['authentication']['hybridauth'])) {
    $error_message = "Authorization parameters for ['hybridauth'] not found.";
  }
  if (!empty($error_message)) {
    watchdog('restclient', $error_message, NULL, WATCHDOG_ERROR);
    return FALSE;
  }
  $hybridauth_instance = hybridauth_get_instance();
  $session_data = $hybridauth_instance
    ->storage()
    ->getSessionData();
  global $user;
  if (!empty($hybridauth_instance) and !empty($session_data) and $user->uid != 1) {

    // Get the HybridAuth client ID
    $hybridauth_client_id = $variables['authentication']['hybridauth']['client_id'];
    $hybridauth_adapter = $hybridauth_instance
      ->getAdapter($hybridauth_client_id);
    try {
      if (!$hybridauth_adapter
        ->isUserConnected()) {
        $hybridauth_instance
          ->authenticate($hybridauth_adapter->id);
      }
      $hybridauth_tokens = $hybridauth_adapter
        ->getAccessToken();
      $oauth_token = $hybridauth_tokens['access_token'];
    } catch (Exception $e) {

      // Something went wrong.
      watchdog('restclient', 'An exception occurred during HybridAuth processing: @e', array(
        '@e' => $e
          ->getMessage(),
      ), WATCHDOG_ERROR);
    }
  }

  // At this point, no point in continuing if the token is empty.
  if (empty($oauth_token)) {
    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;
}