Modules

OAuth_Request_Credentials
extends Kohana_OAuth_Request_Credentials
extends OAuth_Request
extends Kohana_OAuth_Request

OAuth Credentials Request

package
Kohana/OAuth
category
Request
author
Kohana Team
copyright
© 2010 Kohana Team
license
http://kohanaframework.org/license
since
3.0.7

Class declared in MODPATH/oauth/classes/oauth/request/credentials.php on line 3.

Properties

public boolean $send_header

send Authorization header?

public integer $timeout

connection timeout

protected string $method

request method: GET, POST, etc

protected string $name

request type name: token, authorize, access, resource

protected array $params

request parameters

protected array $required

required parameters

protected array $upload

upload parameters

protected string $url

request URL

Methods

public __construct( string $method , string $url [, array $params = NULL ] ) (defined in Kohana_OAuth_Request)

Set the request URL, method, and parameters.

Parameters

  • string $method required - Request method
  • string $url required - Request URL
  • array $params = NULL - Request parameters

Tags

Source Code

public function __construct($method, $url, array $params = NULL)
{
	if ($method)
	{
		// Set the request method
		$this->method = strtoupper($method);
	}

	// Separate the URL and query string, which will be used as additional
	// default parameters
	list ($url, $default) = OAuth::parse_url($url);

	// Set the request URL
	$this->url = $url;

	if ($default)
	{
		// Set the default parameters
		$this->params($default);
	}

	if ($params)
	{
		// Set the request parameters
		$this->params($params);
	}

	if ($this->required('oauth_version') AND ! isset($this->params['oauth_version']))
	{
		// Set the version of this request
		$this->params['oauth_version'] = OAuth::$version;
	}

	if ($this->required('oauth_timestamp') AND ! isset($this->params['oauth_timestamp']))
	{
		// Set the timestamp of this request
		$this->params['oauth_timestamp'] = $this->timestamp();
	}

	if ($this->required('oauth_nonce') AND ! isset($this->params['oauth_nonce']))
	{
		// Set the unique nonce of this request
		$this->params['oauth_nonce'] = $this->nonce();
	}
}

public __get( string $key ) (defined in Kohana_OAuth_Request)

Return the value of any protected class variable.

// Get the request parameters
$params = $request->params;

// Get the request URL
$url = $request->url;

Parameters

  • string $key required - Variable name

Return Values

  • mixed

Source Code

public function __get($key)
{
	return $this->$key;
}

public as_header( ) (defined in Kohana_OAuth_Request)

Convert the request parameters into an Authorization header.

$header = $request->as_header();

This method implements OAuth 1.0 Spec 5.4.1.

Return Values

  • string

Source Code

public function as_header()
{
	$header = array();

	foreach ($this->params as $name => $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);
}

public as_query( [ boolean $include_oauth = NULL , boolean $as_string = bool TRUE ] ) (defined in Kohana_OAuth_Request)

Convert the request parameters into a query string, suitable for GET and POST requests.

$query = $request->as_query();

This method implements OAuth 1.0 Spec 5.2 (2,3).

Parameters

  • boolean $include_oauth = NULL - Include oauth parameters?
  • boolean $as_string = bool TRUE - Return a normalized string?

Return Values

  • string

Source Code

public function as_query($include_oauth = NULL, $as_string = TRUE)
{
	if ($include_oauth === NULL)
	{
		// If we are sending a header, OAuth parameters should not be
		// included in the query string.
		$include_oauth = ! $this->send_header;
	}

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

	return $as_string ? OAuth::normalize_params($params) : $params;
}

public as_url( ) (defined in Kohana_OAuth_Request)

Return the entire request URL with the parameters as a GET string.

$url = $request->as_url();

Tags

Return Values

  • string

Source Code

public function as_url()
{
	return $this->url.'?'.$this->as_query(TRUE);
}

public base_string( ) (defined in Kohana_OAuth_Request)

Get the base signature string for a request.

$base = $request->base_string();

This method implements OAuth 1.0 Spec A5.1.

Tags

Return Values

  • string

Source Code

public function base_string()
{
	$url = $this->url;

	// Get the request parameters
	$params = array_diff_key($this->params, $this->upload);

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

	// method & url & sorted-parameters
	return implode('&', array(
		$this->method,
		OAuth::urlencode($url),
		OAuth::urlencode(OAuth::normalize_params($params)),
	));
}

public check( ) (defined in Kohana_OAuth_Request)

Checks that all required request parameters have been set. Throws an exception if any parameters are missing.

try
{
    $request->check();
}
catch (OAuth_Exception $e)
{
    // Request has missing parameters
}

Tags

Return Values

  • TRUE

Source Code

public function check()
{
	foreach ($this->required as $param => $required)
	{
		if ($required AND ! isset($this->params[$param]))
		{
			throw new Kohana_OAuth_Exception('Request to :url requires missing parameter ":param"', array(
				':url'   => $this->url,
				':param' => $param,
			));
		}
	}

	return TRUE;
}

public execute( [ array $options = NULL ] ) (defined in Kohana_OAuth_Request)

Execute the request and return a response.

Parameters

  • array $options = NULL - Additional cURL options

Tags

Return Values

  • string - Request response body

Source Code

public function execute(array $options = NULL)
{
	// Check that all required fields are set
	$this->check();

	// Get the URL of the request
	$url = $this->url;

	if ( ! isset($options[CURLOPT_CONNECTTIMEOUT]))
	{
		// Use the request default timeout
		$options[CURLOPT_CONNECTTIMEOUT] = $this->timeout;
	}

	if ($this->send_header)
	{
		// Get the the current headers
		$headers = Arr::get($options, CURLOPT_HTTPHEADER, array());

		// Add the Authorization header
		$headers[] = 'Authorization: '.$this->as_header();

		// Store the new headers
		$options[CURLOPT_HTTPHEADER] = $headers;
	}

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

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

	return OAuth::remote($url, $options);
}

public static factory( string $type , string $method [, string $url = NULL , array $params = NULL ] ) (defined in Kohana_OAuth_Request)

Create a new request object.

$request = OAuth_Request::factory('token', 'http://example.com/oauth/request_token');

Parameters

  • string $type required - Request type
  • string $method required - Request URL
  • string $url = NULL - Request method
  • array $params = NULL - Request parameters

Return Values

  • OAuth_Request

Source Code

public static function factory($type, $method, $url = NULL, array $params = NULL)
{
	$class = 'OAuth_Request_'.$type;

	return new $class($method, $url, $params);
}

public nonce( ) (defined in Kohana_OAuth_Request)

Generates the nonce for a request.

$nonce = $request->nonce();

This method implements OAuth 1.0 Spec 8.

Tags

Return Values

  • string

Source Code

public function nonce()
{
	return Text::random('alnum', 40);
}

public param( string $name [, mixed $value = NULL , boolean $duplicate = bool FALSE ] ) (defined in Kohana_OAuth_Request)

Parameter getter and setter. Setting the value to NULL will remove it.

// Set the "oauth_consumer_key" to a new value
$request->param('oauth_consumer_key', $key);

// Get the "oauth_consumer_key" value
$key = $request->param('oauth_consumer_key');

Parameters

  • string $name required - Parameter name
  • mixed $value = NULL - Parameter value
  • boolean $duplicate = bool FALSE - Allow duplicates?

Tags

Return Values

  • mixed - When getting
  • $this - When setting

Source Code

public function param($name, $value = NULL, $duplicate = FALSE)
{
	if ($value === NULL)
	{
		// Get the parameter
		return Arr::get($this->params, $name);
	}

	if (isset($this->params[$name]) AND $duplicate)
	{
		if ( ! is_array($this->params[$name]))
		{
			// Convert the parameter into an array
			$this->params[$name] = array($this->params[$name]);
		}

		// Add the duplicate value
		$this->params[$name][] = $value;
	}
	else
	{
		// Set the parameter value
		$this->params[$name] = $value;
	}

	return $this;
}

public params( array $params [, boolean $duplicate = bool FALSE ] ) (defined in Kohana_OAuth_Request)

Set multiple parameters.

$request->params($params);

Parameters

  • array $params required - Parameters
  • boolean $duplicate = bool FALSE - Allow duplicates?

Tags

Return Values

  • $this

Source Code

public function params(array $params, $duplicate = FALSE)
{
	foreach ($params as $name => $value)
	{
		$this->param($name, $value, $duplicate);
	}

	return $this;
}

public required( string $param [, boolean $value = NULL ] ) (defined in Kohana_OAuth_Request)

Get and set required parameters.

$request->required($field, $value);

Parameters

  • string $param required - Parameter name
  • boolean $value = NULL - Field value

Return Values

  • boolean - When getting
  • $this - When setting

Source Code

public function required($param, $value = NULL)
{
	if ($value === NULL)
	{
		// Get the current status
		return ! empty($this->required[$param]);
	}

	// Change the requirement value
	$this->required[$param] = (boolean) $value;

	return $this;
}

public sign( OAuth_Signature $signature , OAuth_Consumer $consumer [, OAuth_Token $token = NULL ] ) (defined in Kohana_OAuth_Request)

Sign the request, setting the oauth_signature_method and oauth_signature.

Parameters

  • OAuth_Signature $signature required - Signature
  • OAuth_Consumer $consumer required - Consumer
  • OAuth_Token $token = NULL - Token

Tags

Return Values

  • $this

Source Code

public function sign(OAuth_Signature $signature, OAuth_Consumer $consumer, OAuth_Token $token = NULL)
{
	// Create a new signature class from the method
	$this->param('oauth_signature_method', $signature->name);

	// Sign the request using the consumer and token
	$this->param('oauth_signature', $signature->sign($this, $consumer, $token));

	return $this;
}

public timestamp( ) (defined in Kohana_OAuth_Request)

Generates the UNIX timestamp for a request.

$time = $request->timestamp();

This method implements OAuth 1.0 Spec 8.

Return Values

  • integer

Source Code

public function timestamp()
{
	return time();
}

public upload( string $name [, mixed $value = NULL ] ) (defined in Kohana_OAuth_Request)

Upload getter and setter. Setting the value to NULL will remove it.

// Set the "image" file path for uploading
$request->upload('image', $file_path);

// Get the "image" file path
$key = $request->param('oauth_consumer_key');

Parameters

  • string $name required - Upload name
  • mixed $value = NULL - Upload file path

Tags

Return Values

  • mixed - When getting
  • $this - When setting

Source Code

public function upload($name, $value = NULL)
{
	if ($value !== NULL)
	{
		// This is an upload parameter
		$this->upload[$name] = TRUE;

		// Get the mime type of the image
		$mime = File::mime($value);

		// Format the image path for CURL
		$value = "@{$value};type={$mime}";
	}

	return $this->param($name, $value, FALSE);
}