You are here

README.txt in OAuth2 Client 8

Same filename and directory in other branches
  1. 7.2 README.txt
  2. 7 README.txt
This module is a a complement to the module oauth2_server.

*Note:* The modules oauth2_server and oauth2_client have conflicts
with the module oauth2, so they should not be installed at the same
time.

* How to use it

  Define oauth2 clients in your code like this:
  #+BEGIN_EXAMPLE
  /**
   * 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;
  }
  #+END_EXAMPLE

  Then use them like this:
  #+BEGIN_EXAMPLE
    try {
      $oauth2_client = oauth2_client_load('test1');
      $access_token = $oauth2_client->getAccessToken();
    }
    catch (Exception $e) {
      drupal_set_message($e->getMessage(), 'error');
    }
  #+END_EXAMPLE

  The only thing that oauth2_client does is to get an access_token
  from the oauth2_server, so that it can be used for accessing web
  services.


* More about using it

  Another form of usage is like this:
  #+BEGIN_EXAMPLE
    $oauth2_config = array(
      'token_endpoint' => $server_url . '/oauth2/token',
      'auth_flow' => 'user-password',
      'client_id' => 'test1',
      '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');
    }
  #+END_EXAMPLE

* Custom usage

  Sometimes (or rather often) 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, you can extend
  Drupal\oauth2_client\Service\OAuth2Client, then swap out the oauth2.service
  provided by this module, using the technique described here:
  https://www.previousnext.com.au/blog/overriding-services-drupal-8-advanced-cases

* How the module works

  An access token and its related data are stored on the session
  ($_SESSION['oauth2_client']['token'][$client_id]), so that it can be
  reused while it is not expired yet. The data that are stored for
  each token are: access_token, expires_in, token_type, scope,
  refresh_token and expiration_time. They are the values that come
  from the oauth2 server, except the last one, which is calculated as
  (REQUEST_TIME + expires_in).

  When the token has expired (expiration_time > time() + 10), a new
  token is requested from the oauth2 server, using the refresh_token.
  If the refresh token fails for some reason (maybe refresh_token
  expired or any other reason), then the whole process of
  authorization is performed from the beginning.

  For the client-credentials and user-password authorization flows
  this does not involve a user interaction with the oauth2 server.

  However, for the server-side flow the user has to authorize again
  the application. This is done in these steps, first the user is
  redirected to the oauth2 server to authorize the application again,
  from there it is redirected back to the application with an
  authorization code, then the application uses the authorization code
  to request a new access token.

  In order to remember the part of the client application that
  initiated the authorization request, a session variable is used:
  $_SESSION['oauth2_client']['redirect'][$state]['uri'].  Then,
  drupal_goto() is used to jump again to that path of the application.

* Integrating with other oauth2 clients

  Other oauth2 clients for Drupal can integrate with oauth2_client.
  This means that they can use the same client that is registered on
  the oauth2_server for the oauth2_client.

  The oauth2_server sends the authorization reply to the redirect_uri
  that is registered for the client. If this client has been
  registered for being used by the module oauth2_client, then its
  redirect_uri is like this:
  https://server.example.org/oauth2/authorized . A reply sent to this
  redirect_uri will be routed to the callback function supplied by
  oauth2_client. So, in general, the other oauth2 clients cannot use
  the same client_id and client_secret that are registered in the
  server. They will have to register their own client_id,
  client_secret and redirect_uri.

  However this is not very convenient. That's why oauth2_client allows
  the other oauth2 clients to use the same client_id and
  client_secret, but the reply has to pass through oauth2_client,
  since redirect_uri sends it there.

  It works like this: Suppose that another oauth2 client starts the
  authentication workflow.  On the parameters of the request it sets
  redirect_uri to the one belonging to oauth2_client (since this is
  the one that is reckognized and accepted by the server). However at
  the same time it notifies oauth2_client that the reply of this
  request should be forwarded to it. It does it by calling the
  function: oauth2_client_set_redirect($state, $redirect).

  The parameter $state is 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.  The
  parameter $redirect is an 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 coming from the
      server

  Once another oauth2 client that has been successfully authenticated
  and has received an access_token, it can share it with the
  oauth2_client, so that oauth2_client does not have to repeat the
  authentication process again. It can be done by calling the
  function: oauth2_client_set_token($client_id, $token).

File

README.txt
View source
  1. This module is a a complement to the module oauth2_server.
  2. *Note:* The modules oauth2_server and oauth2_client have conflicts
  3. with the module oauth2, so they should not be installed at the same
  4. time.
  5. * How to use it
  6. Define oauth2 clients in your code like this:
  7. #+BEGIN_EXAMPLE
  8. /**
  9. * Implements hook_oauth2_clients().
  10. */
  11. function MYMODULE_oauth2_clients() {
  12. $server_url = 'https://oauth2_server.example.org';
  13. $client_url = 'https://oauth2_client.example.org';
  14. // user-password flow
  15. $oauth2_clients['test1'] = array(
  16. 'token_endpoint' => $server_url . '/oauth2/token',
  17. 'auth_flow' => 'user-password',
  18. 'client_id' => 'test1',
  19. 'client_secret' => 'test1',
  20. 'username' => 'user1',
  21. 'password' => 'user1',
  22. );
  23. // client-credentials flow
  24. $oauth2_clients['test2'] = array(
  25. 'token_endpoint' => $server_url . '/oauth2/token',
  26. 'auth_flow' => 'client-credentials',
  27. 'client_id' => 'test2',
  28. 'client_secret' => 'test2',
  29. );
  30. // server-side flow
  31. $oauth2_clients['test3'] = array(
  32. 'token_endpoint' => $server_url . '/oauth2/token',
  33. 'auth_flow' => 'server-side',
  34. 'client_id' => 'test1',
  35. 'client_secret' => 'test1',
  36. 'authorization_endpoint' => $server_url . '/oauth2/authorize',
  37. 'redirect_uri' => $client_url . '/oauth2/authorized',
  38. );
  39. return $oauth2_clients;
  40. }
  41. #+END_EXAMPLE
  42. Then use them like this:
  43. #+BEGIN_EXAMPLE
  44. try {
  45. $oauth2_client = oauth2_client_load('test1');
  46. $access_token = $oauth2_client->getAccessToken();
  47. }
  48. catch (Exception $e) {
  49. drupal_set_message($e->getMessage(), 'error');
  50. }
  51. #+END_EXAMPLE
  52. The only thing that oauth2_client does is to get an access_token
  53. from the oauth2_server, so that it can be used for accessing web
  54. services.
  55. * More about using it
  56. Another form of usage is like this:
  57. #+BEGIN_EXAMPLE
  58. $oauth2_config = array(
  59. 'token_endpoint' => $server_url . '/oauth2/token',
  60. 'auth_flow' => 'user-password',
  61. 'client_id' => 'test1',
  62. 'client_secret' => '12345',
  63. 'username' => $username,
  64. 'password' => $password,
  65. );
  66. try {
  67. $oauth2_client = \Drupal->service('oauth2.client');
  68. $oauth2_client->init($oauth2_config, $client_id);
  69. $access_token = $oauth2_client->getAccessToken();
  70. }
  71. catch (Exception $e) {
  72. drupal_set_message($e->getMessage(), 'error');
  73. }
  74. #+END_EXAMPLE
  75. * Custom usage
  76. Sometimes (or rather often) oauth2 servers have special requirements
  77. that are different from the OAuth2 standard and different from other
  78. oauth2 implementations. This client cannot possibly cover all these
  79. special requirements. In such a case, you can extend
  80. Drupal\oauth2_client\Service\OAuth2Client, then swap out the oauth2.service
  81. provided by this module, using the technique described here:
  82. https://www.previousnext.com.au/blog/overriding-services-drupal-8-advanced-cases
  83. * How the module works
  84. An access token and its related data are stored on the session
  85. ($_SESSION['oauth2_client']['token'][$client_id]), so that it can be
  86. reused while it is not expired yet. The data that are stored for
  87. each token are: access_token, expires_in, token_type, scope,
  88. refresh_token and expiration_time. They are the values that come
  89. from the oauth2 server, except the last one, which is calculated as
  90. (REQUEST_TIME + expires_in).
  91. When the token has expired (expiration_time > time() + 10), a new
  92. token is requested from the oauth2 server, using the refresh_token.
  93. If the refresh token fails for some reason (maybe refresh_token
  94. expired or any other reason), then the whole process of
  95. authorization is performed from the beginning.
  96. For the client-credentials and user-password authorization flows
  97. this does not involve a user interaction with the oauth2 server.
  98. However, for the server-side flow the user has to authorize again
  99. the application. This is done in these steps, first the user is
  100. redirected to the oauth2 server to authorize the application again,
  101. from there it is redirected back to the application with an
  102. authorization code, then the application uses the authorization code
  103. to request a new access token.
  104. In order to remember the part of the client application that
  105. initiated the authorization request, a session variable is used:
  106. $_SESSION['oauth2_client']['redirect'][$state]['uri']. Then,
  107. drupal_goto() is used to jump again to that path of the application.
  108. * Integrating with other oauth2 clients
  109. Other oauth2 clients for Drupal can integrate with oauth2_client.
  110. This means that they can use the same client that is registered on
  111. the oauth2_server for the oauth2_client.
  112. The oauth2_server sends the authorization reply to the redirect_uri
  113. that is registered for the client. If this client has been
  114. registered for being used by the module oauth2_client, then its
  115. redirect_uri is like this:
  116. https://server.example.org/oauth2/authorized . A reply sent to this
  117. redirect_uri will be routed to the callback function supplied by
  118. oauth2_client. So, in general, the other oauth2 clients cannot use
  119. the same client_id and client_secret that are registered in the
  120. server. They will have to register their own client_id,
  121. client_secret and redirect_uri.
  122. However this is not very convenient. That's why oauth2_client allows
  123. the other oauth2 clients to use the same client_id and
  124. client_secret, but the reply has to pass through oauth2_client,
  125. since redirect_uri sends it there.
  126. It works like this: Suppose that another oauth2 client starts the
  127. authentication workflow. On the parameters of the request it sets
  128. redirect_uri to the one belonging to oauth2_client (since this is
  129. the one that is reckognized and accepted by the server). However at
  130. the same time it notifies oauth2_client that the reply of this
  131. request should be forwarded to it. It does it by calling the
  132. function: oauth2_client_set_redirect($state, $redirect).
  133. The parameter $state is the random parameter that is used on the
  134. authentication url in order to mittigate CSRF attacks. In this case
  135. it is used as a key for identifying the authentication request. The
  136. parameter $redirect is an associative array that contains the keys:
  137. - uri: the uri of the oauth2 client that is requesting a
  138. redirect
  139. - params: associative array of other parameters that should be
  140. appended to the uri, along with the $_REQUEST coming from the
  141. server
  142. Once another oauth2 client that has been successfully authenticated
  143. and has received an access_token, it can share it with the
  144. oauth2_client, so that oauth2_client does not have to repeat the
  145. authentication process again. It can be done by calling the
  146. function: oauth2_client_set_token($client_id, $token).