PageRenderTime 49ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Zend/Gdata/AuthSub.php

https://bitbucket.org/Ebozavrik/test-application
PHP | 254 lines | 126 code | 27 blank | 101 comment | 14 complexity | dee51469715881b33d5887b607a00fb1 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-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 24593 2012-01-05 20:35:02Z 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. *
  88. * @return string The upgraded token value
  89. * @throws Zend_Gdata_App_AuthException
  90. * @throws Zend_Gdata_App_HttpException
  91. */
  92. public static function getAuthSubSessionToken (
  93. $token, $client = null,
  94. $request_uri = self::AUTHSUB_SESSION_TOKEN_URI)
  95. {
  96. $client = self::getHttpClient($token, $client);
  97. if ($client instanceof Zend_Gdata_HttpClient) {
  98. $filterResult = $client->filterHttpRequest('GET', $request_uri);
  99. $url = $filterResult['url'];
  100. $headers = $filterResult['headers'];
  101. $client->setHeaders($headers);
  102. $client->setUri($url);
  103. } else {
  104. $client->setUri($request_uri);
  105. }
  106. try {
  107. $response = $client->request('GET');
  108. } catch (Zend_Http_Client_Exception $e) {
  109. require_once 'Zend/Gdata/App/HttpException.php';
  110. throw new Zend_Gdata_App_HttpException( $e->getMessage(), $e );
  111. }
  112. // Parse Google's response
  113. if ($response->isSuccessful()) {
  114. $goog_resp = array();
  115. foreach (explode("\n", $response->getBody()) as $l) {
  116. $l = chop($l);
  117. if ($l) {
  118. list( $key, $val ) = explode('=', chop($l), 2);
  119. $goog_resp[$key] = $val;
  120. }
  121. }
  122. return $goog_resp['Token'];
  123. } else {
  124. require_once 'Zend/Gdata/App/AuthException.php';
  125. throw new Zend_Gdata_App_AuthException(
  126. 'Token upgrade failed. Reason: ' . $response->getBody() );
  127. }
  128. }
  129. /**
  130. * Revoke a token
  131. *
  132. * @param string $token The token to revoke
  133. * @param Zend_Http_Client $client (optional) HTTP client to use to make the request
  134. * @param string $request_uri (optional) URI to which to direct the revokation request
  135. *
  136. * @return boolean Whether the revokation was successful
  137. * @throws Zend_Gdata_App_HttpException
  138. */
  139. public static function AuthSubRevokeToken ($token, $client = null,
  140. $request_uri = self::AUTHSUB_REVOKE_TOKEN_URI)
  141. {
  142. $client = self::getHttpClient($token, $client);
  143. if ($client instanceof Zend_Gdata_HttpClient) {
  144. $filterResult = $client->filterHttpRequest('GET', $request_uri);
  145. $url = $filterResult['url'];
  146. $headers = $filterResult['headers'];
  147. $client->setHeaders($headers);
  148. $client->setUri($url);
  149. $client->resetParameters();
  150. } else {
  151. $client->setUri($request_uri);
  152. }
  153. ob_start();
  154. try {
  155. $response = $client->request('GET');
  156. } catch (Zend_Http_Client_Exception $e) {
  157. ob_end_clean();
  158. require_once 'Zend/Gdata/App/HttpException.php';
  159. throw new Zend_Gdata_App_HttpException( $e->getMessage(), $e );
  160. }
  161. ob_end_clean();
  162. // Parse Google's response
  163. if ($response->isSuccessful()) {
  164. return true;
  165. } else {
  166. return false;
  167. }
  168. }
  169. /**
  170. * get token information
  171. *
  172. * @param string $token The token to retrieve information about
  173. * @param Zend_Http_Client $client (optional) HTTP client to use to
  174. * make the request
  175. * @param string $request_uri (optional) URI to which to direct
  176. * the information request
  177. */
  178. public static function getAuthSubTokenInfo (
  179. $token, $client = null, $request_uri = self::AUTHSUB_TOKEN_INFO_URI)
  180. {
  181. $client = self::getHttpClient($token, $client);
  182. if ($client instanceof Zend_Gdata_HttpClient) {
  183. $filterResult = $client->filterHttpRequest('GET', $request_uri);
  184. $url = $filterResult['url'];
  185. $headers = $filterResult['headers'];
  186. $client->setHeaders($headers);
  187. $client->setUri($url);
  188. } else {
  189. $client->setUri($request_uri);
  190. }
  191. ob_start();
  192. try {
  193. $response = $client->request('GET');
  194. } catch (Zend_Http_Client_Exception $e) {
  195. ob_end_clean();
  196. require_once 'Zend/Gdata/App/HttpException.php';
  197. throw new Zend_Gdata_App_HttpException( $e->getMessage(), $e );
  198. }
  199. ob_end_clean();
  200. return $response->getBody();
  201. }
  202. /**
  203. * Retrieve a HTTP client object with AuthSub credentials attached
  204. * as the Authorization header
  205. *
  206. * @param string $token The token to retrieve information about
  207. * @param Zend_Gdata_HttpClient $client (optional) HTTP client to use to make the request
  208. */
  209. public static function getHttpClient ($token, $client = null)
  210. {
  211. if ($client == null) {
  212. $client = new Zend_Gdata_HttpClient();
  213. }
  214. if (!$client instanceof Zend_Gdata_HttpClient) {
  215. require_once 'Zend/Gdata/App/HttpException.php';
  216. throw new Zend_Gdata_App_HttpException( 'Client is not an instance of Zend_Gdata_HttpClient.' );
  217. }
  218. $useragent = 'Zend_Framework_Gdata/' . Zend_Version::VERSION;
  219. $client->setConfig(array(
  220. 'strictredirects' => true,
  221. 'useragent' => $useragent
  222. )
  223. );
  224. $client->setAuthSubToken($token);
  225. return $client;
  226. }
  227. }