PageRenderTime 42ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Lampcms/Modules/Google/src/auth/apiOAuth.php

http://github.com/snytkine/LampCMS
PHP | 250 lines | 162 code | 12 blank | 76 comment | 21 complexity | 11a4a1d6403b2bf7a937cd0fb3cc9b97 MD5 | raw file
Possible License(s): LGPL-3.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 "external/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. * @author Chris Chabot <chabotc@google.com>
  26. *
  27. */
  28. class apiOAuth extends apiAuth {
  29. public $cacheKey;
  30. protected $consumerToken;
  31. protected $accessToken;
  32. protected $privateKeyFile;
  33. protected $developerKey;
  34. public $service;
  35. /**
  36. * Instantiates the class, but does not initiate the login flow, leaving it
  37. * to the discretion of the caller.
  38. */
  39. public function __construct() {
  40. global $apiConfig;
  41. if (!empty($apiConfig['developer_key'])) {
  42. $this->setDeveloperKey($apiConfig['developer_key']);
  43. }
  44. $this->consumerToken = new apiClientOAuthConsumer($apiConfig['oauth_consumer_key'], $apiConfig['oauth_consumer_secret'], NULL);
  45. $this->signatureMethod = new apiClientOAuthSignatureMethod_HMAC_SHA1();
  46. $this->cacheKey = 'OAuth:' . $apiConfig['oauth_consumer_key']; // Scope data to the local user as well, or else multiple local users will share the same OAuth credentials.
  47. }
  48. /**
  49. * The 3 legged oauth class needs a way to store the access key and token
  50. * it uses the apiCache class to do so.
  51. *
  52. * Constructing this class will initiate the 3 legged oauth work flow, including redirecting
  53. * to the OAuth provider's site if required(!)
  54. *
  55. * @param string $consumerKey
  56. * @param string $consumerSecret
  57. * @return apiOAuth3Legged the logged-in provider instance
  58. */
  59. public function authenticate($service) {
  60. global $apiConfig;
  61. $this->service = $service;
  62. $this->service['authorization_token_url'] .= '?scope=' . apiClientOAuthUtil::urlencodeRFC3986($service['scope']) . '&domain=' . apiClientOAuthUtil::urlencodeRFC3986($apiConfig['site_name']) . '&oauth_token=';
  63. if (isset($_GET['oauth_verifier']) && isset($_GET['oauth_token']) && isset($_GET['uid'])) {
  64. $uid = $_GET['uid'];
  65. $secret = apiClient::$cache->get($this->cacheKey.":nonce:" . $uid);
  66. apiClient::$cache->delete($this->cacheKey.":nonce:" . $uid);
  67. $token = $this->upgradeRequestToken($_GET['oauth_token'], $secret, $_GET['oauth_verifier']);
  68. return json_encode($token);
  69. } else {
  70. // Initialize the OAuth dance, first request a request token, then kick the client to the authorize URL
  71. // First we store the current URL in our cache, so that when the oauth dance is completed we can return there
  72. $callbackUrl = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') ? 'https://' : 'http://') . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
  73. $uid = uniqid();
  74. $token = $this->obtainRequestToken($callbackUrl, $uid);
  75. apiClient::$cache->set($this->cacheKey.":nonce:" . $uid, $token->secret);
  76. $this->redirectToAuthorization($token);
  77. }
  78. }
  79. /**
  80. * Sets the internal oauth access token (which is returned by the authenticate function), a user should only
  81. * go through the authenticate() flow once (which involces a bunch of browser redirections and authentication screens, not fun)
  82. * and every time the user comes back the access token from the authentication() flow should be re-used (it essentially never expires)
  83. * @param object $accessToken
  84. */
  85. public function setAccessToken($accessToken) {
  86. $accessToken = json_decode($accessToken, true);
  87. if ($accessToken == null) {
  88. throw new apiAuthException("Could not json decode the access token");
  89. }
  90. if (! isset($accessToken['key']) || ! isset($accessToken['secret'])) {
  91. throw new apiAuthException("Invalid OAuth token, missing key and/or secret");
  92. }
  93. $this->accessToken = new apiClientOAuthConsumer($accessToken['key'], $accessToken['secret']);
  94. }
  95. /**
  96. * Returns the current access token
  97. */
  98. public function getAccessToken() {
  99. return $this->accessToken;
  100. }
  101. /**
  102. * Set the developer key to use, these are obtained through the API Console
  103. */
  104. public function setDeveloperKey($developerKey) {
  105. $this->developerKey = $developerKey;
  106. }
  107. /**
  108. * Upgrades an existing request token to an access token.
  109. *
  110. * @param apiCache $cache cache class to use (file,apc,memcache,mysql)
  111. * @param oauthVerifier
  112. */
  113. public function upgradeRequestToken($requestToken, $requestTokenSecret, $oauthVerifier) {
  114. $ret = $this->requestAccessToken($requestToken, $requestTokenSecret, $oauthVerifier);
  115. $matches = array();
  116. @parse_str($ret, $matches);
  117. if (!isset($matches['oauth_token']) || !isset($matches['oauth_token_secret'])) {
  118. throw new apiAuthException("Error authorizing access key (result was: {$ret})");
  119. }
  120. // The token was upgraded to an access token, we can now continue to use it.
  121. $this->accessToken = new apiClientOAuthConsumer(apiClientOAuthUtil::urldecodeRFC3986($matches['oauth_token']), apiClientOAuthUtil::urldecodeRFC3986($matches['oauth_token_secret']));
  122. return $this->accessToken;
  123. }
  124. /**
  125. * Sends the actual request to exchange an existing request token for an access token.
  126. *
  127. * @param string $requestToken the existing request token
  128. * @param string $requestTokenSecret the request token secret
  129. * @return array('http_code' => HTTP response code (200, 404, 401, etc), 'data' => the html document)
  130. */
  131. protected function requestAccessToken($requestToken, $requestTokenSecret, $oauthVerifier) {
  132. $accessToken = new apiClientOAuthConsumer($requestToken, $requestTokenSecret);
  133. $accessRequest = apiClientOAuthRequest::from_consumer_and_token($this->consumerToken, $accessToken, "GET", $this->service['access_token_url'], array('oauth_verifier' => $oauthVerifier));
  134. $accessRequest->sign_request($this->signatureMethod, $this->consumerToken, $accessToken);
  135. $request = apiClient::$io->makeRequest(new apiHttpRequest($accessRequest));
  136. if ($request->getResponseHttpCode() != 200) {
  137. throw new apiAuthException("Could not fetch access token, http code: " . $request->getResponseHttpCode() . ', response body: '. $request->getResponseBody());
  138. }
  139. return $request->getResponseBody();
  140. }
  141. /**
  142. * Obtains a request token from the specified provider.
  143. */
  144. public function obtainRequestToken($callbackUrl, $uid) {
  145. $callbackParams = (strpos($_SERVER['REQUEST_URI'], '?') !== false ? '&' : '?') . 'uid=' . urlencode($uid);
  146. $ret = $this->requestRequestToken($callbackUrl . $callbackParams);
  147. $matches = array();
  148. preg_match('/oauth_token=(.*)&oauth_token_secret=(.*)&oauth_callback_confirmed=(.*)/', $ret, $matches);
  149. if (!is_array($matches) || count($matches) != 4) {
  150. throw new apiAuthException("Error retrieving request key ({$ret})");
  151. }
  152. return new apiClientOAuthToken(apiClientOAuthUtil::urldecodeRFC3986($matches[1]), apiClientOAuthUtil::urldecodeRFC3986($matches[2]));
  153. }
  154. /**
  155. * Sends the actual request to obtain a request token.
  156. *
  157. * @return array('http_code' => HTTP response code (200, 404, 401, etc), 'data' => the html document)
  158. */
  159. protected function requestRequestToken($callbackUrl) {
  160. $requestTokenRequest = apiClientOAuthRequest::from_consumer_and_token($this->consumerToken, NULL, "GET", $this->service['request_token_url'], array());
  161. $requestTokenRequest->set_parameter('scope', $this->service['scope']);
  162. $requestTokenRequest->set_parameter('oauth_callback', $callbackUrl);
  163. $requestTokenRequest->sign_request($this->signatureMethod, $this->consumerToken, NULL);
  164. $request = apiClient::$io->makeRequest(new apiHttpRequest($requestTokenRequest));
  165. if ($request->getResponseHttpCode() != 200) {
  166. throw new apiAuthException("Couldn't fetch request token, http code: " . $request->getResponseHttpCode() . ', response body: '. $request->getResponseBody());
  167. }
  168. return $request->getResponseBody();
  169. }
  170. /**
  171. * Redirect the uset to the (provider's) authorize page, if approved it should kick the user back to the call back URL
  172. * which hopefully means we'll end up in the constructor of this class again, but with oauth_continue=1 set
  173. *
  174. * @param OAuthToken $token the request token
  175. * @param string $callbackUrl the URL to return to post-authorization (passed to login site)
  176. */
  177. public function redirectToAuthorization($token) {
  178. $authorizeRedirect = $this->service['authorization_token_url']. $token->key;
  179. header("Location: $authorizeRedirect");
  180. }
  181. /**
  182. * Sign the request using OAuth. This uses the consumer token and key
  183. *
  184. * @param string $method the method (get/put/delete/post)
  185. * @param string $url the url to sign (http://site/social/rest/people/1/@me)
  186. * @param array $params the params that should be appended to the url (count=20 fields=foo, etc)
  187. * @param string $postBody for POST/PUT requests, the postBody is included in the signature
  188. * @return string the signed url
  189. */
  190. public function sign(apiHttpRequest $request) {
  191. // add the developer key to the request before signing it
  192. if ($this->developerKey) {
  193. $request->setUrl($request->getUrl() . ((strpos($request->getUrl(), '?') === false) ? '?' : '&') . 'key='.urlencode($this->developerKey));
  194. }
  195. // and sign the request
  196. $oauthRequest = apiClientOAuthRequest::from_request($request->getMethod(), $request->getBaseUrl(), $request->getQueryParams());
  197. $params = $this->mergeParameters($request->getQueryParams());
  198. foreach ($params as $key => $val) {
  199. if (is_array($val)) {
  200. $val = implode(',', $val);
  201. }
  202. $oauthRequest->set_parameter($key, $val);
  203. }
  204. $oauthRequest->sign_request($this->signatureMethod, $this->consumerToken, $this->accessToken);
  205. $authHeaders = $oauthRequest->to_header();
  206. $headers = $request->getHeaders();
  207. $headers[] = $authHeaders;
  208. $request->setHeaders($headers);
  209. // and add the access token key to it (since it doesn't include the secret, it's still secure to store this in cache)
  210. $request->accessKey = $this->accessToken->key;
  211. return $request;
  212. }
  213. /**
  214. * Merges the supplied parameters with reasonable defaults for 2 legged oauth. User-supplied parameters
  215. * will have precedent over the defaults.
  216. *
  217. * @param array $params the user-supplied params that will be appended to the url
  218. * @return array the combined parameters
  219. */
  220. protected function mergeParameters($params) {
  221. $defaults = array(
  222. 'oauth_nonce' => md5(microtime() . mt_rand()),
  223. 'oauth_version' => apiClientOAuthRequest::$version, 'oauth_timestamp' => time(),
  224. 'oauth_consumer_key' => $this->consumerToken->key
  225. );
  226. if ($this->accessToken != null) {
  227. $params['oauth_token'] = $this->accessToken->key;
  228. }
  229. return array_merge($defaults, $params);
  230. }
  231. public function createAuthUrl($scope) {return null;}
  232. public function refreshToken($refreshToken) {/* noop*/}
  233. public function revokeToken() {/* noop*/}
  234. }