8 * This source file is subject to the new BSD license that is bundled
9 * with this package in the file LICENSE.txt.
10 * It is also available through the world-wide-web at this URL:
11 * http://framework.zend.com/license/new-bsd
12 * If you did not receive a copy of the license and are unable to
13 * obtain it through the world-wide-web, please send an email
14 * to license@zend.com so we can send you a copy immediately.
19 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
20 * @license http://framework.zend.com/license/new-bsd New BSD License
25 * Zend_Gdata_HttpClient
27 require_once 'Zend/Gdata/HttpClient.php';
32 require_once 'Zend/Version.php';
35 * Wrapper around Zend_Http_Client to facilitate Google's "Account Authentication
36 * Proxy for Web-Based Applications".
38 * @see http://code.google.com/apis/accounts/AuthForWebApps.html
43 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
44 * @license http://framework.zend.com/license/new-bsd New BSD License
46 class Zend_Gdata_AuthSub
49 const AUTHSUB_REQUEST_URI = 'https://www.google.com/accounts/AuthSubRequest';
51 const AUTHSUB_SESSION_TOKEN_URI = 'https://www.google.com/accounts/AuthSubSessionToken';
53 const AUTHSUB_REVOKE_TOKEN_URI = 'https://www.google.com/accounts/AuthSubRevokeToken';
55 const AUTHSUB_TOKEN_INFO_URI = 'https://www.google.com/accounts/AuthSubTokenInfo';
58 * Creates a URI to request a single-use AuthSub token.
60 * @param string $next (required) URL identifying the service to be
62 * The resulting token will enable access to the specified service only.
63 * Some services may limit scope further, such as read-only access.
64 * @param string $scope (required) URL identifying the service to be
65 * accessed. The resulting token will enable
66 * access to the specified service only.
67 * Some services may limit scope further, such
68 * as read-only access.
69 * @param int $secure (optional) Boolean flag indicating whether the
70 * authentication transaction should issue a secure
71 * token (1) or a non-secure token (0). Secure tokens
72 * are available to registered applications only.
73 * @param int $session (optional) Boolean flag indicating whether
74 * the one-time-use token may be exchanged for
75 * a session token (1) or not (0).
76 * @param string $request_uri (optional) URI to which to direct the
77 * authentication request.
79 public static function getAuthSubTokenUri($next, $scope, $secure=0, $session=0,
80 $request_uri = self::AUTHSUB_REQUEST_URI)
82 $querystring = '?next=' . urlencode($next)
83 . '&scope=' . urldecode($scope)
84 . '&secure=' . urlencode($secure)
85 . '&session=' . urlencode($session);
86 return $request_uri . $querystring;
91 * Upgrades a single use token to a session token
93 * @param string $token The single use token which is to be upgraded
94 * @param Zend_Http_Client $client (optional) HTTP client to use to
96 * @param string $request_uri (optional) URI to which to direct
97 * the session token upgrade
98 * @return string The upgraded token value
99 * @throws Zend_Gdata_App_AuthException
100 * @throws Zend_Gdata_App_HttpException
102 public static function getAuthSubSessionToken(
103 $token, $client = null,
104 $request_uri = self::AUTHSUB_SESSION_TOKEN_URI)
106 $client = self::getHttpClient($token, $client);
108 if ($client instanceof Zend_Gdata_HttpClient) {
109 $filterResult = $client->filterHttpRequest('GET', $request_uri);
110 $url = $filterResult['url'];
111 $headers = $filterResult['headers'];
112 $client->setHeaders($headers);
113 $client->setUri($url);
115 $client->setUri($request_uri);
119 $response = $client->request('GET');
120 } catch (Zend_Http_Client_Exception $e) {
121 require_once 'Zend/Gdata/App/HttpException.php';
122 throw new Zend_Gdata_App_HttpException($e->getMessage(), $e);
125 // Parse Google's response
126 if ($response->isSuccessful()) {
127 $goog_resp = array();
128 foreach (explode("\n", $response->getBody()) as $l) {
131 list($key, $val) = explode('=', chop($l), 2);
132 $goog_resp[$key] = $val;
135 return $goog_resp['Token'];
137 require_once 'Zend/Gdata/App/AuthException.php';
138 throw new Zend_Gdata_App_AuthException(
139 'Token upgrade failed. Reason: ' . $response->getBody());
146 * @param string $token The token to revoke
147 * @param Zend_Http_Client $client (optional) HTTP client to use to make the request
148 * @param string $request_uri (optional) URI to which to direct the revokation request
149 * @return boolean Whether the revokation was successful
150 * @throws Zend_Gdata_App_HttpException
152 public static function AuthSubRevokeToken($token, $client = null,
153 $request_uri = self::AUTHSUB_REVOKE_TOKEN_URI)
155 $client = self::getHttpClient($token, $client);
157 if ($client instanceof Zend_Gdata_HttpClient) {
158 $filterResult = $client->filterHttpRequest('GET', $request_uri);
159 $url = $filterResult['url'];
160 $headers = $filterResult['headers'];
161 $client->setHeaders($headers);
162 $client->setUri($url);
163 $client->resetParameters();
165 $client->setUri($request_uri);
170 $response = $client->request('GET');
171 } catch (Zend_Http_Client_Exception $e) {
172 require_once 'Zend/Gdata/App/HttpException.php';
173 throw new Zend_Gdata_App_HttpException($e->getMessage(), $e);
176 // Parse Google's response
177 if ($response->isSuccessful()) {
186 * get token information
188 * @param string $token The token to retrieve information about
189 * @param Zend_Http_Client $client (optional) HTTP client to use to
191 * @param string $request_uri (optional) URI to which to direct
192 * the information request
194 public static function getAuthSubTokenInfo(
195 $token, $client = null, $request_uri = self::AUTHSUB_TOKEN_INFO_URI)
197 $client = self::getHttpClient($token, $client);
199 if ($client instanceof Zend_Gdata_HttpClient) {
200 $filterResult = $client->filterHttpRequest('GET', $request_uri);
201 $url = $filterResult['url'];
202 $headers = $filterResult['headers'];
203 $client->setHeaders($headers);
204 $client->setUri($url);
206 $client->setUri($request_uri);
211 $response = $client->request('GET');
212 } catch (Zend_Http_Client_Exception $e) {
213 require_once 'Zend/Gdata/App/HttpException.php';
214 throw new Zend_Gdata_App_HttpException($e->getMessage(), $e);
217 return $response->getBody();
221 * Retrieve a HTTP client object with AuthSub credentials attached
222 * as the Authorization header
224 * @param string $token The token to retrieve information about
225 * @param Zend_Gdata_HttpClient $client (optional) HTTP client to use to make the request
227 public static function getHttpClient($token, $client = null)
229 if ($client == null) {
230 $client = new Zend_Gdata_HttpClient();
232 if (!$client instanceof Zend_Http_Client) {
233 require_once 'Zend/Gdata/App/HttpException.php';
234 throw new Zend_Gdata_App_HttpException('Client is not an instance of Zend_Http_Client.');
236 $useragent = 'Zend_Framework_Gdata/' . Zend_Version::VERSION;
237 $client->setConfig(array(
238 'strictredirects' => true,
239 'useragent' => $useragent
242 $client->setAuthSubToken($token);