source: extensions/Google2Piwigo/include/Zend/Gdata/AuthSub.php @ 17475

Last change on this file since 17475 was 17475, checked in by mistic100, 12 years ago

new extension: Google2Piwigo

File size: 9.1 KB
Line 
1<?php
2
3/**
4 * Zend Framework
5 *
6 * LICENSE
7 *
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.
15 *
16 * @category   Zend
17 * @package    Zend_Gdata
18 * @subpackage Gdata
19 * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
20 * @license    http://framework.zend.com/license/new-bsd     New BSD License
21 * @version    $Id: AuthSub.php 24594 2012-01-05 21:27:01Z matthew $
22 */
23
24/**
25 * Zend_Gdata_HttpClient
26 */
27require_once 'Zend/Gdata/HttpClient.php';
28
29/**
30 * Zend_Version
31 */
32require_once 'Zend/Version.php';
33
34/**
35 * Wrapper around Zend_Http_Client to facilitate Google's "Account Authentication
36 * Proxy for Web-Based Applications".
37 *
38 * @see http://code.google.com/apis/accounts/AuthForWebApps.html
39 *
40 * @category   Zend
41 * @package    Zend_Gdata
42 * @subpackage Gdata
43 * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
44 * @license    http://framework.zend.com/license/new-bsd     New BSD License
45 */
46class Zend_Gdata_AuthSub
47{
48
49    const AUTHSUB_REQUEST_URI      = 'https://www.google.com/accounts/AuthSubRequest';
50
51    const AUTHSUB_SESSION_TOKEN_URI = 'https://www.google.com/accounts/AuthSubSessionToken';
52
53    const AUTHSUB_REVOKE_TOKEN_URI  = 'https://www.google.com/accounts/AuthSubRevokeToken';
54
55    const AUTHSUB_TOKEN_INFO_URI    = 'https://www.google.com/accounts/AuthSubTokenInfo';
56
57     /**
58      * Creates a URI to request a single-use AuthSub token.
59      *
60      * @param string $next (required) URL identifying the service to be
61      *                     accessed.
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.
78      */
79     public static function getAuthSubTokenUri($next, $scope, $secure=0, $session=0,
80                                               $request_uri = self::AUTHSUB_REQUEST_URI)
81     {
82         $querystring = '?next=' . urlencode($next)
83             . '&scope=' . urldecode($scope)
84             . '&secure=' . urlencode($secure)
85             . '&session=' . urlencode($session);
86         return $request_uri . $querystring;
87     }
88
89
90    /**
91     * Upgrades a single use token to a session token
92     *
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
95     *                                 make the request
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
101     */
102    public static function getAuthSubSessionToken(
103            $token, $client = null,
104            $request_uri = self::AUTHSUB_SESSION_TOKEN_URI)
105    {
106        $client = self::getHttpClient($token, $client);
107
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);
114        } else {
115            $client->setUri($request_uri);
116        }
117
118        try {
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);
123        }
124
125        // Parse Google's response
126        if ($response->isSuccessful()) {
127            $goog_resp = array();
128            foreach (explode("\n", $response->getBody()) as $l) {
129                $l = chop($l);
130                if ($l) {
131                    list($key, $val) = explode('=', chop($l), 2);
132                    $goog_resp[$key] = $val;
133                }
134            }
135            return $goog_resp['Token'];
136        } else {
137            require_once 'Zend/Gdata/App/AuthException.php';
138            throw new Zend_Gdata_App_AuthException(
139                    'Token upgrade failed. Reason: ' . $response->getBody());
140        }
141    }
142
143    /**
144     * Revoke a token
145     *
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
151     */
152    public static function AuthSubRevokeToken($token, $client = null,
153                                              $request_uri = self::AUTHSUB_REVOKE_TOKEN_URI)
154    {
155        $client = self::getHttpClient($token, $client);
156
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();
164        } else {
165            $client->setUri($request_uri);
166        }
167
168        ob_start();
169        try {
170            $response = $client->request('GET');
171        } catch (Zend_Http_Client_Exception $e) {
172            ob_end_clean();
173            require_once 'Zend/Gdata/App/HttpException.php';
174            throw new Zend_Gdata_App_HttpException($e->getMessage(), $e);
175        }
176        ob_end_clean();
177        // Parse Google's response
178        if ($response->isSuccessful()) {
179            return true;
180        } else {
181            return false;
182        }
183    }
184
185
186    /**
187     * get token information
188     *
189     * @param string $token The token to retrieve information about
190     * @param Zend_Http_Client $client (optional) HTTP client to use to
191     *                                 make the request
192     * @param string $request_uri (optional) URI to which to direct
193     *                            the information request
194     */
195    public static function getAuthSubTokenInfo(
196            $token, $client = null, $request_uri = self::AUTHSUB_TOKEN_INFO_URI)
197    {
198        $client = self::getHttpClient($token, $client);
199
200        if ($client instanceof Zend_Gdata_HttpClient) {
201            $filterResult = $client->filterHttpRequest('GET', $request_uri);
202            $url = $filterResult['url'];
203            $headers = $filterResult['headers'];
204            $client->setHeaders($headers);
205            $client->setUri($url);
206        } else {
207            $client->setUri($request_uri);
208        }
209
210        ob_start();
211        try {
212            $response = $client->request('GET');
213        } catch (Zend_Http_Client_Exception $e) {
214            ob_end_clean();
215            require_once 'Zend/Gdata/App/HttpException.php';
216            throw new Zend_Gdata_App_HttpException($e->getMessage(), $e);
217        }
218        ob_end_clean();
219        return $response->getBody();
220    }
221
222    /**
223     * Retrieve a HTTP client object with AuthSub credentials attached
224     * as the Authorization header
225     *
226     * @param string $token The token to retrieve information about
227     * @param Zend_Gdata_HttpClient $client (optional) HTTP client to use to make the request
228     */
229    public static function getHttpClient($token, $client = null)
230    {
231        if ($client == null) {
232            $client = new Zend_Gdata_HttpClient();
233        }
234        if (!$client instanceof Zend_Gdata_HttpClient) {
235            require_once 'Zend/Gdata/App/HttpException.php';
236            throw new Zend_Gdata_App_HttpException('Client is not an instance of Zend_Gdata_HttpClient.');
237        }
238        $useragent = 'Zend_Framework_Gdata/' . Zend_Version::VERSION;
239        $client->setConfig(array(
240                'strictredirects' => true,
241                'useragent' => $useragent
242            )
243        );
244        $client->setAuthSubToken($token);
245        return $client;
246    }
247
248}
Note: See TracBrowser for help on using the repository browser.