Skip to content

Commit

Permalink
Merge commit 'origin/master' into docs
Browse files Browse the repository at this point in the history
  • Loading branch information
bluehawk committed Sep 21, 2010
2 parents b0d8669 + a3dfdef commit 059df80
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 65 deletions.
22 changes: 8 additions & 14 deletions classes/kohana/oauth.php
Expand Up @@ -3,7 +3,7 @@
* OAuth Library
*
* @package Kohana/OAuth
* @package Base
* @category Base
* @author Kohana Team
* @copyright (c) 2010 Kohana Team
* @license http://kohanaframework.org/license
Expand Down Expand Up @@ -86,8 +86,14 @@ public static function urldecode($input)
* @return string
* @uses OAuth::urlencode
*/
public static function normalize_params(array $params)
public static function normalize_params(array $params = NULL)
{
if ( ! $params)
{
// Nothing to do
return '';
}

// Encode the parameter keys and values
$keys = OAuth::urlencode(array_keys($params));
$values = OAuth::urlencode(array_values($params));
Expand Down Expand Up @@ -124,18 +130,6 @@ public static function normalize_params(array $params)
return implode('&', $query);
}

public static function normalize_post(array $params)
{
$query = array();

foreach ($params as $field => $value)
{
$query[] = $field.'='.$value;
}

return implode('&', $query);
}

/**
* Parse the query string out of the URL and return it as parameters.
* All GET parameters must be removed from the request URL when building
Expand Down
2 changes: 1 addition & 1 deletion classes/kohana/oauth/consumer.php
Expand Up @@ -3,7 +3,7 @@
* OAuth Consumer
*
* @package Kohana/OAuth
* @package Base
* @category Base
* @author Kohana Team
* @copyright (c) 2010 Kohana Team
* @license http://kohanaframework.org/license
Expand Down
11 changes: 11 additions & 0 deletions classes/kohana/oauth/provider.php
Expand Up @@ -28,6 +28,11 @@ public static function factory($name, array $options = NULL)
return new $class($options);
}

/**
* @var string provider name
*/
public $name;

/**
* @var array additional request parameters to be used for remote requests
*/
Expand Down Expand Up @@ -58,6 +63,12 @@ public function __construct(array $options = NULL)
// Convert the signature name into an object
$this->signature = OAuth_Signature::factory($this->signature);
}

if ( ! $this->name)
{
// Attempt to guess the name from the class name
$this->name = strtolower(substr(get_class($this), strlen('OAuth_Provider_')));
}
}

/**
Expand Down
2 changes: 2 additions & 0 deletions classes/kohana/oauth/provider/google.php
Expand Up @@ -19,6 +19,8 @@
*/
class Kohana_OAuth_Provider_Google extends OAuth_Provider {

public $name = 'google';

protected $signature = 'HMAC-SHA1';

public function url_request_token()
Expand Down
2 changes: 2 additions & 0 deletions classes/kohana/oauth/provider/twitter.php
Expand Up @@ -17,6 +17,8 @@
*/
class Kohana_OAuth_Provider_Twitter extends OAuth_Provider {

public $name = 'twitter';

protected $signature = 'HMAC-SHA1';

public function url_request_token()
Expand Down
86 changes: 37 additions & 49 deletions classes/kohana/oauth/request.php
Expand Up @@ -64,11 +64,6 @@ public static function factory($type, $method, $url = NULL, array $params = NULL
*/
protected $required = array();

/**
* @var array POST body
*/
protected $post = array();

/**
* Set the request URL, method, and parameters.
*
Expand Down Expand Up @@ -179,7 +174,6 @@ public function nonce()
* @param OAuth_Request request to sign
* @return string
* @uses OAuth::urlencode
* @uses OAuth::normalize_post
* @uses OAuth::normalize_params
*/
public function base_string()
Expand All @@ -189,12 +183,6 @@ public function base_string()
// Get the request parameters
$params = $this->params;

if ($this->post)
{
// Add the POST fields to the parameters
$params += $this->post;
}

// "oauth_signature" is never included in the base string!
unset($params['oauth_signature']);

Expand Down Expand Up @@ -281,30 +269,6 @@ public function params(array $params, $duplicate = FALSE)
return $this;
}

/**
* Get and set POST fields. Adding a value will cause the request to be
* sent with the HTTP POST method.
*
* $request->post($field, $value);
*
* @param string field name
* @param string field value
* @return string when getting
* @return $this when setting
* @uses Arr::get
*/
public function post($field, $value = NULL)
{
if (func_num_args() < 2)
{
return Arr::get($this->post, $field);
}

$this->post[$field] = $value;

return $this;
}

/**
* Get and set required parameters.
*
Expand Down Expand Up @@ -343,9 +307,12 @@ public function as_header()

foreach ($this->params as $name => $value)
{
// OAuth Spec 5.4.1
// "Parameter names and values are encoded per Parameter Encoding [RFC 3986]."
$header[] = OAuth::urlencode($name).'="'.OAuth::urlencode($value).'"';
if (strpos($name, 'oauth_') === 0)
{
// OAuth Spec 5.4.1
// "Parameter names and values are encoded per Parameter Encoding [RFC 3986]."
$header[] = OAuth::urlencode($name).'="'.OAuth::urlencode($value).'"';
}
}

return 'OAuth '.implode(', ', $header);
Expand All @@ -359,11 +326,32 @@ public function as_header()
*
* [!!] This method implements [OAuth 1.0 Spec 5.2 (2,3)](http://oauth.net/core/1.0/#rfc.section.5.2).
*
* @param boolean include oauth parameters
* @return string
*/
public function as_query()
public function as_query($include_oauth = NULL)
{
return OAuth::normalize_params($this->params);
if ($include_oauth !== TRUE AND $this->send_header)
{
// If we are sending a header, OAuth parameters should not be
// included in the query string.

$params = array();
foreach ($this->params as $name => $value)
{
if (strpos($name, 'oauth_') !== 0)
{
// This is not an OAuth parameter
$params[$name] = $value;
}
}
}
else
{
$params = $this->params;
}

return OAuth::normalize_params($params);
}

/**
Expand All @@ -376,7 +364,7 @@ public function as_query()
*/
public function as_url()
{
return $this->url.'?'.$this->as_query();
return $this->url.'?'.$this->as_query(TRUE);
}

/**
Expand Down Expand Up @@ -466,23 +454,23 @@ public function execute(array $options = NULL)
// Store the new headers
$options[CURLOPT_HTTPHEADER] = $headers;
}
elseif ($query = $this->as_query())
{
// Append the parameters to the query string
$url = "{$url}?{$query}";
}

if ($this->method === 'POST')
{
// Send the request as a POST
$options[CURLOPT_POST] = TRUE;

if ($this->post)
if ($post = $this->as_query())
{
// Attach the post fields to the request
$options[CURLOPT_POSTFIELDS] = OAuth::normalize_post($this->post);
$options[CURLOPT_POSTFIELDS] = $post;
}
}
elseif ($query = $this->as_query())
{
// Append the parameters to the query string
$url = "{$url}?{$query}";
}

return Remote::get($url, $options);
}
Expand Down
2 changes: 1 addition & 1 deletion classes/kohana/oauth/response.php
Expand Up @@ -3,7 +3,7 @@
* OAuth Response
*
* @package Kohana/OAuth
* @package Base
* @category Base
* @author Kohana Team
* @copyright (c) 2010 Kohana Team
* @license http://kohanaframework.org/license
Expand Down

0 comments on commit 059df80

Please sign in to comment.