PageRenderTime 40ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/src/auth/buzzOAuth.php

http://buzz-php-client.googlecode.com/
PHP | 259 lines | 131 code | 15 blank | 113 comment | 25 complexity | 398b1837f63fe1ae63c2e79b09ae2efe MD5 | raw file
Possible License(s): Apache-2.0
  1. <?php
  2. /*
  3. * Copyright 2008 Google Inc.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. require_once "OAuth.php";
  18. /**
  19. * Authentication class that deals with 3-Legged OAuth 1.0a authentication
  20. *
  21. * This class uses the OAuth 1.0a spec which has a slightly different work flow in
  22. * how callback urls, request & access tokens are dealt with to prevent a possible
  23. * man in the middle attack.
  24. *
  25. */
  26. class buzzOAuth {
  27. public $localUserId;
  28. public $storageKey;
  29. protected $consumerToken;
  30. protected $accessToken;
  31. protected $privateKeyFile;
  32. private $storage;
  33. /**
  34. * Instantiates the class, but does not initiate the login flow, leaving it
  35. * to the discretion of the caller.
  36. *
  37. * @param string $consumerKey
  38. * @param string $consumerSecret
  39. * @param buzzStorage $storage storage class to use (file,apc,memcache,mysql)
  40. * @param any $localUser the *local* user ID (this is not the user's ID on the social network site, but the user id on YOUR site, this is used to link the oauth access token to a local login)
  41. */
  42. public function __construct(buzzStorage $storage, $localUserId) {
  43. global $buzzConfig;
  44. $buzzConfig['authorization_token_url'] = str_replace('www.example.org', OAuthUtil::urlencodeRFC3986($buzzConfig['site_name']), $buzzConfig['authorization_token_url']);
  45. $this->localUserId = $localUserId;
  46. $this->consumerToken = new OAuthConsumer($buzzConfig['oauth_consumer_key'], $buzzConfig['oauth_consumer_secret'], NULL);
  47. $this->signatureMethod = new OAuthSignatureMethod_HMAC_SHA1();
  48. $this->storage = $storage;
  49. $this->storageKey = 'OAuth:' . $buzzConfig['oauth_consumer_key'] . ':' . $localUserId; // Scope data to the local user as well, or else multiple local users will share the same OAuth credentials.
  50. if (($token = $storage->get($this->storageKey)) !== false) {
  51. $this->accessToken = $token;
  52. }
  53. }
  54. /**
  55. * The 3 legged oauth class needs a way to store the access key and token
  56. * it uses the buzzStorage class to do so.
  57. *
  58. * Constructing this class will initiate the 3 legged oauth work flow, including redirecting
  59. * to the OAuth provider's site if required(!)
  60. *
  61. * @param string $consumerKey
  62. * @param string $consumerSecret
  63. * @param buzzStorage $storage storage class to use (file,apc,memcache,mysql)
  64. * @param any $localUser the *local* user ID (this is not the user's ID on the social network site, but the user id on YOUR site, this is used to link the oauth access token to a local login)
  65. * @return buzzOAuth3Legged the logged-in provider instance
  66. */
  67. public static function performOAuthLogin(buzzStorage $storage, $localUserId = null) {
  68. global $buzzConfig;
  69. $auth = new buzzOAuth($storage, $localUserId);
  70. if (($token = $storage->get($auth->storageKey)) !== false) {
  71. $auth->accessToken = $token;
  72. } else {
  73. if (isset($_GET['oauth_verifier']) && isset($_GET['oauth_token']) && isset($_GET['uid'])) {
  74. $uid = $_GET['uid'];
  75. $secret = $auth->storage->get($auth->storageKey.":nonce" . $uid);
  76. $auth->storage->delete($auth->storageKey.":nonce" . $uid);
  77. $token = $auth->upgradeRequestToken($_GET['oauth_token'], $secret, $_GET['oauth_verifier']);
  78. $auth->redirectToOriginal();
  79. } else {
  80. // Initialize the OAuth dance, first request a request token, then kick the client to the authorize URL
  81. // First we store the current URL in our storage, so that when the oauth dance is completed we can return there
  82. $callbackUrl = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') ? 'https://' : 'http://') . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
  83. $uid = uniqid();
  84. $token = $auth->obtainRequestToken($callbackUrl, $uid);
  85. $auth->storage->set($auth->storageKey.":nonce" . $uid, $token->secret);
  86. $auth->redirectToAuthorization($token);
  87. }
  88. }
  89. return $auth;
  90. }
  91. /**
  92. * Upgrades an existing request token to an access token.
  93. *
  94. * @param buzzStorage $storage storage class to use (file,apc,memcache,mysql)
  95. * @param oauthVerifier
  96. */
  97. public function upgradeRequestToken($requestToken, $requestTokenSecret, $oauthVerifier) {
  98. $ret = $this->requestAccessToken($requestToken, $requestTokenSecret, $oauthVerifier);
  99. if ($ret['http_code'] == '200') {
  100. $matches = array();
  101. @parse_str($ret['data'], $matches);
  102. if (!isset($matches['oauth_token']) || !isset($matches['oauth_token_secret'])) {
  103. throw new buzzException("Error authorizing access key (result was: {$ret['data']})");
  104. }
  105. // The token was upgraded to an access token, we can now continue to use it.
  106. $this->accessToken = new OAuthConsumer(OAuthUtil::urldecodeRFC3986($matches['oauth_token']), OAuthUtil::urldecodeRFC3986($matches['oauth_token_secret']));
  107. $this->storage->set($this->storageKey, $this->accessToken);
  108. return $this->accessToken;
  109. } else {
  110. throw new buzzException("Error requesting oauth access token, code " . $ret['http_code'] . ", message: " . $ret['data']);
  111. }
  112. }
  113. /**
  114. * Sends the actual request to exchange an existing request token for an access token.
  115. *
  116. * @param string $requestToken the existing request token
  117. * @param string $requestTokenSecret the request token secret
  118. * @return array('http_code' => HTTP response code (200, 404, 401, etc), 'data' => the html document)
  119. */
  120. protected function requestAccessToken($requestToken, $requestTokenSecret, $oauthVerifier) {
  121. global $buzzConfig;
  122. $accessToken = new OAuthConsumer($requestToken, $requestTokenSecret);
  123. $accessRequest = OAuthRequest::from_consumer_and_token($this->consumerToken, $accessToken, "GET", $buzzConfig['access_token_url'], array('oauth_verifier' => $oauthVerifier));
  124. $accessRequest->sign_request($this->signatureMethod, $this->consumerToken, $accessToken);
  125. return buzzIO::send($accessRequest, 'GET');
  126. }
  127. /**
  128. * Redirects the page to the original url, prior to OAuth initialization. This removes the extraneous
  129. * parameters from the URL, adding latency, but increasing user-friendliness.
  130. *
  131. * @param buzzStorage $storage storage class to use (file,apc,memcache,mysql)
  132. */
  133. public function redirectToOriginal() {
  134. $originalUrl = $this->storage->get($this->storageKey.":originalUrl");
  135. if ($originalUrl && !empty($originalUrl)) {
  136. // The url was retrieve successfully, remove the temporary original url from storage, and redirect
  137. $this->storage->delete($this->storageKey.":originalUrl");
  138. header("Location: $originalUrl");
  139. }
  140. }
  141. /**
  142. * Obtains a request token from the specified provider.
  143. *
  144. * @param buzzStorage $storage storage class to use (file,apc,memcache,mysql)
  145. */
  146. public function obtainRequestToken($callbackUrl, $uid) {
  147. $this->storage->set($this->storageKey.":originalUrl", $callbackUrl);
  148. $callbackParams = (strpos($_SERVER['REQUEST_URI'], '?') !== false ? '&' : '?') . 'uid=' . urlencode($uid);
  149. $ret = $this->requestRequestToken($callbackUrl . $callbackParams);
  150. if ($ret['http_code'] == '200') {
  151. $matches = array();
  152. preg_match('/oauth_token=(.*)&oauth_token_secret=(.*)&oauth_callback_confirmed=(.*)/', $ret['data'], $matches);
  153. if (!is_array($matches) || count($matches) != 4) {
  154. throw new buzzException("Error retrieving request key ({$ret['data']})");
  155. }
  156. return new OAuthToken(OAuthUtil::urldecodeRFC3986($matches[1]), OAuthUtil::urldecodeRFC3986($matches[2]));
  157. } else {
  158. throw new buzzException("Error requesting oauth request token, code " . $ret['http_code'] . ", message: " . $ret['data']);
  159. }
  160. }
  161. /**
  162. * Sends the actual request to obtain a request token.
  163. *
  164. * @return array('http_code' => HTTP response code (200, 404, 401, etc), 'data' => the html document)
  165. */
  166. protected function requestRequestToken($callbackUrl) {
  167. global $buzzConfig;
  168. $requestTokenRequest = OAuthRequest::from_consumer_and_token($this->consumerToken, NULL, "GET", $buzzConfig['request_token_url'], array());
  169. $requestTokenRequest->set_parameter('scope', $buzzConfig['oauth_scope']);
  170. $requestTokenRequest->set_parameter('oauth_callback', $callbackUrl);
  171. $requestTokenRequest->sign_request($this->signatureMethod, $this->consumerToken, NULL);
  172. return buzzIO::send($requestTokenRequest, 'GET');
  173. }
  174. /**
  175. * Redirect the uset to the (provider's) authorize page, if approved it should kick the user back to the call back URL
  176. * which hopefully means we'll end up in the constructor of this class again, but with oauth_continue=1 set
  177. *
  178. * @param OAuthToken $token the request token
  179. * @param string $callbackUrl the URL to return to post-authorization (passed to login site)
  180. */
  181. public function redirectToAuthorization($token) {
  182. global $buzzConfig;
  183. $authorizeRedirect = $buzzConfig['authorization_token_url']. $token->key;
  184. header("Location: $authorizeRedirect");
  185. }
  186. /**
  187. * Returns the user ID on behalf of which this auth is making requests.
  188. * @return String The user ID specified in the constructor.
  189. */
  190. public function getUserId() {
  191. return $this->userId;
  192. }
  193. /**
  194. * Sets the user ID on behalf of which this auth is making requests.
  195. * @param String $userId A user ID.
  196. */
  197. public function setUserId($userId) {
  198. $this->userId = $userId;
  199. }
  200. /**
  201. * Sign the request using OAuth. This uses the consumer token and key
  202. * but 2 legged oauth doesn't require an access token and key. In situations where you want to
  203. * do a 'reverse phone home' (aka: gadget does a makeRequest to your server
  204. * and your server wants to retrieve more social information) this is the prefered
  205. * method.
  206. *
  207. * @param string $method the method (get/put/delete/post)
  208. * @param string $url the url to sign (http://site/social/rest/people/1/@me)
  209. * @param array $params the params that should be appended to the url (count=20 fields=foo, etc)
  210. * @param string $postBody for POST/PUT requests, the postBody is included in the signature
  211. * @return string the signed url
  212. */
  213. public function sign($method, $url, $params = array(), $postBody = false, &$headers = array()) {
  214. $oauthRequest = OAuthRequest::from_request($method, $url, $params);
  215. $params = $this->mergeParameters($params);
  216. foreach ($params as $key => $val) {
  217. if (is_array($val)) {
  218. $val = implode(',', $val);
  219. }
  220. $oauthRequest->set_parameter($key, $val);
  221. }
  222. $oauthRequest->sign_request($this->signatureMethod, $this->consumerToken, $this->accessToken);
  223. $signedUrl = $oauthRequest->to_url();
  224. return $signedUrl;
  225. }
  226. /**
  227. * Merges the supplied parameters with reasonable defaults for 2 legged oauth. User-supplied parameters
  228. * will have precedent over the defaults.
  229. *
  230. * @param array $params the user-supplied params that will be appended to the url
  231. * @return array the combined parameters
  232. */
  233. protected function mergeParameters($params) {
  234. $defaults = array(
  235. 'oauth_nonce' => md5(microtime() . mt_rand()),
  236. 'oauth_version' => OAuthRequest::$version, 'oauth_timestamp' => time(),
  237. 'oauth_consumer_key' => $this->consumerToken->key
  238. );
  239. if ($this->accessToken != null) {
  240. $params['oauth_token'] = $this->accessToken->key;
  241. }
  242. return array_merge($defaults, $params);
  243. }
  244. }