2 // vim: foldmethod=marker
4 /* Generic exception class
6 class OAuthException extends Exception {
14 function __construct($key, $secret, $callback_url=NULL) {
16 $this->secret = $secret;
17 $this->callback_url = $callback_url;
20 function __toString() {
21 return "OAuthConsumer[key=$this->key,secret=$this->secret]";
26 // access tokens and request tokens
32 * secret = the token secret
34 function __construct($key, $secret) {
36 $this->secret = $secret;
40 * generates the basic string serialization of a token that a server
41 * would respond to request_token and access_token calls with
43 function to_string() {
44 return "oauth_token=" .
45 OAuthUtil::urlencode_rfc3986($this->key) .
46 "&oauth_token_secret=" .
47 OAuthUtil::urlencode_rfc3986($this->secret);
50 function __toString() {
51 return $this->to_string();
56 * A class for implementing a Signature Method
57 * See section 9 ("Signing Requests") in the spec
59 abstract class OAuthSignatureMethod {
61 * Needs to return the name of the Signature Method (ie HMAC-SHA1)
64 abstract public function get_name();
67 * Build up the signature
68 * NOTE: The output of this function MUST NOT be urlencoded.
69 * the encoding is handled in OAuthRequest when the final
70 * request is serialized
71 * @param OAuthRequest $request
72 * @param OAuthConsumer $consumer
73 * @param OAuthToken $token
76 abstract public function build_signature($request, $consumer, $token);
79 * Verifies that a given signature is correct
80 * @param OAuthRequest $request
81 * @param OAuthConsumer $consumer
82 * @param OAuthToken $token
83 * @param string $signature
86 public function check_signature($request, $consumer, $token, $signature) {
87 $built = $this->build_signature($request, $consumer, $token);
88 return $built == $signature;
93 * The HMAC-SHA1 signature method uses the HMAC-SHA1 signature algorithm as defined in [RFC2104]
94 * where the Signature Base String is the text and the key is the concatenated values (each first
95 * encoded per Parameter Encoding) of the Consumer Secret and Token Secret, separated by an '&'
96 * character (ASCII code 38) even if empty.
97 * - Chapter 9.2 ("HMAC-SHA1")
99 class OAuthSignatureMethod_HMAC_SHA1 extends OAuthSignatureMethod {
100 function get_name() {
104 public function build_signature($request, $consumer, $token) {
105 $base_string = $request->get_signature_base_string();
106 $request->base_string = $base_string;
110 ($token) ? $token->secret : ""
113 $key_parts = OAuthUtil::urlencode_rfc3986($key_parts);
114 $key = implode('&', $key_parts);
116 return base64_encode(hash_hmac('sha1', $base_string, $key, true));
121 * The PLAINTEXT method does not provide any security protection and SHOULD only be used
122 * over a secure channel such as HTTPS. It does not use the Signature Base String.
123 * - Chapter 9.4 ("PLAINTEXT")
125 class OAuthSignatureMethod_PLAINTEXT extends OAuthSignatureMethod {
126 public function get_name() {
131 * oauth_signature is set to the concatenated encoded values of the Consumer Secret and
132 * Token Secret, separated by a '&' character (ASCII code 38), even if either secret is
133 * empty. The result MUST be encoded again.
134 * - Chapter 9.4.1 ("Generating Signatures")
136 * Please note that the second encoding MUST NOT happen in the SignatureMethod, as
137 * OAuthRequest handles this!
139 public function build_signature($request, $consumer, $token) {
142 ($token) ? $token->secret : ""
145 $key_parts = OAuthUtil::urlencode_rfc3986($key_parts);
146 $key = implode('&', $key_parts);
147 $request->base_string = $key;
154 * The RSA-SHA1 signature method uses the RSASSA-PKCS1-v1_5 signature algorithm as defined in
155 * [RFC3447] section 8.2 (more simply known as PKCS#1), using SHA-1 as the hash function for
156 * EMSA-PKCS1-v1_5. It is assumed that the Consumer has provided its RSA public key in a
157 * verified way to the Service Provider, in a manner which is beyond the scope of this
159 * - Chapter 9.3 ("RSA-SHA1")
161 abstract class OAuthSignatureMethod_RSA_SHA1 extends OAuthSignatureMethod {
162 public function get_name() {
166 // Up to the SP to implement this lookup of keys. Possible ideas are:
167 // (1) do a lookup in a table of trusted certs keyed off of consumer
168 // (2) fetch via http using a url provided by the requester
169 // (3) some sort of specific discovery code based on request
171 // Either way should return a string representation of the certificate
172 protected abstract function fetch_public_cert(&$request);
174 // Up to the SP to implement this lookup of keys. Possible ideas are:
175 // (1) do a lookup in a table of trusted certs keyed off of consumer
177 // Either way should return a string representation of the certificate
178 protected abstract function fetch_private_cert(&$request);
180 public function build_signature($request, $consumer, $token) {
181 $base_string = $request->get_signature_base_string();
182 $request->base_string = $base_string;
184 // Fetch the private key cert based on the request
185 $cert = $this->fetch_private_cert($request);
187 // Pull the private key ID from the certificate
188 $privatekeyid = openssl_get_privatekey($cert);
190 // Sign using the key
191 $ok = openssl_sign($base_string, $signature, $privatekeyid);
193 // Release the key resource
194 openssl_free_key($privatekeyid);
196 return base64_encode($signature);
199 public function check_signature($request, $consumer, $token, $signature) {
200 $decoded_sig = base64_decode($signature);
202 $base_string = $request->get_signature_base_string();
204 // Fetch the public key cert based on the request
205 $cert = $this->fetch_public_cert($request);
207 // Pull the public key ID from the certificate
208 $publickeyid = openssl_get_publickey($cert);
210 // Check the computed signature against the one passed in the query
211 $ok = openssl_verify($base_string, $decoded_sig, $publickeyid);
213 // Release the key resource
214 openssl_free_key($publickeyid);
221 protected $parameters;
222 protected $http_method;
224 // for debug purposes
226 public static $version = '1.0';
227 public static $POST_INPUT = 'php://input';
229 function __construct($http_method, $http_url, $parameters=NULL) {
230 $parameters = ($parameters) ? $parameters : array();
231 $parameters = array_merge( OAuthUtil::parse_parameters(parse_url($http_url, PHP_URL_QUERY)), $parameters);
232 $this->parameters = $parameters;
233 $this->http_method = $http_method;
234 $this->http_url = $http_url;
239 * attempt to build up a request from what was passed to the server
241 public static function from_request($http_method=NULL, $http_url=NULL, $parameters=NULL) {
242 $scheme = (!isset($_SERVER['HTTPS']) || $_SERVER['HTTPS'] != "on")
245 $http_url = ($http_url) ? $http_url : $scheme .
246 '://' . $_SERVER['HTTP_HOST'] .
248 $_SERVER['SERVER_PORT'] .
249 $_SERVER['REQUEST_URI'];
250 $http_method = ($http_method) ? $http_method : $_SERVER['REQUEST_METHOD'];
252 // We weren't handed any parameters, so let's find the ones relevant to
254 // If you run XML-RPC or similar you should use this to provide your own
255 // parsed parameter-list
257 // Find request headers
258 $request_headers = OAuthUtil::get_headers();
260 // Parse the query-string to find GET parameters
261 $parameters = OAuthUtil::parse_parameters($_SERVER['QUERY_STRING']);
263 // It's a POST request of the proper content-type, so parse POST
264 // parameters and add those overriding any duplicates from GET
265 if ($http_method == "POST"
266 && isset($request_headers['Content-Type'])
267 && strstr($request_headers['Content-Type'],
268 'application/x-www-form-urlencoded')
270 $post_data = OAuthUtil::parse_parameters(
271 file_get_contents(self::$POST_INPUT)
273 $parameters = array_merge($parameters, $post_data);
276 // We have a Authorization-header with OAuth data. Parse the header
277 // and add those overriding any duplicates from GET or POST
278 if (isset($request_headers['Authorization']) && substr($request_headers['Authorization'], 0, 6) == 'OAuth ') {
279 $header_parameters = OAuthUtil::split_header(
280 $request_headers['Authorization']
282 $parameters = array_merge($parameters, $header_parameters);
287 return new OAuthRequest($http_method, $http_url, $parameters);
291 * pretty much a helper function to set up the request
293 public static function from_consumer_and_token($consumer, $token, $http_method, $http_url, $parameters=NULL) {
294 $parameters = ($parameters) ? $parameters : array();
295 $defaults = array("oauth_version" => OAuthRequest::$version,
296 "oauth_nonce" => OAuthRequest::generate_nonce(),
297 "oauth_timestamp" => OAuthRequest::generate_timestamp(),
298 "oauth_consumer_key" => $consumer->key);
300 $defaults['oauth_token'] = $token->key;
302 $parameters = array_merge($defaults, $parameters);
304 return new OAuthRequest($http_method, $http_url, $parameters);
307 public function set_parameter($name, $value, $allow_duplicates = true) {
308 if ($allow_duplicates && isset($this->parameters[$name])) {
309 // We have already added parameter(s) with this name, so add to the list
310 if (is_scalar($this->parameters[$name])) {
311 // This is the first duplicate, so transform scalar (string)
312 // into an array so we can add the duplicates
313 $this->parameters[$name] = array($this->parameters[$name]);
316 $this->parameters[$name][] = $value;
318 $this->parameters[$name] = $value;
322 public function get_parameter($name) {
323 return isset($this->parameters[$name]) ? $this->parameters[$name] : null;
326 public function get_parameters() {
327 return $this->parameters;
330 public function unset_parameter($name) {
331 unset($this->parameters[$name]);
335 * The request parameters, sorted and concatenated into a normalized string.
338 public function get_signable_parameters() {
339 // Grab all parameters
340 $params = $this->parameters;
342 // Remove oauth_signature if present
343 // Ref: Spec: 9.1.1 ("The oauth_signature parameter MUST be excluded.")
344 if (isset($params['oauth_signature'])) {
345 unset($params['oauth_signature']);
348 return OAuthUtil::build_http_query($params);
352 * Returns the base string of this request
354 * The base string defined as the method, the url
355 * and the parameters (normalized), each urlencoded
356 * and the concated with &.
358 public function get_signature_base_string() {
360 $this->get_normalized_http_method(),
361 $this->get_normalized_http_url(),
362 $this->get_signable_parameters()
365 $parts = OAuthUtil::urlencode_rfc3986($parts);
367 return implode('&', $parts);
371 * just uppercases the http method
373 public function get_normalized_http_method() {
374 return strtoupper($this->http_method);
378 * parses the url and rebuilds it to be
381 public function get_normalized_http_url() {
382 $parts = parse_url($this->http_url);
384 $scheme = (isset($parts['scheme'])) ? $parts['scheme'] : 'http';
385 $port = (isset($parts['port'])) ? $parts['port'] : (($scheme == 'https') ? '443' : '80');
386 $host = (isset($parts['host'])) ? $parts['host'] : '';
387 $path = (isset($parts['path'])) ? $parts['path'] : '';
389 if (($scheme == 'https' && $port != '443')
390 || ($scheme == 'http' && $port != '80')) {
391 $host = "$host:$port";
393 return "$scheme://$host$path";
397 * builds a url usable for a GET request
399 public function to_url() {
400 $post_data = $this->to_postdata();
401 $out = $this->get_normalized_http_url();
403 $out .= '?'.$post_data;
409 * builds the data one would send in a POST request
411 public function to_postdata() {
412 return OAuthUtil::build_http_query($this->parameters);
416 * builds the Authorization: header
418 public function to_header($realm=null) {
421 $out = 'Authorization: OAuth realm="' . OAuthUtil::urlencode_rfc3986($realm) . '"';
424 $out = 'Authorization: OAuth';
427 foreach ($this->parameters as $k => $v) {
428 if (substr($k, 0, 5) != "oauth") continue;
430 throw new OAuthException('Arrays not supported in headers');
432 $out .= ($first) ? ' ' : ',';
433 $out .= OAuthUtil::urlencode_rfc3986($k) .
435 OAuthUtil::urlencode_rfc3986($v) .
442 public function __toString() {
443 return $this->to_url();
447 public function sign_request($signature_method, $consumer, $token) {
448 $this->set_parameter(
449 "oauth_signature_method",
450 $signature_method->get_name(),
453 $signature = $this->build_signature($signature_method, $consumer, $token);
454 $this->set_parameter("oauth_signature", $signature, false);
457 public function build_signature($signature_method, $consumer, $token) {
458 $signature = $signature_method->build_signature($this, $consumer, $token);
463 * util function: current timestamp
465 private static function generate_timestamp() {
470 * util function: current nonce
472 private static function generate_nonce() {
476 return md5($mt . $rand); // md5s look nicer than numbers
481 protected $timestamp_threshold = 300; // in seconds, five minutes
482 protected $version = '1.0'; // hi blaine
483 protected $signature_methods = array();
485 protected $data_store;
487 function __construct($data_store) {
488 $this->data_store = $data_store;
491 public function add_signature_method($signature_method) {
492 $this->signature_methods[$signature_method->get_name()] =
496 // high level functions
499 * process a request_token request
500 * returns the request token on success
502 public function fetch_request_token(&$request) {
503 $this->get_version($request);
505 $consumer = $this->get_consumer($request);
507 // no token required for the initial token request
510 $this->check_signature($request, $consumer, $token);
513 $callback = $request->get_parameter('oauth_callback');
514 $new_token = $this->data_store->new_request_token($consumer, $callback);
520 * process an access_token request
521 * returns the access token on success
523 public function fetch_access_token(&$request) {
524 $this->get_version($request);
526 $consumer = $this->get_consumer($request);
528 // requires authorized request token
529 $token = $this->get_token($request, $consumer, "request");
531 $this->check_signature($request, $consumer, $token);
534 $verifier = $request->get_parameter('oauth_verifier');
535 $new_token = $this->data_store->new_access_token($token, $consumer, $verifier);
541 * verify an api call, checks all the parameters
543 public function verify_request(&$request) {
544 $this->get_version($request);
545 $consumer = $this->get_consumer($request);
546 $token = $this->get_token($request, $consumer, "access");
547 $this->check_signature($request, $consumer, $token);
548 return array($consumer, $token);
551 // Internals from here
555 private function get_version(&$request) {
556 $version = $request->get_parameter("oauth_version");
558 // Service Providers MUST assume the protocol version to be 1.0 if this parameter is not present.
559 // Chapter 7.0 ("Accessing Protected Ressources")
562 if ($version !== $this->version) {
563 throw new OAuthException("OAuth version '$version' not supported");
569 * figure out the signature with some defaults
571 private function get_signature_method($request) {
572 $signature_method = $request instanceof OAuthRequest
573 ? $request->get_parameter("oauth_signature_method")
576 if (!$signature_method) {
577 // According to chapter 7 ("Accessing Protected Ressources") the signature-method
578 // parameter is required, and we can't just fallback to PLAINTEXT
579 throw new OAuthException('No signature method parameter. This parameter is required');
582 if (!in_array($signature_method,
583 array_keys($this->signature_methods))) {
584 throw new OAuthException(
585 "Signature method '$signature_method' not supported " .
586 "try one of the following: " .
587 implode(", ", array_keys($this->signature_methods))
590 return $this->signature_methods[$signature_method];
594 * try to find the consumer for the provided request's consumer key
596 private function get_consumer($request) {
597 $consumer_key = $request instanceof OAuthRequest
598 ? $request->get_parameter("oauth_consumer_key")
601 if (!$consumer_key) {
602 throw new OAuthException("Invalid consumer key");
605 $consumer = $this->data_store->lookup_consumer($consumer_key);
607 throw new OAuthException("Invalid consumer");
614 * try to find the token for the provided request's token key
616 private function get_token($request, $consumer, $token_type="access") {
617 $token_field = $request instanceof OAuthRequest
618 ? $request->get_parameter('oauth_token')
621 $token = $this->data_store->lookup_token(
622 $consumer, $token_type, $token_field
625 throw new OAuthException("Invalid $token_type token: $token_field");
631 * all-in-one function to check the signature on a request
632 * should guess the signature method appropriately
634 private function check_signature($request, $consumer, $token) {
635 // this should probably be in a different method
636 $timestamp = $request instanceof OAuthRequest
637 ? $request->get_parameter('oauth_timestamp')
639 $nonce = $request instanceof OAuthRequest
640 ? $request->get_parameter('oauth_nonce')
643 $this->check_timestamp($timestamp);
644 $this->check_nonce($consumer, $token, $nonce, $timestamp);
646 $signature_method = $this->get_signature_method($request);
648 $signature = $request->get_parameter('oauth_signature');
649 $valid_sig = $signature_method->check_signature(
657 throw new OAuthException("Invalid signature");
662 * check that the timestamp is new enough
664 private function check_timestamp($timestamp) {
666 throw new OAuthException(
667 'Missing timestamp parameter. The parameter is required'
670 // verify that timestamp is recentish
672 if (abs($now - $timestamp) > $this->timestamp_threshold) {
673 throw new OAuthException(
674 "Expired timestamp, yours $timestamp, ours $now"
680 * check that the nonce is not repeated
682 private function check_nonce($consumer, $token, $nonce, $timestamp) {
684 throw new OAuthException(
685 'Missing nonce parameter. The parameter is required'
688 // verify that the nonce is uniqueish
689 $found = $this->data_store->lookup_nonce(
696 throw new OAuthException("Nonce already used: $nonce");
702 class OAuthDataStore {
703 function lookup_consumer($consumer_key) {
707 function lookup_token($consumer, $token_type, $token) {
711 function lookup_nonce($consumer, $token, $nonce, $timestamp) {
715 function new_request_token($consumer, $callback = null) {
716 // return a new token attached to this consumer
719 function new_access_token($token, $consumer, $verifier = null) {
720 // return a new access token attached to this consumer
721 // for the user associated with this token if the request token
723 // should also invalidate the request token
729 public static function urlencode_rfc3986($input) {
730 if (is_array($input)) {
731 return array_map(array('OAuthUtil', 'urlencode_rfc3986'), $input);
732 } else if (is_scalar($input)) {
736 str_replace('%7E', '~', rawurlencode($input))
744 // This decode function isn't taking into consideration the above
745 // modifications to the encoding process. However, this method doesn't
746 // seem to be used anywhere so leaving it as is.
747 public static function urldecode_rfc3986($string) {
748 return urldecode($string);
751 // Utility function for turning the Authorization: header into
752 // parameters, has to do some unescaping
753 // Can filter out any non-oauth parameters if needed (default behaviour)
754 // May 28th, 2010 - method updated to tjerk.meesters for a speed improvement.
755 // see http://code.google.com/p/oauth/issues/detail?id=163
756 public static function split_header($header, $only_allow_oauth_parameters = true) {
758 if (preg_match_all('/('.($only_allow_oauth_parameters ? 'oauth_' : '').'[a-z_-]*)=(:?"([^"]*)"|([^,]*))/', $header, $matches)) {
759 foreach ($matches[1] as $i => $h) {
760 $params[$h] = OAuthUtil::urldecode_rfc3986(empty($matches[3][$i]) ? $matches[4][$i] : $matches[3][$i]);
762 if (isset($params['realm'])) {
763 unset($params['realm']);
769 // helper to try to sort out headers for people who aren't running apache
770 public static function get_headers() {
771 if (function_exists('apache_request_headers')) {
772 // we need this to get the actual Authorization: header
773 // because apache tends to tell us it doesn't exist
774 $headers = apache_request_headers();
776 // sanitize the output of apache_request_headers because
777 // we always want the keys to be Cased-Like-This and arh()
778 // returns the headers in the same case as they are in the
781 foreach ($headers AS $key => $value) {
785 ucwords(strtolower(str_replace("-", " ", $key)))
790 // otherwise we don't have apache and are just going to have to hope
791 // that $_SERVER actually contains what we need
793 if( isset($_SERVER['CONTENT_TYPE']) )
794 $out['Content-Type'] = $_SERVER['CONTENT_TYPE'];
795 if( isset($_ENV['CONTENT_TYPE']) )
796 $out['Content-Type'] = $_ENV['CONTENT_TYPE'];
798 foreach ($_SERVER as $key => $value) {
799 if (substr($key, 0, 5) == "HTTP_") {
800 // this is chaos, basically it is just there to capitalize the first
801 // letter of every word that is not an initial HTTP and strip HTTP
806 ucwords(strtolower(str_replace("_", " ", substr($key, 5))))
815 // This function takes a input like a=b&a=c&d=e and returns the parsed
816 // parameters like this
817 // array('a' => array('b','c'), 'd' => 'e')
818 public static function parse_parameters( $input ) {
819 if (!isset($input) || !$input) return array();
821 $pairs = explode('&', $input);
823 $parsed_parameters = array();
824 foreach ($pairs as $pair) {
825 $split = explode('=', $pair, 2);
826 $parameter = OAuthUtil::urldecode_rfc3986($split[0]);
827 $value = isset($split[1]) ? OAuthUtil::urldecode_rfc3986($split[1]) : '';
829 if (isset($parsed_parameters[$parameter])) {
830 // We have already recieved parameter(s) with this name, so add to the list
831 // of parameters with this name
833 if (is_scalar($parsed_parameters[$parameter])) {
834 // This is the first duplicate, so transform scalar (string) into an array
835 // so we can add the duplicates
836 $parsed_parameters[$parameter] = array($parsed_parameters[$parameter]);
839 $parsed_parameters[$parameter][] = $value;
841 $parsed_parameters[$parameter] = $value;
844 return $parsed_parameters;
847 public static function build_http_query($params) {
848 if (!$params) return '';
850 // Urlencode both keys and values
851 $keys = OAuthUtil::urlencode_rfc3986(array_keys($params));
852 $values = OAuthUtil::urlencode_rfc3986(array_values($params));
853 $params = array_combine($keys, $values);
855 // Parameters are sorted by name, using lexicographical byte value ordering.
856 // Ref: Spec: 9.1.1 (1)
857 uksort($params, 'strcmp');
860 foreach ($params as $parameter => $value) {
861 if (is_array($value)) {
862 // If two or more parameters share the same name, they are sorted by their value
863 // Ref: Spec: 9.1.1 (1)
864 // June 12th, 2010 - changed to sort because of issue 164 by hidetaka
865 sort($value, SORT_STRING);
866 foreach ($value as $duplicate_value) {
867 $pairs[] = $parameter . '=' . $duplicate_value;
870 $pairs[] = $parameter . '=' . $value;
873 // For each parameter, the name is separated from the corresponding value by an '=' character (ASCII code 61)
874 // Each name-value pair is separated by an '&' character (ASCII code 38)
875 return implode('&', $pairs);