PageRenderTime 49ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/bundles/laravel-oauth2/OAuth2_Provider.php

https://gitlab.com/waltspence/gtd-pad
PHP | 213 lines | 103 code | 37 blank | 73 comment | 6 complexity | 9e0da158bed760a634985ec2d32db801 MD5 | raw file
  1. <?php
  2. /**
  3. * OAuth Provider
  4. *
  5. * @package CodeIgniter/OAuth
  6. * @category Provider
  7. * @author Phil Sturgeon
  8. * @copyright Phil Sturgeon
  9. * @license http://philsturgeon.co.uk/code/dbad-license
  10. */
  11. abstract class OAuth2_Provider {
  12. /**
  13. * @var string provider name
  14. */
  15. public $name;
  16. /**
  17. * @var string uid key name
  18. */
  19. public $uid_key = 'uid';
  20. /**
  21. * @var string scope separator, most use "," but some like Google are spaces
  22. */
  23. public $scope_seperator = ',';
  24. /**
  25. * @var string additional request parameters to be used for remote requests
  26. */
  27. public $callback = null;
  28. /**
  29. * @var string scope
  30. */
  31. public $scope;
  32. /**
  33. * @var array additional request parameters to be used for remote requests
  34. */
  35. protected $params = array();
  36. /**
  37. * @var string the method to use when requesting tokens
  38. */
  39. protected $method = 'GET';
  40. /**
  41. * Overloads default class properties from the options.
  42. *
  43. * Any of the provider options can be set here, such as app_id or secret.
  44. *
  45. * @param array provider options
  46. * @return void
  47. */
  48. public function __construct(array $options = array())
  49. {
  50. if ( ! $this->name)
  51. {
  52. // Attempt to guess the name from the class name
  53. $this->name = strtolower(substr(get_class($this), strlen('OAuth2_Provider_')));
  54. }
  55. if (empty($options['id']))
  56. {
  57. throw new Exception('Required option not provided: id');
  58. }
  59. $this->client_id = $options['id'];
  60. isset($options['callback']) and $this->callback = $options['callback'];
  61. isset($options['secret']) and $this->client_secret = $options['secret'];
  62. isset($options['scope']) and $this->scope = $options['scope'];
  63. $this->redirect_uri = URL::to(URI::current()); // '/'.ltrim(Laravel\URI::current(), '/');
  64. }
  65. /**
  66. * Return the value of any protected class variable.
  67. *
  68. * // Get the provider signature
  69. * $signature = $provider->signature;
  70. *
  71. * @param string variable name
  72. * @return mixed
  73. */
  74. public function __get($key)
  75. {
  76. return $this->$key;
  77. }
  78. /**
  79. * Returns the authorization URL for the provider.
  80. *
  81. * $url = $provider->url_authorize();
  82. *
  83. * @return string
  84. */
  85. abstract public function url_authorize();
  86. /**
  87. * Returns the access token endpoint for the provider.
  88. *
  89. * $url = $provider->url_access_token();
  90. *
  91. * @return string
  92. */
  93. abstract public function url_access_token();
  94. /*
  95. * Get an authorization code from Facebook. Redirects to Facebook, which this redirects back to the app using the redirect address you've set.
  96. */
  97. public function authorize($options = array())
  98. {
  99. $state = md5(uniqid(rand(), TRUE));
  100. Laravel\Session::put('state', $state);
  101. $params = array(
  102. 'client_id' => $this->client_id,
  103. 'redirect_uri' => isset($options['redirect_uri']) ? $options['redirect_uri'] : $this->redirect_uri,
  104. 'state' => $state,
  105. 'scope' => is_array($this->scope) ? implode($this->scope_seperator, $this->scope) : $this->scope,
  106. 'response_type' => 'code'//,
  107. //'approval_prompt' => 'force' // - google force-recheck
  108. );
  109. $url = $this->url_authorize().'?'.http_build_query($params);
  110. return Laravel\Redirect::to($url);
  111. }
  112. /*
  113. * Get access to the API
  114. *
  115. * @param string The access code
  116. * @return object Success or failure along with the response details
  117. */
  118. public function access($code, $options = array())
  119. {
  120. $params = array(
  121. 'client_id' => $this->client_id,
  122. 'client_secret' => $this->client_secret,
  123. 'grant_type' => isset($options['grant_type']) ? $options['grant_type'] : 'authorization_code',
  124. );
  125. switch ($params['grant_type'])
  126. {
  127. case 'authorization_code':
  128. $params['code'] = $code;
  129. $params['redirect_uri'] = isset($options['redirect_uri']) ? $options['redirect_uri'] : $this->redirect_uri;
  130. break;
  131. case 'refresh_token':
  132. $params['refresh_token'] = $code;
  133. break;
  134. }
  135. $response = null;
  136. $url = $this->url_access_token();
  137. switch ($this->method)
  138. {
  139. case 'GET':
  140. // Need to switch to Request library, but need to test it on one that works
  141. $url .= '?'.http_build_query($params);
  142. $response = file_get_contents($url);
  143. parse_str($response, $return);
  144. break;
  145. case 'POST':
  146. $postdata = http_build_query($params);
  147. $opts = array(
  148. 'http' => array(
  149. 'method' => 'POST',
  150. 'header' => 'Content-type: application/x-www-form-urlencoded',
  151. 'content' => $postdata
  152. )
  153. );
  154. $_default_opts = stream_context_get_params(stream_context_get_default());
  155. $context = stream_context_create(array_merge_recursive($_default_opts['options'], $opts));
  156. $response = file_get_contents($url, false, $context);
  157. $return = json_decode($response, true);
  158. break;
  159. default:
  160. throw new OutOfBoundsException("Method '{$this->method}' must be either GET or POST");
  161. }
  162. if (isset($return['error']))
  163. {
  164. throw new OAuth2_Exception($return);
  165. }
  166. switch ($params['grant_type'])
  167. {
  168. case 'authorization_code':
  169. return OAuth2_Token::factory('access', $return);
  170. break;
  171. case 'refresh_token':
  172. return OAuth2_Token::factory('refresh', $return);
  173. break;
  174. }
  175. }
  176. }