Joomla Platform  13.1
Documentation des API du framework Joomla Platform
 Tout Classes Espaces de nommage Fichiers Fonctions Variables Pages
oauth2.php
Aller à la documentation de ce fichier.
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  */
21 {
22  /**
23  * @var JOAuth2Client OAuth client for the Google authentication object.
24  * @since 12.3
25  */
26  protected $client;
27 
28  /**
29  * Constructor.
30  *
31  * @param JRegistry $options JGoogleAuth options object.
32  * @param JOAuth2Client $client OAuth client for Google authentication.
33  *
34  * @since 12.3
35  */
36  public function __construct(JRegistry $options = null, JOAuth2Client $client = null)
37  {
38  $this->options = isset($options) ? $options : new JRegistry;
39  $this->client = isset($client) ? $client : new JOAuth2Client($this->options);
40  }
41 
42  /**
43  * Method to authenticate to Google
44  *
45  * @return boolean True on success.
46  *
47  * @since 12.3
48  */
49  public function authenticate()
50  {
51  $this->googlize();
52 
53  return $this->client->authenticate();
54  }
55 
56  /**
57  * Verify if the client has been authenticated
58  *
59  * @return boolean Is authenticated
60  *
61  * @since 12.3
62  */
63  public function isAuthenticated()
64  {
65  return $this->client->isAuthenticated();
66  }
67 
68  /**
69  * Method to retrieve data from Google
70  *
71  * @param string $url The URL for the request.
72  * @param mixed $data The data to include in the request.
73  * @param array $headers The headers to send with the request.
74  * @param string $method The type of http request to send.
75  *
76  * @return mixed Data from Google.
77  *
78  * @since 12.3
79  */
80  public function query($url, $data = null, $headers = null, $method = 'get')
81  {
82  $this->googlize();
83 
84  return $this->client->query($url, $data, $headers, $method);
85  }
86 
87  /**
88  * Method to fill in Google-specific OAuth settings
89  *
90  * @return JOAuth2Client Google-configured Oauth2 client.
91  *
92  * @since 12.3
93  */
94  protected function googlize()
95  {
96  if (!$this->client->getOption('authurl'))
97  {
98  $this->client->setOption('authurl', 'https://accounts.google.com/o/oauth2/auth');
99  }
100  if (!$this->client->getOption('tokenurl'))
101  {
102  $this->client->setOption('tokenurl', 'https://accounts.google.com/o/oauth2/token');
103  }
104  if (!$this->client->getOption('requestparams'))
105  {
106  $this->client->setOption('requestparams', Array());
107  }
108 
109  $params = $this->client->getOption('requestparams');
110 
111  if (!array_key_exists('access_type', $params))
112  {
113  $params['access_type'] = 'offline';
114  }
115  if ($params['access_type'] == 'offline' && $this->client->getOption('userefresh') === null)
116  {
117  $this->client->setOption('userefresh', true);
118  }
119  if (!array_key_exists('approval_prompt', $params))
120  {
121  $params['approval_prompt'] = 'auto';
122  }
123 
124  $this->client->setOption('requestparams', $params);
125 
126  return $this->client;
127  }
128 }