Source for file oauth2.php

Documentation is available at oauth2.php

  1. <?php
  2. /**
  3.  * @package     Joomla.Platform
  4.  * @subpackage  Google
  5.  *
  6.  * @copyright   Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved
  7.  * @license     GNU General Public License version 2 or later; see LICENSE
  8.  */
  9.  
  10. defined('JPATH_PLATFORM'or die;
  11. jimport('joomla.oauth.v2client');
  12.  
  13. /**
  14.  * Google OAuth authentication class
  15.  *
  16.  * @package     Joomla.Platform
  17.  * @subpackage  Google
  18.  * @since       12.3
  19.  */
  20. {
  21.     /**
  22.      * @var    JOAuth2Client  OAuth client for the Google authentication object.
  23.      * @since  12.3
  24.      */
  25.     protected $client;
  26.  
  27.     /**
  28.      * Constructor.
  29.      *
  30.      * @param   JRegistry      $options  JGoogleAuth options object.
  31.      * @param   JOAuth2Client  $client   OAuth client for Google authentication.
  32.      *
  33.      * @since   12.3
  34.      */
  35.     public function __construct(JRegistry $options nullJOAuth2Client $client null)
  36.     {
  37.         $this->options = isset($options$options new JRegistry;
  38.         $this->client = isset($client$client new JOAuth2Client($this->options);
  39.     }
  40.  
  41.     /**
  42.      * Method to authenticate to Google
  43.      *
  44.      * @return  boolean  True on success.
  45.      *
  46.      * @since   12.3
  47.      */
  48.     public function authenticate()
  49.     {
  50.         $this->googlize();
  51.  
  52.         return $this->client->authenticate();
  53.     }
  54.  
  55.     /**
  56.      * Verify if the client has been authenticated
  57.      *
  58.      * @return  boolean  Is authenticated
  59.      *
  60.      * @since   12.3
  61.      */
  62.     public function isAuthenticated()
  63.     {
  64.         return $this->client->isAuthenticated();
  65.     }
  66.  
  67.     /**
  68.      * Method to retrieve data from Google
  69.      *
  70.      * @param   string  $url      The URL for the request.
  71.      * @param   mixed   $data     The data to include in the request.
  72.      * @param   array   $headers  The headers to send with the request.
  73.      * @param   string  $method   The type of http request to send.
  74.      *
  75.      * @return  mixed  Data from Google.
  76.      *
  77.      * @since   12.3
  78.      */
  79.     public function query($url$data null$headers null$method 'get')
  80.     {
  81.         $this->googlize();
  82.  
  83.         return $this->client->query($url$data$headers$method);
  84.     }
  85.  
  86.     /**
  87.      * Method to fill in Google-specific OAuth settings
  88.      *
  89.      * @return  JOAuth2Client  Google-configured Oauth2 client.
  90.      *
  91.      * @since   12.3
  92.      */
  93.     protected function googlize()
  94.     {
  95.         if (!$this->client->getOption('authurl'))
  96.         {
  97.             $this->client->setOption('authurl''https://accounts.google.com/o/oauth2/auth');
  98.         }
  99.         if (!$this->client->getOption('tokenurl'))
  100.         {
  101.             $this->client->setOption('tokenurl''https://accounts.google.com/o/oauth2/token');
  102.         }
  103.         if (!$this->client->getOption('requestparams'))
  104.         {
  105.             $this->client->setOption('requestparams'Array());
  106.         }
  107.  
  108.         $params $this->client->getOption('requestparams');
  109.  
  110.         if (!array_key_exists('access_type'$params))
  111.         {
  112.             $params['access_type''offline';
  113.         }
  114.         if ($params['access_type'== 'offline' && $this->client->getOption('userefresh'=== null)
  115.         {
  116.             $this->client->setOption('userefresh'true);
  117.         }
  118.         if (!array_key_exists('approval_prompt'$params))
  119.         {
  120.             $params['approval_prompt''auto';
  121.         }
  122.  
  123.         $this->client->setOption('requestparams'$params);
  124.  
  125.         return $this->client;
  126.     }
  127. }

Documentation generated on Tue, 19 Nov 2013 15:09:42 +0100 by phpDocumentor 1.4.3