11 if (!class_exists(
'OAuthException')) {
21 if (!class_exists(
'OAuthConsumer')) {
28 public function __construct(
$key, $secret, $callback_url = null)
31 $this->secret = $secret;
32 $this->callback_url = $callback_url;
35 public function __toString()
37 return "OAuthConsumer[key=$this->key,secret=$this->secret]";
55 $this->secret = $secret;
64 return "oauth_token=" .
66 "&oauth_token_secret=" .
68 "&oauth_callback_confirmed=true";
73 return $this->to_string();
87 abstract public function get_name();
99 abstract public function build_signature(
$request, $consumer,
$token);
114 if (strlen($built) == 0 || strlen($signature) == 0) {
118 if (strlen($built) != strlen($signature)) {
124 for (
$i = 0;
$i < strlen($signature);
$i++) {
148 $base_string =
$request->get_signature_base_string();
149 $request->base_string = $base_string;
157 $key = implode(
'&', $key_parts);
159 return base64_encode(hash_hmac(
'sha1', $base_string,
$key,
true));
192 $key = implode(
'&', $key_parts);
220 abstract protected function fetch_public_cert(&
$request);
226 abstract protected function fetch_private_cert(&
$request);
230 $base_string =
$request->get_signature_base_string();
231 $request->base_string = $base_string;
234 $cert = $this->fetch_private_cert(
$request);
237 $privatekeyid = openssl_get_privatekey($cert);
240 openssl_sign($base_string, $signature, $privatekeyid);
243 openssl_free_key($privatekeyid);
245 return base64_encode($signature);
250 $decoded_sig = base64_decode($signature);
252 $base_string =
$request->get_signature_base_string();
255 $cert = $this->fetch_public_cert(
$request);
258 $publickeyid = openssl_get_publickey($cert);
261 $ok = openssl_verify($base_string, $decoded_sig, $publickeyid);
264 openssl_free_key($publickeyid);
278 public static $POST_INPUT =
'php://input';
280 public function __construct($http_method, $http_url, $parameters = null)
282 $parameters = ($parameters) ? $parameters : array();
284 $this->parameters = $parameters;
285 $this->http_method = $http_method;
286 $this->http_url = $http_url;
293 public static function from_request($http_method = null, $http_url = null, $parameters = null)
298 $http_url = ($http_url) ? $http_url : $scheme .
303 $http_method = ($http_method) ? $http_method :
$_SERVER[
'REQUEST_METHOD'];
318 if ($http_method ==
"POST" 319 && isset($request_headers[
'Content-Type'])
320 && strstr($request_headers[
'Content-Type'],
321 'application/x-www-form-urlencoded')
324 file_get_contents(self::$POST_INPUT)
326 $parameters = array_merge($parameters, $post_data);
331 if (isset($request_headers[
'Authorization']) && substr($request_headers[
'Authorization'], 0, 6) ==
'OAuth ') {
333 $request_headers[
'Authorization']
335 $parameters = array_merge($parameters, $header_parameters);
340 return new OAuthRequest($http_method, $http_url, $parameters);
348 $parameters = ($parameters) ? $parameters : array();
352 "oauth_consumer_key" => $consumer->key);
354 $defaults[
'oauth_token'] =
$token->key;
356 $parameters = array_merge($defaults, $parameters);
358 return new OAuthRequest($http_method, $http_url, $parameters);
363 if ($allow_duplicates && isset($this->parameters[
$name])) {
365 if (is_scalar($this->parameters[$name])) {
368 $this->parameters[
$name] = array($this->parameters[$name]);
371 $this->parameters[
$name][] = $value;
373 $this->parameters[
$name] = $value;
379 return isset($this->parameters[
$name]) ? $this->parameters[
$name] : null;
384 return $this->parameters;
389 unset($this->parameters[
$name]);
403 if (isset(
$params[
'oauth_signature'])) {
404 unset(
$params[
'oauth_signature']);
420 $this->get_normalized_http_method(),
421 $this->get_normalized_http_url(),
422 $this->get_signable_parameters()
427 return implode(
'&', $parts);
435 return strtoupper($this->http_method);
444 $parts = parse_url($this->http_url);
446 $scheme = (isset($parts[
'scheme'])) ? $parts[
'scheme'] :
'http';
447 $port = (isset($parts[
'port'])) ? $parts[
'port'] : (($scheme ==
'https') ?
'443' :
'80');
448 $host = (isset($parts[
'host'])) ? strtolower($parts[
'host']) :
'';
449 $path = (isset($parts[
'path'])) ? $parts[
'path'] :
'';
451 if (($scheme ==
'https' && $port !=
'443')
452 || ($scheme ==
'http' && $port !=
'80')) {
453 $host =
"$host:$port";
455 return "$scheme://$host$path";
463 $post_data = $this->to_postdata();
464 $out = $this->get_normalized_http_url();
466 $out .=
'?'.$post_data;
489 $out =
'Authorization: OAuth';
492 foreach ($this->parameters as $k => $v) {
493 if (substr($k, 0, 5) !=
"oauth") {
497 throw new OAuthException(
'Arrays not supported in headers');
499 $out .= ($first) ?
' ' :
',';
511 return $this->to_url();
517 $this->set_parameter(
518 "oauth_signature_method",
519 $signature_method->get_name(),
522 $signature = $this->build_signature($signature_method, $consumer,
$token);
523 $this->set_parameter(
"oauth_signature", $signature,
false);
528 $signature = $signature_method->build_signature($this, $consumer,
$token);
548 return md5($mt . $rand);
554 protected $timestamp_threshold = 300;
556 protected $signature_methods = array();
562 $this->data_store = $data_store;
567 $this->signature_methods[$signature_method->get_name()] =
581 $consumer = $this->get_consumer(
$request);
589 $callback =
$request->get_parameter(
'oauth_callback');
590 $new_token = $this->data_store->new_request_token($consumer, $callback);
603 $consumer = $this->get_consumer(
$request);
611 $verifier =
$request->get_parameter(
'oauth_verifier');
612 $new_token = $this->data_store->new_access_token(
$token, $consumer, $verifier);
623 $consumer = $this->get_consumer(
$request);
626 return array($consumer,
$token);
642 throw new OAuthException(
"OAuth version '$version' not supported");
653 ?
$request->get_parameter(
"oauth_signature_method")
656 if (!$signature_method) {
659 throw new OAuthException(
'No signature method parameter. This parameter is required');
662 if (!in_array($signature_method,
663 array_keys($this->signature_methods))) {
664 throw new OAuthException(
665 "Signature method '$signature_method' not supported " .
666 "try one of the following: " .
667 implode(
", ", array_keys($this->signature_methods))
670 return $this->signature_methods[$signature_method];
679 ?
$request->get_parameter(
"oauth_consumer_key")
682 if (!$consumer_key) {
683 throw new OAuthException(
"Invalid consumer key");
686 $consumer = $this->data_store->lookup_consumer($consumer_key);
688 throw new OAuthException(
"Invalid consumer");
700 ?
$request->get_parameter(
'oauth_token')
703 if (!empty($token_field)) {
704 $token = $this->data_store->lookup_token(
705 $consumer, $token_type, $token_field
708 throw new OAuthException(
"Invalid $token_type token: $token_field");
725 ?
$request->get_parameter(
'oauth_timestamp')
728 ?
$request->get_parameter(
'oauth_nonce')
734 $signature_method = $this->get_signature_method(
$request);
736 $signature =
$request->get_parameter(
'oauth_signature');
737 $valid_sig = $signature_method->check_signature(
745 throw new OAuthException(
"Invalid signature");
755 throw new OAuthException(
756 'Missing timestamp parameter. The parameter is required' 762 if (abs($now -
$timestamp) > $this->timestamp_threshold) {
763 throw new OAuthException(
764 "Expired timestamp, yours $timestamp, ours $now" 775 throw new OAuthException(
776 'Missing nonce parameter. The parameter is required' 781 $found = $this->data_store->lookup_nonce(
788 throw new OAuthException(
"Nonce already used: $nonce");
830 return array_map(array(
'OAuthUtil',
'urlencode_rfc3986'),
$input);
831 }
else if (is_scalar(
$input)) {
835 str_replace(
'%7E',
'~', rawurlencode(
$input))
848 return urldecode($string);
859 if (preg_match_all(
'/('.($only_allow_oauth_parameters ?
'oauth_' :
'').
'[a-z_-]*)=(:?"([^"]*)"|([^,]*))/',
$header, $matches)) {
860 foreach ($matches[1] as
$i =>
$h) {
873 if (function_exists(
'apache_request_headers')) {
876 $headers = apache_request_headers();
883 foreach ($headers as
$key => $value) {
887 ucwords(strtolower(str_replace(
"-",
" ",
$key)))
895 if (isset(
$_SERVER[
'CONTENT_TYPE'])) {
898 if (isset($_ENV[
'CONTENT_TYPE'])) {
899 $out[
'Content-Type'] = $_ENV[
'CONTENT_TYPE'];
903 if (substr(
$key, 0, 5) ==
"HTTP_") {
910 ucwords(strtolower(str_replace(
"_",
" ", substr(
$key, 5))))
916 if (isset(
$out[
'Auth'])) {
917 $out[
'Authorization'] =
$out[
'Auth'];
932 $pairs = explode(
'&',
$input);
934 $parsed_parameters = array();
935 foreach ($pairs as $pair) {
936 $split = explode(
'=', $pair, 2);
940 if (isset($parsed_parameters[$parameter])) {
944 if (is_scalar($parsed_parameters[$parameter])) {
947 $parsed_parameters[$parameter] = array($parsed_parameters[$parameter]);
950 $parsed_parameters[$parameter][] = $value;
952 $parsed_parameters[$parameter] = $value;
955 return $parsed_parameters;
974 foreach (
$params as $parameter => $value) {
975 if (is_array($value)) {
979 sort($value, SORT_STRING);
980 foreach ($value as $duplicate_value) {
981 $pairs[] = $parameter .
'=' . $duplicate_value;
984 $pairs[] = $parameter .
'=' . $value;
989 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...
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)
static parse_parameters($input)
foreach($paths as $path) $request
static from_request($http_method=null, $http_url=null, $parameters=null)
attempt to build up a request from what was passed to the server
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
add_signature_method($signature_method)
get_signable_parameters()
The request parameters, sorted and concatenated into a normalized string.
__construct($http_method, $http_url, $parameters=null)
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
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...
get_normalized_http_url()
parses the url and rebuilds it to be scheme://host/path
foreach($mandatory_scripts as $file) $timestamp
build_signature($request, $consumer, $token)
static from_consumer_and_token($consumer, $token, $http_method, $http_url, $parameters=null)
pretty much a helper function to set up the request
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.
__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