8000 Refactor _sign method · balitax/codebird-php@486ad09 · GitHub
[go: up one dir, main page]

Skip to content

Commit 486ad09

Browse files
committed
Refactor _sign method
1 parent a5f52e7 commit 486ad09

File tree

1 file changed

+50
-24
lines changed

1 file changed

+50
-24
lines changed

src/codebird.php

Lines changed: 50 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1085,6 +1085,34 @@ protected function _nonce($length = 8)
10851085
}
10861086
return substr(md5(microtime(true)), 0, $length);
10871087
}
1088+
1089+
/**
1090+
* Signature helper
1091+
*
1092+
* @param string $httpmethod Usually either 'GET' or 'POST' or 'DELETE'
1093+
* @param string $method The API method to call
1094+
* @param array $base_params The signature base parameters
1095+
*
1096+
* @return string signature
1097+
*/
1098+
protected function _getSignature($httpmethod, $method, $base_params)
1099+
{
1100+
// convert params to string
1101+
$base_string = '';
1102+
foreach ($base_params as $key => $value) {
1103+
$base_string .= $key . '=' . $value . '&';
1104+
}
1105+
1106+
// trim last ampersand
1107+
$base_string = substr($base_string, 0, -1);
1108+
1109+
// hash it
1110+
return $this->_sha1(
1111+
$httpmethod . '&' .
1112+
$this->_url($method) . '&' .
1113+
$this->_url($base_string)
1114+
);
1115+
}
10881116

10891117
/**
10901118
* Generates an OAuth signature
@@ -1101,45 +1129,43 @@ protected function _sign($httpmethod, $method, $params = [], $append_to_get = fa
11011129
if (self::$_oauth_consumer_key === null) {
11021130
throw new \Exception('To generate a signature, the consumer key must be set.');
11031131
}
1104-
$sign_params = [
1105-
'consumer_key' => self::$_oauth_consumer_key,
1106-
'version' => '1.0',
1107-
'timestamp' => time(),
1108-
'nonce' => $this->_nonce(),
1109-
'signature_method' => 'HMAC-SHA1'
1110-
];
1111-
$sign_base_params = [];
1112-
foreach ($sign_params as $key => $value) {
1113-
$sign_base_params['oauth_' . $key] = $this->_url($value);
1114-
}
1132+
$sign_base_params = array_map(
1133+
[$this, '_url'],
1134+
[
1135+
'oauth_consumer_key' => self::$_oauth_consumer_key,
1136+
'oauth_version' => '1.0',
1137+
'oauth_timestamp' => time(),
1138+
'oauth_nonce' => $this->_nonce(),
1139+
'oauth_signature_method' => 'HMAC-SHA1'
1140+
]
1141+
);
11151142
if ($this->_oauth_token !== null) {
11161143
$sign_base_params['oauth_token'] = $this->_url($this->_oauth_token);
11171144
}
11181145
$oauth_params = $sign_base_params;
1119-
foreach ($params as $key => $value) {
1120-
$sign_base_params[$key] = $this->_url($value);
1121-
}
1146+
1147+
// merge in the non-OAuth params
1148+
$sign_base_params = array_merge(
1149+
$sign_base_params,
1150+
array_map([$this, '_url'], $params)
1151+
);
11221152
ksort($sign_base_params);
1123-
$sign_base_string = '';
1124-
foreach ($sign_base_params as $key => $value) {
1125-
$sign_base_string .= $key . '=' . $value . '&';
1126-
}
1127-
$sign_base_string = substr($sign_base_string, 0, -1);
1128-
$signature = $this->_sha1($httpmethod . '&' . $this->_url($method) . '&' . $this->_url($sign_base_string));
1153+
1154+
$signature = $this->_getSignature($httpmethod, $method, $sign_base_params);
11291155

11301156
$params = $append_to_get ? $sign_base_params : $oauth_params;
11311157
$params['oauth_signature'] = $signature;
1132-
$keys = $params;
1133-
ksort($keys);
1158+
1159+
ksort($params);
11341160
if ($append_to_get) {
11351161
$authorization = '';
1136-
foreach ($keys as $key => $value) {
1162+
foreach ($params as $key => $value) {
11371163
$authorization .= $key . '="' . $this->_url($value) . '", ';
11381164
}
11391165
return substr($authorization, 0, -1);
11401166
}
11411167
$authorization = 'OAuth ';
1142-
foreach ($keys as $key => $value) {
1168+
foreach ($params as $key => $value) {
11431169
$authorization .= $key . "=\"" . $this->_url($value) . "\", ";
11441170
}
11451171
return substr($authorization, 0, -2);

0 commit comments

Comments
 (0)
0