OAuth2 Client

Component ID

2133827

Component name

OAuth2 Client

Component type

module

Maintenance status

Development status

Component security advisory coverage

covered

Downloads

14024

Component created

Component changed

Component body

Drupal module for OAuth2 clients. Is supposed to work well with
https://drupal.org/project/oauth2_server.

Drupal 8 Version

Version 8.x-1.0-alpha1 has been released, but still needs lots of work.

  1. It has only been tested with auth_flow: server-side
  2. It hasn't been tested with definitions in hook_oauth2_clients(), it has only been tested by defining a client programattically and using it.
  3. Automated tests need to be created

If you try the module with any untested scenarios, and find them either to work, or not, please let us know. Also, if anyone wants to do build automated tests, it would be appreciated.

How to use this module

Define oauth2 clients in your code like this:

/**
 * Implements hook_oauth2_clients().
 */
function MYMODULE_oauth2_clients() {
  $server_url = 'https://oauth2_server.example.org';
  $client_url = 'https://oauth2_client.example.org';

  // user-password flow
  $oauth2_clients['test1'] = array(
    'token_endpoint' => $server_url . '/oauth2/token',
    'auth_flow' => 'user-password',
    'client_id' => 'test1',
    'client_secret' => 'test1',
    'username' => 'user1',
    'password' => 'user1',
  );

  // client-credentials flow
  $oauth2_clients['test2'] = array(
    'token_endpoint' => $server_url . '/oauth2/token',
    'auth_flow' => 'client-credentials',
    'client_id' => 'test2',
    'client_secret' => 'test2',
  );

  // server-side flow
  $oauth2_clients['test3'] = array(
    'token_endpoint' => $server_url . '/oauth2/token',
    'auth_flow' => 'server-side',
    'client_id' => 'test1',
    'client_secret' => 'test1',
    'authorization_endpoint' => $server_url . '/oauth2/authorize',
    'redirect_uri' => $client_url . '/oauth2/authorized',
  );

  return $oauth2_clients;
}

Then call them like this:

    try {
      $oauth2_client = oauth2_client_load('test1');
      $access_token = $oauth2_client->getAccessToken();
    }
    catch (Exception $e) {
      drupal_set_message($e->getMessage(), 'error');
    }

Other ways to use the module

Another form of usage is like this:

D7

  $client_id = 'some_client_id';
  $oauth2_config = array(
    'token_endpoint' => $server_url . '/oauth2/token',
    'auth_flow' => 'user-password',
    'client_id' => $client_id,
    'client_secret' => '12345',
    'username' => $username,
    'password' => $password,
  );
  try {
    $oauth2_client = new OAuth2Client($oauth2_config, $client_id);
    $access_token = $oauth2_client->getAccessToken();
  }
  catch (Exception $e) {
    drupal_set_message($e->getMessage(), 'error');
  }

D8

  $client_id = 'some_client_id';
  $oauth2_config = array(
    'token_endpoint' => $server_url . '/oauth2/token',
    'auth_flow' => 'user-password',
    'client_id' => $client_id,
    'client_secret' => '12345',
    'username' => $username,
    'password' => $password,
  );
  try {
    $oauth2_client = Drupal::service('oauth2.client');
    $oauth2_client->init($oauth2_config, $client_id);
    $access_token = $oauth2_client->getAccessToken();
  }
  catch (Exception $e) {
    drupal_set_message($e->getMessage(), 'error');
  }

Custom usage

Sometimes oauth2 servers have special requirements that are different from the OAuth2 standard and different from other oauth2 implementations. This client cannot possibly cover all these special requirements. In such a case, this module can be extened as follows.

D7

  namespace OAuth2;

  class MyClient extends Client {
    protected function getToken($data) {
      // Implement the custom logic that is needed by the oauth2 server.
    }
  }

And then use it like this:

  try {
    $oauth2_client = new OAuth2MyClient($oauth2_config);
    $access_token = $oauth2_client->getAccessToken();
  }
  catch (Exception $e) {
    drupal_set_message($e->getMessage(), 'error');
  }

D8

1) Extend Drupaloauth2_clientServiceOAuth2Client
2) Swap out the oauth2.service provided by this module, using the technique described here: https://www.previousnext.com.au/blog/overriding-services-drupal-8-advanc...