/application/third_party/hybridauth/Hybrid/thirdparty/OAuth/OAuth2Client.php

https://bitbucket.org/paulkish/no-cms · PHP · 243 lines · 164 code · 53 blank · 26 comment · 25 complexity · 8bdc2340313102d4fabc59e213779163 MD5 · raw file

  1. <?php
  2. /*!
  3. * HybridAuth
  4. * http://hybridauth.sourceforge.net | http://github.com/hybridauth/hybridauth
  5. * (c) 2009-2012, HybridAuth authors | http://hybridauth.sourceforge.net/licenses.html
  6. */
  7. // A service client for the OAuth 2 flow.
  8. // v0.1
  9. class OAuth2Client
  10. {
  11. public $api_base_url = "";
  12. public $authorize_url = "";
  13. public $token_url = "";
  14. public $token_info_url = "";
  15. public $client_id = "" ;
  16. public $client_secret = "" ;
  17. public $redirect_uri = "" ;
  18. public $access_token = "" ;
  19. public $refresh_token = "" ;
  20. public $access_token_expires_in = "" ;
  21. public $access_token_expires_at = "" ;
  22. //--
  23. public $sign_token_name = "access_token";
  24. public $decode_json = true;
  25. public $curl_time_out = 30;
  26. public $curl_connect_time_out = 30;
  27. public $curl_ssl_verifypeer = false;
  28. public $curl_header = array();
  29. public $curl_useragent = "OAuth/2 Simple PHP Client v0.1; HybridAuth http://hybridauth.sourceforge.net/";
  30. public $curl_authenticate_method = "POST";
  31. public $curl_proxy = null;
  32. //--
  33. public $http_code = "";
  34. public $http_info = "";
  35. //--
  36. public function __construct( $client_id = false, $client_secret = false, $redirect_uri='' )
  37. {
  38. $this->client_id = $client_id;
  39. $this->client_secret = $client_secret;
  40. $this->redirect_uri = $redirect_uri;
  41. }
  42. public function authorizeUrl( $extras = array() )
  43. {
  44. $params = array(
  45. "client_id" => $this->client_id,
  46. "redirect_uri" => $this->redirect_uri,
  47. "response_type" => "code"
  48. );
  49. if( count($extras) )
  50. foreach( $extras as $k=>$v )
  51. $params[$k] = $v;
  52. return $this->authorize_url . "?" . http_build_query( $params );
  53. }
  54. public function authenticate( $code )
  55. {
  56. $params = array(
  57. "client_id" => $this->client_id,
  58. "client_secret" => $this->client_secret,
  59. "grant_type" => "authorization_code",
  60. "redirect_uri" => $this->redirect_uri,
  61. "code" => $code
  62. );
  63. $response = $this->request( $this->token_url, $params, $this->curl_authenticate_method );
  64. $response = $this->parseRequestResult( $response );
  65. if( ! $response || ! isset( $response->access_token ) ){
  66. throw new Exception( "The Authorization Service has return: " . $response->error );
  67. }
  68. if( isset( $response->access_token ) ) $this->access_token = $response->access_token;
  69. if( isset( $response->refresh_token ) ) $this->refresh_token = $response->refresh_token;
  70. if( isset( $response->expires_in ) ) $this->access_token_expires_in = $response->expires_in;
  71. // calculate when the access token expire
  72. $this->access_token_expires_at = time() + $response->expires_in;
  73. return $response;
  74. }
  75. public function authenticated()
  76. {
  77. if ( $this->access_token ){
  78. if ( $this->token_info_url && $this->refresh_token ){
  79. // check if this access token has expired,
  80. $tokeninfo = $this->tokenInfo( $this->access_token );
  81. // if yes, access_token has expired, then ask for a new one
  82. if( $tokeninfo && isset( $tokeninfo->error ) ){
  83. $response = $this->refreshToken( $this->refresh_token );
  84. // if wrong response
  85. if( ! isset( $response->access_token ) || ! $response->access_token ){
  86. throw new Exception( "The Authorization Service has return an invalid response while requesting a new access token. given up!" );
  87. }
  88. // set new access_token
  89. $this->access_token = $response->access_token;
  90. }
  91. }
  92. return true;
  93. }
  94. return false;
  95. }
  96. /**
  97. * Format and sign an oauth for provider api
  98. */
  99. public function api( $url, $method = "GET", $parameters = array() )
  100. {
  101. if ( strrpos($url, 'http://') !== 0 && strrpos($url, 'https://') !== 0 ) {
  102. $url = $this->api_base_url . $url;
  103. }
  104. $parameters[$this->sign_token_name] = $this->access_token;
  105. $response = null;
  106. switch( $method ){
  107. case 'GET' : $response = $this->request( $url, $parameters, "GET" ); break;
  108. case 'POST' : $response = $this->request( $url, $parameters, "POST" ); break;
  109. }
  110. if( $response && $this->decode_json ){
  111. $response = json_decode( $response );
  112. }
  113. return $response;
  114. }
  115. /**
  116. * GET wrappwer for provider apis request
  117. */
  118. function get( $url, $parameters = array() )
  119. {
  120. return $this->api( $url, 'GET', $parameters );
  121. }
  122. /**
  123. * POST wreapper for provider apis request
  124. */
  125. function post( $url, $parameters = array() )
  126. {
  127. return $this->api( $url, 'POST', $parameters );
  128. }
  129. // -- tokens
  130. public function tokenInfo($accesstoken)
  131. {
  132. $params['access_token'] = $this->access_token;
  133. $response = $this->request( $this->token_info_url, $params );
  134. return $this->parseRequestResult( $response );
  135. }
  136. public function refreshToken( $parameters = array() )
  137. {
  138. $params = array(
  139. "client_id" => $this->client_id,
  140. "client_secret" => $this->client_secret,
  141. "grant_type" => "refresh_token"
  142. );
  143. foreach($parameters as $k=>$v ){
  144. $params[$k] = $v;
  145. }
  146. $response = $this->request( $this->token_url, $params, "POST" );
  147. return $this->parseRequestResult( $response );
  148. }
  149. // -- utilities
  150. private function request( $url, $params=false, $type="GET" )
  151. {
  152. Hybrid_Logger::info( "Enter OAuth2Client::request( $url )" );
  153. Hybrid_Logger::debug( "OAuth2Client::request(). dump request params: ", serialize( $params ) );
  154. if( $type == "GET" ){
  155. $url = $url . ( strpos( $url, '?' ) ? '&' : '?' ) . http_build_query( $params );
  156. }
  157. $this->http_info = array();
  158. $ch = curl_init();
  159. curl_setopt($ch, CURLOPT_URL , $url );
  160. curl_setopt($ch, CURLOPT_RETURNTRANSFER , 1 );
  161. curl_setopt($ch, CURLOPT_TIMEOUT , $this->curl_time_out );
  162. curl_setopt($ch, CURLOPT_USERAGENT , $this->curl_useragent );
  163. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT , $this->curl_connect_time_out );
  164. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER , $this->curl_ssl_verifypeer );
  165. curl_setopt($ch, CURLOPT_HTTPHEADER , $this->curl_header );
  166. if($this->curl_proxy){
  167. curl_setopt( $ch, CURLOPT_PROXY , $this->curl_proxy);
  168. }
  169. if( $type == "POST" ){
  170. curl_setopt($ch, CURLOPT_POST, 1);
  171. if($params) curl_setopt( $ch, CURLOPT_POSTFIELDS, $params );
  172. }
  173. $response = curl_exec($ch);
  174. Hybrid_Logger::debug( "OAuth2Client::request(). dump request info: ", serialize( curl_getinfo($ch) ) );
  175. Hybrid_Logger::debug( "OAuth2Client::request(). dump request result: ", serialize( $response ) );
  176. $this->http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  177. $this->http_info = array_merge($this->http_info, curl_getinfo($ch));
  178. curl_close ($ch);
  179. return $response;
  180. }
  181. private function parseRequestResult( $result )
  182. {
  183. if( json_decode( $result ) ) return json_decode( $result );
  184. parse_str( $result, $ouput );
  185. $result = new StdClass();
  186. foreach( $ouput as $k => $v )
  187. $result->$k = $v;
  188. return $result;
  189. }
  190. }