1 <?php
2 /**
3 * @package Joomla.Platform
4 * @subpackage GitHub
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 /**
13 * GitHub API Authorization class for the Joomla Platform.
14 *
15 * @documentation https://developer.github.com/v3/oauth/
16 *
17 * @since 12.3
18 * @deprecated 4.0 Use the `joomla/github` package via Composer instead
19 */
20 class JGithubPackageAuthorization extends JGithubPackage
21 {
22 /**
23 * Method to create an authorization.
24 *
25 * @param array $scopes A list of scopes that this authorization is in.
26 * @param string $note A note to remind you what the OAuth token is for.
27 * @param string $url A URL to remind you what app the OAuth token is for.
28 *
29 * @throws DomainException
30 * @since 12.3
31 *
32 * @return object
33 */
34 public function create(array $scopes = array(), $note = '', $url = '')
35 {
36 // Build the request path.
37 $path = '/authorizations';
38
39 $data = json_encode(
40 array('scopes' => $scopes, 'note' => $note, 'note_url' => $url)
41 );
42
43 // Send the request.
44 $response = $this->client->post($this->fetchUrl($path), $data);
45
46 // Validate the response code.
47 if ($response->code != 201)
48 {
49 // Decode the error response and throw an exception.
50 $error = json_decode($response->body);
51 throw new DomainException($error->message, $response->code);
52 }
53
54 return json_decode($response->body);
55 }
56
57 /**
58 * Method to delete an authorization
59 *
60 * @param integer $id ID of the authorization to delete
61 *
62 * @throws DomainException
63 * @since 12.3
64 *
65 * @return object
66 */
67 public function delete($id)
68 {
69 // Build the request path.
70 $path = '/authorizations/' . $id;
71
72 // Send the request.
73 $response = $this->client->delete($this->fetchUrl($path));
74
75 // Validate the response code.
76 if ($response->code != 204)
77 {
78 // Decode the error response and throw an exception.
79 $error = json_decode($response->body);
80 throw new DomainException($error->message, $response->code);
81 }
82
83 return json_decode($response->body);
84 }
85
86 /**
87 * Method to edit an authorization.
88 *
89 * @param integer $id ID of the authorization to edit
90 * @param array $scopes Replaces the authorization scopes with these.
91 * @param array $addScopes A list of scopes to add to this authorization.
92 * @param array $removeScopes A list of scopes to remove from this authorization.
93 * @param string $note A note to remind you what the OAuth token is for.
94 * @param string $url A URL to remind you what app the OAuth token is for.
95 *
96 * @throws RuntimeException
97 * @throws DomainException
98 * @since 12.3
99 *
100 * @return object
101 */
102 public function edit($id, array $scopes = array(), array $addScopes = array(), array $removeScopes = array(), $note = '', $url = '')
103 {
104 // Check if more than one scopes array contains data
105 $scopesCount = 0;
106
107 if (!empty($scopes))
108 {
109 $scope = 'scopes';
110 $scopeData = $scopes;
111 $scopesCount++;
112 }
113
114 if (!empty($addScopes))
115 {
116 $scope = 'add_scopes';
117 $scopeData = $addScopes;
118 $scopesCount++;
119 }
120
121 if (!empty($removeScopes))
122 {
123 $scope = 'remove_scopes';
124 $scopeData = $removeScopes;
125 $scopesCount++;
126 }
127
128 // Only allowed to send data for one scope parameter
129 if ($scopesCount >= 2)
130 {
131 throw new RuntimeException('You can only send one scope key in this request.');
132 }
133
134 // Build the request path.
135 $path = '/authorizations/' . $id;
136
137 $data = json_encode(
138 array(
139 $scope => $scopeData,
140 'note' => $note,
141 'note_url' => $url,
142 )
143 );
144
145 // Send the request.
146 $response = $this->client->patch($this->fetchUrl($path), $data);
147
148 // Validate the response code.
149 if ($response->code != 200)
150 {
151 // Decode the error response and throw an exception.
152 $error = json_decode($response->body);
153 throw new DomainException($error->message, $response->code);
154 }
155
156 return json_decode($response->body);
157 }
158
159 /**
160 * Method to get details about an authorised application for the authenticated user.
161 *
162 * @param integer $id ID of the authorization to retrieve
163 *
164 * @throws DomainException
165 * @since 12.3
166 * @note This method will only accept Basic Authentication
167 *
168 * @return object
169 */
170 public function get($id)
171 {
172 // Build the request path.
173 $path = '/authorizations/' . $id;
174
175 // Send the request.
176 $response = $this->client->get($this->fetchUrl($path));
177
178 // Validate the response code.
179 if ($response->code != 200)
180 {
181 // Decode the error response and throw an exception.
182 $error = json_decode($response->body);
183 throw new DomainException($error->message, $response->code);
184 }
185
186 return json_decode($response->body);
187 }
188
189 /**
190 * Method to get the authorised applications for the authenticated user.
191 *
192 * @throws DomainException
193 * @since 12.3
194 * @note This method will only accept Basic Authentication
195 *
196 * @return object
197 */
198 public function getList()
199 {
200 // Build the request path.
201 $path = '/authorizations';
202
203 // Send the request.
204 $response = $this->client->get($this->fetchUrl($path));
205
206 // Validate the response code.
207 if ($response->code != 200)
208 {
209 // Decode the error response and throw an exception.
210 $error = json_decode($response->body);
211 throw new DomainException($error->message, $response->code);
212 }
213
214 return json_decode($response->body);
215 }
216
217 /**
218 * Method to get the rate limit for the authenticated user.
219 *
220 * @throws DomainException
221 * @since 12.3
222 *
223 * @return object
224 */
225 public function getRateLimit()
226 {
227 // Build the request path.
228 $path = '/rate_limit';
229
230 // Send the request.
231 $response = $this->client->get($this->fetchUrl($path));
232
233 // Validate the response code.
234 if ($response->code != 200)
235 {
236 // Decode the error response and throw an exception.
237 $error = json_decode($response->body);
238 throw new DomainException($error->message, $response->code);
239 }
240
241 return json_decode($response->body);
242 }
243
244 /**
245 * 1. Request authorization on GitHub.
246 *
247 * @param string $client_id The client ID you received from GitHub when you registered.
248 * @param string $redirect_uri URL in your app where users will be sent after authorization.
249 * @param string $scope Comma separated list of scopes.
250 * @param string $state An unguessable random string. It is used to protect against
251 * cross-site request forgery attacks.
252 *
253 * @since 3.3 (CMS)
254 *
255 * @return JUri
256 */
257 public function getAuthorizationLink($client_id, $redirect_uri = '', $scope = '', $state = '')
258 {
259 $uri = new JUri('https://github.com/login/oauth/authorize');
260
261 $uri->setVar('client_id', $client_id);
262
263 if ($redirect_uri)
264 {
265 $uri->setVar('redirect_uri', urlencode($redirect_uri));
266 }
267
268 if ($scope)
269 {
270 $uri->setVar('scope', $scope);
271 }
272
273 if ($state)
274 {
275 $uri->setVar('state', $state);
276 }
277
278 return (string) $uri;
279 }
280
281 /**
282 * 2. Request the access token.
283 *
284 * @param string $client_id The client ID you received from GitHub when you registered.
285 * @param string $client_secret The client secret you received from GitHub when you registered.
286 * @param string $code The code you received as a response to Step 1.
287 * @param string $redirect_uri URL in your app where users will be sent after authorization.
288 * @param string $format The response format (json, xml, ).
289 *
290 * @throws UnexpectedValueException
291 * @since 3.3 (CMS)
292 *
293 * @return string
294 */
295 public function requestToken($client_id, $client_secret, $code, $redirect_uri = '', $format = '')
296 {
297 $uri = 'https://github.com/login/oauth/access_token';
298
299 $data = array(
300 'client_id' => $client_id,
301 'client_secret' => $client_secret,
302 'code' => $code,
303 );
304
305 if ($redirect_uri)
306 {
307 $data['redirect_uri'] = $redirect_uri;
308 }
309
310 $headers = array();
311
312 switch ($format)
313 {
314 case 'json' :
315 $headers['Accept'] = 'application/json';
316 break;
317 case 'xml' :
318 $headers['Accept'] = 'application/xml';
319 break;
320 default :
321 if ($format)
322 {
323 throw new UnexpectedValueException('Invalid format');
324 }
325 break;
326 }
327
328 // Send the request.
329 return $this->processResponse(
330 $this->client->post($uri, $data, $headers),
331 200, false
332 );
333 }
334 }
335