PageRenderTime 50ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/wordpress-social-login/hybridauth/Hybrid/thirdparty/OAuth/OAuth2Client.php

https://gitlab.com/code26/selah
PHP | 229 lines | 156 code | 49 blank | 24 comment | 21 complexity | 6396b124a6627a20e9865832907bf8be MD5 | raw file
  1. <?php
  2. /**
  3. * HybridAuth
  4. * http://hybridauth.sourceforge.net | http://github.com/hybridauth/hybridauth
  5. * (c) 2009-2014, 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_ssl_verifyhost = false;
  29. public $curl_header = array();
  30. public $curl_useragent = "OAuth/2 Simple PHP Client v0.1; HybridAuth http://hybridauth.sourceforge.net/";
  31. public $curl_authenticate_method = "POST";
  32. public $curl_proxy = null;
  33. //--
  34. public $http_code = "";
  35. public $http_info = "";
  36. //--
  37. public function __construct( $client_id = false, $client_secret = false, $redirect_uri='' )
  38. {
  39. $this->client_id = $client_id;
  40. $this->client_secret = $client_secret;
  41. $this->redirect_uri = $redirect_uri;
  42. }
  43. public function authorizeUrl( $extras = array() )
  44. {
  45. $params = array(
  46. "client_id" => $this->client_id,
  47. "redirect_uri" => $this->redirect_uri,
  48. "response_type" => "code"
  49. );
  50. if( count($extras) )
  51. foreach( $extras as $k=>$v )
  52. $params[$k] = $v;
  53. return $this->authorize_url . "?" . http_build_query($params, '', '&');
  54. }
  55. public function authenticate( $code )
  56. {
  57. $params = array(
  58. "client_id" => $this->client_id,
  59. "client_secret" => $this->client_secret,
  60. "grant_type" => "authorization_code",
  61. "redirect_uri" => $this->redirect_uri,
  62. "code" => $code
  63. );
  64. $response = $this->request( $this->token_url, $params, $this->curl_authenticate_method );
  65. $response = $this->parseRequestResult( $response );
  66. if( ! $response || ! isset( $response->access_token ) ){
  67. throw new Exception( "The Authorization Service has return: " . $response->error );
  68. }
  69. if( isset( $response->access_token ) ) $this->access_token = $response->access_token;
  70. if( isset( $response->refresh_token ) ) $this->refresh_token = $response->refresh_token;
  71. if( isset( $response->expires_in ) ) $this->access_token_expires_in = $response->expires_in;
  72. // calculate when the access token expire
  73. if( isset($response->expires_in)) {
  74. $this->access_token_expires_at = time() + $response->expires_in;
  75. }
  76. return $response;
  77. }
  78. /**
  79. * Format and sign an oauth for provider api
  80. */
  81. public function api( $url, $method = "GET", $parameters = array() )
  82. {
  83. if ( strrpos($url, 'http://') !== 0 && strrpos($url, 'https://') !== 0 ) {
  84. $url = $this->api_base_url . $url;
  85. }
  86. $parameters[$this->sign_token_name] = $this->access_token;
  87. $response = null;
  88. switch( $method ){
  89. case 'GET' : $response = $this->request( $url, $parameters, "GET" ); break;
  90. case 'POST' : $response = $this->request( $url, $parameters, "POST" ); break;
  91. }
  92. if( $response && $this->decode_json ){
  93. $response = json_decode( $response );
  94. }
  95. return $response;
  96. }
  97. /**
  98. * GET wrapper for provider apis request
  99. */
  100. function get( $url, $parameters = array() )
  101. {
  102. return $this->api( $url, 'GET', $parameters );
  103. }
  104. /**
  105. * POST wrapper for provider apis request
  106. */
  107. function post( $url, $parameters = array() )
  108. {
  109. return $this->api( $url, 'POST', $parameters );
  110. }
  111. // -- tokens
  112. public function tokenInfo($accesstoken)
  113. {
  114. $params['access_token'] = $this->access_token;
  115. $response = $this->request( $this->token_info_url, $params );
  116. return $this->parseRequestResult( $response );
  117. }
  118. public function refreshToken( $parameters = array() )
  119. {
  120. $params = array(
  121. "client_id" => $this->client_id,
  122. "client_secret" => $this->client_secret,
  123. "grant_type" => "refresh_token"
  124. );
  125. foreach($parameters as $k=>$v ){
  126. $params[$k] = $v;
  127. }
  128. $response = $this->request( $this->token_url, $params, "POST" );
  129. return $this->parseRequestResult( $response );
  130. }
  131. // -- utilities
  132. function request( $url, $params=false, $type="GET" )
  133. {
  134. if( $type == "GET" ){
  135. $url = $url . ( strpos( $url, '?' ) ? '&' : '?' ) . http_build_query($params, '', '&');
  136. }
  137. $this->http_info = array();
  138. $ch = curl_init();
  139. curl_setopt($ch, CURLOPT_URL , $url );
  140. curl_setopt($ch, CURLOPT_RETURNTRANSFER , 1 );
  141. curl_setopt($ch, CURLOPT_TIMEOUT , $this->curl_time_out );
  142. curl_setopt($ch, CURLOPT_USERAGENT , $this->curl_useragent );
  143. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT , $this->curl_connect_time_out );
  144. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER , $this->curl_ssl_verifypeer );
  145. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST , $this->curl_ssl_verifyhost );
  146. curl_setopt($ch, CURLOPT_HTTPHEADER , $this->curl_header );
  147. if($this->curl_proxy){
  148. curl_setopt( $ch, CURLOPT_PROXY , $this->curl_proxy);
  149. }
  150. if( $type == "POST" ){
  151. curl_setopt($ch, CURLOPT_POST, 1);
  152. if($params) curl_setopt( $ch, CURLOPT_POSTFIELDS, $params );
  153. }
  154. $response = curl_exec($ch);
  155. $this->http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  156. $this->http_info = array_merge($this->http_info, curl_getinfo($ch));
  157. curl_close ($ch);
  158. //-
  159. Hybrid_Error::deleteApiError();
  160. if( $this->http_code != 200 )
  161. {
  162. Hybrid_Error::setApiError( $this->http_code . '. ' . preg_replace('/\s+/', ' ', $response ) );
  163. }
  164. if( defined( 'WORDPRESS_SOCIAL_LOGIN_DEBUG_API_CALLS' ) )
  165. {
  166. do_action( 'wsl_log_provider_api_call', 'OAuth2', $url, $type, $params, $this->http_code, $this->http_info, $response );
  167. }
  168. //-
  169. return $response;
  170. }
  171. function parseRequestResult( $result )
  172. {
  173. if( json_decode( $result ) ) return json_decode( $result );
  174. parse_str( $result, $output );
  175. $result = new StdClass();
  176. foreach( $output as $k => $v )
  177. $result->$k = $v;
  178. return $result;
  179. }
  180. }