1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Part of the Joomla Framework Github Package |
4
|
|
|
* |
5
|
|
|
* @copyright Copyright (C) 2005 - 2015 Open Source Matters, Inc. All rights reserved. |
6
|
|
|
* @license GNU General Public License version 2 or later; see LICENSE |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Joomla\Github\Package; |
10
|
|
|
|
11
|
|
|
use Joomla\Github\AbstractPackage; |
12
|
|
|
use Joomla\Http\Exception\UnexpectedResponseException; |
13
|
|
|
use Joomla\Uri\Uri; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* GitHub API Authorization class for the Joomla Framework. |
17
|
|
|
* |
18
|
|
|
* @documentation http://developer.github.com/v3/oauth/ |
19
|
|
|
* @documentation http://developer.github.com/v3/oauth_authorizations/ |
20
|
|
|
* |
21
|
|
|
* @note The methods in this class are only accessible with Basic Authentication |
22
|
|
|
* @since 1.0 |
23
|
|
|
*/ |
24
|
|
|
class Authorization extends AbstractPackage |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* Method to create an authorization. |
28
|
|
|
* |
29
|
|
|
* @param array $scopes A list of scopes that this authorization is in. |
30
|
|
|
* @param string $note A note to remind you what the OAuth token is for. |
31
|
|
|
* @param string $url A URL to remind you what app the OAuth token is for. |
32
|
|
|
* |
33
|
|
|
* @return object |
34
|
|
|
* |
35
|
|
|
* @since 1.0 |
36
|
|
|
* @throws \DomainException |
37
|
|
|
*/ |
38
|
|
|
public function create(array $scopes = array(), $note = '', $url = '') |
39
|
|
|
{ |
40
|
|
|
// Build the request path. |
41
|
|
|
$path = '/authorizations'; |
42
|
|
|
|
43
|
|
|
$data = json_encode( |
44
|
|
|
array('scopes' => $scopes, 'note' => $note, 'note_url' => $url) |
45
|
|
|
); |
46
|
|
|
|
47
|
|
|
// Send the request. |
48
|
|
|
return $this->processResponse($this->client->post($this->fetchUrl($path), $data), 201); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Method to delete an authorization |
53
|
|
|
* |
54
|
|
|
* @param integer $id ID of the authorization to delete |
55
|
|
|
* |
56
|
|
|
* @return object |
57
|
|
|
* |
58
|
|
|
* @since 1.0 |
59
|
|
|
* @throws \DomainException |
60
|
|
|
*/ |
61
|
|
|
public function delete($id) |
62
|
|
|
{ |
63
|
|
|
// Build the request path. |
64
|
|
|
$path = '/authorizations/' . $id; |
65
|
|
|
|
66
|
|
|
// Send the request. |
67
|
|
|
return $this->processResponse($this->client->delete($this->fetchUrl($path)), 204); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Delete a grant |
72
|
|
|
* |
73
|
|
|
* Deleting an OAuth application's grant will also delete all OAuth tokens associated with the application for your user. |
74
|
|
|
* |
75
|
|
|
* @param integer $id ID of the authorization to delete |
76
|
|
|
* |
77
|
|
|
* @return object |
78
|
|
|
* |
79
|
|
|
* @since 1.5.0 |
80
|
|
|
* @throws \DomainException |
81
|
|
|
*/ |
82
|
|
|
public function deleteGrant($id) |
83
|
|
|
{ |
84
|
|
|
// Build the request path. |
85
|
|
|
$path = '/authorizations/grants/' . $id; |
86
|
|
|
|
87
|
|
|
// Send the request. |
88
|
|
|
return $this->processResponse($this->client->delete($this->fetchUrl($path)), 204); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Method to edit an authorization. |
93
|
|
|
* |
94
|
|
|
* @param integer $id ID of the authorization to edit |
95
|
|
|
* @param array $scopes Replaces the authorization scopes with these. |
96
|
|
|
* @param array $addScopes A list of scopes to add to this authorization. |
97
|
|
|
* @param array $removeScopes A list of scopes to remove from this authorization. |
98
|
|
|
* @param string $note A note to remind you what the OAuth token is for. |
99
|
|
|
* @param string $url A URL to remind you what app the OAuth token is for. |
100
|
|
|
* |
101
|
|
|
* @return object |
102
|
|
|
* |
103
|
|
|
* @since 1.0 |
104
|
|
|
* @throws \DomainException |
105
|
|
|
* @throws \RuntimeException |
106
|
|
|
*/ |
107
|
|
|
public function edit($id, array $scopes = array(), array $addScopes = array(), array $removeScopes = array(), $note = '', $url = '') |
108
|
|
|
{ |
109
|
|
|
// Check if more than one scopes array contains data |
110
|
|
|
$scopesCount = 0; |
111
|
|
|
$scope = ''; |
112
|
|
|
$scopeData = ''; |
113
|
|
|
|
114
|
|
|
if (!empty($scopes)) |
115
|
|
|
{ |
116
|
|
|
$scope = 'scopes'; |
117
|
|
|
$scopeData = $scopes; |
118
|
|
|
$scopesCount++; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
if (!empty($addScopes)) |
122
|
|
|
{ |
123
|
|
|
$scope = 'add_scopes'; |
124
|
|
|
$scopeData = $addScopes; |
125
|
|
|
$scopesCount++; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
if (!empty($removeScopes)) |
129
|
|
|
{ |
130
|
|
|
$scope = 'remove_scopes'; |
131
|
|
|
$scopeData = $removeScopes; |
132
|
|
|
$scopesCount++; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
// Only allowed to send data for one scope parameter |
136
|
|
|
if ($scopesCount >= 2) |
137
|
|
|
{ |
138
|
|
|
throw new \RuntimeException('You can only send one scope key in this request.'); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
// Build the request path. |
142
|
|
|
$path = '/authorizations/' . $id; |
143
|
|
|
|
144
|
|
|
$data = json_encode( |
145
|
|
|
array( |
146
|
|
|
$scope => $scopeData, |
147
|
|
|
'note' => $note, |
148
|
|
|
'note_url' => $url |
149
|
|
|
) |
150
|
|
|
); |
151
|
|
|
|
152
|
|
|
// Send the request. |
153
|
|
|
return $this->processResponse($this->client->patch($this->fetchUrl($path), $data)); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Method to get details about an authorised application for the authenticated user. |
158
|
|
|
* |
159
|
|
|
* @param integer $id ID of the authorization to retrieve |
160
|
|
|
* |
161
|
|
|
* @return object |
162
|
|
|
* |
163
|
|
|
* @since 1.0 |
164
|
|
|
* @throws \DomainException |
165
|
|
|
*/ |
166
|
|
|
public function get($id) |
167
|
|
|
{ |
168
|
|
|
// Build the request path. |
169
|
|
|
$path = '/authorizations/' . $id; |
170
|
|
|
|
171
|
|
|
// Send the request. |
172
|
|
|
return $this->processResponse($this->client->get($this->fetchUrl($path))); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Get a single grant |
177
|
|
|
* |
178
|
|
|
* @param integer $id ID of the authorization to retrieve |
179
|
|
|
* |
180
|
|
|
* @return object |
181
|
|
|
* |
182
|
|
|
* @since 1.5.0 |
183
|
|
|
* @throws \DomainException |
184
|
|
|
*/ |
185
|
|
|
public function getGrant($id) |
186
|
|
|
{ |
187
|
|
|
// Build the request path. |
188
|
|
|
$path = '/authorizations/grants/' . $id; |
189
|
|
|
|
190
|
|
|
// Send the request. |
191
|
|
|
return $this->processResponse($this->client->get($this->fetchUrl($path))); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Method to get the authorised applications for the authenticated user. |
196
|
|
|
* |
197
|
|
|
* @return object |
198
|
|
|
* |
199
|
|
|
* @since 1.0 |
200
|
|
|
* @throws \DomainException |
201
|
|
|
*/ |
202
|
|
|
public function getList() |
203
|
|
|
{ |
204
|
|
|
// Build the request path. |
205
|
|
|
$path = '/authorizations'; |
206
|
|
|
|
207
|
|
|
// Send the request. |
208
|
|
|
return $this->processResponse($this->client->get($this->fetchUrl($path))); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* List your grants. |
213
|
|
|
* |
214
|
|
|
* You can use this API to list the set of OAuth applications that have been granted access to your account. |
215
|
|
|
* |
216
|
|
|
* @return object |
217
|
|
|
* |
218
|
|
|
* @since 1.5.0 |
219
|
|
|
* @throws \DomainException |
220
|
|
|
*/ |
221
|
|
|
public function getListGrants() |
222
|
|
|
{ |
223
|
|
|
// Build the request path. |
224
|
|
|
$path = '/authorizations/grants'; |
225
|
|
|
|
226
|
|
|
// Send the request. |
227
|
|
|
return $this->processResponse($this->client->get($this->fetchUrl($path))); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* Method to get the rate limit for the authenticated user. |
232
|
|
|
* |
233
|
|
|
* @return object Returns an object with the properties of `limit` and `remaining`. If there is no limit, the |
234
|
|
|
* `limit` property will be false. |
235
|
|
|
* |
236
|
|
|
* @since 1.0 |
237
|
|
|
* @throws UnexpectedResponseException |
238
|
|
|
*/ |
239
|
|
|
public function getRateLimit() |
240
|
|
|
{ |
241
|
|
|
// Build the request path. |
242
|
|
|
$path = '/rate_limit'; |
243
|
|
|
|
244
|
|
|
// Send the request. |
245
|
|
|
$response = $this->client->get($this->fetchUrl($path)); |
246
|
|
|
|
247
|
|
|
// Validate the response code. |
248
|
|
|
if ($response->code != 200) |
249
|
|
|
{ |
250
|
|
|
if ($response->code == 404) |
251
|
|
|
{ |
252
|
|
|
// Unlimited rate for Github Enterprise sites and trusted users. |
253
|
|
|
return (object) array('limit' => false, 'remaining' => null); |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
// Decode the error response and throw an exception. |
257
|
|
|
$error = json_decode($response->body); |
258
|
|
|
throw new UnexpectedResponseException($response, $error->message, $response->code); |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
return json_decode($response->body); |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* 1. Request authorization on GitHub. |
266
|
|
|
* |
267
|
|
|
* @param string $client_id The client ID you received from GitHub when you registered. |
268
|
|
|
* @param string $redirect_uri URL in your app where users will be sent after authorization. |
269
|
|
|
* @param string $scope Comma separated list of scopes. |
270
|
|
|
* @param string $state An unguessable random string. It is used to protect against |
271
|
|
|
* cross-site request forgery attacks. |
272
|
|
|
* |
273
|
|
|
* @return string |
274
|
|
|
* |
275
|
|
|
* @since 1.0 |
276
|
|
|
*/ |
277
|
|
|
public function getAuthorizationLink($client_id, $redirect_uri = '', $scope = '', $state = '') |
278
|
|
|
{ |
279
|
|
|
$uri = new Uri('https://github.com/login/oauth/authorize'); |
280
|
|
|
|
281
|
|
|
$uri->setVar('client_id', $client_id); |
282
|
|
|
|
283
|
|
|
if ($redirect_uri) |
284
|
|
|
{ |
285
|
|
|
$uri->setVar('redirect_uri', urlencode($redirect_uri)); |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
if ($scope) |
289
|
|
|
{ |
290
|
|
|
$uri->setVar('scope', $scope); |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
if ($state) |
294
|
|
|
{ |
295
|
|
|
$uri->setVar('state', $state); |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
return (string) $uri; |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
/** |
302
|
|
|
* 2. Request the access token. |
303
|
|
|
* |
304
|
|
|
* @param string $client_id The client ID you received from GitHub when you registered. |
305
|
|
|
* @param string $client_secret The client secret you received from GitHub when you registered. |
306
|
|
|
* @param string $code The code you received as a response to Step 1. |
307
|
|
|
* @param string $redirect_uri URL in your app where users will be sent after authorization. |
308
|
|
|
* @param string $format The response format (json, xml, ). |
309
|
|
|
* |
310
|
|
|
* @return string |
311
|
|
|
* |
312
|
|
|
* @since 1.0 |
313
|
|
|
* @throws \UnexpectedValueException |
314
|
|
|
*/ |
315
|
|
|
public function requestToken($client_id, $client_secret, $code, $redirect_uri = '', $format = '') |
316
|
|
|
{ |
317
|
|
|
$uri = 'https://github.com/login/oauth/access_token'; |
318
|
|
|
|
319
|
|
|
$data = array( |
320
|
|
|
'client_id' => $client_id, |
321
|
|
|
'client_secret' => $client_secret, |
322
|
|
|
'code' => $code |
323
|
|
|
); |
324
|
|
|
|
325
|
|
|
if ($redirect_uri) |
326
|
|
|
{ |
327
|
|
|
$data['redirect_uri'] = $redirect_uri; |
328
|
|
|
} |
329
|
|
|
|
330
|
|
|
$headers = array(); |
331
|
|
|
|
332
|
|
|
switch ($format) |
333
|
|
|
{ |
334
|
|
|
case 'json' : |
335
|
|
|
$headers['Accept'] = 'application/json'; |
336
|
|
|
break; |
337
|
|
|
case 'xml' : |
338
|
|
|
$headers['Accept'] = 'application/xml'; |
339
|
|
|
break; |
340
|
|
|
default : |
341
|
|
|
if ($format) |
342
|
|
|
{ |
343
|
|
|
throw new \UnexpectedValueException('Invalid format'); |
344
|
|
|
} |
345
|
|
|
break; |
346
|
|
|
} |
347
|
|
|
|
348
|
|
|
// Send the request. |
349
|
|
|
return $this->processResponse( |
350
|
|
|
$this->client->post($uri, $data, $headers), |
351
|
|
|
200 |
352
|
|
|
); |
353
|
|
|
} |
354
|
|
|
|
355
|
|
|
/** |
356
|
|
|
* Revoke a grant for an application |
357
|
|
|
* |
358
|
|
|
* OAuth application owners can revoke a grant for their OAuth application and a specific user. |
359
|
|
|
* |
360
|
|
|
* @param integer $clientId The application client ID |
361
|
|
|
* @param integer $accessToken The access token to revoke |
362
|
|
|
* |
363
|
|
|
* @return object |
364
|
|
|
* |
365
|
|
|
* @since 1.5.0 |
366
|
|
|
* @throws \DomainException |
367
|
|
|
*/ |
368
|
|
|
public function revokeGrantForApplication($clientId, $accessToken) |
369
|
|
|
{ |
370
|
|
|
// Build the request path. |
371
|
|
|
$path = "/applications/$clientId/grants/$accessToken"; |
372
|
|
|
|
373
|
|
|
// Send the request. |
374
|
|
|
return $this->processResponse($this->client->delete($this->fetchUrl($path)), 204); |
375
|
|
|
} |
376
|
|
|
} |
377
|
|
|
|