/wp-content/plugins/wordpress-social-login/hybridauth/Hybrid/Provider_Model_OAuth1.php

https://gitlab.com/code26/selah · PHP · 180 lines · 91 code · 32 blank · 57 comment · 17 complexity · c9dd9e6685e2f405802334e4e50f553b 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. /**
  8. * To implement an OAuth 1 based service provider, Hybrid_Provider_Model_OAuth1
  9. * can be used to save the hassle of the authentication flow.
  10. *
  11. * Each class that inherit from Hybrid_Provider_Model_OAuth1 have to implement
  12. * at least 2 methods:
  13. * Hybrid_Providers_{provider_name}::initialize() to setup the provider api end-points urls
  14. * Hybrid_Providers_{provider_name}::getUserProfile() to grab the user profile
  15. *
  16. * Hybrid_Provider_Model_OAuth1 use OAuth1Client v0.1 which can be found on
  17. * Hybrid/thirdparty/OAuth/OAuth1Client.php
  18. */
  19. class Hybrid_Provider_Model_OAuth1 extends Hybrid_Provider_Model
  20. {
  21. /**
  22. * request_tokens as received from provider
  23. * @var object
  24. */
  25. public $request_tokens_raw = null;
  26. /**
  27. * access_tokens as received from provider
  28. * @var object
  29. */
  30. public $access_tokens_raw = null;
  31. /**
  32. * Try to get the error message from provider api
  33. * @param Numeric $code
  34. */
  35. function errorMessageByStatus( $code = null ) {
  36. $http_status_codes = ARRAY(
  37. 200 => "OK: Success!",
  38. 304 => "Not Modified: There was no new data to return.",
  39. 400 => "Bad Request: The request was invalid.",
  40. 401 => "Unauthorized.",
  41. 403 => "Forbidden: The request is understood, but it has been refused.",
  42. 404 => "Not Found: The URI requested is invalid or the resource requested does not exists.",
  43. 406 => "Not Acceptable.",
  44. 500 => "Internal Server Error: Something is broken.",
  45. 502 => "Bad Gateway.",
  46. 503 => "Service Unavailable."
  47. );
  48. if( ! $code && $this->api )
  49. $code = $this->api->http_code;
  50. if( isset( $http_status_codes[ $code ] ) )
  51. return $code . " " . $http_status_codes[ $code ];
  52. }
  53. // --------------------------------------------------------------------
  54. /**
  55. * adapter initializer
  56. */
  57. function initialize()
  58. {
  59. // 1 - check application credentials
  60. if ( ! $this->config["keys"]["key"] || ! $this->config["keys"]["secret"] ){
  61. throw new Exception( "Your application key and secret are required in order to connect to {$this->providerId}.", 4 );
  62. }
  63. // 2 - include OAuth lib and client
  64. if ( ! class_exists('OAuthConsumer') ) {
  65. require_once realpath( dirname( __FILE__ ) ) . "/thirdparty/OAuth/OAuth.php";
  66. }
  67. require_once realpath( dirname( __FILE__ ) ) . "/thirdparty/OAuth/OAuth1Client.php";
  68. // 3.1 - setup access_token if any stored
  69. if( $this->token( "access_token" ) ){
  70. $this->api = new OAuth1Client(
  71. $this->config["keys"]["key"], $this->config["keys"]["secret"],
  72. $this->token( "access_token" ), $this->token( "access_token_secret" )
  73. );
  74. }
  75. // 3.2 - setup request_token if any stored, in order to exchange with an access token
  76. elseif( $this->token( "request_token" ) ){
  77. $this->api = new OAuth1Client(
  78. $this->config["keys"]["key"], $this->config["keys"]["secret"],
  79. $this->token( "request_token" ), $this->token( "request_token_secret" )
  80. );
  81. }
  82. // 3.3 - instanciate OAuth client with client credentials
  83. else{
  84. $this->api = new OAuth1Client( $this->config["keys"]["key"], $this->config["keys"]["secret"] );
  85. }
  86. // Set curl proxy if exist
  87. if( isset( Hybrid_Auth::$config["proxy"] ) ){
  88. $this->api->curl_proxy = Hybrid_Auth::$config["proxy"];
  89. }
  90. }
  91. // --------------------------------------------------------------------
  92. /**
  93. * begin login step
  94. */
  95. function loginBegin()
  96. {
  97. $tokens = $this->api->requestToken( $this->endpoint );
  98. // request tokens as received from provider
  99. $this->request_tokens_raw = $tokens;
  100. // check the last HTTP status code returned
  101. if ( $this->api->http_code != 200 ){
  102. throw new Exception( "Authentication failed! {$this->providerId} returned an error. " . $this->errorMessageByStatus( $this->api->http_code ), 5 );
  103. }
  104. if ( ! isset( $tokens["oauth_token"] ) ){
  105. throw new Exception( "Authentication failed! {$this->providerId} returned an invalid oauth_token.", 5 );
  106. }
  107. $this->token( "request_token" , $tokens["oauth_token"] );
  108. $this->token( "request_token_secret", $tokens["oauth_token_secret"] );
  109. # redirect the user to the provider authentication url
  110. Hybrid_Auth::redirect( $this->api->authorizeUrl( $tokens ) );
  111. }
  112. // --------------------------------------------------------------------
  113. /**
  114. * finish login step
  115. */
  116. function loginFinish()
  117. {
  118. $denied = (array_key_exists('denied',$_REQUEST))?$_REQUEST['denied']:"";
  119. $oauth_token = (array_key_exists('oauth_token',$_REQUEST))?$_REQUEST['oauth_token']:"";
  120. $oauth_verifier = (array_key_exists('oauth_verifier',$_REQUEST))?$_REQUEST['oauth_verifier']:"";
  121. if ( $denied ){
  122. throw new Exception( "Authentication denied! {$this->providerId} returned denied token: " . htmlentities( $denied ), 5 );
  123. }
  124. if ( ! $oauth_token || ! $oauth_verifier ){
  125. throw new Exception( "Authentication failed! {$this->providerId} returned an invalid oauth_verifier.", 5 );
  126. }
  127. // request an access token
  128. $tokens = $this->api->accessToken( $oauth_verifier );
  129. // access tokens as received from provider
  130. $this->access_tokens_raw = $tokens;
  131. // check the last HTTP status code returned
  132. if ( $this->api->http_code != 200 ){
  133. throw new Exception( "Authentication failed! {$this->providerId} returned an error. " . $this->errorMessageByStatus( $this->api->http_code ), 5 );
  134. }
  135. // we should have an access_token, or else, something has gone wrong
  136. if ( ! isset( $tokens["oauth_token"] ) ){
  137. throw new Exception( "Authentication failed! {$this->providerId} returned an invalid oauth_token.", 5 );
  138. }
  139. // we no more need to store request tokens
  140. $this->deleteToken( "request_token" );
  141. $this->deleteToken( "request_token_secret" );
  142. // store access_token for later user
  143. $this->token( "access_token" , $tokens['oauth_token'] );
  144. $this->token( "access_token_secret" , $tokens['oauth_token_secret'] );
  145. // set user as logged in to the current provider
  146. $this->setUserConnected();
  147. }
  148. }