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

/station-games/vendor/cakephp/cakephp/src/Network/Http/Auth/Oauth.php

https://gitlab.com/ViniciusP/project-games
PHP | 262 lines | 147 code | 19 blank | 96 comment | 12 complexity | 4e2e5d5c04fa4570ad934413eaff5419 MD5 | raw file
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * Redistributions of files must retain the above copyright notice.
  8. *
  9. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  10. * @link http://cakephp.org CakePHP(tm) Project
  11. * @since 3.0.0
  12. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  13. */
  14. namespace Cake\Network\Http\Auth;
  15. use Cake\Core\Exception\Exception;
  16. use Cake\Network\Http\Request;
  17. /**
  18. * Oauth 1 authentication strategy for Cake\Network\Http\Client
  19. *
  20. * This object does not handle getting Oauth access tokens from the service
  21. * provider. It only handles make client requests *after* you have obtained the Oauth
  22. * tokens.
  23. *
  24. * Generally not directly constructed, but instead used by Cake\Network\Http\Client
  25. * when $options['auth']['type'] is 'oauth'
  26. */
  27. class Oauth
  28. {
  29. /**
  30. * Add headers for Oauth authorization.
  31. *
  32. * @param \Cake\Network\Http\Request $request The request object.
  33. * @param array $credentials Authentication credentials.
  34. * @return void
  35. * @throws \Cake\Core\Exception\Exception On invalid signature types.
  36. */
  37. public function authentication(Request $request, array $credentials)
  38. {
  39. $hasKeys = isset(
  40. $credentials['consumerSecret'],
  41. $credentials['consumerKey'],
  42. $credentials['token'],
  43. $credentials['tokenSecret']
  44. );
  45. if (!$hasKeys) {
  46. return;
  47. }
  48. if (empty($credentials['method'])) {
  49. $credentials['method'] = 'hmac-sha1';
  50. }
  51. $credentials['method'] = strtoupper($credentials['method']);
  52. switch ($credentials['method']) {
  53. case 'HMAC-SHA1':
  54. $value = $this->_hmacSha1($request, $credentials);
  55. break;
  56. case 'PLAINTEXT':
  57. $value = $this->_plaintext($request, $credentials);
  58. break;
  59. default:
  60. throw new Exception(sprintf('Unknown Oauth signature method %s', $credentials['method']));
  61. }
  62. $request->header('Authorization', $value);
  63. }
  64. /**
  65. * Plaintext signing
  66. *
  67. * This method is **not** suitable for plain HTTP.
  68. * You should only ever use PLAINTEXT when dealing with SSL
  69. * services.
  70. *
  71. * @param \Cake\Network\Http\Request $request The request object.
  72. * @param array $credentials Authentication credentials.
  73. * @return string Authorization header.
  74. */
  75. protected function _plaintext($request, $credentials)
  76. {
  77. $values = [
  78. 'oauth_version' => '1.0',
  79. 'oauth_nonce' => uniqid(),
  80. 'oauth_timestamp' => time(),
  81. 'oauth_signature_method' => 'PLAINTEXT',
  82. 'oauth_token' => $credentials['token'],
  83. 'oauth_consumer_key' => $credentials['consumerKey'],
  84. ];
  85. if (isset($credentials['realm'])) {
  86. $values['oauth_realm'] = $credentials['realm'];
  87. }
  88. $key = [$credentials['consumerSecret'], $credentials['tokenSecret']];
  89. $key = implode('&', $key);
  90. $values['oauth_signature'] = $key;
  91. return $this->_buildAuth($values);
  92. }
  93. /**
  94. * Use HMAC-SHA1 signing.
  95. *
  96. * This method is suitable for plain HTTP or HTTPS.
  97. *
  98. * @param \Cake\Network\Http\Request $request The request object.
  99. * @param array $credentials Authentication credentials.
  100. * @return string
  101. */
  102. protected function _hmacSha1($request, $credentials)
  103. {
  104. $nonce = isset($credentials['nonce']) ? $credentials['nonce'] : uniqid();
  105. $timestamp = isset($credentials['timestamp']) ? $credentials['timestamp'] : time();
  106. $values = [
  107. 'oauth_version' => '1.0',
  108. 'oauth_nonce' => $nonce,
  109. 'oauth_timestamp' => $timestamp,
  110. 'oauth_signature_method' => 'HMAC-SHA1',
  111. 'oauth_token' => $credentials['token'],
  112. 'oauth_consumer_key' => $credentials['consumerKey'],
  113. ];
  114. $baseString = $this->baseString($request, $values);
  115. if (isset($credentials['realm'])) {
  116. $values['oauth_realm'] = $credentials['realm'];
  117. }
  118. $key = [$credentials['consumerSecret'], $credentials['tokenSecret']];
  119. $key = array_map([$this, '_encode'], $key);
  120. $key = implode('&', $key);
  121. $values['oauth_signature'] = base64_encode(
  122. hash_hmac('sha1', $baseString, $key, true)
  123. );
  124. return $this->_buildAuth($values);
  125. }
  126. /**
  127. * Generate the Oauth basestring
  128. *
  129. * - Querystring, request data and oauth_* parameters are combined.
  130. * - Values are sorted by name and then value.
  131. * - Request values are concatenated and urlencoded.
  132. * - The request URL (without querystring) is normalized.
  133. * - The HTTP method, URL and request parameters are concatenated and returned.
  134. *
  135. * @param \Cake\Network\Http\Request $request The request object.
  136. * @param array $oauthValues Oauth values.
  137. * @return string
  138. */
  139. public function baseString($request, $oauthValues)
  140. {
  141. $parts = [
  142. $request->method(),
  143. $this->_normalizedUrl($request->url()),
  144. $this->_normalizedParams($request, $oauthValues),
  145. ];
  146. $parts = array_map([$this, '_encode'], $parts);
  147. return implode('&', $parts);
  148. }
  149. /**
  150. * Builds a normalized URL
  151. *
  152. * Section 9.1.2. of the Oauth spec
  153. *
  154. * @param string $url URL
  155. * @return string Normalized URL
  156. * @throws \Cake\Core\Exception\Exception On invalid URLs
  157. */
  158. protected function _normalizedUrl($url)
  159. {
  160. $parts = parse_url($url);
  161. if (!$parts) {
  162. throw new Exception('Unable to parse URL');
  163. }
  164. $scheme = strtolower($parts['scheme'] ?: 'http');
  165. $defaultPorts = [
  166. 'http' => 80,
  167. 'https' => 443
  168. ];
  169. if (isset($parts['port']) && $parts['port'] != $defaultPorts[$scheme]) {
  170. $parts['host'] .= ':' . $parts['port'];
  171. }
  172. $out = $scheme . '://';
  173. $out .= strtolower($parts['host']);
  174. $out .= $parts['path'];
  175. return $out;
  176. }
  177. /**
  178. * Sorts and normalizes request data and oauthValues
  179. *
  180. * Section 9.1.1 of Oauth spec.
  181. *
  182. * - URL encode keys + values.
  183. * - Sort keys & values by byte value.
  184. *
  185. * @param \Cake\Network\Http\Request $request The request object.
  186. * @param array $oauthValues Oauth values.
  187. * @return string sorted and normalized values
  188. */
  189. protected function _normalizedParams($request, $oauthValues)
  190. {
  191. $query = parse_url($request->url(), PHP_URL_QUERY);
  192. parse_str($query, $queryArgs);
  193. $post = [];
  194. $body = $request->body();
  195. if (is_array($body)) {
  196. $post = $body;
  197. }
  198. $args = array_merge($queryArgs, $oauthValues, $post);
  199. uksort($args, 'strcmp');
  200. $pairs = [];
  201. foreach ($args as $k => $val) {
  202. if (is_array($val)) {
  203. sort($val, SORT_STRING);
  204. foreach ($val as $nestedVal) {
  205. $pairs[] = "$k=$nestedVal";
  206. }
  207. } else {
  208. $pairs[] = "$k=$val";
  209. }
  210. }
  211. return implode('&', $pairs);
  212. }
  213. /**
  214. * Builds the Oauth Authorization header value.
  215. *
  216. * @param array $data The oauth_* values to build
  217. * @return string
  218. */
  219. protected function _buildAuth($data)
  220. {
  221. $out = 'OAuth ';
  222. $params = [];
  223. foreach ($data as $key => $value) {
  224. $params[] = $key . '="' . $this->_encode($value) . '"';
  225. }
  226. $out .= implode(',', $params);
  227. return $out;
  228. }
  229. /**
  230. * URL Encodes a value based on rules of rfc3986
  231. *
  232. * @param string $value Value to encode.
  233. * @return string
  234. */
  235. protected function _encode($value)
  236. {
  237. return str_replace(
  238. '+',
  239. ' ',
  240. str_replace('%7E', '~', rawurlencode($value))
  241. );
  242. }
  243. }