You are here

oauth2_client.module in OAuth2 Client 8

Provides OAuth2 client functionality.

File

oauth2_client.module
View source
<?php

/**
 * @file
 * Provides OAuth2 client functionality.
 */
use Drupal\Core\Url;
use Drupal\oauth2_client\Service\OAuth2Client;

/**
 * Gets all defined oauth2_clients.
 */
function oauth2_client_get_all() {
  $data = [];
  $module_handler = \Drupal::moduleHandler();
  foreach ($module_handler
    ->getImplementations('oauth2_clients') as $module) {
    $result = call_user_func($module . '_oauth2_clients');
    if (isset($result) && is_array($result)) {
      foreach ($result as $name => $item) {
        $item += [
          'module' => $module,
        ];
        $data[$name] = $item;
      }
    }
  }
  \Drupal::moduleHandler()
    ->alter('oauth2_clients', $data);
  return $data;
}

/**
 * Load an oauth2 client.
 *
 * @param string $name
 *   Name of the client.
 *
 * @return \Drupal\oauth2_client\Service\OAuth2Client
 *   Returns an OAuth2Client object
 *
 * @throws \Exception
 */
function oauth2_client_load($name) {
  $oauth2_clients = oauth2_client_get_all();
  if (!isset($oauth2_clients[$name])) {
    throw new Exception(t('The client %name has not been defined.', [
      '%name' => $name,
    ]));
  }
  $oauth2_client = \Drupal::service('oauth2.client');
  $oauth2_client
    ->init($oauth2_clients[$name], $name);
  return $oauth2_client;
}

/**
 * Return the redirect_uri of oauth2_client.
 */
function oauth2_client_get_redirect_uri() {
  return Url::fromRoute('oauth2.redirect_url', [], [
    'absolute' => TRUE,
  ])
    ->toString();
}

/**
 * Set a redirect request.
 *
 * This can be used by other oauth2 clients to integrate with
 * oauth2_client, i.e. to use the same client that is registered
 * on the server for the oauth2_client.
 *
 * The oauth2_server sends the authorization reply to the
 * redirect_uri that is registered for the client, which is
 * the one corresponding to oauth2_client. If another oauth2
 * client would like to get this authorization reply, it has
 * to set a redirect request with this function, and then
 * oauth2_client will forward the reply to it.
 *
 * Example:
 * ```
 *   $state = md5(uniqid(rand(), TRUE));
 *   $hybridauth_config['state'] = $state;
 *   $hybridauth_config['redirect_uri'] = oauth2_client_get_redirect_uri();
 *   oauth2_client_set_redirect($state, array(
 *       'uri' => 'hybridauth/endpoint',
 *       'params' => array(
 *         'hauth.done' => 'DrupalOAuth2',
 *       )
 *     ));
 * ```
 *
 * @param string $state
 *   The random parameter that is used on the authentication url
 *   in order to mittigate CSRF attacks. In this case it is used
 *   as a key for identifying the authentication request.
 * @param array $redirect
 *   Associative array that contains the keys:
 *   - 'uri': the uri of the oauth2 client that is requesting a redirect
 *   - 'params': associative array of other parameters that should be
 *     appended to the uri, along with the $_REQUEST.
 */
function oauth2_client_set_redirect($state, array $redirect) {
  OAuth2Client::setRedirect($state, $redirect);
}

/**
 * Share an access token with oauth2_client.
 *
 * Another oauth2 client that has been successfully authenticated
 * and has received an access_token, can share it with oauth2_client,
 * so that oauth2_client does not have to repeat the authentication
 * process again.
 *
 * Example:
 *   $client_id = $hybridauth->api->client_id;
 *   $token = array(
 *     'access_token' => $hybridauth->api->access_token,
 *     'refresh_token' => $hybridauth->api->refresh_token,
 *     'expires_in' => $hybridauth->api->access_token_expires_in,
 *     'expiration_time' => $hybridauth->api->access_token_expires_at,
 *     'scope' => $hybridauth->scope,
 *   );
 *   $token_endpoint = $oauth2->api->token_endpoint;
 *   $client_id = $oauth2->api->client_id;
 *   $auth_flow = 'server-side';
 *   $id = md5($token_endpoint . $client_id . $auth_flow);
 *   oauth2_client_set_token($id, $token);
 */
function oauth2_client_set_token($id, $token) {
  $tempstore = \Drupal::service('tempstore.private')
    ->get('oauth2_client');
  $tokens = $tempstore
    ->get('token');
  $tokens[$id] = $token;
  $tempstore
    ->set('token', $tokens);
}

/**
 * Returns the access token of the oauth2_client with the given $id.
 */
function oauth2_client_get_token($id) {
  $tempstore = \Drupal::service('tempstore.private')
    ->get('oauth2_client');
  $tokens = $tempstore
    ->get('token');
  if (isset($tokens[$id])) {
    return $tokens[$id];
  }
  else {
    return [
      'access_token' => NULL,
      'refresh_token' => NULL,
      'expires_in' => NULL,
      'expiration_time' => NULL,
      'scope' => NULL,
    ];
  }
}

Functions

Namesort descending Description
oauth2_client_get_all Gets all defined oauth2_clients.
oauth2_client_get_redirect_uri Return the redirect_uri of oauth2_client.
oauth2_client_get_token Returns the access token of the oauth2_client with the given $id.
oauth2_client_load Load an oauth2 client.
oauth2_client_set_redirect Set a redirect request.
oauth2_client_set_token Share an access token with oauth2_client.