PageRenderTime 28ms CodeModel.GetById 5ms RepoModel.GetById 1ms app.codeStats 0ms

/bundles/oneauth/libraries/oauth/provider.php

https://bitbucket.org/chakkimatti/vv2
PHP | 289 lines | 112 code | 32 blank | 145 comment | 7 complexity | d20ab4dbc2b91c9dd53bcebc88635329 MD5 | raw file
  1. <?php namespace OneAuth\OAuth;
  2. /**
  3. * Ported from Kohana\OAuth package
  4. *
  5. * @package Kohana/OAuth
  6. * @author Kohana Team
  7. * @copyright (c) 2010 Kohana Team
  8. * @license http://kohanaframework.org/license
  9. * @since 3.0.7
  10. */
  11. use \Input;
  12. abstract class Provider
  13. {
  14. /**
  15. * The third-party driver registrar.
  16. *
  17. * @var array
  18. */
  19. public static $registrar = array();
  20. /**
  21. * Register a third-party authentication driver.
  22. *
  23. * @param string $driver
  24. * @param Closure $resolver
  25. * @return void
  26. */
  27. public static function extend($driver, Closure $resolver)
  28. {
  29. static::$registrar[$driver] = $resolver;
  30. OnaAuth\Auth\Strategy::add('OAuth', $driver);
  31. }
  32. /**
  33. * Create a new provider.
  34. *
  35. * // Load the Twitter provider
  36. * $provider = Provider::make('twitter');
  37. *
  38. * @param string provider name
  39. * @param array provider options
  40. * @return Provider
  41. */
  42. public static function make($driver, array $options = null)
  43. {
  44. if (isset(static::$registrar[$driver]))
  45. {
  46. $resolver = static::$registrar[$driver];
  47. return $resolver();
  48. }
  49. switch ($driver)
  50. {
  51. case 'dropbox' :
  52. return new Provider\Dropbox($options);
  53. break;
  54. case 'flickr' :
  55. return new Provider\Flickr($options);
  56. break;
  57. case 'linkedin' :
  58. return new Provider\Linkedin($options);
  59. break;
  60. case 'tumblr' :
  61. return new Provider\Tumblr($options);
  62. break;
  63. case 'twitter' :
  64. return new Provider\Twitter($options);
  65. break;
  66. case 'vimeo' :
  67. return new Provider\Vimeo($options);
  68. break;
  69. }
  70. }
  71. /**
  72. * @var string provider name
  73. */
  74. public $name;
  75. /**
  76. * @var string signature type
  77. */
  78. protected $signature = 'HMAC-SHA1';
  79. /**
  80. * @var string uid key name
  81. */
  82. public $uid_key = 'uid';
  83. /**
  84. * @var array additional request parameters to be used for remote
  85. * requests
  86. */
  87. protected $params = array();
  88. /**
  89. * @var string scope separator, most use "," but some like Google are
  90. * spaces
  91. */
  92. public $scope_seperator = ',';
  93. /**
  94. * Overloads default class properties from the options.
  95. *
  96. * Any of the provider options can be set here:
  97. *
  98. * Type | Option | Description | Default Value
  99. * ----------|---------------|------------------------------------------------|-----------------
  100. * mixed | signature | Signature method name or object | provider default
  101. *
  102. * @param array provider options
  103. * @return void
  104. */
  105. public function __construct(array $options = null)
  106. {
  107. if (isset($options['signature']))
  108. {
  109. // Set the signature method name or object
  110. $this->signature = $options['signature'];
  111. }
  112. if ( ! is_object($this->signature))
  113. {
  114. // Convert the signature name into an object
  115. $this->signature = Signature::make($this->signature);
  116. }
  117. }
  118. /**
  119. * Return the value of any protected class variable.
  120. *
  121. * // Get the provider signature
  122. * $signature = $provider->signature;
  123. *
  124. * @param string variable name
  125. * @return mixed
  126. */
  127. public function __get($key)
  128. {
  129. return $this->$key;
  130. }
  131. /**
  132. * Returns the request token URL for the provider.
  133. *
  134. * $url = $provider->url_request_token();
  135. *
  136. * @return string
  137. */
  138. abstract public function url_request_token();
  139. /**
  140. * Returns the authorization URL for the provider.
  141. *
  142. * $url = $provider->url_authorize();
  143. *
  144. * @return string
  145. */
  146. abstract public function url_authorize();
  147. /**
  148. * Returns the access token endpoint for the provider.
  149. *
  150. * $url = $provider->url_access_token();
  151. *
  152. * @return string
  153. */
  154. abstract public function url_access_token();
  155. /**
  156. * Returns basic information about the user.
  157. *
  158. * $url = $provider->get_user_info();
  159. *
  160. * @return string
  161. */
  162. abstract public function get_user_info(Token $token, Consumer $consumer);
  163. /**
  164. * Ask for a request token from the OAuth provider.
  165. *
  166. * $token = $provider->request_token($consumer);
  167. *
  168. * @param Consumer consumer
  169. * @param array additional request parameters
  170. * @return Token_Request
  171. * @uses Request_Token
  172. */
  173. public function request_token(Consumer $consumer, array $params = null)
  174. {
  175. // Create a new GET request for a request token with the required
  176. // parameters
  177. $request = Request::make('token', 'GET', $this->url_request_token(), array(
  178. 'oauth_consumer_key' => $consumer->key,
  179. 'oauth_callback' => $consumer->callback,
  180. 'scope' => is_array($consumer->scope) ? implode($this->scope_seperator, $consumer->scope) : $consumer->scope,
  181. ));
  182. if ($params)
  183. {
  184. // Load user parameters
  185. $request->params($params);
  186. }
  187. // Sign the request using only the consumer, no token is available
  188. // yet
  189. $request->sign($this->signature, $consumer);
  190. // Create a response from the request
  191. $response = $request->execute();
  192. // Store this token somewhere useful
  193. return Token::make('request', array(
  194. 'access_token' => $response->param('oauth_token'),
  195. 'secret' => $response->param('oauth_token_secret'),
  196. ));
  197. }
  198. /**
  199. * Get the authorization URL for the request token.
  200. *
  201. * Response::redirect($provider->authorize_url($token));
  202. *
  203. * @param Token_Request token
  204. * @param array additional request parameters
  205. * @return string
  206. */
  207. public function authorize_url(Token\Request $token, array $params = null)
  208. {
  209. // Create a new GET request for a request token with the required
  210. // parameters
  211. $request = Request::make('authorize', 'GET', $this->url_authorize(), array(
  212. 'oauth_token' => $token->access_token,
  213. ));
  214. if ($params)
  215. {
  216. // Load user parameters
  217. $request->params($params);
  218. }
  219. return $request->as_url();
  220. }
  221. /**
  222. * Exchange the request token for an access token.
  223. *
  224. * $token = $provider->access_token($consumer, $token);
  225. *
  226. * @param Consumer consumer
  227. * @param Token_Request token
  228. * @param array additional request parameters
  229. * @return Token_Access
  230. */
  231. public function access_token(Token\Request $token, Consumer $consumer, array $params = null)
  232. {
  233. // Create a new GET request for a request token with the required
  234. // parameters
  235. $request = Request::make('access', 'GET', $this->url_access_token(), array(
  236. 'oauth_consumer_key' => $consumer->key,
  237. 'oauth_token' => $token->access_token,
  238. 'oauth_verifier' => $token->verifier,
  239. ));
  240. if ($params)
  241. {
  242. // Load user parameters
  243. $request->params($params);
  244. }
  245. // Sign the request using only the consumer, no token is available
  246. // yet
  247. $request->sign($this->signature, $consumer, $token);
  248. // Create a response from the request
  249. $response = $request->execute();
  250. // Store this token somewhere useful
  251. return Token::make('access', array(
  252. 'access_token' => $response->param('oauth_token'),
  253. 'secret' => $response->param('oauth_token_secret'),
  254. 'uid' => $response->param($this->uid_key) ?: Input::get($this->uid_key),
  255. ));
  256. }
  257. }