You are here

README.txt in OAuth2 Client 7

Same filename and directory in other branches
  1. 8 README.txt
  2. 7.2 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 = new OAuth2\Client($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, a possible solution can be to
  extend the class *OAuth2\Client* like this:
  #+BEGIN_EXAMPLE
    <?php
    namespace OAuth2;

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

  And then use it like this:
  #+BEGIN_EXAMPLE
    try {
      $oauth2_client = new OAuth2\MyClient($oauth2_config);
      $access_token = $oauth2_client->getAccessToken();
    }
    catch (Exception $e) {
      drupal_set_message($e->getMessage(), 'error');
    }
  #+END_EXAMPLE

* How it 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 comming 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 = new OAuth2\Client($oauth2_config, $client_id);
  68. $access_token = $oauth2_client->getAccessToken();
  69. }
  70. catch (Exception $e) {
  71. drupal_set_message($e->getMessage(), 'error');
  72. }
  73. #+END_EXAMPLE
  74. * Custom usage
  75. Sometimes (or rather often) oauth2 servers have special requirements
  76. that are different from the OAuth2 standard and different from other
  77. oauth2 implementations. This client cannot possibly cover all these
  78. special requirements. In such a case, a possible solution can be to
  79. extend the class *OAuth2\Client* like this:
  80. #+BEGIN_EXAMPLE
  81. namespace OAuth2;
  82. class MyClient extends Client {
  83. protected function getToken($data) {
  84. // Implement the custom logic that is needed by the oauth2 server.
  85. }
  86. }
  87. #+END_EXAMPLE
  88. And then use it like this:
  89. #+BEGIN_EXAMPLE
  90. try {
  91. $oauth2_client = new OAuth2\MyClient($oauth2_config);
  92. $access_token = $oauth2_client->getAccessToken();
  93. }
  94. catch (Exception $e) {
  95. drupal_set_message($e->getMessage(), 'error');
  96. }
  97. #+END_EXAMPLE
  98. * How it works
  99. An access token and its related data are stored on the session
  100. ($_SESSION['oauth2_client']['token'][$client_id]), so that it can be
  101. reused while it is not expired yet. The data that are stored for
  102. each token are: access_token, expires_in, token_type, scope,
  103. refresh_token and expiration_time. They are the values that come
  104. from the oauth2 server, except the last one, which is calculated as
  105. (REQUEST_TIME + expires_in).
  106. When the token has expired (expiration_time > time() + 10), a new
  107. token is requested from the oauth2 server, using the refresh_token.
  108. If the refresh token fails for some reason (maybe refresh_token
  109. expired or any other reason), then the whole process of
  110. authorization is performed from the beginning.
  111. For the client-credentials and user-password authorization flows
  112. this does not involve a user interaction with the oauth2 server.
  113. However, for the server-side flow the user has to authorize again
  114. the application. This is done in these steps, first the user is
  115. redirected to the oauth2 server to authorize the application again,
  116. from there it is redirected back to the application with an
  117. authorization code, then the application uses the authorization code
  118. to request a new access token.
  119. In order to remember the part of the client application that
  120. initiated the authorization request, a session variable is used:
  121. $_SESSION['oauth2_client']['redirect'][$state]['uri']. Then,
  122. drupal_goto() is used to jump again to that path of the application.
  123. * Integrating with other oauth2 clients
  124. Other oauth2 clients for Drupal can integrate with oauth2_client.
  125. This means that they can use the same client that is registered on
  126. the oauth2_server for the oauth2_client.
  127. The oauth2_server sends the authorization reply to the redirect_uri
  128. that is registered for the client. If this client has been
  129. registered for being used by the module oauth2_client, then its
  130. redirect_uri is like this:
  131. https://server.example.org/oauth2/authorized . A reply sent to this
  132. redirect_uri will be routed to the callback function supplied by
  133. oauth2_client. So, in general, the other oauth2 clients cannot use
  134. the same client_id and client_secret that are registered in the
  135. server. They will have to register their own client_id,
  136. client_secret and redirect_uri.
  137. However this is not very convenient. That's why oauth2_client allows
  138. the other oauth2 clients to use the same client_id and
  139. client_secret, but the reply has to pass through oauth2_client,
  140. since redirect_uri sends it there.
  141. It works like this: Suppose that another oauth2 client starts the
  142. authentication workflow. On the parameters of the request it sets
  143. redirect_uri to the one belonging to oauth2_client (since this is
  144. the one that is reckognized and accepted by the server). However at
  145. the same time it notifies oauth2_client that the reply of this
  146. request should be forwarded to it. It does it by calling the
  147. function: oauth2_client_set_redirect($state, $redirect).
  148. The parameter $state is the random parameter that is used on the
  149. authentication url in order to mittigate CSRF attacks. In this case
  150. it is used as a key for identifying the authentication request. The
  151. parameter $redirect is an associative array that contains the keys:
  152. - uri: the uri of the oauth2 client that is requesting a
  153. redirect
  154. - params: associative array of other parameters that should be
  155. appended to the uri, along with the $_REQUEST comming from the
  156. server
  157. Once another oauth2 client that has been successfully authenticated
  158. and has received an access_token, it can share it with the
  159. oauth2_client, so that oauth2_client does not have to repeat the
  160. authentication process again. It can be done by calling the
  161. function: oauth2_client_set_token($client_id, $token).