11 if (!class_exists(
'OAuthException')) {
20 if (!class_exists(
'OAuthConsumer')) {
25 function __construct(
$key,
$secret, $callback_url=NULL) {
28 $this->callback_url = $callback_url;
31 function __toString() {
32 return "OAuthConsumer[key=$this->key,secret=$this->secret]";
56 return "oauth_token=" .
58 "&oauth_token_secret=" .
60 "&oauth_callback_confirmed=true";
64 return $this->to_string();
77 abstract public function get_name();
89 abstract public function build_signature($request,
$consumer, $token);
100 $built = $this->build_signature($request,
$consumer, $token);
103 if (strlen($built) == 0 || strlen($signature) == 0) {
107 if (strlen($built) != strlen($signature)) {
113 for (
$i = 0;
$i < strlen($signature);
$i++) {
134 $base_string = $request->get_signature_base_string();
135 $request->base_string = $base_string;
139 ($token) ? $token->secret :
"" 143 $key = implode(
'&', $key_parts);
145 return base64_encode(hash_hmac(
'sha1', $base_string,
$key,
true));
171 ($token) ? $token->secret :
"" 175 $key = implode(
'&', $key_parts);
176 $request->base_string =
$key;
201 protected abstract function fetch_public_cert(&$request);
207 protected abstract function fetch_private_cert(&$request);
210 $base_string = $request->get_signature_base_string();
211 $request->base_string = $base_string;
214 $cert = $this->fetch_private_cert($request);
217 $privatekeyid = openssl_get_privatekey($cert);
220 $ok = openssl_sign($base_string, $signature, $privatekeyid);
223 openssl_free_key($privatekeyid);
225 return base64_encode($signature);
229 $decoded_sig = base64_decode($signature);
231 $base_string = $request->get_signature_base_string();
234 $cert = $this->fetch_public_cert($request);
237 $publickeyid = openssl_get_publickey($cert);
240 $ok = openssl_verify($base_string, $decoded_sig, $publickeyid);
243 openssl_free_key($publickeyid);
256 public static $POST_INPUT =
'php://input';
259 $parameters = ($parameters) ? $parameters :
array();
261 $this->parameters = $parameters;
262 $this->http_method = $http_method;
263 $this->http_url = $http_url;
270 public static function from_request($http_method=NULL, $http_url=NULL, $parameters=NULL) {
274 $http_url = ($http_url) ? $http_url : $scheme .
279 $http_method = ($http_method) ? $http_method :
$_SERVER[
'REQUEST_METHOD'];
294 if ($http_method ==
"POST" 295 && isset($request_headers[
'Content-Type'])
296 && strstr($request_headers[
'Content-Type'],
297 'application/x-www-form-urlencoded')
300 file_get_contents(self::$POST_INPUT)
302 $parameters = array_merge($parameters, $post_data);
307 if (isset($request_headers[
'Authorization']) && substr($request_headers[
'Authorization'], 0, 6) ==
'OAuth ') {
309 $request_headers[
'Authorization']
311 $parameters = array_merge($parameters, $header_parameters);
316 return new OAuthRequest($http_method, $http_url, $parameters);
323 $parameters = ($parameters) ? $parameters :
array();
329 $defaults[
'oauth_token'] = $token->key;
331 $parameters = array_merge($defaults, $parameters);
333 return new OAuthRequest($http_method, $http_url, $parameters);
337 if ($allow_duplicates && isset($this->parameters[
$name])) {
339 if (is_scalar($this->parameters[$name])) {
342 $this->parameters[
$name] =
array($this->parameters[$name]);
345 $this->parameters[
$name][] = $value;
347 $this->parameters[
$name] = $value;
352 return isset($this->parameters[
$name]) ? $this->parameters[
$name] : null;
356 return $this->parameters;
360 unset($this->parameters[
$name]);
373 if (isset(
$params[
'oauth_signature'])) {
374 unset(
$params[
'oauth_signature']);
389 $this->get_normalized_http_method(),
390 $this->get_normalized_http_url(),
391 $this->get_signable_parameters()
396 return implode(
'&', $parts);
403 return strtoupper($this->http_method);
411 $parts = parse_url($this->http_url);
413 $scheme = (isset($parts[
'scheme'])) ? $parts[
'scheme'] :
'http';
414 $port = (isset($parts[
'port'])) ? $parts[
'port'] : (($scheme ==
'https') ?
'443' :
'80');
415 $host = (isset($parts[
'host'])) ? strtolower($parts[
'host']) :
'';
416 $path = (isset($parts[
'path'])) ? $parts[
'path'] :
'';
418 if (($scheme ==
'https' && $port !=
'443')
419 || ($scheme ==
'http' && $port !=
'80')) {
420 $host =
"$host:$port";
422 return "$scheme://$host$path";
429 $post_data = $this->to_postdata();
430 $out = $this->get_normalized_http_url();
432 $out .=
'?'.$post_data;
453 $out =
'Authorization: OAuth';
456 foreach ($this->parameters as $k => $v) {
457 if (substr($k, 0, 5) !=
"oauth")
continue;
459 throw new OAuthException(
'Arrays not supported in headers');
461 $out .= ($first) ?
' ' :
',';
472 return $this->to_url();
477 $this->set_parameter(
478 "oauth_signature_method",
479 $signature_method->get_name(),
482 $signature = $this->build_signature($signature_method,
$consumer, $token);
483 $this->set_parameter(
"oauth_signature", $signature,
false);
487 $signature = $signature_method->build_signature($this,
$consumer, $token);
505 return md5($mt . $rand);
510 protected $timestamp_threshold = 300;
512 protected $signature_methods =
array();
517 $this->data_store = $data_store;
521 $this->signature_methods[$signature_method->get_name()] =
532 $this->get_version($request);
534 $consumer = $this->get_consumer($request);
539 $this->check_signature($request,
$consumer, $token);
542 $callback = $request->get_parameter(
'oauth_callback');
543 $new_token = $this->data_store->new_request_token(
$consumer, $callback);
553 $this->get_version($request);
555 $consumer = $this->get_consumer($request);
558 $token = $this->get_token($request,
$consumer,
"request");
560 $this->check_signature($request,
$consumer, $token);
563 $verifier = $request->get_parameter(
'oauth_verifier');
564 $new_token = $this->data_store->new_access_token($token,
$consumer, $verifier);
573 $this->get_version($request);
574 $consumer = $this->get_consumer($request);
575 $token = $this->get_token($request,
$consumer,
"access");
576 $this->check_signature($request,
$consumer, $token);
585 $version = $request->get_parameter(
"oauth_version");
592 throw new OAuthException(
"OAuth version '$version' not supported");
605 if (!$signature_method) {
608 throw new OAuthException(
'No signature method parameter. This parameter is required');
611 if (!in_array($signature_method,
612 array_keys($this->signature_methods))) {
613 throw new OAuthException(
614 "Signature method '$signature_method' not supported " .
615 "try one of the following: " .
616 implode(
", ", array_keys($this->signature_methods))
619 return $this->signature_methods[$signature_method];
630 if (!$consumer_key) {
631 throw new OAuthException(
"Invalid consumer key");
634 $consumer = $this->data_store->lookup_consumer($consumer_key);
636 throw new OAuthException(
"Invalid consumer");
650 if (!empty($token_field)) {
651 $token = $this->data_store->lookup_token(
655 throw new OAuthException(
"Invalid $token_type token: $token_field");
680 $signature_method = $this->get_signature_method($request);
682 $signature = $request->get_parameter(
'oauth_signature');
683 $valid_sig = $signature_method->check_signature(
691 throw new OAuthException(
"Invalid signature");
700 throw new OAuthException(
701 'Missing timestamp parameter. The parameter is required' 706 if (abs($now -
$timestamp) > $this->timestamp_threshold) {
707 throw new OAuthException(
708 "Expired timestamp, yours $timestamp, ours $now" 718 throw new OAuthException(
719 'Missing nonce parameter. The parameter is required' 723 $found = $this->data_store->lookup_nonce(
730 throw new OAuthException(
"Nonce already used: $nonce");
764 if (is_array($input)) {
765 return array_map(
array(
'OAuthUtil',
'urlencode_rfc3986'), $input);
766 }
else if (is_scalar($input)) {
770 str_replace(
'%7E',
'~', rawurlencode($input))
782 return urldecode($string);
792 if (preg_match_all(
'/('.($only_allow_oauth_parameters ?
'oauth_' :
'').
'[a-z_-]*)=(:?"([^"]*)"|([^,]*))/',
$header, $matches)) {
793 foreach ($matches[1] as
$i =>
$h) {
805 if (function_exists(
'apache_request_headers')) {
808 $headers = apache_request_headers();
815 foreach ($headers AS
$key => $value) {
819 ucwords(strtolower(str_replace(
"-",
" ",
$key)))
827 if( isset(
$_SERVER[
'CONTENT_TYPE']) )
829 if( isset($_ENV[
'CONTENT_TYPE']) )
830 $out[
'Content-Type'] = $_ENV[
'CONTENT_TYPE'];
833 if (substr(
$key, 0, 5) ==
"HTTP_") {
840 ucwords(strtolower(str_replace(
"_",
" ", substr(
$key, 5))))
846 if (isset(
$out[
'Auth'])) {
847 $out[
'Authorization'] =
$out[
'Auth'];
857 if (!isset($input) || !$input)
return array();
859 $pairs = explode(
'&', $input);
861 $parsed_parameters =
array();
862 foreach ($pairs as $pair) {
863 $split = explode(
'=', $pair, 2);
867 if (isset($parsed_parameters[$parameter])) {
871 if (is_scalar($parsed_parameters[$parameter])) {
874 $parsed_parameters[$parameter] =
array($parsed_parameters[$parameter]);
877 $parsed_parameters[$parameter][] = $value;
879 $parsed_parameters[$parameter] = $value;
882 return $parsed_parameters;
898 foreach (
$params as $parameter => $value) {
899 if (is_array($value)) {
903 sort($value, SORT_STRING);
904 foreach ($value as $duplicate_value) {
905 $pairs[] = $parameter .
'=' . $duplicate_value;
908 $pairs[] = $parameter .
'=' . $value;
913 return implode(
'&', $pairs);
to_string()
generates the basic string serialization of a token that a server would respond to request_token and ...
new_access_token($token, $consumer, $verifier=null)
check_timestamp($timestamp)
check that the timestamp is new enough
A class for implementing a Signature Method See section 9 ("Signing Requests") in the spec...
__construct($http_method, $http_url, $parameters=NULL)
if((!isset($_SERVER['DOCUMENT_ROOT'])) OR(empty($_SERVER['DOCUMENT_ROOT']))) $_SERVER['DOCUMENT_ROOT']
static generate_timestamp()
util function: current timestamp
check_signature($request, $consumer, $token, $signature)
set_parameter($name, $value, $allow_duplicates=true)
static urlencode_rfc3986($input)
OAuth PECL extension includes an OAuth Exception class, so we need to wrap the definition of this cla...
build_signature($request, $consumer, $token)
oauth_signature is set to the concatenated encoded values of the Consumer Secret and Token Secret...
check_nonce($consumer, $token, $nonce, $timestamp)
check that the nonce is not repeated
static urldecode_rfc3986($string)
lookup_nonce($consumer, $token, $nonce, $timestamp)
get_normalized_http_method()
just uppercases the http method
static generate_nonce()
util function: current nonce
static parse_parameters( $input)
add_signature_method($signature_method)
get_signable_parameters()
The request parameters, sorted and concatenated into a normalized string.
check_signature($request, $consumer, $token)
all-in-one function to check the signature on a request should guess the signature method appropriate...
check_signature($request, $consumer, $token, $signature)
Verifies that a given signature is correct.
get_version(&$request)
version 1
static from_consumer_and_token($consumer, $token, $http_method, $http_url, $parameters=NULL)
pretty much a helper function to set up the request
verify_request(&$request)
verify an api call, checks all the parameters
static build_http_query($params)
lookup_consumer($consumer_key)
fetch_request_token(&$request)
process a request_token request returns the request token on success
lookup_token($consumer, $token_type, $token)
to_url()
builds a url usable for a GET request
The PLAINTEXT method does not provide any security protection and SHOULD only be used over a secure c...
Create styles array
The data for the language used.
get_normalized_http_url()
parses the url and rebuilds it to be scheme://host/path
static from_request($http_method=NULL, $http_url=NULL, $parameters=NULL)
attempt to build up a request from what was passed to the server
foreach($mandatory_scripts as $file) $timestamp
build_signature($request, $consumer, $token)
get_signature_method($request)
figure out the signature with some defaults
build_signature($request, $consumer, $token)
The RSA-SHA1 signature method uses the RSASSA-PKCS1-v1_5 signature algorithm as defined in [RFC3447] ...
new_request_token($consumer, $callback=null)
get_token($request, $consumer, $token_type="access")
try to find the token for the provided request's token key
get_consumer($request)
try to find the consumer for the provided request's consumer key
sign_request($signature_method, $consumer, $token)
The HMAC-SHA1 signature method uses the HMAC-SHA1 signature algorithm as defined in [RFC2104] where t...
get_signature_base_string()
Returns the base string of this request.
Add data(end) time
Method that wraps PHPs time in order to allow simulations with the workflow.
__construct($key, $secret)
key = the token secret = the token secret
to_postdata()
builds the data one would send in a POST request
build_signature($signature_method, $consumer, $token)
fetch_access_token(&$request)
process an access_token request returns the access token on success
static split_header($header, $only_allow_oauth_parameters=true)
to_header($realm=null)
builds the Authorization: header