PageRenderTime 53ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/oauth/classes/kohana/oauth/provider.php

https://bitbucket.org/seyar/ari100krat.local
PHP | 215 lines | 78 code | 25 blank | 112 comment | 6 complexity | b24f4337ae281e0955870097b7186e0e MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.1
  1. <?php defined('SYSPATH') OR die('No direct access allowed.');
  2. /**
  3. * OAuth Provider
  4. *
  5. * @package Kohana/OAuth
  6. * @category Provider
  7. * @author Kohana Team
  8. * @copyright (c) 2010 Kohana Team
  9. * @license http://kohanaframework.org/license
  10. * @since 3.0.7
  11. */
  12. abstract class Kohana_OAuth_Provider {
  13. /**
  14. * Create a new provider.
  15. *
  16. * // Load the Twitter provider
  17. * $provider = OAuth_Provider::factory('twitter');
  18. *
  19. * @param string provider name
  20. * @param array provider options
  21. * @return OAuth_Provider
  22. */
  23. public static function factory($name, array $options = NULL)
  24. {
  25. $class = 'OAuth_Provider_'.$name;
  26. return new $class($options);
  27. }
  28. /**
  29. * @var string provider name
  30. */
  31. public $name;
  32. /**
  33. * @var array additional request parameters to be used for remote requests
  34. */
  35. protected $params = array();
  36. /**
  37. * Overloads default class properties from the options.
  38. *
  39. * Any of the provider options can be set here:
  40. *
  41. * Type | Option | Description | Default Value
  42. * ----------|---------------|------------------------------------------------|-----------------
  43. * mixed | signature | Signature method name or object | provider default
  44. *
  45. * @param array provider options
  46. * @return void
  47. */
  48. public function __construct(array $options = NULL)
  49. {
  50. if (isset($options['signature']))
  51. {
  52. // Set the signature method name or object
  53. $this->signature = $options['signature'];
  54. }
  55. if ( ! is_object($this->signature))
  56. {
  57. // Convert the signature name into an object
  58. $this->signature = OAuth_Signature::factory($this->signature);
  59. }
  60. if ( ! $this->name)
  61. {
  62. // Attempt to guess the name from the class name
  63. $this->name = strtolower(substr(get_class($this), strlen('OAuth_Provider_')));
  64. }
  65. }
  66. /**
  67. * Return the value of any protected class variable.
  68. *
  69. * // Get the provider signature
  70. * $signature = $provider->signature;
  71. *
  72. * @param string variable name
  73. * @return mixed
  74. */
  75. public function __get($key)
  76. {
  77. return $this->$key;
  78. }
  79. /**
  80. * Returns the request token URL for the provider.
  81. *
  82. * $url = $provider->url_request_token();
  83. *
  84. * @return string
  85. */
  86. abstract public function url_request_token();
  87. /**
  88. * Returns the authorization URL for the provider.
  89. *
  90. * $url = $provider->url_authorize();
  91. *
  92. * @return string
  93. */
  94. abstract public function url_authorize();
  95. /**
  96. * Returns the access token endpoint for the provider.
  97. *
  98. * $url = $provider->url_access_token();
  99. *
  100. * @return string
  101. */
  102. abstract public function url_access_token();
  103. /**
  104. * Ask for a request token from the OAuth provider.
  105. *
  106. * $token = $provider->request_token($consumer);
  107. *
  108. * @param OAuth_Consumer consumer
  109. * @param array additional request parameters
  110. * @return OAuth_Token_Request
  111. * @uses OAuth_Request_Token
  112. */
  113. public function request_token(OAuth_Consumer $consumer, array $params = NULL)
  114. {
  115. // Create a new GET request for a request token with the required parameters
  116. $request = OAuth_Request::factory('token', 'GET', $this->url_request_token(), array(
  117. 'oauth_consumer_key' => $consumer->key,
  118. 'oauth_callback' => $consumer->callback,
  119. ));
  120. if ($params)
  121. {
  122. // Load user parameters
  123. $request->params($params);
  124. }
  125. // Sign the request using only the consumer, no token is available yet
  126. $request->sign($this->signature, $consumer);
  127. // Create a response from the request
  128. $response = $request->execute();
  129. // Store this token somewhere useful
  130. return OAuth_Token::factory('request', array(
  131. 'token' => $response->param('oauth_token'),
  132. 'secret' => $response->param('oauth_token_secret'),
  133. ));
  134. }
  135. /**
  136. * Get the authorization URL for the request token.
  137. *
  138. * $this->request->redirect($provider->authorize_url($token));
  139. *
  140. * @param OAuth_Token_Request token
  141. * @param array additional request parameters
  142. * @return string
  143. */
  144. public function authorize_url(OAuth_Token_Request $token, array $params = NULL)
  145. {
  146. // Create a new GET request for a request token with the required parameters
  147. $request = OAuth_Request::factory('authorize', 'GET', $this->url_authorize(), array(
  148. 'oauth_token' => $token->token,
  149. ));
  150. if ($params)
  151. {
  152. // Load user parameters
  153. $request->params($params);
  154. }
  155. return $request->as_url();
  156. }
  157. /**
  158. * Exchange the request token for an access token.
  159. *
  160. * $token = $provider->access_token($consumer, $token);
  161. *
  162. * @param OAuth_Consumer consumer
  163. * @param OAuth_Token_Request token
  164. * @param array additional request parameters
  165. * @return OAuth_Token_Access
  166. */
  167. public function access_token(OAuth_Consumer $consumer, OAuth_Token_Request $token, array $params = NULL)
  168. {
  169. // Create a new GET request for a request token with the required parameters
  170. $request = OAuth_Request::factory('access', 'GET', $this->url_access_token(), array(
  171. 'oauth_consumer_key' => $consumer->key,
  172. 'oauth_token' => $token->token,
  173. 'oauth_verifier' => $token->verifier,
  174. ));
  175. if ($params)
  176. {
  177. // Load user parameters
  178. $request->params($params);
  179. }
  180. // Sign the request using only the consumer, no token is available yet
  181. $request->sign($this->signature, $consumer, $token);
  182. // Create a response from the request
  183. $response = $request->execute();
  184. // Store this token somewhere useful
  185. return OAuth_Token::factory('access', array(
  186. 'token' => $response->param('oauth_token'),
  187. 'secret' => $response->param('oauth_token_secret'),
  188. ));
  189. }
  190. } // End OAuth_Signature