File indexing completed on 2024-05-12 06:02:40

0001 <?php
0002 
0003 /**
0004  * Zend Framework
0005  *
0006  * LICENSE
0007  *
0008  * This source file is subject to the new BSD license that is bundled
0009  * with this package in the file LICENSE.txt.
0010  * It is also available through the world-wide-web at this URL:
0011  * http://framework.zend.com/license/new-bsd
0012  * If you did not receive a copy of the license and are unable to
0013  * obtain it through the world-wide-web, please send an email
0014  * to license@zend.com so we can send you a copy immediately.
0015  *
0016  * @category   Zend
0017  * @package    Zend_Gdata
0018  * @subpackage Gdata
0019  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0020  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0021  * @version    $Id$
0022  */
0023 
0024 /**
0025  * Zend_Gdata_HttpClient
0026  */
0027 // require_once 'Zend/Gdata/HttpClient.php';
0028 
0029 /**
0030  * Zend_Version
0031  */
0032 // require_once 'Zend/Version.php';
0033 
0034 /**
0035  * Wrapper around Zend_Http_Client to facilitate Google's "Account Authentication
0036  * Proxy for Web-Based Applications".
0037  *
0038  * @see http://code.google.com/apis/accounts/AuthForWebApps.html
0039  *
0040  * @category   Zend
0041  * @package    Zend_Gdata
0042  * @subpackage Gdata
0043  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0044  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0045  */
0046 class Zend_Gdata_AuthSub
0047 {
0048 
0049     const AUTHSUB_REQUEST_URI      = 'https://www.google.com/accounts/AuthSubRequest';
0050 
0051     const AUTHSUB_SESSION_TOKEN_URI = 'https://www.google.com/accounts/AuthSubSessionToken';
0052 
0053     const AUTHSUB_REVOKE_TOKEN_URI  = 'https://www.google.com/accounts/AuthSubRevokeToken';
0054 
0055     const AUTHSUB_TOKEN_INFO_URI    = 'https://www.google.com/accounts/AuthSubTokenInfo';
0056 
0057      /**
0058       * Creates a URI to request a single-use AuthSub token.
0059       *
0060       * @param string $next (required) URL identifying the service to be
0061       *                     accessed.
0062       *  The resulting token will enable access to the specified service only.
0063       *  Some services may limit scope further, such as read-only access.
0064       * @param string $scope (required) URL identifying the service to be
0065       *                      accessed.  The resulting token will enable
0066       *                      access to the specified service only.
0067       *                      Some services may limit scope further, such
0068       *                      as read-only access.
0069       * @param int $secure (optional) Boolean flag indicating whether the
0070       *                    authentication transaction should issue a secure
0071       *                    token (1) or a non-secure token (0). Secure tokens
0072       *                    are available to registered applications only.
0073       * @param int $session (optional) Boolean flag indicating whether
0074       *                     the one-time-use  token may be exchanged for
0075       *                     a session token (1) or not (0).
0076       * @param string $request_uri (optional) URI to which to direct the
0077       *                            authentication request.
0078       */
0079      public static function getAuthSubTokenUri($next, $scope, $secure=0, $session=0,
0080                                                $request_uri = self::AUTHSUB_REQUEST_URI)
0081      {
0082          $querystring = '?next=' . urlencode($next)
0083              . '&scope=' . urldecode($scope)
0084              . '&secure=' . urlencode($secure)
0085              . '&session=' . urlencode($session);
0086          return $request_uri . $querystring;
0087      }
0088 
0089 
0090     /**
0091      * Upgrades a single use token to a session token
0092      *
0093      * @param string $token The single use token which is to be upgraded
0094      * @param Zend_Http_Client $client (optional) HTTP client to use to
0095      *                                 make the request
0096      * @param string $request_uri (optional) URI to which to direct
0097      *                            the session token upgrade
0098      * @return string The upgraded token value
0099      * @throws Zend_Gdata_App_AuthException
0100      * @throws Zend_Gdata_App_HttpException
0101      */
0102     public static function getAuthSubSessionToken(
0103             $token, $client = null,
0104             $request_uri = self::AUTHSUB_SESSION_TOKEN_URI)
0105     {
0106         $client = self::getHttpClient($token, $client);
0107 
0108         if ($client instanceof Zend_Gdata_HttpClient) {
0109             $filterResult = $client->filterHttpRequest('GET', $request_uri);
0110             $url = $filterResult['url'];
0111             $headers = $filterResult['headers'];
0112             $client->setHeaders($headers);
0113             $client->setUri($url);
0114         } else {
0115             $client->setUri($request_uri);
0116         }
0117 
0118         try {
0119             $response = $client->request('GET');
0120         } catch (Zend_Http_Client_Exception $e) {
0121             // require_once 'Zend/Gdata/App/HttpException.php';
0122             throw new Zend_Gdata_App_HttpException($e->getMessage(), $e);
0123         }
0124 
0125         // Parse Google's response
0126         if ($response->isSuccessful()) {
0127             $goog_resp = array();
0128             foreach (explode("\n", $response->getBody()) as $l) {
0129                 $l = chop($l);
0130                 if ($l) {
0131                     list($key, $val) = explode('=', chop($l), 2);
0132                     $goog_resp[$key] = $val;
0133                 }
0134             }
0135             return $goog_resp['Token'];
0136         } else {
0137             // require_once 'Zend/Gdata/App/AuthException.php';
0138             throw new Zend_Gdata_App_AuthException(
0139                     'Token upgrade failed. Reason: ' . $response->getBody());
0140         }
0141     }
0142 
0143     /**
0144      * Revoke a token
0145      *
0146      * @param string $token The token to revoke
0147      * @param Zend_Http_Client $client (optional) HTTP client to use to make the request
0148      * @param string $request_uri (optional) URI to which to direct the revokation request
0149      * @return boolean Whether the revokation was successful
0150      * @throws Zend_Gdata_App_HttpException
0151      */
0152     public static function AuthSubRevokeToken($token, $client = null,
0153                                               $request_uri = self::AUTHSUB_REVOKE_TOKEN_URI)
0154     {
0155         $client = self::getHttpClient($token, $client);
0156 
0157         if ($client instanceof Zend_Gdata_HttpClient) {
0158             $filterResult = $client->filterHttpRequest('GET', $request_uri);
0159             $url = $filterResult['url'];
0160             $headers = $filterResult['headers'];
0161             $client->setHeaders($headers);
0162             $client->setUri($url);
0163             $client->resetParameters();
0164         } else {
0165             $client->setUri($request_uri);
0166         }
0167 
0168         ob_start();
0169         try {
0170             $response = $client->request('GET');
0171         } catch (Zend_Http_Client_Exception $e) {
0172             ob_end_clean();
0173             // require_once 'Zend/Gdata/App/HttpException.php';
0174             throw new Zend_Gdata_App_HttpException($e->getMessage(), $e);
0175         }
0176         ob_end_clean();
0177         // Parse Google's response
0178         if ($response->isSuccessful()) {
0179             return true;
0180         } else {
0181             return false;
0182         }
0183     }
0184 
0185 
0186     /**
0187      * get token information
0188      *
0189      * @param string $token The token to retrieve information about
0190      * @param Zend_Http_Client $client (optional) HTTP client to use to
0191      *                                 make the request
0192      * @param string $request_uri (optional) URI to which to direct
0193      *                            the information request
0194      */
0195     public static function getAuthSubTokenInfo(
0196             $token, $client = null, $request_uri = self::AUTHSUB_TOKEN_INFO_URI)
0197     {
0198         $client = self::getHttpClient($token, $client);
0199 
0200         if ($client instanceof Zend_Gdata_HttpClient) {
0201             $filterResult = $client->filterHttpRequest('GET', $request_uri);
0202             $url = $filterResult['url'];
0203             $headers = $filterResult['headers'];
0204             $client->setHeaders($headers);
0205             $client->setUri($url);
0206         } else {
0207             $client->setUri($request_uri);
0208         }
0209 
0210         ob_start();
0211         try {
0212             $response = $client->request('GET');
0213         } catch (Zend_Http_Client_Exception $e) {
0214             ob_end_clean();
0215             // require_once 'Zend/Gdata/App/HttpException.php';
0216             throw new Zend_Gdata_App_HttpException($e->getMessage(), $e);
0217         }
0218         ob_end_clean();
0219         return $response->getBody();
0220     }
0221 
0222     /**
0223      * Retrieve a HTTP client object with AuthSub credentials attached
0224      * as the Authorization header
0225      *
0226      * @param string $token The token to retrieve information about
0227      * @param Zend_Gdata_HttpClient $client (optional) HTTP client to use to make the request
0228      */
0229     public static function getHttpClient($token, $client = null)
0230     {
0231         if ($client == null) {
0232             $client = new Zend_Gdata_HttpClient();
0233         }
0234         if (!$client instanceof Zend_Gdata_HttpClient) {
0235             // require_once 'Zend/Gdata/App/HttpException.php';
0236             throw new Zend_Gdata_App_HttpException('Client is not an instance of Zend_Gdata_HttpClient.');
0237         }
0238         $useragent = 'Zend_Framework_Gdata/' . Zend_Version::VERSION;
0239         $client->setConfig(array(
0240                 'strictredirects' => true,
0241                 'useragent' => $useragent
0242             )
0243         );
0244         $client->setAuthSubToken($token);
0245         return $client;
0246     }
0247 
0248 }