-
Notifications
You must be signed in to change notification settings - Fork 4
/
class-goauth.php
302 lines (274 loc) · 8.12 KB
/
class-goauth.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
<?php
/*
Copyright 2012 Sorin Iclanzan (email : sorin@hel.io)
This file is part of Backup.
Backup is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Backup is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Backup. If not, see http://www.gnu.org/licenses/gpl.html.
*/
/**
* Google OAuth2 class
*
* This class handles operations realted to Google's OAuth2 service.
*
* @uses WP_Error for storing error messages.
*/
class GOAuth {
/**
* Stores the base url to Google's OAuth2 server.
*
* @var string
* @access private
*/
private $base_url = 'https://accounts.google.com/o/oauth2/';
/**
* Stores the Client Id.
*
* @var string
* @access private
*/
private $client_id;
/**
* Stores the Client Secret.
*
* @var string
* @access private
*/
private $client_secret;
/**
* Stores the redirect URI.
*
* @var string
* @access private
*/
private $redirect_uri;
/**
* Stores the refresh token.
*
* @var string
* @access private
*/
private $refresh_token;
/**
* Stores the access token.
*
* @var string
* @access private
*/
private $access_token;
/**
* Stores the number of seconds to wait for a response before timing out.
*
* @var integer
* @access private
*/
private $request_timeout;
/**
* Stores whether or not to verify host SSL certificate.
*
* @var boolean
* @access private
*/
private $ssl_verify;
/**
* Constructor - Assigns values to some properties.
*
* @param array $args Optional. The list of options and values to set
*/
function __construct( $args = array() ) {
$default_args = array(
'client_id' => '',
'client_secret' => '',
'redirect_uri' => '',
'refresh_token' => '',
'request_timeout' => 5,
'ssl_verify' => true
);
$this->set_options( array_merge( $default_args, $args ) );
}
/**
* Sets multiple options at once.
*
* @access public
* @param array $args List of options and values to set
* @return boolean Returns TRUE on success, FALSE on failure.
*/
public function set_options( $args ) {
if ( ! is_array( $args ) )
return false;
foreach ( $args as $option => $value )
$this->set_option( $option, $value );
}
/**
* Sets an option.
*
* @access public
* @param string $option The option to set.
* @param mixed $value The value to set the option to.
* @return boolean Returns TRUE on success, FALSE on failure.
*/
public function set_option( $option, $value ) {
switch ( $option ) {
case 'ssl_verify':
$this->ssl_verify = ( bool ) $value;
return;
case 'request_timeout':
if ( intval( $value ) > 0 )
$this->request_timeout = intval( $value );
return;
default:
$this->$option = ( string ) $value;
return;
}
}
/**
* Requests authorization from Google's OAuth2 server to access services for a user.
*
* @access public
* @param array $scope Array of API URLs to services where access is wanted.
* @param string $state A string that is passed back from Google.
* @param boolean $approval_prompt Indicates whether to force prompting for approval (TRUE) or not (FALSE). Defaults to FALSE.
* @return NULL
*/
public function request_authorization( $scope = array() , $state = '', $approval_prompt = false ) {
$params = array(
'response_type' => 'code',
'client_id' => $this->client_id,
'redirect_uri' => $this->redirect_uri,
'scope' => implode( ' ', $scope ),
'access_type' => 'offline',
);
if ( ! empty( $state ) )
$params['state'] = $state;
if ( $approval_prompt )
$params['approval_prompt'] = $force;
header( 'Location: ' . $this->base_url . 'auth?' . http_build_query( $params ) );
}
/**
* Requests a refresh token from Google's OAuth2 server.
*
* @uses wp_remote_post
* @access public
* @param string $code Authorization code received from Google. If empty the method will try to get it from $_GET['code'].
* @return mixed Returns a refresh token on success or an instance of WP_Error on failure.
*/
public function request_refresh_token( $code = '' ) {
if ( $code == '' )
$code = $_GET['code'];
$args = array(
'timeout' => $this->request_timeout,
'ssl_verify' => $this->ssl_verify,
'body' => array(
'code' => $code,
'client_id' => $this->client_id,
'client_secret' => $this->client_secret,
'redirect_uri' => $this->redirect_uri,
'grant_type' => 'authorization_code'
)
);
$result = wp_remote_post( $this->base_url . 'token', $args );
if ( is_wp_error( $result ) )
return $result;
else {
if ( $result['response']['code'] == '200' ) {
$result = json_decode( $result['body'], true );
if ( isset($result['refresh_token']) ) {
$this->refresh_token = $result['refresh_token'];
$this->access_token = $result['access_token'];
return $result['refresh_token'];
}
return new WP_Error('no_refresh_token', "Did not receive a refresh token.");
}
else
return new WP_Error( 'bad_response', 'The server returned code ' . $result['response']['code'] . ' ' . $result['response']['message'] . ' while trying to obtain a refresh token.' );
}
}
/**
* Requests and returns an access token from Google's OAuth2 server.
*
* @uses wp_remote_post
* @access private
* @return mixed Returns a new access token on success or an instance of WP_Error on failure.
*/
private function request_access_token() {
$args = array(
'timeout' => $this->request_timeout,
'ssl_verify' => $this->ssl_verify,
'body' => array(
'refresh_token' => $this->refresh_token,
'client_id' => $this->client_id,
'client_secret' => $this->client_secret,
'grant_type' => 'refresh_token'
)
);
$result = wp_remote_post( $this->base_url . 'token', $args );
if( is_wp_error( $result ) )
return $result;
else {
if ( $result['response']['code'] == '200' ) {
$result = json_decode( $result['body'], true );
$this->access_token = $result['access_token'];
return $result['access_token'];
}
else
return new WP_Error('bad_response', 'The server returned code ' . $result['response']['code'] . ' ' . $result['response']['message'] . ' while trying to obtain an access token.');
}
}
/**
* Returns the access token.
*
* @access public
* @return mixed Returns the access token on success, an instance of WP_Error on failure.
*/
public function get_access_token() {
if ( empty( $this->access_token ) )
if ( empty( $this->refresh_token ) )
return new WP_Error( 'invalid_operation', 'You need a refresh token in order to request an access token.' );
else
return $this->request_access_token();
else
return $this->access_token;
}
/**
* Revoke a refresh token.
*
* @uses wp_remote_get
* @access public
* @return mixed Returns TRUE on success, an instance of WP_Error on failure.
*/
public function revoke_refresh_token() {
if ( ! empty( $this->refresh_token ) ) {
$result = wp_remote_get( $this->base_url . 'revoke?token=' . $this->refresh_token );
if ( is_wp_error( $result ) )
return $result;
else {
if ( $result['response']['code'] == '200' ) {
$this->refresh_token = '';
return true;
}
return new WP_Error("bad_response", "The server returned code " . $result['response']['code'] . " " . $result['response']['message'] . " while trying to revoke the refresh token.");
}
}
return new WP_Error( 'invalid_operation', 'There is no refresh token to revoke.' );
}
/**
* Checks whether the refresh token is set or not.
*
* @access public
* @return boolean Returns TRUE if the refresh token is set, FALSE otherwise.
*/
public function is_authorized() {
return (
!empty( $this->refresh_token ) &&
!empty( $this->client_id ) &&
!empty( $this->client_secret )
);
}
}