/classes/oauthz/extension/token.php

https://github.com/wmmarket/kohana-oauthy · PHP · 216 lines · 126 code · 25 blank · 65 comment · 18 complexity · b46e0ade3d126f323f40f4bb540b59a1 MD5 · raw file

  1. <?php
  2. /**
  3. * Response type is token
  4. *
  5. * Oauth parameter handler for authenticate token request
  6. *
  7. * @author sumh <oalite@gmail.com>
  8. * @package Oauthz
  9. * @copyright (c) 2010 OALite
  10. * @license ISC License (ISCL)
  11. * @link http://oalite.com
  12. * @see Oauthz_Extension
  13. * *
  14. */
  15. class Oauthz_Extension_Token extends Oauthz_Extension {
  16. /**
  17. * REQUIRED
  18. *
  19. * @access public
  20. * @var string $oauth_token
  21. */
  22. public $oauth_token;
  23. /**
  24. * Parameters parsed from Form-Encoded Body
  25. *
  26. * @access protected
  27. * @var string $_params
  28. */
  29. protected $_params;
  30. /**
  31. * Load request parameters from Authorization header, URI-Query parameters, Form-Encoded Body
  32. *
  33. * @access public
  34. * @param array $args
  35. * @return void
  36. * @throw Oauthz_Exception_Token Error code: invalid_request
  37. */
  38. public function __construct(array $args)
  39. {
  40. $params = array();
  41. /**
  42. * TODO move this request data detect into authorization handler
  43. * Load oauth token from authorization header
  44. */
  45. if (isset($_SERVER['HTTP_AUTHORIZATION']) OR $_SERVER['HTTP_AUTHORIZATION'] = getenv('HTTP_AUTHORIZATION'))
  46. {
  47. $offset = 0;
  48. $pattern = '/(([-_a-z]*)=("([^"]*)"|([^,]*)),?)/';
  49. while(preg_match($pattern, $_SERVER['HTTP_AUTHORIZATION'], $matches, PREG_OFFSET_CAPTURE, $offset) > 0)
  50. {
  51. $match = $matches[0];
  52. $name = $matches[2][0];
  53. $offset = $match[1] + strlen($match[0]);
  54. if($value = Oauthz::urldecode(isset($matches[5]) ? $matches[5][0] : $matches[4][0]))
  55. {
  56. $params[$name] = $value;
  57. }
  58. }
  59. // Replace the name of token to oauth_token
  60. if(isset($params['token']))
  61. {
  62. $params['oauth_token'] = $params['token'];
  63. unset($params['token']);
  64. // Check all required parameters should NOT be empty
  65. foreach($args as $key => $val)
  66. {
  67. if($val === TRUE)
  68. {
  69. if( ! empty($params[$key]))
  70. {
  71. throw new Oauthz_Exception_Token('invalid_request');
  72. }
  73. }
  74. }
  75. }
  76. $this->method = 'HEADER';
  77. }
  78. /**
  79. * Load oauth_token from form-encoded body
  80. */
  81. if(isset($_POST['oauth_token']))
  82. {
  83. isset($_SERVER['CONTENT_TYPE']) OR $_SERVER['CONTENT_TYPE'] = getenv('CONTENT_TYPE');
  84. // oauth_token already send in authorization header or the encrypt Content-Type is not single-part
  85. if(isset($params['oauth_token']) OR stripos($_SERVER['CONTENT_TYPE'], 'application/x-www-form-urlencoded') === FALSE)
  86. {
  87. throw new Oauthz_Exception_Token('invalid_request');
  88. }
  89. else
  90. {
  91. // TODO move this request data detect into authorization handler
  92. if(isset($_SERVER['PHP_AUTH_USER']) AND isset($_SERVER['PHP_AUTH_PW']))
  93. {
  94. $_POST += array('client_id' => $_SERVER['PHP_AUTH_USER'], 'client_secret' => $_SERVER['PHP_AUTH_PW']);
  95. }
  96. // TODO Digest HTTP authentication
  97. //else if( ! empty($_SERVER['PHP_AUTH_DIGEST']) AND $digest = parent::parse_digest($_SERVER['PHP_AUTH_DIGEST']))
  98. //{
  99. // $_POST += array('client_id' => $digest['username'], 'client_secret' => $digest['']);
  100. //}
  101. // Check all required parameters should NOT be empty
  102. foreach($args as $key => $val)
  103. {
  104. if($val === TRUE)
  105. {
  106. if(isset($_POST[$key]) AND $value = Oauthz::urldecode($_POST[$key]))
  107. {
  108. $params[$key] = $value;
  109. }
  110. else
  111. {
  112. throw new Oauthz_Exception_Token('invalid_request');
  113. }
  114. }
  115. }
  116. }
  117. $this->method = 'POST';
  118. }
  119. /**
  120. * Load oauth_token from uri-query component
  121. */
  122. if(isset($_GET['oauth_token']))
  123. {
  124. // oauth_token already send in authorization header or form-encoded body
  125. if(isset($params['oauth_token']))
  126. {
  127. throw new Oauthz_Exception_Token('invalid_request');
  128. }
  129. else
  130. {
  131. // Check all required parameters should NOT be empty
  132. foreach($args as $key => $val)
  133. {
  134. if($val === TRUE)
  135. {
  136. if(isset($_GET[$key]) AND $value = Oauthz::urldecode($_GET[$key]))
  137. {
  138. $params[$key] = $value;
  139. }
  140. else
  141. {
  142. throw new Oauthz_Exception_Token('invalid_request');
  143. }
  144. }
  145. }
  146. }
  147. $this->method = 'GET';
  148. }
  149. if(empty($params))
  150. {
  151. throw new Oauthz_Exception_Token('invalid_request');
  152. }
  153. $this->oauth_token = $params['oauth_token'];
  154. unset($params['oauth_token']);
  155. $this->_params = $params;
  156. }
  157. /**
  158. * No need to authorization any more
  159. *
  160. * @access public
  161. * @param array $client
  162. * @return Oauthz_Token
  163. * @throw Oauthz_Exception_Token Error codes: invalid_scope, redirect_uri_mismatch
  164. */
  165. public function execute()
  166. {
  167. // Verify the client and the code, load the access token if successes
  168. if($client = Oauthz_Model::factory('Token')->oauth_token($this->client_id, $this->code))
  169. {
  170. $client['expires_in'] = $this->_configs['durations']['oauth_token'];
  171. }
  172. else
  173. {
  174. // Invalid client_id
  175. $exception = new Oauthz_Exception_Token('invalid_client');
  176. $exception->redirect_uri = $this->redirect_uri;
  177. $exception->state = $this->state;
  178. throw $exception;
  179. }
  180. $response = new Oauthz_Token;
  181. if(isset($this->_params['token_secret']) AND $client['token_secret'] !== sha1($this->_params['token_secret']))
  182. {
  183. throw new Oauthz_Exception_Token('invalid_request');
  184. }
  185. if(isset($this->_params['timestamp']) AND $client['timestamp'] < $this->_params['timestamp'])
  186. {
  187. throw new Oauthz_Exception_Token('unauthorized_client');
  188. }
  189. return $this->redirect_uri.'#'.$response->as_query();
  190. }
  191. } // END Oauthz_Extension_Token