1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
|
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_Oauth
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @version $Id: SignatureAbstract.php 20217 2010-01-12 16:01:57Z matthew $
*/
/** Zend_Oauth_Http_Utility */
require_once 'Zend/Oauth/Http/Utility.php';
/** Zend_Uri_Http */
require_once 'Zend/Uri/Http.php';
/**
* @category Zend
* @package Zend_Oauth
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
abstract class Zend_Oauth_Signature_SignatureAbstract
{
/**
* Hash algorithm to use when generating signature
* @var string
*/
protected $_hashAlgorithm = null;
/**
* Key to use when signing
* @var string
*/
protected $_key = null;
/**
* Consumer secret
* @var string
*/
protected $_consumerSecret = null;
/**
* Token secret
* @var string
*/
protected $_tokenSecret = '';
/**
* Constructor
*
* @param string $consumerSecret
* @param null|string $tokenSecret
* @param null|string $hashAlgo
* @return void
*/
public function __construct($consumerSecret, $tokenSecret = null, $hashAlgo = null)
{
$this->_consumerSecret = $consumerSecret;
if (isset($tokenSecret)) {
$this->_tokenSecret = $tokenSecret;
}
$this->_key = $this->_assembleKey();
if (isset($hashAlgo)) {
$this->_hashAlgorithm = $hashAlgo;
}
}
/**
* Sign a request
*
* @param array $params
* @param null|string $method
* @param null|string $url
* @return string
*/
public abstract function sign(array $params, $method = null, $url = null);
/**
* Normalize the base signature URL
*
* @param string $url
* @return string
*/
public function normaliseBaseSignatureUrl($url)
{
$uri = Zend_Uri_Http::fromString($url);
if ($uri->getScheme() == 'http' && $uri->getPort() == '80') {
$uri->setPort('');
} elseif ($uri->getScheme() == 'https' && $uri->getPort() == '443') {
$uri->setPort('');
}
$uri->setQuery('');
$uri->setFragment('');
$uri->setHost(strtolower($uri->getHost()));
return $uri->getUri(true);
}
/**
* Assemble key from consumer and token secrets
*
* @return string
*/
protected function _assembleKey()
{
$parts = array($this->_consumerSecret);
if (!is_null($this->_tokenSecret)) {
$parts[] = $this->_tokenSecret;
}
foreach ($parts as $key => $secret) {
$parts[$key] = Zend_Oauth_Http_Utility::urlEncode($secret);
}
return implode('&', $parts);
}
/**
* Get base signature string
*
* @param array $params
* @param null|string $method
* @param null|string $url
* @return string
*/
protected function _getBaseSignatureString(array $params, $method = null, $url = null)
{
$encodedParams = array();
foreach ($params as $key => $value) {
$encodedParams[Zend_Oauth_Http_Utility::urlEncode($key)] =
Zend_Oauth_Http_Utility::urlEncode($value);
}
$baseStrings = array();
if (isset($method)) {
$baseStrings[] = strtoupper($method);
}
if (isset($url)) {
// should normalise later
$baseStrings[] = Zend_Oauth_Http_Utility::urlEncode(
$this->normaliseBaseSignatureUrl($url)
);
}
if (isset($encodedParams['oauth_signature'])) {
unset($encodedParams['oauth_signature']);
}
$baseStrings[] = Zend_Oauth_Http_Utility::urlEncode(
$this->_toByteValueOrderedQueryString($encodedParams)
);
return implode('&', $baseStrings);
}
/**
* Transform an array to a byte value ordered query string
*
* @param array $params
* @return string
*/
protected function _toByteValueOrderedQueryString(array $params)
{
$return = array();
uksort($params, 'strnatcmp');
foreach ($params as $key => $value) {
if (is_array($value)) {
natsort($value);
foreach ($value as $keyduplicate) {
$return[] = $key . '=' . $keyduplicate;
}
} else {
$return[] = $key . '=' . $value;
}
}
return implode('&', $return);
}
}
|