PageRenderTime 25ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/inc/libs/oauth/class.oauth.1.client.php

https://bitbucket.org/JcDenis/scmsocialme
PHP | 238 lines | 143 code | 31 blank | 64 comment | 27 complexity | 5986ba950ce82e20b8bf4ac39b75ebb8 MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. # -- BEGIN LICENSE BLOCK ----------------------------------
  3. #
  4. # This file is part of scmSocialme, a plugin for Dotclear 2.
  5. #
  6. # Copyright (c) 2009-2013 Jean-Christian Denis and contributors
  7. # contact@jcdenis.fr http://jcd.lv
  8. #
  9. # Licensed under the GPL version 2.0 license.
  10. # A copy of this license is available in LICENSE file or at
  11. # http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  12. #
  13. # -- END LICENSE BLOCK ------------------------------------
  14. /** @file
  15. * @brief oAuth 1.0a Client methods
  16. */
  17. /**
  18. * @ingroup SCM_LIB_OAUTH
  19. * @nosubgrouping
  20. * @brief oAuth 1.0a Client
  21. *
  22. * oAuth 1.0a RFC5849 flow and request utilities
  23. */
  24. class oAuth1Client extends oAuthClient
  25. {
  26. protected $signer_oauth_version = '1.0';
  27. public function requestClientCredential()
  28. {
  29. # Check if client is localy registred
  30. if (!$this->hasClient()) {
  31. throw new Exception('Client is not set.');
  32. }
  33. $params = array();
  34. # Client does not support callback or callback is set elsewhere #section-2.1
  35. if ($this->client['client_disable_redirection']) {
  36. $params['oauth_callback'] = 'oob'; # out-of-band
  37. }
  38. # Define callback url
  39. elseif (!empty($this->redirection_endpoint)) {
  40. $params['oauth_callback'] = $this->redirection_endpoint;
  41. # Add state params
  42. if (!empty($this->state)) {
  43. $params['oauth_callback'] .= '?'.http_build_query(array('state'=>$this->state),'','&');
  44. }
  45. }
  46. # Request temporary credential
  47. $response = $this->accessProtectedResource($this->client['server_request_uri'],'POST',$params,false);
  48. # Check server response http code
  49. if ($this->http_rsp_code != 200) {
  50. throw new Exception('Failed to request temporaly credential');
  51. }
  52. # Check and parse server response body
  53. parse_str($response,$token);
  54. if (empty($token)) {
  55. throw new Exception('Failed to parse server response.');
  56. }
  57. # Check server version
  58. if (!isset($token['oauth_callback_confirmed']) || $token['oauth_callback_confirmed'] != 'true') {
  59. throw new Exception('Required oAuth 1.0a compliant server.');
  60. }
  61. # Set temporary credential
  62. $this->updateUser(
  63. array(
  64. 'user_key' => !empty($token['oauth_token']) ? $token['oauth_token'] : null,
  65. 'user_secret' => !empty($token['oauth_token_secret']) ? $token['oauth_token_secret'] : null,
  66. 'user_flow' => 1
  67. )
  68. );
  69. # Redirect user to server to query grant
  70. http::redirect($this->client['server_authorize_uri'].'?'.http_build_query(array('oauth_token'=>$token['oauth_token']),'','&'));
  71. }
  72. public function requestUserCredential()
  73. {
  74. # Check if client is localy registred
  75. if (!$this->hasClient()) {
  76. throw new Exception('Client is not set.');
  77. }
  78. # Check oAuth flow
  79. if ($this->user['user_flow'] != 1) {
  80. throw new Exception('Authorization not complete. Request authorization first.');
  81. }
  82. # Check if redirection is too old
  83. if (isset($_REQUEST['oauth_token']) && $_REQUEST['oauth_token'] != $this->user['user_key']) {
  84. throw new Exception('Failed to request access. Expired token');
  85. }
  86. # Add oauth verifier to params if needed
  87. if (isset($_REQUEST['oauth_verifier']) && $_REQUEST['oauth_verifier'] != '') {
  88. $params['oauth_verifier'] = $_REQUEST['oauth_verifier'];
  89. }
  90. # Query server
  91. $response = $this->accessProtectedResource($this->client['server_token_uri'],'POST',$params,false);
  92. # Query failed
  93. if ($this->http_rsp_code != 200) {
  94. throw new Exception('Failed to request credential token.');
  95. }
  96. # Check and parse server response body
  97. parse_str($response,$rsp);
  98. if (empty($rsp)) {
  99. throw new Exception('Failed to parse server response.');
  100. }
  101. if (empty($rsp['oauth_token']) || empty($rsp['oauth_token_secret'])) {
  102. throw new Exception('Failed to find token in server response.');
  103. }
  104. # Set final token credential
  105. $this->updateUser(
  106. array(
  107. 'user_key' => $rsp['oauth_token'],
  108. 'user_secret' => $rsp['oauth_token_secret'],
  109. 'user_flow' => 2
  110. )
  111. );
  112. # All right baby, let's dance
  113. return $rsp;
  114. }
  115. /**
  116. * @todo implement it
  117. */
  118. public function refreshUserCredential()
  119. {
  120. return true;
  121. }
  122. public function revokeUserCredential()
  123. {
  124. $this->deleteUser();
  125. return true;
  126. }
  127. public function setDefaultSignerParameters()
  128. {
  129. $defaults = array(
  130. 'oauth_version' => $this->signer_oauth_version,
  131. 'oauth_nonce' => md5(microtime().mt_rand()),
  132. 'oauth_timestamp' => time(),
  133. 'oauth_consumer_key' => $this->client['client_key']
  134. );
  135. if ($this->user['user_key'] != '') {
  136. $defaults['oauth_token'] = $this->user['user_key'];
  137. }
  138. $this->signer_parameters = array_merge(
  139. oAuthClient::parseSignerParameters(
  140. parse_url($this->signer_http_url,PHP_URL_QUERY)
  141. ),
  142. $defaults,
  143. $this->signer_parameters
  144. );
  145. }
  146. /**
  147. * HMAC-SHA1 signature method
  148. *
  149. * Add signature parameters to request parameters
  150. */
  151. protected function signatureMethod_HMAC_SHA1()
  152. {
  153. $this->setSignerParameter(
  154. 'oauth_signature_method',
  155. $this->signer_sign_method,
  156. false
  157. );
  158. $this->setSignerParameter(
  159. 'oauth_signature',
  160. $this->buildSignature(),
  161. false
  162. );
  163. }
  164. /**
  165. * Build signature
  166. *
  167. * @retval string Signature
  168. */
  169. protected function buildSignature()
  170. {
  171. return base64_encode(hash_hmac('sha1',
  172. $this->getSignatureBaseString(),
  173. implode('&',oAuthClient::urlencode(array(
  174. $this->client['client_secret'],
  175. ($this->user['user_secret'] != '') ? $this->user['user_secret'] : ''
  176. ))),
  177. true
  178. ));
  179. }
  180. /**
  181. * Get signatuer base string
  182. *
  183. * @retval string Query style signature base string
  184. */
  185. protected function getSignatureBaseString()
  186. {
  187. return implode('&',oAuthClient::urlencode(array(
  188. $this->getNormalizedHttpMethod(),
  189. $this->getNormalizedHttpUrl(),
  190. $this->getSignableParameters()
  191. )));
  192. }
  193. /**
  194. * Get signable parameters
  195. *
  196. * @retval string Query style signature base parameters
  197. */
  198. protected function getSignableParameters()
  199. {
  200. $params = $this->signer_parameters;
  201. if (isset($params['oauth_signature'])) {
  202. unset($params['oauth_signature']);
  203. }
  204. return oAuthClient::buildHttpQuery($params);
  205. }
  206. }
  207. ?>