SignatureAbstract.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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-2015 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. /** Zend_Oauth_Http_Utility */
  22. #require_once 'Zend/Oauth/Http/Utility.php';
  23. /** Zend_Uri_Http */
  24. #require_once 'Zend/Uri/Http.php';
  25. /**
  26. * @category Zend
  27. * @package Zend_Oauth
  28. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  29. * @license http://framework.zend.com/license/new-bsd New BSD License
  30. */
  31. abstract class Zend_Oauth_Signature_SignatureAbstract
  32. {
  33. /**
  34. * Hash algorithm to use when generating signature
  35. * @var string
  36. */
  37. protected $_hashAlgorithm = null;
  38. /**
  39. * Key to use when signing
  40. * @var string
  41. */
  42. protected $_key = null;
  43. /**
  44. * Consumer secret
  45. * @var string
  46. */
  47. protected $_consumerSecret = null;
  48. /**
  49. * Token secret
  50. * @var string
  51. */
  52. protected $_tokenSecret = '';
  53. /**
  54. * Constructor
  55. *
  56. * @param string $consumerSecret
  57. * @param null|string $tokenSecret
  58. * @param null|string $hashAlgo
  59. * @return void
  60. */
  61. public function __construct($consumerSecret, $tokenSecret = null, $hashAlgo = null)
  62. {
  63. $this->_consumerSecret = $consumerSecret;
  64. if (isset($tokenSecret)) {
  65. $this->_tokenSecret = $tokenSecret;
  66. }
  67. $this->_key = $this->_assembleKey();
  68. if (isset($hashAlgo)) {
  69. $this->_hashAlgorithm = $hashAlgo;
  70. }
  71. }
  72. /**
  73. * Sign a request
  74. *
  75. * @param array $params
  76. * @param null|string $method
  77. * @param null|string $url
  78. * @return string
  79. */
  80. public abstract function sign(array $params, $method = null, $url = null);
  81. /**
  82. * Normalize the base signature URL
  83. *
  84. * @param string $url
  85. * @return string
  86. */
  87. public function normaliseBaseSignatureUrl($url)
  88. {
  89. $uri = Zend_Uri_Http::fromString($url);
  90. if ($uri->getScheme() == 'http' && $uri->getPort() == '80') {
  91. $uri->setPort('');
  92. } elseif ($uri->getScheme() == 'https' && $uri->getPort() == '443') {
  93. $uri->setPort('');
  94. }
  95. $uri->setQuery('');
  96. $uri->setFragment('');
  97. $uri->setHost(strtolower($uri->getHost()));
  98. return $uri->getUri(true);
  99. }
  100. /**
  101. * Assemble key from consumer and token secrets
  102. *
  103. * @return string
  104. */
  105. protected function _assembleKey()
  106. {
  107. $parts = array($this->_consumerSecret);
  108. if ($this->_tokenSecret !== null) {
  109. $parts[] = $this->_tokenSecret;
  110. }
  111. foreach ($parts as $key => $secret) {
  112. $parts[$key] = Zend_Oauth_Http_Utility::urlEncode($secret);
  113. }
  114. return implode('&', $parts);
  115. }
  116. /**
  117. * Get base signature string
  118. *
  119. * @param array $params
  120. * @param null|string $method
  121. * @param null|string $url
  122. * @return string
  123. */
  124. protected function _getBaseSignatureString(array $params, $method = null, $url = null)
  125. {
  126. $encodedParams = array();
  127. foreach ($params as $key => $value) {
  128. $encodedParams[Zend_Oauth_Http_Utility::urlEncode($key)] =
  129. Zend_Oauth_Http_Utility::urlEncode($value);
  130. }
  131. $baseStrings = array();
  132. if (isset($method)) {
  133. $baseStrings[] = strtoupper($method);
  134. }
  135. if (isset($url)) {
  136. // should normalise later
  137. $baseStrings[] = Zend_Oauth_Http_Utility::urlEncode(
  138. $this->normaliseBaseSignatureUrl($url)
  139. );
  140. }
  141. if (isset($encodedParams['oauth_signature'])) {
  142. unset($encodedParams['oauth_signature']);
  143. }
  144. $baseStrings[] = Zend_Oauth_Http_Utility::urlEncode(
  145. $this->_toByteValueOrderedQueryString($encodedParams)
  146. );
  147. return implode('&', $baseStrings);
  148. }
  149. /**
  150. * Transform an array to a byte value ordered query string
  151. *
  152. * @param array $params
  153. * @return string
  154. */
  155. protected function _toByteValueOrderedQueryString(array $params)
  156. {
  157. $return = array();
  158. uksort($params, 'strnatcmp');
  159. foreach ($params as $key => $value) {
  160. if (is_array($value)) {
  161. natsort($value);
  162. foreach ($value as $keyduplicate) {
  163. $return[] = $key . '=' . $keyduplicate;
  164. }
  165. } else {
  166. $return[] = $key . '=' . $value;
  167. }
  168. }
  169. return implode('&', $return);
  170. }
  171. }