AuthSub.php 9.0 KB

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