/library/Zend/Gdata/AuthSub.php

https://github.com/coder-int21h/noses · PHP · 243 lines · 124 code · 23 blank · 96 comment · 14 complexity · f308175bb4c25fd4673b46b814e22919 MD5 · raw file

  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Gdata
  17. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. */
  20. /**
  21. * Zend_Gdata_HttpClient
  22. */
  23. require_once 'Zend/Gdata/HttpClient.php';
  24. /**
  25. * Zend_Version
  26. */
  27. require_once 'Zend/Version.php';
  28. /**
  29. * Wrapper around Zend_Http_Client to facilitate Google's "Account Authentication
  30. * Proxy for Web-Based Applications".
  31. *
  32. * @see http://code.google.com/apis/accounts/AuthForWebApps.html
  33. *
  34. * @category Zend
  35. * @package Zend_Gdata
  36. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  37. * @license http://framework.zend.com/license/new-bsd New BSD License
  38. */
  39. class Zend_Gdata_AuthSub
  40. {
  41. const AUTHSUB_REQUEST_URI = 'https://www.google.com/accounts/AuthSubRequest';
  42. const AUTHSUB_SESSION_TOKEN_URI = 'https://www.google.com/accounts/AuthSubSessionToken';
  43. const AUTHSUB_REVOKE_TOKEN_URI = 'https://www.google.com/accounts/AuthSubRevokeToken';
  44. const AUTHSUB_TOKEN_INFO_URI = 'https://www.google.com/accounts/AuthSubTokenInfo';
  45. /**
  46. * Creates a URI to request a single-use AuthSub token.
  47. *
  48. * @param string $next (required) URL identifying the service to be
  49. * accessed.
  50. * The resulting token will enable access to the specified service only.
  51. * Some services may limit scope further, such as read-only access.
  52. * @param string $scope (required) URL identifying the service to be
  53. * accessed. The resulting token will enable
  54. * access to the specified service only.
  55. * Some services may limit scope further, such
  56. * as read-only access.
  57. * @param int $secure (optional) Boolean flag indicating whether the
  58. * authentication transaction should issue a secure
  59. * token (1) or a non-secure token (0). Secure tokens
  60. * are available to registered applications only.
  61. * @param int $session (optional) Boolean flag indicating whether
  62. * the one-time-use token may be exchanged for
  63. * a session token (1) or not (0).
  64. * @param string $request_uri (optional) URI to which to direct the
  65. * authentication request.
  66. */
  67. public static function getAuthSubTokenUri($next, $scope, $secure=0, $session=0,
  68. $request_uri = self::AUTHSUB_REQUEST_URI)
  69. {
  70. $querystring = '?next=' . urlencode($next)
  71. . '&scope=' . urldecode($scope)
  72. . '&secure=' . urlencode($secure)
  73. . '&session=' . urlencode($session);
  74. return $request_uri . $querystring;
  75. }
  76. /**
  77. * Upgrades a single use token to a session token
  78. *
  79. * @param string $token The single use token which is to be upgraded
  80. * @param Zend_Http_Client $client (optional) HTTP client to use to
  81. * make the request
  82. * @param string $request_uri (optional) URI to which to direct
  83. * the session token upgrade
  84. * @return string The upgraded token value
  85. * @throws Zend_Gdata_App_AuthException
  86. * @throws Zend_Gdata_App_HttpException
  87. */
  88. public static function getAuthSubSessionToken(
  89. $token, $client = null,
  90. $request_uri = self::AUTHSUB_SESSION_TOKEN_URI)
  91. {
  92. $client = self::getHttpClient($token, $client);
  93. if ($client instanceof Zend_Gdata_HttpClient) {
  94. $filterResult = $client->filterHttpRequest('GET', $request_uri);
  95. $url = $filterResult['url'];
  96. $headers = $filterResult['headers'];
  97. $client->setHeaders($headers);
  98. $client->setUri($url);
  99. } else {
  100. $client->setUri($request_uri);
  101. }
  102. try {
  103. $response = $client->request('GET');
  104. } catch (Zend_Http_Client_Exception $e) {
  105. require_once 'Zend/Gdata/App/HttpException.php';
  106. throw new Zend_Gdata_App_HttpException($e->getMessage(), $e);
  107. }
  108. // Parse Google's response
  109. if ($response->isSuccessful()) {
  110. $goog_resp = array();
  111. foreach (explode("\n", $response->getBody()) as $l) {
  112. $l = chop($l);
  113. if ($l) {
  114. list($key, $val) = explode('=', chop($l), 2);
  115. $goog_resp[$key] = $val;
  116. }
  117. }
  118. return $goog_resp['Token'];
  119. } else {
  120. require_once 'Zend/Gdata/App/AuthException.php';
  121. throw new Zend_Gdata_App_AuthException(
  122. 'Token upgrade failed. Reason: ' . $response->getBody());
  123. }
  124. }
  125. /**
  126. * Revoke a token
  127. *
  128. * @param string $token The token to revoke
  129. * @param Zend_Http_Client $client (optional) HTTP client to use to make the request
  130. * @param string $request_uri (optional) URI to which to direct the revokation request
  131. * @return boolean Whether the revokation was successful
  132. * @throws Zend_Gdata_App_HttpException
  133. */
  134. public static function AuthSubRevokeToken($token, $client = null,
  135. $request_uri = self::AUTHSUB_REVOKE_TOKEN_URI)
  136. {
  137. $client = self::getHttpClient($token, $client);
  138. if ($client instanceof Zend_Gdata_HttpClient) {
  139. $filterResult = $client->filterHttpRequest('GET', $request_uri);
  140. $url = $filterResult['url'];
  141. $headers = $filterResult['headers'];
  142. $client->setHeaders($headers);
  143. $client->setUri($url);
  144. $client->resetParameters();
  145. } else {
  146. $client->setUri($request_uri);
  147. }
  148. ob_start();
  149. try {
  150. $response = $client->request('GET');
  151. } catch (Zend_Http_Client_Exception $e) {
  152. require_once 'Zend/Gdata/App/HttpException.php';
  153. throw new Zend_Gdata_App_HttpException($e->getMessage(), $e);
  154. }
  155. ob_end_clean();
  156. // Parse Google's response
  157. if ($response->isSuccessful()) {
  158. return true;
  159. } else {
  160. return false;
  161. }
  162. }
  163. /**
  164. * get token information
  165. *
  166. * @param string $token The token to retrieve information about
  167. * @param Zend_Http_Client $client (optional) HTTP client to use to
  168. * make the request
  169. * @param string $request_uri (optional) URI to which to direct
  170. * the information request
  171. */
  172. public static function getAuthSubTokenInfo(
  173. $token, $client = null, $request_uri = self::AUTHSUB_TOKEN_INFO_URI)
  174. {
  175. $client = self::getHttpClient($token, $client);
  176. if ($client instanceof Zend_Gdata_HttpClient) {
  177. $filterResult = $client->filterHttpRequest('GET', $request_uri);
  178. $url = $filterResult['url'];
  179. $headers = $filterResult['headers'];
  180. $client->setHeaders($headers);
  181. $client->setUri($url);
  182. } else {
  183. $client->setUri($request_uri);
  184. }
  185. ob_start();
  186. try {
  187. $response = $client->request('GET');
  188. } catch (Zend_Http_Client_Exception $e) {
  189. require_once 'Zend/Gdata/App/HttpException.php';
  190. throw new Zend_Gdata_App_HttpException($e->getMessage(), $e);
  191. }
  192. ob_end_clean();
  193. return $response->getBody();
  194. }
  195. /**
  196. * Retrieve a HTTP client object with AuthSub credentials attached
  197. * as the Authorization header
  198. *
  199. * @param string $token The token to retrieve information about
  200. * @param Zend_Gdata_HttpClient $client (optional) HTTP client to use to make the request
  201. */
  202. public static function getHttpClient($token, $client = null)
  203. {
  204. if ($client == null) {
  205. $client = new Zend_Gdata_HttpClient();
  206. }
  207. if (!$client instanceof Zend_Http_Client) {
  208. require_once 'Zend/Gdata/App/HttpException.php';
  209. throw new Zend_Gdata_App_HttpException('Client is not an instance of Zend_Http_Client.');
  210. }
  211. $useragent = 'Zend_Framework_Gdata/' . Zend_Version::VERSION;
  212. $client->setConfig(array(
  213. 'strictredirects' => true,
  214. 'useragent' => $useragent
  215. )
  216. );
  217. $client->setAuthSubToken($token);
  218. return $client;
  219. }
  220. }