[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/lib/zend/Zend/Gdata/ -> AuthSub.php (source)

   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 [email protected] so we can send you a copy immediately.
  15   *
  16   * @category   Zend
  17   * @package    Zend_Gdata
  18   * @subpackage Gdata
  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
  21   * @version    $Id$
  22   */
  23  
  24  /**
  25   * Zend_Gdata_HttpClient
  26   */
  27  require_once 'Zend/Gdata/HttpClient.php';
  28  
  29  /**
  30   * Zend_Version
  31   */
  32  require_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-2010 Zend Technologies USA Inc. (http://www.zend.com)
  44   * @license    http://framework.zend.com/license/new-bsd     New BSD License
  45   */
  46  class 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              require_once 'Zend/Gdata/App/HttpException.php';
 173              throw new Zend_Gdata_App_HttpException($e->getMessage(), $e);
 174          }
 175          ob_end_clean();
 176          // Parse Google's response
 177          if ($response->isSuccessful()) {
 178              return true;
 179          } else {
 180              return false;
 181          }
 182      }
 183  
 184  
 185      /**
 186       * get token information
 187       *
 188       * @param string $token The token to retrieve information about
 189       * @param Zend_Http_Client $client (optional) HTTP client to use to
 190       *                                 make the request
 191       * @param string $request_uri (optional) URI to which to direct
 192       *                            the information request
 193       */
 194      public static function getAuthSubTokenInfo(
 195              $token, $client = null, $request_uri = self::AUTHSUB_TOKEN_INFO_URI)
 196      {
 197          $client = self::getHttpClient($token, $client);
 198  
 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);
 205          } else {
 206              $client->setUri($request_uri);
 207          }
 208  
 209          ob_start();
 210          try {
 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);
 215          }
 216          ob_end_clean();
 217          return $response->getBody();
 218      }
 219  
 220      /**
 221       * Retrieve a HTTP client object with AuthSub credentials attached
 222       * as the Authorization header
 223       *
 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
 226       */
 227      public static function getHttpClient($token, $client = null)
 228      {
 229          if ($client == null) {
 230              $client = new Zend_Gdata_HttpClient();
 231          }
 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.');
 235          }
 236          $useragent = 'Zend_Framework_Gdata/' . Zend_Version::VERSION;
 237          $client->setConfig(array(
 238                  'strictredirects' => true,
 239                  'useragent' => $useragent
 240              )
 241          );
 242          $client->setAuthSubToken($token);
 243          return $client;
 244      }
 245  
 246  }


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1