2 // vim: foldmethod=marker
4 /* Generic exception class
6 class OAuthException extends Exception {/*{{{*/
10 class OAuthConsumer {/*{{{*/
14 function __construct($key, $secret, $callback_url=NULL) {/*{{{*/
16 $this->secret = $secret;
17 $this->callback_url = $callback_url;
21 class OAuthToken {/*{{{*/
22 // access tokens and request tokens
28 * secret = the token secret
30 function __construct($key, $secret) {/*{{{*/
32 $this->secret = $secret;
36 * generates the basic string serialization of a token that a server
37 * would respond to request_token and access_token calls with
39 function to_string() {/*{{{*/
40 return "oauth_token=" . OAuthUtil::urlencodeRFC3986($this->key) .
41 "&oauth_token_secret=" . OAuthUtil::urlencodeRFC3986($this->secret);
44 function __toString() {/*{{{*/
45 return $this->to_string();
49 class OAuthSignatureMethod {/*{{{*/
50 public function check_signature(&$request, $consumer, $token, $signature) {
51 $built = $this->build_signature($request, $consumer, $token);
52 return $built == $signature;
56 class OAuthSignatureMethod_HMAC_SHA1 extends OAuthSignatureMethod {/*{{{*/
57 function get_name() {/*{{{*/
61 public function build_signature($request, $consumer, $token) {/*{{{*/
62 $base_string = $request->get_signature_base_string();
63 $request->base_string = $base_string;
67 ($token) ? $token->secret : ""
70 $key_parts = array_map(array('OAuthUtil','urlencodeRFC3986'), $key_parts);
71 $key = implode('&', $key_parts);
73 return base64_encode( hash_hmac('sha1', $base_string, $key, true));
77 class OAuthSignatureMethod_PLAINTEXT extends OAuthSignatureMethod {/*{{{*/
78 public function get_name() {/*{{{*/
82 public function build_signature($request, $consumer, $token) {/*{{{*/
84 OAuthUtil::urlencodeRFC3986($consumer->secret)
88 array_push($sig, OAuthUtil::urlencodeRFC3986($token->secret));
93 $raw = implode("&", $sig);
95 $request->base_string = $raw;
97 return OAuthUtil::urlencodeRFC3986($raw);
101 class OAuthSignatureMethod_RSA_SHA1 extends OAuthSignatureMethod {/*{{{*/
102 public function get_name() {/*{{{*/
106 protected function fetch_public_cert(&$request) {/*{{{*/
107 // not implemented yet, ideas are:
108 // (1) do a lookup in a table of trusted certs keyed off of consumer
109 // (2) fetch via http using a url provided by the requester
110 // (3) some sort of specific discovery code based on request
112 // either way should return a string representation of the certificate
113 throw Exception("fetch_public_cert not implemented");
116 protected function fetch_private_cert(&$request) {/*{{{*/
117 // not implemented yet, ideas are:
118 // (1) do a lookup in a table of trusted certs keyed off of consumer
120 // either way should return a string representation of the certificate
121 throw Exception("fetch_private_cert not implemented");
124 public function build_signature(&$request, $consumer, $token) {/*{{{*/
125 $base_string = $request->get_signature_base_string();
126 $request->base_string = $base_string;
128 // Fetch the private key cert based on the request
129 $cert = $this->fetch_private_cert($request);
131 // Pull the private key ID from the certificate
132 $privatekeyid = openssl_get_privatekey($cert);
134 // Sign using the key
135 $ok = openssl_sign($base_string, $signature, $privatekeyid);
137 // Release the key resource
138 openssl_free_key($privatekeyid);
140 return base64_encode($signature);
143 public function check_signature(&$request, $consumer, $token, $signature) {/*{{{*/
144 $decoded_sig = base64_decode($signature);
146 $base_string = $request->get_signature_base_string();
148 // Fetch the public key cert based on the request
149 $cert = $this->fetch_public_cert($request);
151 // Pull the public key ID from the certificate
152 $publickeyid = openssl_get_publickey($cert);
154 // Check the computed signature against the one passed in the query
155 $ok = openssl_verify($base_string, $decoded_sig, $publickeyid);
157 // Release the key resource
158 openssl_free_key($publickeyid);
164 class OAuthRequest {/*{{{*/
166 private $http_method;
168 // for debug purposes
170 public static $version = '1.0';
172 function __construct($http_method, $http_url, $parameters=NULL) {/*{{{*/
173 @$parameters or $parameters = array();
174 $this->parameters = $parameters;
175 $this->http_method = $http_method;
176 $this->http_url = $http_url;
181 * attempt to build up a request from what was passed to the server
183 public static function from_request($http_method=NULL, $http_url=NULL, $parameters=NULL) {/*{{{*/
184 $scheme = (!isset($_SERVER['HTTPS']) || $_SERVER['HTTPS'] != "on") ? 'http' : 'https';
185 @$http_url or $http_url = $scheme . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
186 @$http_method or $http_method = $_SERVER['REQUEST_METHOD'];
188 $request_headers = OAuthRequest::get_headers();
190 // let the library user override things however they'd like, if they know
191 // which parameters to use then go for it, for example XMLRPC might want to
194 $req = new OAuthRequest($http_method, $http_url, $parameters);
196 // next check for the auth header, we need to do some extra stuff
197 // if that is the case, namely suck in the parameters from GET or POST
198 // so that we can include them in the signature
199 else if (@substr($request_headers['Authorization'], 0, 5) == "OAuth") {
200 $header_parameters = OAuthRequest::split_header($request_headers['Authorization']);
201 if ($http_method == "GET") {
202 $req_parameters = $_GET;
204 else if ($http_method == "POST") {
205 $req_parameters = $_POST;
207 $parameters = array_merge($header_parameters, $req_parameters);
208 $req = new OAuthRequest($http_method, $http_url, $parameters);
210 else if ($http_method == "GET") {
211 $req = new OAuthRequest($http_method, $http_url, $_GET);
213 else if ($http_method == "POST") {
214 $req = new OAuthRequest($http_method, $http_url, $_POST);
220 * pretty much a helper function to set up the request
222 public static function from_consumer_and_token($consumer, $token, $http_method, $http_url, $parameters=NULL) {/*{{{*/
223 @$parameters or $parameters = array();
224 $defaults = array("oauth_version" => OAuthRequest::$version,
225 "oauth_nonce" => OAuthRequest::generate_nonce(),
226 "oauth_timestamp" => OAuthRequest::generate_timestamp(),
227 "oauth_consumer_key" => $consumer->key);
228 $parameters = array_merge($defaults, $parameters);
231 $parameters['oauth_token'] = $token->key;
233 return new OAuthRequest($http_method, $http_url, $parameters);
236 public function set_parameter($name, $value) {/*{{{*/
237 $this->parameters[$name] = $value;
240 public function get_parameter($name) {/*{{{*/
241 return $this->parameters[$name];
244 public function get_parameters() {/*{{{*/
245 return $this->parameters;
249 * Returns the normalized parameters of the request
251 * This will be all (except oauth_signature) parameters,
252 * sorted first by key, and if duplicate keys, then by
255 * The returned string will be all the key=value pairs
260 public function get_signable_parameters() {/*{{{*/
261 // Grab all parameters
262 $params = $this->parameters;
264 // Remove oauth_signature if present
265 if (isset($params['oauth_signature'])) {
266 unset($params['oauth_signature']);
269 // Urlencode both keys and values
270 $keys = array_map(array('OAuthUtil', 'urlencodeRFC3986'), array_keys($params));
271 $values = array_map(array('OAuthUtil', 'urlencodeRFC3986'), array_values($params));
272 $params = array_combine($keys, $values);
274 // Sort by keys (natsort)
275 uksort($params, 'strnatcmp');
277 // Generate key=value pairs
279 foreach ($params as $key=>$value ) {
280 if (is_array($value)) {
281 // If the value is an array, it's because there are multiple
282 // with the same key, sort them, then add all the pairs
284 foreach ($value as $v2) {
285 $pairs[] = $key . '=' . $v2;
288 $pairs[] = $key . '=' . $value;
292 // Return the pairs, concated with &
293 return implode('&', $pairs);
297 * Returns the base string of this request
299 * The base string defined as the method, the url
300 * and the parameters (normalized), each urlencoded
301 * and the concated with &.
303 public function get_signature_base_string() {/*{{{*/
305 $this->get_normalized_http_method(),
306 $this->get_normalized_http_url(),
307 $this->get_signable_parameters()
310 $parts = array_map(array('OAuthUtil', 'urlencodeRFC3986'), $parts);
312 return implode('&', $parts);
316 * just uppercases the http method
318 public function get_normalized_http_method() {/*{{{*/
319 return strtoupper($this->http_method);
323 * parses the url and rebuilds it to be
326 public function get_normalized_http_url() {/*{{{*/
327 $parts = parse_url($this->http_url);
329 $port = @$parts['port'];
330 $scheme = $parts['scheme'];
331 $host = $parts['host'];
332 $path = @$parts['path'];
334 $port or $port = ($scheme == 'https') ? '443' : '80';
336 if (($scheme == 'https' && $port != '443')
337 || ($scheme == 'http' && $port != '80')) {
338 $host = "$host:$port";
340 return "$scheme://$host$path";
344 * builds a url usable for a GET request
346 public function to_url() {/*{{{*/
347 $out = $this->get_normalized_http_url() . "?";
348 $out .= $this->to_postdata();
353 * builds the data one would send in a POST request
355 public function to_postdata() {/*{{{*/
357 foreach ($this->parameters as $k => $v) {
358 $total[] = OAuthUtil::urlencodeRFC3986($k) . "=" . OAuthUtil::urlencodeRFC3986($v);
360 $out = implode("&", $total);
365 * builds the Authorization: header
367 public function to_header($realm="") {/*{{{*/
368 $out ='"Authorization: OAuth realm="' . $realm . '",';
370 foreach ($this->parameters as $k => $v) {
371 if (substr($k, 0, 5) != "oauth") continue;
372 $out .= ',' . OAuthUtil::urlencodeRFC3986($k) . '="' . OAuthUtil::urlencodeRFC3986($v) . '"';
377 public function __toString() {/*{{{*/
378 return $this->to_url();
382 public function sign_request($signature_method, $consumer, $token) {/*{{{*/
383 $this->set_parameter("oauth_signature_method", $signature_method->get_name());
384 $signature = $this->build_signature($signature_method, $consumer, $token);
385 $this->set_parameter("oauth_signature", $signature);
388 public function build_signature($signature_method, $consumer, $token) {/*{{{*/
389 $signature = $signature_method->build_signature($this, $consumer, $token);
394 * util function: current timestamp
396 private static function generate_timestamp() {/*{{{*/
401 * util function: current nonce
403 private static function generate_nonce() {/*{{{*/
407 return md5($mt . $rand); // md5s look nicer than numbers
411 * util function for turning the Authorization: header into
412 * parameters, has to do some unescaping
414 private static function split_header($header) {/*{{{*/
415 // remove 'OAuth ' at the start of a header
416 $header = substr($header, 6);
418 // error cases: commas in parameter values?
419 $parts = explode(",", $header);
421 foreach ($parts as $param) {
422 $param = ltrim($param);
423 // skip the "realm" param, nobody ever uses it anyway
424 if (substr($param, 0, 5) != "oauth") continue;
426 $param_parts = explode("=", $param);
428 // rawurldecode() used because urldecode() will turn a "+" in the
429 // value into a space
430 $out[$param_parts[0]] = rawurldecode(substr($param_parts[1], 1, -1));
436 * helper to try to sort out headers for people who aren't running apache
438 private static function get_headers() {/*{{{*/
439 if (function_exists('apache_request_headers')) {
440 // we need this to get the actual Authorization: header
441 // because apache tends to tell us it doesn't exist
442 return apache_request_headers();
444 // otherwise we don't have apache and are just going to have to hope
445 // that $_SERVER actually contains what we need
447 foreach ($_SERVER as $key => $value) {
448 if (substr($key, 0, 5) == "HTTP_") {
449 // this is chaos, basically it is just there to capitalize the first
450 // letter of every word that is not an initial HTTP and strip HTTP
452 $key = str_replace(" ", "-", ucwords(strtolower(str_replace("_", " ", substr($key, 5)))));
460 class OAuthServer {/*{{{*/
461 protected $timestamp_threshold = 300; // in seconds, five minutes
462 protected $version = 1.0; // hi blaine
463 protected $signature_methods = array();
465 protected $data_store;
467 function __construct($data_store) {/*{{{*/
468 $this->data_store = $data_store;
471 public function add_signature_method($signature_method) {/*{{{*/
472 $this->signature_methods[$signature_method->get_name()] =
476 // high level functions
479 * process a request_token request
480 * returns the request token on success
482 public function fetch_request_token(&$request) {/*{{{*/
483 $this->get_version($request);
485 $consumer = $this->get_consumer($request);
487 // no token required for the initial token request
490 $this->check_signature($request, $consumer, $token);
492 $new_token = $this->data_store->new_request_token($consumer);
498 * process an access_token request
499 * returns the access token on success
501 public function fetch_access_token(&$request) {/*{{{*/
502 $this->get_version($request);
504 $consumer = $this->get_consumer($request);
506 // requires authorized request token
507 $token = $this->get_token($request, $consumer, "request");
509 $this->check_signature($request, $consumer, $token);
511 $new_token = $this->data_store->new_access_token($token, $consumer);
517 * verify an api call, checks all the parameters
519 public function verify_request(&$request) {/*{{{*/
520 $this->get_version($request);
521 $consumer = $this->get_consumer($request);
522 $token = $this->get_token($request, $consumer, "access");
523 $this->check_signature($request, $consumer, $token);
524 return array($consumer, $token);
527 // Internals from here
531 private function get_version(&$request) {/*{{{*/
532 $version = $request->get_parameter("oauth_version");
536 if ($version && $version != $this->version) {
537 throw new OAuthException("OAuth version '$version' not supported");
543 * figure out the signature with some defaults
545 private function get_signature_method(&$request) {/*{{{*/
547 @$request->get_parameter("oauth_signature_method");
548 if (!$signature_method) {
549 $signature_method = "PLAINTEXT";
551 if (!in_array($signature_method,
552 array_keys($this->signature_methods))) {
553 throw new OAuthException(
554 "Signature method '$signature_method' not supported try one of the following: " . implode(", ", array_keys($this->signature_methods))
557 return $this->signature_methods[$signature_method];
561 * try to find the consumer for the provided request's consumer key
563 private function get_consumer(&$request) {/*{{{*/
564 $consumer_key = @$request->get_parameter("oauth_consumer_key");
565 if (!$consumer_key) {
566 throw new OAuthException("Invalid consumer key");
569 $consumer = $this->data_store->lookup_consumer($consumer_key);
571 throw new OAuthException("Invalid consumer");
578 * try to find the token for the provided request's token key
580 private function get_token(&$request, $consumer, $token_type="access") {/*{{{*/
581 $token_field = @$request->get_parameter('oauth_token');
582 $token = $this->data_store->lookup_token(
583 $consumer, $token_type, $token_field
586 throw new OAuthException("Invalid $token_type token: $token_field");
592 * all-in-one function to check the signature on a request
593 * should guess the signature method appropriately
595 private function check_signature(&$request, $consumer, $token) {/*{{{*/
596 // this should probably be in a different method
597 $timestamp = @$request->get_parameter('oauth_timestamp');
598 $nonce = @$request->get_parameter('oauth_nonce');
600 $this->check_timestamp($timestamp);
601 $this->check_nonce($consumer, $token, $nonce, $timestamp);
603 $signature_method = $this->get_signature_method($request);
605 $signature = $request->get_parameter('oauth_signature');
606 $valid_sig = $signature_method->check_signature(
614 throw new OAuthException("Invalid signature");
619 * check that the timestamp is new enough
621 private function check_timestamp($timestamp) {/*{{{*/
622 // verify that timestamp is recentish
624 if ($now - $timestamp > $this->timestamp_threshold) {
625 throw new OAuthException("Expired timestamp, yours $timestamp, ours $now");
630 * check that the nonce is not repeated
632 private function check_nonce($consumer, $token, $nonce, $timestamp) {/*{{{*/
633 // verify that the nonce is uniqueish
634 $found = $this->data_store->lookup_nonce($consumer, $token, $nonce, $timestamp);
636 throw new OAuthException("Nonce already used: $nonce");
644 class OAuthDataStore {/*{{{*/
645 function lookup_consumer($consumer_key) {/*{{{*/
649 function lookup_token($consumer, $token_type, $token) {/*{{{*/
653 function lookup_nonce($consumer, $token, $nonce, $timestamp) {/*{{{*/
657 function fetch_request_token($consumer) {/*{{{*/
658 // return a new token attached to this consumer
661 function fetch_access_token($token, $consumer) {/*{{{*/
662 // return a new access token attached to this consumer
663 // for the user associated with this token if the request token
665 // should also invalidate the request token
671 /* A very naive dbm-based oauth storage
673 class SimpleOAuthDataStore extends OAuthDataStore {/*{{{*/
676 function __construct($path = "oauth.gdbm") {/*{{{*/
677 $this->dbh = dba_popen($path, 'c', 'gdbm');
680 function __destruct() {/*{{{*/
681 dba_close($this->dbh);
684 function lookup_consumer($consumer_key) {/*{{{*/
685 $rv = dba_fetch("consumer_$consumer_key", $this->dbh);
689 $obj = unserialize($rv);
690 if (!($obj instanceof OAuthConsumer)) {
696 function lookup_token($consumer, $token_type, $token) {/*{{{*/
697 $rv = dba_fetch("${token_type}_${token}", $this->dbh);
701 $obj = unserialize($rv);
702 if (!($obj instanceof OAuthToken)) {
708 function lookup_nonce($consumer, $token, $nonce, $timestamp) {/*{{{*/
709 if (dba_exists("nonce_$nonce", $this->dbh)) {
712 dba_insert("nonce_$nonce", "1", $this->dbh);
717 function new_token($consumer, $type="request") {/*{{{*/
719 $secret = time() + time();
720 $token = new OAuthToken($key, md5(md5($secret)));
721 if (!dba_insert("${type}_$key", serialize($token), $this->dbh)) {
722 throw new OAuthException("doooom!");
727 function new_request_token($consumer) {/*{{{*/
728 return $this->new_token($consumer, "request");
731 function new_access_token($token, $consumer) {/*{{{*/
733 $token = $this->new_token($consumer, 'access');
734 dba_delete("request_" . $token->key, $this->dbh);
739 class OAuthUtil {/*{{{*/
740 public static function urlencodeRFC3986($string) {/*{{{*/
741 return str_replace('+', ' ',
742 str_replace('%7E', '~', rawurlencode($string)));
747 // This decode function isn't taking into consideration the above
748 // modifications to the encoding process. However, this method doesn't
749 // seem to be used anywhere so leaving it as is.
750 public static function urldecodeRFC3986($string) {/*{{{*/
751 return rawurldecode($string);