Skip to content
Browse files

Cleaning up OAuth providers:

- Removed OAuth_Provider::$params
- Removed OAuth_Provider::$urls and added url_request_token, url_authorize, and url_access_token as methods
- OAuth_Provider_Google::request_token now requires the "scope" parameter
- Removed all non-auth methods from providers
- Added API links to OAuth_Provider_Twitter
  • Loading branch information...
1 parent f35de8f commit 206c20033e209f233c9e7dc72836bfb786bd7e34 @shadowhand shadowhand committed
Showing with 63 additions and 66 deletions.
  1. +37 −26 classes/oauth/provider.php
  2. +10 −22 classes/oauth/provider/google.php
  3. +16 −18 classes/oauth/provider/twitter.php
View
63 classes/oauth/provider.php
@@ -28,11 +28,6 @@ public static function factory($name, array $options = NULL)
}
/**
- * @var array end point URLs to be used for remote requests
- */
- protected $urls = array();
-
- /**
* @var array additional request parameters to be used for remote requests
*/
protected $params = array();
@@ -40,7 +35,14 @@ public static function factory($name, array $options = NULL)
/**
* Overloads default class properties from the options.
*
+ * Any of the provider options can be set here:
+ *
+ * Type | Option | Description | Default Value
+ * ----------|---------------|------------------------------------------------|-----------------
+ * mixed | signature | Signature method name or object | provider default
+ *
* @param array provider options
+ * @return void
*/
public function __construct(array $options = NULL)
{
@@ -72,6 +74,33 @@ public function __get($key)
}
/**
+ * Returns the request token URL for the provider.
+ *
+ * $url = $provider->url_request_token();
+ *
+ * @return string
+ */
+ abstract public function url_request_token();
+
+ /**
+ * Returns the authorization URL for the provider.
+ *
+ * $url = $provider->url_authorize();
+ *
+ * @return string
+ */
+ abstract public function url_authorize();
+
+ /**
+ * Returns the access token endpoint for the provider.
+ *
+ * $url = $provider->url_access_token();
+ *
+ * @return string
+ */
+ abstract public function url_access_token();
+
+ /**
* Ask for a request token from the OAuth provider.
*
* $token = $provider->request_token($consumer);
@@ -84,17 +113,11 @@ public function __get($key)
public function request_token(OAuth_Consumer $consumer, array $params = NULL)
{
// Create a new GET request for a request token with the required parameters
- $request = OAuth_Request::factory('token', 'GET', $this->urls['request_token'], array(
+ $request = OAuth_Request::factory('token', 'GET', $this->url_request_token(), array(
'oauth_consumer_key' => $consumer->key,
'oauth_callback' => $consumer->callback,
));
- if (isset($this->params['request_token']))
- {
- // Load additional provider parameters
- $request->params($this->params['request_token']);
- }
-
if ($params)
{
// Load user parameters
@@ -126,16 +149,10 @@ public function request_token(OAuth_Consumer $consumer, array $params = NULL)
public function authorize_url(OAuth_Token_Request $token, array $params = NULL)
{
// Create a new GET request for a request token with the required parameters
- $request = OAuth_Request::factory('authorize', 'GET', $this->urls['authorize_url'], array(
+ $request = OAuth_Request::factory('authorize', 'GET', $this->url_authorize(), array(
'oauth_token' => $token->token,
));
- if (isset($this->params['authorize_url']))
- {
- // Load additional provider parameters
- $request->params($this->params['authorize_url']);
- }
-
if ($params)
{
// Load user parameters
@@ -158,18 +175,12 @@ public function authorize_url(OAuth_Token_Request $token, array $params = NULL)
public function access_token(OAuth_Consumer $consumer, OAuth_Token_Request $token, array $params = NULL)
{
// Create a new GET request for a request token with the required parameters
- $request = OAuth_Request::factory('access', 'GET', $this->urls['access_token'], array(
+ $request = OAuth_Request::factory('access', 'GET', $this->url_access_token(), array(
'oauth_consumer_key' => $consumer->key,
'oauth_token' => $token->token,
'oauth_verifier' => $token->verifier,
));
- if (isset($this->params['access_token']))
- {
- // Load additional provider parameters
- $request->params($this->params['access_token']);
- }
-
if ($params)
{
// Load user parameters
View
32 classes/oauth/provider/google.php
@@ -20,30 +20,18 @@ class OAuth_Provider_Google extends OAuth_Provider {
protected $signature = 'HMAC-SHA1';
- protected $urls = array(
- 'request_token' => 'https://www.google.com/accounts/OAuthGetRequestToken',
- 'authorize_url' => 'https://www.google.com/accounts/OAuthAuthorizeToken',
- 'access_token' => 'https://www.google.com/accounts/OAuthGetAccessToken',
- );
-
- protected $params = array(
- 'request_token' => array(
- 'scope' => 'http://www-opensocial.googleusercontent.com/api/people/',
- ),
- );
-
- public function user_profile(OAuth_Consumer $consumer, OAuth_Token_Access $token, $format = 'json')
+ public function request_token(OAuth_Consumer $consumer, array $params = NULL)
{
- $request = OAuth_Request::factory('resource', 'GET', "http://www-opensocial.googleusercontent.com/api/people/@me/@self", array(
- 'oauth_consumer_key' => $consumer->key,
- 'oauth_token' => $token->token,
- ));
-
- // Sign the request using only the consumer, no token is available yet
- $request->sign($this->signature, $consumer, $token);
+ if ( ! isset($params['scope']))
+ {
+ // All request tokens must specify the data scope to access
+ // http://code.google.com/apis/accounts/docs/OAuth.html#prepScope
+ throw new OAuth_Exception('Required parameter to not passed: :param', array(
+ ':param' => 'scope';
+ ));
+ }
- // Return the response
- return $request->execute();
+ return parent::request_token($consumer, $params);
}
} // End OAuth_Provider_Google
View
34 classes/oauth/provider/twitter.php
@@ -2,6 +2,12 @@
/**
* OAuth Twitter Provider
*
+ * Documents for implementing Twitter OAuth can be found at
+ * <http://dev.twitter.com/pages/auth>.
+ *
+ * [!!] This class does not implement the Twitter API. It is only an
+ * implementation of standard OAuth with Twitter as the service provider.
+ *
* @package Kohana/OAuth
* @category Provider
* @author Kohana Team
@@ -12,27 +18,19 @@ class OAuth_Provider_Twitter extends OAuth_Provider {
protected $signature = 'HMAC-SHA1';
- protected $urls = array(
- 'request_token' => 'https://api.twitter.com/oauth/request_token',
- 'authorize_url' => 'https://api.twitter.com/oauth/authorize',
- 'access_token' => 'https://api.twitter.com/oauth/access_token',
- );
-
- public function status_update(OAuth_Consumer $consumer, OAuth_Token_Access $token, $status, $format = 'json')
+ public function url_request_token()
{
- $request = OAuth_Request::factory('resource', 'POST', "https://api.twitter.com/1/statuses/update.{$format}", array(
- 'oauth_consumer_key' => $consumer->key,
- 'oauth_token' => $token->token,
- ));
-
- // Add "status" to the POST body
- $request->post('status', $status);
+ return 'https://api.twitter.com/oauth/request_token';
+ }
- // Sign the request using only the consumer, no token is available yet
- $request->sign($this->signature, $consumer, $token);
+ public function url_authorize()
+ {
+ return 'https://api.twitter.com/oauth/authenticate';
+ }
- // Return the response
- return $request->execute();
+ public function url_access_token()
+ {
+ return 'https://api.twitter.com/oauth/access_token';
}
} // End OAuth_Provider_Twitter

0 comments on commit 206c200

Please sign in to comment.
Something went wrong with that request. Please try again.