/library/Zend/GData/AuthSub.php

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