Refactoring
This commit is contained in:
parent
2feec37a98
commit
26a92abbdc
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
namespace Eher\OAuth;
|
namespace Eher\OAuth;
|
||||||
|
|
||||||
class OAuthConsumer {
|
class Consumer {
|
||||||
public $key;
|
public $key;
|
||||||
public $secret;
|
public $secret;
|
||||||
|
|
@ -23,7 +23,7 @@ class HmacSha1 extends SignatureMethod {
|
|||||||
($token) ? $token->secret : ""
|
($token) ? $token->secret : ""
|
||||||
);
|
);
|
||||||
|
|
||||||
$key_parts = OAuthUtil::urlencode_rfc3986($key_parts);
|
$key_parts = Util::urlencode_rfc3986($key_parts);
|
||||||
$key = implode('&', $key_parts);
|
$key = implode('&', $key_parts);
|
||||||
|
|
||||||
return base64_encode(hash_hmac('sha1', $base_string, $key, true));
|
return base64_encode(hash_hmac('sha1', $base_string, $key, true));
|
||||||
|
@ -27,7 +27,7 @@ class PlainText extends SignatureMethod {
|
|||||||
($token) ? $token->secret : ""
|
($token) ? $token->secret : ""
|
||||||
);
|
);
|
||||||
|
|
||||||
$key_parts = OAuthUtil::urlencode_rfc3986($key_parts);
|
$key_parts = Util::urlencode_rfc3986($key_parts);
|
||||||
$key = implode('&', $key_parts);
|
$key = implode('&', $key_parts);
|
||||||
$request->base_string = $key;
|
$request->base_string = $key;
|
||||||
|
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
namespace Eher\OAuth;
|
namespace Eher\OAuth;
|
||||||
|
|
||||||
class OAuthRequest {
|
class Request {
|
||||||
protected $parameters;
|
protected $parameters;
|
||||||
protected $http_method;
|
protected $http_method;
|
||||||
protected $http_url;
|
protected $http_url;
|
||||||
@ -13,7 +13,7 @@ class OAuthRequest {
|
|||||||
|
|
||||||
function __construct($http_method, $http_url, $parameters=NULL) {
|
function __construct($http_method, $http_url, $parameters=NULL) {
|
||||||
$parameters = ($parameters) ? $parameters : array();
|
$parameters = ($parameters) ? $parameters : array();
|
||||||
$parameters = array_merge( OAuthUtil::parse_parameters(parse_url($http_url, PHP_URL_QUERY)), $parameters);
|
$parameters = array_merge( Util::parse_parameters(parse_url($http_url, PHP_URL_QUERY)), $parameters);
|
||||||
$this->parameters = $parameters;
|
$this->parameters = $parameters;
|
||||||
$this->http_method = $http_method;
|
$this->http_method = $http_method;
|
||||||
$this->http_url = $http_url;
|
$this->http_url = $http_url;
|
||||||
@ -40,10 +40,10 @@ class OAuthRequest {
|
|||||||
// parsed parameter-list
|
// parsed parameter-list
|
||||||
if (!$parameters) {
|
if (!$parameters) {
|
||||||
// Find request headers
|
// Find request headers
|
||||||
$request_headers = OAuthUtil::get_headers();
|
$request_headers = Util::get_headers();
|
||||||
|
|
||||||
// Parse the query-string to find GET parameters
|
// Parse the query-string to find GET parameters
|
||||||
$parameters = OAuthUtil::parse_parameters($_SERVER['QUERY_STRING']);
|
$parameters = Util::parse_parameters($_SERVER['QUERY_STRING']);
|
||||||
|
|
||||||
// It's a POST request of the proper content-type, so parse POST
|
// It's a POST request of the proper content-type, so parse POST
|
||||||
// parameters and add those overriding any duplicates from GET
|
// parameters and add those overriding any duplicates from GET
|
||||||
@ -52,7 +52,7 @@ class OAuthRequest {
|
|||||||
&& strstr($request_headers['Content-Type'],
|
&& strstr($request_headers['Content-Type'],
|
||||||
'application/x-www-form-urlencoded')
|
'application/x-www-form-urlencoded')
|
||||||
) {
|
) {
|
||||||
$post_data = OAuthUtil::parse_parameters(
|
$post_data = Util::parse_parameters(
|
||||||
file_get_contents(self::$POST_INPUT)
|
file_get_contents(self::$POST_INPUT)
|
||||||
);
|
);
|
||||||
$parameters = array_merge($parameters, $post_data);
|
$parameters = array_merge($parameters, $post_data);
|
||||||
@ -61,7 +61,7 @@ class OAuthRequest {
|
|||||||
// We have a Authorization-header with OAuth data. Parse the header
|
// We have a Authorization-header with OAuth data. Parse the header
|
||||||
// and add those overriding any duplicates from GET or POST
|
// and add those overriding any duplicates from GET or POST
|
||||||
if (isset($request_headers['Authorization']) && substr($request_headers['Authorization'], 0, 6) == 'OAuth ') {
|
if (isset($request_headers['Authorization']) && substr($request_headers['Authorization'], 0, 6) == 'OAuth ') {
|
||||||
$header_parameters = OAuthUtil::split_header(
|
$header_parameters = Util::split_header(
|
||||||
$request_headers['Authorization']
|
$request_headers['Authorization']
|
||||||
);
|
);
|
||||||
$parameters = array_merge($parameters, $header_parameters);
|
$parameters = array_merge($parameters, $header_parameters);
|
||||||
@ -69,7 +69,7 @@ class OAuthRequest {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return new OAuthRequest($http_method, $http_url, $parameters);
|
return new Request($http_method, $http_url, $parameters);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -77,16 +77,16 @@ class OAuthRequest {
|
|||||||
*/
|
*/
|
||||||
public static function from_consumer_and_token($consumer, $token, $http_method, $http_url, $parameters=NULL) {
|
public static function from_consumer_and_token($consumer, $token, $http_method, $http_url, $parameters=NULL) {
|
||||||
$parameters = ($parameters) ? $parameters : array();
|
$parameters = ($parameters) ? $parameters : array();
|
||||||
$defaults = array("oauth_version" => OAuthRequest::$version,
|
$defaults = array("oauth_version" => Request::$version,
|
||||||
"oauth_nonce" => OAuthRequest::generate_nonce(),
|
"oauth_nonce" => Request::generate_nonce(),
|
||||||
"oauth_timestamp" => OAuthRequest::generate_timestamp(),
|
"oauth_timestamp" => Request::generate_timestamp(),
|
||||||
"oauth_consumer_key" => $consumer->key);
|
"oauth_consumer_key" => $consumer->key);
|
||||||
if ($token)
|
if ($token)
|
||||||
$defaults['oauth_token'] = $token->key;
|
$defaults['oauth_token'] = $token->key;
|
||||||
|
|
||||||
$parameters = array_merge($defaults, $parameters);
|
$parameters = array_merge($defaults, $parameters);
|
||||||
|
|
||||||
return new OAuthRequest($http_method, $http_url, $parameters);
|
return new Request($http_method, $http_url, $parameters);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function set_parameter($name, $value, $allow_duplicates = true) {
|
public function set_parameter($name, $value, $allow_duplicates = true) {
|
||||||
@ -130,7 +130,7 @@ class OAuthRequest {
|
|||||||
unset($params['oauth_signature']);
|
unset($params['oauth_signature']);
|
||||||
}
|
}
|
||||||
|
|
||||||
return OAuthUtil::build_http_query($params);
|
return Util::build_http_query($params);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -147,7 +147,7 @@ class OAuthRequest {
|
|||||||
$this->get_signable_parameters()
|
$this->get_signable_parameters()
|
||||||
);
|
);
|
||||||
|
|
||||||
$parts = OAuthUtil::urlencode_rfc3986($parts);
|
$parts = Util::urlencode_rfc3986($parts);
|
||||||
|
|
||||||
return implode('&', $parts);
|
return implode('&', $parts);
|
||||||
}
|
}
|
||||||
@ -194,7 +194,7 @@ class OAuthRequest {
|
|||||||
* builds the data one would send in a POST request
|
* builds the data one would send in a POST request
|
||||||
*/
|
*/
|
||||||
public function to_postdata() {
|
public function to_postdata() {
|
||||||
return OAuthUtil::build_http_query($this->parameters);
|
return Util::build_http_query($this->parameters);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -203,7 +203,7 @@ class OAuthRequest {
|
|||||||
public function to_header($realm=null) {
|
public function to_header($realm=null) {
|
||||||
$first = true;
|
$first = true;
|
||||||
if($realm) {
|
if($realm) {
|
||||||
$out = 'Authorization: OAuth realm="' . OAuthUtil::urlencode_rfc3986($realm) . '"';
|
$out = 'Authorization: OAuth realm="' . Util::urlencode_rfc3986($realm) . '"';
|
||||||
$first = false;
|
$first = false;
|
||||||
} else
|
} else
|
||||||
$out = 'Authorization: OAuth';
|
$out = 'Authorization: OAuth';
|
||||||
@ -215,9 +215,9 @@ class OAuthRequest {
|
|||||||
throw new OAuthException('Arrays not supported in headers');
|
throw new OAuthException('Arrays not supported in headers');
|
||||||
}
|
}
|
||||||
$out .= ($first) ? ' ' : ',';
|
$out .= ($first) ? ' ' : ',';
|
||||||
$out .= OAuthUtil::urlencode_rfc3986($k) .
|
$out .= Util::urlencode_rfc3986($k) .
|
||||||
'="' .
|
'="' .
|
||||||
OAuthUtil::urlencode_rfc3986($v) .
|
Util::urlencode_rfc3986($v) .
|
||||||
'"';
|
'"';
|
||||||
$first = false;
|
$first = false;
|
||||||
}
|
}
|
@ -1,8 +1,8 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace OAuth;
|
namespace Eher\OAuth;
|
||||||
|
|
||||||
class OAuthToken {
|
class Token {
|
||||||
// access tokens and request tokens
|
// access tokens and request tokens
|
||||||
public $key;
|
public $key;
|
||||||
public $secret;
|
public $secret;
|
||||||
@ -22,9 +22,9 @@ class OAuthToken {
|
|||||||
*/
|
*/
|
||||||
function to_string() {
|
function to_string() {
|
||||||
return "oauth_token=" .
|
return "oauth_token=" .
|
||||||
OAuthUtil::urlencode_rfc3986($this->key) .
|
Util::urlencode_rfc3986($this->key) .
|
||||||
"&oauth_token_secret=" .
|
"&oauth_token_secret=" .
|
||||||
OAuthUtil::urlencode_rfc3986($this->secret);
|
Util::urlencode_rfc3986($this->secret);
|
||||||
}
|
}
|
||||||
|
|
||||||
function __toString() {
|
function __toString() {
|
@ -1,11 +1,11 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace OAuth;
|
namespace Eher\OAuth;
|
||||||
|
|
||||||
class OAuthUtil {
|
class Util {
|
||||||
public static function urlencode_rfc3986($input) {
|
public static function urlencode_rfc3986($input) {
|
||||||
if (is_array($input)) {
|
if (is_array($input)) {
|
||||||
return array_map(array('app\models\oauth\OAuthUtil', 'urlencode_rfc3986'), $input);
|
return array_map(array('Eher\OAuth\Util', 'urlencode_rfc3986'), $input);
|
||||||
} else if (is_scalar($input)) {
|
} else if (is_scalar($input)) {
|
||||||
return str_replace(
|
return str_replace(
|
||||||
'+',
|
'+',
|
||||||
@ -34,7 +34,7 @@ class OAuthUtil {
|
|||||||
$params = array();
|
$params = array();
|
||||||
if (preg_match_all('/('.($only_allow_oauth_parameters ? 'oauth_' : '').'[a-z_-]*)=(:?"([^"]*)"|([^,]*))/', $header, $matches)) {
|
if (preg_match_all('/('.($only_allow_oauth_parameters ? 'oauth_' : '').'[a-z_-]*)=(:?"([^"]*)"|([^,]*))/', $header, $matches)) {
|
||||||
foreach ($matches[1] as $i => $h) {
|
foreach ($matches[1] as $i => $h) {
|
||||||
$params[$h] = OAuthUtil::urldecode_rfc3986(empty($matches[3][$i]) ? $matches[4][$i] : $matches[3][$i]);
|
$params[$h] = Util::urldecode_rfc3986(empty($matches[3][$i]) ? $matches[4][$i] : $matches[3][$i]);
|
||||||
}
|
}
|
||||||
if (isset($params['realm'])) {
|
if (isset($params['realm'])) {
|
||||||
unset($params['realm']);
|
unset($params['realm']);
|
||||||
@ -100,8 +100,8 @@ class OAuthUtil {
|
|||||||
$parsed_parameters = array();
|
$parsed_parameters = array();
|
||||||
foreach ($pairs as $pair) {
|
foreach ($pairs as $pair) {
|
||||||
$split = explode('=', $pair, 2);
|
$split = explode('=', $pair, 2);
|
||||||
$parameter = OAuthUtil::urldecode_rfc3986($split[0]);
|
$parameter = Util::urldecode_rfc3986($split[0]);
|
||||||
$value = isset($split[1]) ? OAuthUtil::urldecode_rfc3986($split[1]) : '';
|
$value = isset($split[1]) ? Util::urldecode_rfc3986($split[1]) : '';
|
||||||
|
|
||||||
if (isset($parsed_parameters[$parameter])) {
|
if (isset($parsed_parameters[$parameter])) {
|
||||||
// We have already recieved parameter(s) with this name, so add to the list
|
// We have already recieved parameter(s) with this name, so add to the list
|
||||||
@ -125,8 +125,8 @@ class OAuthUtil {
|
|||||||
if (!$params) return '';
|
if (!$params) return '';
|
||||||
|
|
||||||
// Urlencode both keys and values
|
// Urlencode both keys and values
|
||||||
$keys = OAuthUtil::urlencode_rfc3986(array_keys($params));
|
$keys = Util::urlencode_rfc3986(array_keys($params));
|
||||||
$values = OAuthUtil::urlencode_rfc3986(array_values($params));
|
$values = Util::urlencode_rfc3986(array_values($params));
|
||||||
$params = array_combine($keys, $values);
|
$params = array_combine($keys, $values);
|
||||||
|
|
||||||
// Parameters are sorted by name, using lexicographical byte value ordering.
|
// Parameters are sorted by name, using lexicographical byte value ordering.
|
Loading…
Reference in New Issue
Block a user