client.php
13.9 KB
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
<?php
/**
* @package Joomla.Platform
* @subpackage OAuth1
*
* @copyright Copyright (C) 2005 - 2019 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
*/
defined('JPATH_PLATFORM') or die();
use Joomla\Registry\Registry;
/**
* Joomla Platform class for interacting with an OAuth 1.0 and 1.0a server.
*
* @since 3.2.0
* @deprecated 4.0 Use the `joomla/oauth1` framework package that will be bundled instead
*/
abstract class JOAuth1Client
{
/**
* @var Registry Options for the JOAuth1Client object.
* @since 3.2.0
*/
protected $options;
/**
* @var array Contains access token key, secret and verifier.
* @since 3.2.0
*/
protected $token = array();
/**
* @var JHttp The HTTP client object to use in sending HTTP requests.
* @since 3.2.0
*/
protected $client;
/**
* @var JInput The input object to use in retrieving GET/POST data.
* @since 3.2.0
*/
protected $input;
/**
* @var JApplicationWeb The application object to send HTTP headers for redirects.
* @since 3.2.0
*/
protected $application;
/**
* @var string Selects which version of OAuth to use: 1.0 or 1.0a.
* @since 3.2.0
*/
protected $version;
/**
* Constructor.
*
* @param Registry $options OAuth1Client options object.
* @param JHttp $client The HTTP client object.
* @param JInput $input The input object
* @param JApplicationWeb $application The application object
* @param string $version Specify the OAuth version. By default we are using 1.0a.
*
* @since 3.2.0
*/
public function __construct(Registry $options = null, JHttp $client = null, JInput $input = null, JApplicationWeb $application = null,
$version = null)
{
$this->options = isset($options) ? $options : new Registry;
$this->client = isset($client) ? $client : JHttpFactory::getHttp($this->options);
$this->input = isset($input) ? $input : JFactory::getApplication()->input;
$this->application = isset($application) ? $application : new JApplicationWeb;
$this->version = isset($version) ? $version : '1.0a';
}
/**
* Method to for the oauth flow.
*
* @return array Contains access token key, secret and verifier.
*
* @since 3.2.0
* @throws DomainException
*/
public function authenticate()
{
// Already got some credentials stored?
if ($this->token)
{
$response = $this->verifyCredentials();
if ($response)
{
return $this->token;
}
else
{
$this->token = null;
}
}
// Check for callback.
if (strcmp($this->version, '1.0a') === 0)
{
$verifier = $this->input->get('oauth_verifier');
}
else
{
$verifier = $this->input->get('oauth_token');
}
if (empty($verifier))
{
// Generate a request token.
$this->_generateRequestToken();
// Authenticate the user and authorise the app.
$this->_authorise();
}
// Callback
else
{
$session = JFactory::getSession();
// Get token form session.
$this->token = array('key' => $session->get('key', null, 'oauth_token'), 'secret' => $session->get('secret', null, 'oauth_token'));
// Verify the returned request token.
if (strcmp($this->token['key'], $this->input->get('oauth_token')) !== 0)
{
throw new DomainException('Bad session!');
}
// Set token verifier for 1.0a.
if (strcmp($this->version, '1.0a') === 0)
{
$this->token['verifier'] = $this->input->get('oauth_verifier');
}
// Generate access token.
$this->_generateAccessToken();
// Return the access token.
return $this->token;
}
}
/**
* Method used to get a request token.
*
* @return void
*
* @since 3.2.0
* @throws DomainException
*/
private function _generateRequestToken()
{
// Set the callback URL.
if ($this->getOption('callback'))
{
$parameters = array(
'oauth_callback' => $this->getOption('callback'),
);
}
else
{
$parameters = array();
}
// Make an OAuth request for the Request Token.
$response = $this->oauthRequest($this->getOption('requestTokenURL'), 'POST', $parameters);
parse_str($response->body, $params);
if (strcmp($this->version, '1.0a') === 0 && strcmp($params['oauth_callback_confirmed'], 'true') !== 0)
{
throw new DomainException('Bad request token!');
}
// Save the request token.
$this->token = array('key' => $params['oauth_token'], 'secret' => $params['oauth_token_secret']);
// Save the request token in session
$session = JFactory::getSession();
$session->set('key', $this->token['key'], 'oauth_token');
$session->set('secret', $this->token['secret'], 'oauth_token');
}
/**
* Method used to authorise the application.
*
* @return void
*
* @since 3.2.0
*/
private function _authorise()
{
$url = $this->getOption('authoriseURL') . '?oauth_token=' . $this->token['key'];
if ($this->getOption('scope'))
{
$scope = is_array($this->getOption('scope')) ? implode(' ', $this->getOption('scope')) : $this->getOption('scope');
$url .= '&scope=' . urlencode($scope);
}
if ($this->getOption('sendheaders'))
{
$this->application->redirect($url);
}
}
/**
* Method used to get an access token.
*
* @return void
*
* @since 3.2.0
*/
private function _generateAccessToken()
{
// Set the parameters.
$parameters = array(
'oauth_token' => $this->token['key'],
);
if (strcmp($this->version, '1.0a') === 0)
{
$parameters = array_merge($parameters, array('oauth_verifier' => $this->token['verifier']));
}
// Make an OAuth request for the Access Token.
$response = $this->oauthRequest($this->getOption('accessTokenURL'), 'POST', $parameters);
parse_str($response->body, $params);
// Save the access token.
$this->token = array('key' => $params['oauth_token'], 'secret' => $params['oauth_token_secret']);
}
/**
* Method used to make an OAuth request.
*
* @param string $url The request URL.
* @param string $method The request method.
* @param array $parameters Array containing request parameters.
* @param mixed $data The POST request data.
* @param array $headers An array of name-value pairs to include in the header of the request
*
* @return JHttpResponse
*
* @since 3.2.0
* @throws DomainException
*/
public function oauthRequest($url, $method, $parameters, $data = array(), $headers = array())
{
// Set the parameters.
$defaults = array(
'oauth_consumer_key' => $this->getOption('consumer_key'),
'oauth_signature_method' => 'HMAC-SHA1',
'oauth_version' => '1.0',
'oauth_nonce' => $this->generateNonce(),
'oauth_timestamp' => time(),
);
$parameters = array_merge($parameters, $defaults);
// Do not encode multipart parameters. Do not include $data in the signature if $data is not array.
if (isset($headers['Content-Type']) && strpos($headers['Content-Type'], 'multipart/form-data') !== false || !is_array($data))
{
$oauth_headers = $parameters;
}
else
{
// Use all parameters for the signature.
$oauth_headers = array_merge($parameters, $data);
}
// Sign the request.
$oauth_headers = $this->_signRequest($url, $method, $oauth_headers);
// Get parameters for the Authorisation header.
if (is_array($data))
{
$oauth_headers = array_diff_key($oauth_headers, $data);
}
// Send the request.
switch ($method)
{
case 'GET':
$url = $this->toUrl($url, $data);
$response = $this->client->get($url, array('Authorization' => $this->_createHeader($oauth_headers)));
break;
case 'POST':
$headers = array_merge($headers, array('Authorization' => $this->_createHeader($oauth_headers)));
$response = $this->client->post($url, $data, $headers);
break;
case 'PUT':
$headers = array_merge($headers, array('Authorization' => $this->_createHeader($oauth_headers)));
$response = $this->client->put($url, $data, $headers);
break;
case 'DELETE':
$headers = array_merge($headers, array('Authorization' => $this->_createHeader($oauth_headers)));
$response = $this->client->delete($url, $headers);
break;
}
// Validate the response code.
$this->validateResponse($url, $response);
return $response;
}
/**
* Method to validate a response.
*
* @param string $url The request URL.
* @param JHttpResponse $response The response to validate.
*
* @return void
*
* @since 3.2.0
* @throws DomainException
*/
abstract public function validateResponse($url, $response);
/**
* Method used to create the header for the POST request.
*
* @param array $parameters Array containing request parameters.
*
* @return string The header.
*
* @since 3.2.0
*/
private function _createHeader($parameters)
{
$header = 'OAuth ';
foreach ($parameters as $key => $value)
{
if (!strcmp($header, 'OAuth '))
{
$header .= $key . '="' . $this->safeEncode($value) . '"';
}
else
{
$header .= ', ' . $key . '="' . $value . '"';
}
}
return $header;
}
/**
* Method to create the URL formed string with the parameters.
*
* @param string $url The request URL.
* @param array $parameters Array containing request parameters.
*
* @return string The formed URL.
*
* @since 3.2.0
*/
public function toUrl($url, $parameters)
{
foreach ($parameters as $key => $value)
{
if (is_array($value))
{
foreach ($value as $v)
{
if (strpos($url, '?') === false)
{
$url .= '?' . $key . '=' . $v;
}
else
{
$url .= '&' . $key . '=' . $v;
}
}
}
else
{
if (strpos($value, ' ') !== false)
{
$value = $this->safeEncode($value);
}
if (strpos($url, '?') === false)
{
$url .= '?' . $key . '=' . $value;
}
else
{
$url .= '&' . $key . '=' . $value;
}
}
}
return $url;
}
/**
* Method used to sign requests.
*
* @param string $url The URL to sign.
* @param string $method The request method.
* @param array $parameters Array containing request parameters.
*
* @return array
*
* @since 3.2.0
*/
private function _signRequest($url, $method, $parameters)
{
// Create the signature base string.
$base = $this->_baseString($url, $method, $parameters);
$parameters['oauth_signature'] = $this->safeEncode(
base64_encode(
hash_hmac('sha1', $base, $this->_prepareSigningKey(), true)
)
);
return $parameters;
}
/**
* Prepare the signature base string.
*
* @param string $url The URL to sign.
* @param string $method The request method.
* @param array $parameters Array containing request parameters.
*
* @return string The base string.
*
* @since 3.2.0
*/
private function _baseString($url, $method, $parameters)
{
// Sort the parameters alphabetically
uksort($parameters, 'strcmp');
// Encode parameters.
foreach ($parameters as $key => $value)
{
$key = $this->safeEncode($key);
if (is_array($value))
{
foreach ($value as $v)
{
$v = $this->safeEncode($v);
$kv[] = "{$key}={$v}";
}
}
else
{
$value = $this->safeEncode($value);
$kv[] = "{$key}={$value}";
}
}
// Form the parameter string.
$params = implode('&', $kv);
// Signature base string elements.
$base = array(
$method,
$url,
$params,
);
// Return the base string.
return implode('&', $this->safeEncode($base));
}
/**
* Encodes the string or array passed in a way compatible with OAuth.
* If an array is passed each array value will will be encoded.
*
* @param mixed $data The scalar or array to encode.
*
* @return string $data encoded in a way compatible with OAuth.
*
* @since 3.2.0
*/
public function safeEncode($data)
{
if (is_array($data))
{
return array_map(array($this, 'safeEncode'), $data);
}
elseif (is_scalar($data))
{
return str_ireplace(
array('+', '%7E'),
array(' ', '~'),
rawurlencode($data)
);
}
else
{
return '';
}
}
/**
* Method used to generate the current nonce.
*
* @return string The current nonce.
*
* @since 3.2.0
*/
public static function generateNonce()
{
$mt = microtime();
$rand = JCrypt::genRandomBytes();
// The md5s look nicer than numbers.
return md5($mt . $rand);
}
/**
* Prepares the OAuth signing key.
*
* @return string The prepared signing key.
*
* @since 3.2.0
*/
private function _prepareSigningKey()
{
return $this->safeEncode($this->getOption('consumer_secret')) . '&' . $this->safeEncode(($this->token) ? $this->token['secret'] : '');
}
/**
* Returns an HTTP 200 OK response code and a representation of the requesting user if authentication was successful;
* returns a 401 status code and an error message if not.
*
* @return array The decoded JSON response
*
* @since 3.2.0
*/
abstract public function verifyCredentials();
/**
* Get an option from the JOauth1aClient instance.
*
* @param string $key The name of the option to get
*
* @return mixed The option value
*
* @since 3.2.0
*/
public function getOption($key)
{
return $this->options->get($key);
}
/**
* Set an option for the JOauth1aClient instance.
*
* @param string $key The name of the option to set
* @param mixed $value The option value to set
*
* @return JOAuth1Client This object for method chaining
*
* @since 3.2.0
*/
public function setOption($key, $value)
{
$this->options->set($key, $value);
return $this;
}
/**
* Get the oauth token key or secret.
*
* @return array The oauth token key and secret.
*
* @since 3.2.0
*/
public function getToken()
{
return $this->token;
}
/**
* Set the oauth token.
*
* @param array $token The access token key and secret.
*
* @return JOAuth1Client This object for method chaining.
*
* @since 3.2.0
*/
public function setToken($token)
{
$this->token = $token;
return $this;
}
}