/vendor/zend/Zend/OAuth/Signature/AbstractSignature.php

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