Permalink
Browse files

Added all files from oauth_common

  • Loading branch information...
1 parent e1da7de commit edc6dce733387413ea99c8f36e0056778f5d1e91 @hugowetterberg hugowetterberg committed May 20, 2010
View
@@ -0,0 +1,22 @@
+.oauth-common-left-panel {
+ float: left;
+ width: 50%;
+}
+
+.oauth-common-right-panel {
+ float: right;
+ width: 49.9%;
+}
+
+.oauth-common-auth-level {
+ border-bottom: 1px solid #999999;
+ margin-bottom: 1em;
+}
+
+.oauth-common-auth-level > div .form-item:first-child {
+ margin-top: 0;
+}
+
+.oauth-common-auth-level .clear {
+ clear: both;
+}
@@ -0,0 +1,198 @@
+<?php
+// $Id$
+
+class DrupalOAuthClient {
+ private $consumer;
+ private $requestToken;
+ private $accessToken;
+ private $signatureMethod;
+
+ /**
+ * Creates an instance of the DrupalOAuthClient.
+ *
+ * @param DrupalOAuthConsumer $consumer
+ * The consumer.
+ * @param OAuthToken $request_token
+ * Optional. A request token to use.
+ * @param OAuthSignatureMethod $signature_method
+ * Optional. The signature method to use.
+ */
+ public function __construct($consumer, $request_token = NULL, $signature_method = NULL) {
+ $this->consumer = $consumer;
+ $this->requestToken = $request_token;
+ $this->signatureMethod = $signature_method;
+
+ // Set to the default signature method if no method was specified
+ if (!$this->signatureMethod) {
+ $this->signatureMethod = self::signatureMethod();
+ }
+ }
+
+ /**
+ * Convenience function to get signing method implementations.
+ *
+ * @param string $method
+ * Optional. The hmac hashing algorithm to use. Defaults to 'sha512' which
+ * has superseded sha1 as the recommended alternative.
+ * @param bool $fallback_to_sha1
+ * Optional. Whether sha1 should be used as a fallback if the selected
+ * hashing algorithm is unavailable.
+ * @return OAuthSignatureMethod
+ * The signature method object.
+ */
+ public static function signatureMethod($method = 'sha512', $fallback_to_sha1 = TRUE) {
+ $sign = NULL;
+
+ if (in_array($method, hash_algos())) {
+ $sign = new OAuthSignatureMethod_HMAC($method);
+ }
+ else if ($fallback_to_sha1) {
+ $sign = new OAuthSignatureMethod_HMAC_SHA1();
+ }
+
+ return $sign;
+ }
+
+ /**
+ * Gets a request token from the provider.
+ *
+ * @param string $endpoint
+ * Optional. The endpoint path for the provider. Defaults to
+ * '/oauth/request_token'.
+ * @param array $request_params
+ * Optional. A set of parameters to pass to the provider.
+ * @return DrupalOAuthToken
+ * The returned request token.
+ */
+ public function getRequestToken($endpoint = '/oauth/request_token', $request_params = array()) {
+ if (!$this->requestToken) {
+ $response = $this->get($endpoint, FALSE, $request_params);
+ $params = array();
+ parse_str($response, $params);
+
+ if (empty($params['oauth_token']) || empty($params['oauth_token_secret'])) {
+ throw new Exception('No valid request token was returned');
+ }
+
+ $this->requestToken = new DrupalOAuthToken($params['oauth_token'], $params['oauth_token_secret'], array(
+ 'type' => 'request',
+ 'consumer_key' => $this->consumer->key,
+ 'provider_token' => FALSE,
+ ));
+ }
+
+ return clone $this->requestToken;
+ }
+
+ /**
+ * Constructs the url that the user should be sent to to authorize the
+ * request token.
+ *
+ * @param string $callback_url
+ * Optional. The url the user should be sent to after the request token has
+ * been authorized.
+ * @param string $endpoint
+ * Optional. The endpoint path for the provider. Defaults to
+ * '/oauth/authorize'.
+ * @param array $request_params
+ * Optional. Parameters to send to the provider.
+ * @return string
+ * The url.
+ */
+ public function getAuthorizationUrl($callback_url = NULL, $endpoint = '/oauth/authorize', $request_params = array()) {
+ $params = array_merge($request_params, array(
+ 'oauth_token' => $this->requestToken->key,
+ ));
+ if ($callback_url) {
+ $params['oauth_callback'] = $callback_url;
+ }
+ return $this->consumer->configuration['provider_url'] . $endpoint . '?' . http_build_query($params, NULL, '&');
+ }
+
+ /**
+ * Fetches the access token using the request token.
+ *
+ * @param string $endpoint
+ * Optional. The endpoint path for the provider. Defaults to
+ * the default access_endpoint for the $consumer.
+ * @param string $request_params
+ * Optional. Parameters to send to the provider.
+ * @return DrupalOAuthToken
+ * The access token.
+ */
+ public function getAccessToken($endpoint = NULL, $request_params = array()) {
+ if (!$this->accessToken) {
+ if (empty($endpoint) && !empty($this->consumer->configuration['access_endpoint'])) {
+ $this->consumer->configuration['access_endpoint'];
+ }
+ if (empty($endpoint)) {
+ $endpoint = '/oauth/access_token';
+ }
+ $response = $this->get($endpoint, TRUE, $request_params);
+ $params = array();
+ parse_str($response, $params);
+
+ if (empty($params['oauth_token']) || empty($params['oauth_token_secret'])) {
+ throw new Exception('No valid access token was returned');
+ }
+
+ $this->accessToken = new DrupalOAuthToken($params['oauth_token'], $params['oauth_token_secret'], array(
+ 'type' => 'access',
+ 'consumer_key' => $this->consumer->key,
+ 'provider_token' => FALSE,
+ ));
+ }
+
+ return clone $this->accessToken;
+ }
+
+ private function get($path, $use_token = FALSE, $params = array()) {
+ $token = $use_token ? $this->requestToken : NULL;
+ $conf = $this->consumer->configuration;
+ $req = OAuthRequest::from_consumer_and_token($this->consumer, $token,
+ "GET", $conf['provider_url'] . $path, $params);
+ $req->sign_request(self::signatureMethod(), $this->consumer, $token);
+
+ $ch = curl_init();
+ curl_setopt($ch, CURLOPT_URL, $req->to_url());
+ curl_setopt($ch, CURLOPT_HEADER, 1);
+ curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
+
+ $response = curl_exec($ch);
+ $error = curl_error($ch);
+ curl_close($ch);
+
+ if ($error) {
+ throw new Exception($error);
+ }
+
+ $result = $this->interpretResponse($response);
+ if ($result->responseCode != 200) {
+ throw new Exception('Failed to fetch data from url "' . $conf['provider_url'] . $path . '" (HTTP response code ' . $result->responseCode . '): ' . $result->responseMessage, $result->responseCode);
+ }
+
+ return $result->body;
+ }
+
+ private function interpretResponse($res) {
+ list($headers, $body) = preg_split('/\r\n\r\n/', $res, 2);
+
+ $obj = (object)array(
+ 'headers' => $headers,
+ 'body' => $body,
+ );
+
+ $matches = array();
+ if (preg_match('/HTTP\/1.\d (\d{3}) (.*)/', $headers, $matches)) {
+ $obj->responseCode = trim($matches[1]);
+ $obj->responseMessage = trim($matches[2]);
+
+ // Handle HTTP/1.1 100 Continue
+ if ($obj->responseCode == 100) {
+ return $this->interpretResponse($body);
+ }
+ }
+
+ return $obj;
+ }
+}
@@ -0,0 +1,122 @@
+<?php
+// $Id$
+
+class DrupalOAuthConsumer extends OAuthConsumer {
+ public $uid = 0;
+ public $provider_consumer = TRUE;
+ public $name = '';
+ public $context = '';
+ public $created = 0;
+ public $changed = 0;
+ public $callback_url = 'oob';
+ public $configuration = array();
+ public $in_database = FALSE;
+
+ function __construct($key, $secret, $callback_url='oob', $provider_consumer=TRUE, $params=array()) {
+ parent::__construct($key, $secret, $callback_url);
+ $this->provider_consumer = $provider_consumer;
+ foreach($params as $key => $value) {
+ if (isset($this->$key)) {
+ $this->$key = $value;
+ }
+ }
+ }
+
+ /**
+ * Writes the consumer to the database
+ *
+ * @param bool $update
+ * Optional. Set to TRUE if the consumer exists and should be updated. Defaults to FALSE.
+ * @return void
+ */
+ public function write($update=FALSE) {
+ $primary = $update ? array('consumer_key', 'provider_consumer') : array();
+
+ if (!$update) {
+ $this->created = time();
+ }
+ $this->changed = time();
+
+ $values = get_object_vars($this);
+ $values['consumer_key'] = $this->key;
+
+ if (empty($values['configuration'])) {
+ $values['configuration'] = array();
+ }
+ $values['configuration'] = serialize($values['configuration']);
+
+ // Stop the created time from being modified
+ if ($update) {
+ unset($values['created']);
+ }
+
+ drupal_write_record('oauth_common_consumer', $values, $primary);
+ $this->in_database = TRUE;
+ }
+
+ /**
+ * Deletes the consumer from the database
+ *
+ * @return void
+ */
+ public function delete() {
+ self::deleteConsumer($this->key, $this->provider_consumer);
+ }
+
+ /**
+ * Deletes the consumer with the key from the database.
+ *
+ * @param string $key
+ * The consumer key.
+ * @param bool $provider_consumer
+ * Optional. Whether the consumer we're about to delete is a provider or
+ * consumer consumer. Defaults to TRUE.
+ * @return void
+ */
+ public static function deleteConsumer($key, $provider_consumer=TRUE) {
+ db_query("DELETE FROM {oauth_common_consumer}
+ WHERE consumer_key='%s'
+ AND provider_consumer=%d", array(
+ ':consumer_key' => $key,
+ ':provider_consumer' => $provider_consumer,
+ ));
+ }
+
+ /**
+ * Gets the consumer with the specified key
+ *
+ * @param string $key
+ * The key of the consumer to get
+ * @param bool $provider_consumer
+ * Optional. Whether the consumer we're about to load is a provider or
+ * consumer consumer. Defaults to TRUE.
+ * @return DrupalOAuthConsumer
+ * The loaded consumer object or FALSE if load failed
+ */
+ public static function load($key, $provider_consumer=TRUE) {
+ return self::fromResult(db_query("SELECT * FROM {oauth_common_consumer}
+ WHERE consumer_key='%s' AND provider_consumer='%d'", array(
+ ':consumer_key' => $key,
+ ':provider_consumer' => $provider_consumer,
+ )));
+ }
+
+ /**
+ * Constructs a consumer from a db-result resource
+ *
+ * @param resource $res
+ * A database result resource
+ * @return DrupalOAuthConsumer
+ * The constructed consumer object or NULL if no rows could be read or construction failed
+ */
+ public static function fromResult($res) {
+ if ($data = db_fetch_array($res)) {
+ if (!empty($data['configuration'])) {
+ $data['configuration'] = unserialize($data['configuration']);
+ }
+ $data['in_database'] = TRUE;
+ return new DrupalOAuthConsumer($data['consumer_key'], $data['secret'], $data['callback_url'], $data['provider_consumer'], $data);
+ }
+ return NULL;
+ }
+}
Oops, something went wrong.

0 comments on commit edc6dce

Please sign in to comment.