/library/Zend/OAuth/Token/AbstractToken.php

https://github.com/Exercise/zf2 · PHP · 293 lines · 123 code · 23 blank · 147 comment · 10 complexity · 0f8c05a6b7f265f3db110957e6866f70 MD5 · raw file

  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_OAuth
  17. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id$
  20. */
  21. /**
  22. * @namespace
  23. */
  24. namespace Zend\OAuth\Token;
  25. use Zend\Http\Response as HTTPResponse,
  26. Zend\OAuth\Token as OAuthToken,
  27. Zend\OAuth\Http\Utility as HTTPUtility,
  28. Zend\OAuth\Exception as OAuthException;
  29. /**
  30. * @uses Zend\Http\Response
  31. * @uses Zend\OAuth\Http\Utility
  32. * @category Zend
  33. * @package Zend_OAuth
  34. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  35. * @license http://framework.zend.com/license/new-bsd New BSD License
  36. */
  37. abstract class AbstractToken implements OAuthToken
  38. {
  39. /**@+
  40. * Token constants
  41. */
  42. const TOKEN_PARAM_KEY = 'oauth_token';
  43. const TOKEN_SECRET_PARAM_KEY = 'oauth_token_secret';
  44. const TOKEN_PARAM_CALLBACK_CONFIRMED = 'oauth_callback_confirmed';
  45. /**@-*/
  46. /**
  47. * Token parameters
  48. *
  49. * @var array
  50. */
  51. protected $_params = array();
  52. /**
  53. * OAuth response object
  54. *
  55. * @var \Zend\Http\Response
  56. */
  57. protected $_response = null;
  58. /**
  59. * @var \Zend\OAuth\Http\Utility
  60. */
  61. protected $_httpUtility = null;
  62. /**
  63. * Constructor; basic setup for any Token subclass.
  64. *
  65. * @param null|\Zend\Http\Response $response
  66. * @param null|\Zend\OAuth\Http\Utility $utility
  67. * @return void
  68. */
  69. public function __construct(
  70. HTTPResponse $response = null,
  71. HTTPUtility $utility = null
  72. ) {
  73. if ($response !== null) {
  74. $this->_response = $response;
  75. $params = $this->_parseParameters($response);
  76. if (count($params) > 0) {
  77. $this->setParams($params);
  78. }
  79. }
  80. if ($utility !== null) {
  81. $this->_httpUtility = $utility;
  82. } else {
  83. $this->_httpUtility = new HTTPUtility;
  84. }
  85. }
  86. /**
  87. * Attempts to validate the Token parsed from the HTTP response - really
  88. * it's just very basic existence checks which are minimal.
  89. *
  90. * @return bool
  91. */
  92. public function isValid()
  93. {
  94. if (isset($this->_params[self::TOKEN_PARAM_KEY])
  95. && !empty($this->_params[self::TOKEN_PARAM_KEY])
  96. && isset($this->_params[self::TOKEN_SECRET_PARAM_KEY])
  97. ) {
  98. return true;
  99. }
  100. return false;
  101. }
  102. /**
  103. * Return the HTTP response object used to initialise this instance.
  104. *
  105. * @return \Zend\Http\Response
  106. */
  107. public function getResponse()
  108. {
  109. return $this->_response;
  110. }
  111. /**
  112. * Sets the value for the this Token's secret which may be used when signing
  113. * requests with this Token.
  114. *
  115. * @param string $secret
  116. * @return \Zend\OAuth\Token\AbstractToken
  117. */
  118. public function setTokenSecret($secret)
  119. {
  120. $this->setParam(self::TOKEN_SECRET_PARAM_KEY, $secret);
  121. return $this;
  122. }
  123. /**
  124. * Retrieve this Token's secret which may be used when signing
  125. * requests with this Token.
  126. *
  127. * @return string
  128. */
  129. public function getTokenSecret()
  130. {
  131. return $this->getParam(self::TOKEN_SECRET_PARAM_KEY);
  132. }
  133. /**
  134. * Sets the value for a parameter (e.g. token secret or other) and run
  135. * a simple filter to remove any trailing newlines.
  136. *
  137. * @param string $key
  138. * @param string $value
  139. * @return \Zend\OAuth\Token\AbstractToken
  140. */
  141. public function setParam($key, $value)
  142. {
  143. $this->_params[$key] = trim($value, "\n");
  144. return $this;
  145. }
  146. /**
  147. * Sets the value for some parameters (e.g. token secret or other) and run
  148. * a simple filter to remove any trailing newlines.
  149. *
  150. * @param array $params
  151. * @return \Zend\OAuth\Token\AbstractToken
  152. */
  153. public function setParams(array $params)
  154. {
  155. foreach ($params as $key=>$value) {
  156. $this->setParam($key, $value);
  157. }
  158. return $this;
  159. }
  160. /**
  161. * Get the value for a parameter (e.g. token secret or other).
  162. *
  163. * @param string $key
  164. * @return mixed
  165. */
  166. public function getParam($key)
  167. {
  168. if (isset($this->_params[$key])) {
  169. return $this->_params[$key];
  170. }
  171. return null;
  172. }
  173. /**
  174. * Sets the value for a Token.
  175. *
  176. * @param string $token
  177. * @return \Zend\OAuth\Token\AbstractToken
  178. */
  179. public function setToken($token)
  180. {
  181. $this->setParam(self::TOKEN_PARAM_KEY, $token);
  182. return $this;
  183. }
  184. /**
  185. * Gets the value for a Token.
  186. *
  187. * @return string
  188. */
  189. public function getToken()
  190. {
  191. return $this->getParam(self::TOKEN_PARAM_KEY);
  192. }
  193. /**
  194. * Generic accessor to enable access as public properties.
  195. *
  196. * @return string
  197. */
  198. public function __get($key)
  199. {
  200. return $this->getParam($key);
  201. }
  202. /**
  203. * Generic mutator to enable access as public properties.
  204. *
  205. * @param string $key
  206. * @param string $value
  207. * @return void
  208. */
  209. public function __set($key, $value)
  210. {
  211. $this->setParam($key, $value);
  212. }
  213. /**
  214. * Convert Token to a string, specifically a raw encoded query string.
  215. *
  216. * @return string
  217. */
  218. public function toString()
  219. {
  220. return $this->_httpUtility->toEncodedQueryString($this->_params);
  221. }
  222. /**
  223. * Convert Token to a string, specifically a raw encoded query string.
  224. * Aliases to self::toString()
  225. *
  226. * @return string
  227. */
  228. public function __toString()
  229. {
  230. return $this->toString();
  231. }
  232. /**
  233. * Parse a HTTP response body and collect returned parameters
  234. * as raw url decoded key-value pairs in an associative array.
  235. *
  236. * @param \Zend\Http\Response $response
  237. * @return array
  238. */
  239. protected function _parseParameters(HTTPResponse $response)
  240. {
  241. $params = array();
  242. $body = $response->getBody();
  243. if (empty($body)) {
  244. return;
  245. }
  246. // validate body based on acceptable characters...todo
  247. $parts = explode('&', $body);
  248. foreach ($parts as $kvpair) {
  249. $pair = explode('=', $kvpair);
  250. $params[rawurldecode($pair[0])] = rawurldecode($pair[1]);
  251. }
  252. return $params;
  253. }
  254. /**
  255. * Limit serialisation stored data to the parameters
  256. */
  257. public function __sleep()
  258. {
  259. return array('_params');
  260. }
  261. /**
  262. * After serialisation, re-instantiate a HTTP utility class for use
  263. */
  264. public function __wakeup()
  265. {
  266. if ($this->_httpUtility === null) {
  267. $this->_httpUtility = new HTTPUtility;
  268. }
  269. }
  270. }