PageRenderTime 45ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Zend/Gdata/AuthSub.php

https://bitbucket.org/ksekar/campus
PHP | 248 lines | 126 code | 23 blank | 99 comment | 14 complexity | 915d1e0e675bf93dd67247d226cbe696 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.0, MIT
  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-2012 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: AuthSub.php 24594 2012-01-05 21:27:01Z matthew $
  21. */
  22. /**
  23. * Zend_Gdata_HttpClient
  24. */
  25. require_once 'Zend/Gdata/HttpClient.php';
  26. /**
  27. * Zend_Version
  28. */
  29. require_once 'Zend/Version.php';
  30. /**
  31. * Wrapper around Zend_Http_Client to facilitate Google's "Account Authentication
  32. * Proxy for Web-Based Applications".
  33. *
  34. * @see http://code.google.com/apis/accounts/AuthForWebApps.html
  35. *
  36. * @category Zend
  37. * @package Zend_Gdata
  38. * @subpackage Gdata
  39. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  40. * @license http://framework.zend.com/license/new-bsd New BSD License
  41. */
  42. class Zend_Gdata_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 Zend_Gdata_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 (Zend_Http_Client_Exception $e) {
  108. require_once 'Zend/Gdata/App/HttpException.php';
  109. throw new Zend_Gdata_App_HttpException($e->getMessage(), $e);
  110. }
  111. // Parse Google's response
  112. if ($response->isSuccessful()) {
  113. $goog_resp = array();
  114. foreach (explode("\n", $response->getBody()) as $l) {
  115. $l = chop($l);
  116. if ($l) {
  117. list($key, $val) = explode('=', chop($l), 2);
  118. $goog_resp[$key] = $val;
  119. }
  120. }
  121. return $goog_resp['Token'];
  122. } else {
  123. require_once 'Zend/Gdata/App/AuthException.php';
  124. throw new Zend_Gdata_App_AuthException(
  125. 'Token upgrade failed. Reason: ' . $response->getBody());
  126. }
  127. }
  128. /**
  129. * Revoke a token
  130. *
  131. * @param string $token The token to revoke
  132. * @param Zend_Http_Client $client (optional) HTTP client to use to make the request
  133. * @param string $request_uri (optional) URI to which to direct the revokation request
  134. * @return boolean Whether the revokation was successful
  135. * @throws Zend_Gdata_App_HttpException
  136. */
  137. public static function AuthSubRevokeToken($token, $client = null,
  138. $request_uri = self::AUTHSUB_REVOKE_TOKEN_URI)
  139. {
  140. $client = self::getHttpClient($token, $client);
  141. if ($client instanceof Zend_Gdata_HttpClient) {
  142. $filterResult = $client->filterHttpRequest('GET', $request_uri);
  143. $url = $filterResult['url'];
  144. $headers = $filterResult['headers'];
  145. $client->setHeaders($headers);
  146. $client->setUri($url);
  147. $client->resetParameters();
  148. } else {
  149. $client->setUri($request_uri);
  150. }
  151. ob_start();
  152. try {
  153. $response = $client->request('GET');
  154. } catch (Zend_Http_Client_Exception $e) {
  155. ob_end_clean();
  156. require_once 'Zend/Gdata/App/HttpException.php';
  157. throw new Zend_Gdata_App_HttpException($e->getMessage(), $e);
  158. }
  159. ob_end_clean();
  160. // Parse Google's response
  161. if ($response->isSuccessful()) {
  162. return true;
  163. } else {
  164. return false;
  165. }
  166. }
  167. /**
  168. * get token information
  169. *
  170. * @param string $token The token to retrieve information about
  171. * @param Zend_Http_Client $client (optional) HTTP client to use to
  172. * make the request
  173. * @param string $request_uri (optional) URI to which to direct
  174. * the information request
  175. */
  176. public static function getAuthSubTokenInfo(
  177. $token, $client = null, $request_uri = self::AUTHSUB_TOKEN_INFO_URI)
  178. {
  179. $client = self::getHttpClient($token, $client);
  180. if ($client instanceof Zend_Gdata_HttpClient) {
  181. $filterResult = $client->filterHttpRequest('GET', $request_uri);
  182. $url = $filterResult['url'];
  183. $headers = $filterResult['headers'];
  184. $client->setHeaders($headers);
  185. $client->setUri($url);
  186. } else {
  187. $client->setUri($request_uri);
  188. }
  189. ob_start();
  190. try {
  191. $response = $client->request('GET');
  192. } catch (Zend_Http_Client_Exception $e) {
  193. ob_end_clean();
  194. require_once 'Zend/Gdata/App/HttpException.php';
  195. throw new Zend_Gdata_App_HttpException($e->getMessage(), $e);
  196. }
  197. ob_end_clean();
  198. return $response->getBody();
  199. }
  200. /**
  201. * Retrieve a HTTP client object with AuthSub credentials attached
  202. * as the Authorization header
  203. *
  204. * @param string $token The token to retrieve information about
  205. * @param Zend_Gdata_HttpClient $client (optional) HTTP client to use to make the request
  206. */
  207. public static function getHttpClient($token, $client = null)
  208. {
  209. if ($client == null) {
  210. $client = new Zend_Gdata_HttpClient();
  211. }
  212. if (!$client instanceof Zend_Gdata_HttpClient) {
  213. require_once 'Zend/Gdata/App/HttpException.php';
  214. throw new Zend_Gdata_App_HttpException('Client is not an instance of Zend_Gdata_HttpClient.');
  215. }
  216. $useragent = 'Zend_Framework_Gdata/' . Zend_Version::VERSION;
  217. $client->setConfig(array(
  218. 'strictredirects' => true,
  219. 'useragent' => $useragent
  220. )
  221. );
  222. $client->setAuthSubToken($token);
  223. return $client;
  224. }
  225. }