1 <?php
2 /**
3 * @package Joomla.Platform
4 * @subpackage Google
5 *
6 * @copyright Copyright (C) 2005 - 2017 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
12 use Joomla\Registry\Registry;
13
14 /**
15 * Google OAuth authentication class
16 *
17 * @since 12.3
18 * @deprecated 4.0 Use the `joomla/google` package via Composer instead
19 */
20 class JGoogleAuthOauth2 extends JGoogleAuth
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 Registry $options JGoogleAuth options object.
32 * @param JOAuth2Client $client OAuth client for Google authentication.
33 *
34 * @since 12.3
35 */
36 public function __construct(Registry $options = null, JOAuth2Client $client = null)
37 {
38 $this->options = isset($options) ? $options : new Registry;
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
101 if (!$this->client->getOption('tokenurl'))
102 {
103 $this->client->setOption('tokenurl', 'https://accounts.google.com/o/oauth2/token');
104 }
105
106 if (!$this->client->getOption('requestparams'))
107 {
108 $this->client->setOption('requestparams', array());
109 }
110
111 $params = $this->client->getOption('requestparams');
112
113 if (!array_key_exists('access_type', $params))
114 {
115 $params['access_type'] = 'offline';
116 }
117
118 if ($params['access_type'] == 'offline' && $this->client->getOption('userefresh') === null)
119 {
120 $this->client->setOption('userefresh', true);
121 }
122
123 if (!array_key_exists('approval_prompt', $params))
124 {
125 $params['approval_prompt'] = 'auto';
126 }
127
128 $this->client->setOption('requestparams', $params);
129
130 return $this->client;
131 }
132 }
133