|
1
|
|
|
<?php |
|
2
|
|
|
/****************************************************************************** |
|
3
|
|
|
* Wikipedia Account Creation Assistance tool * |
|
4
|
|
|
* * |
|
5
|
|
|
* All code in this file is released into the public domain by the ACC * |
|
6
|
|
|
* Development Team. Please see team.json for a list of contributors. * |
|
7
|
|
|
******************************************************************************/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Waca\Helpers; |
|
10
|
|
|
|
|
11
|
|
|
use MediaWiki\OAuthClient\Client; |
|
12
|
|
|
use MediaWiki\OAuthClient\ClientConfig; |
|
13
|
|
|
use MediaWiki\OAuthClient\Consumer; |
|
14
|
|
|
use MediaWiki\OAuthClient\Token; |
|
15
|
|
|
use Waca\Exceptions\CurlException; |
|
16
|
|
|
|
|
17
|
|
|
class OAuthProtocolHelper implements Interfaces\IOAuthProtocolHelper |
|
18
|
|
|
{ |
|
19
|
|
|
private $oauthClient; |
|
20
|
|
|
|
|
21
|
|
|
private $mediawikiWebServiceEndpoint; |
|
22
|
|
|
|
|
23
|
|
|
private $authUrl; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* OAuthHelper constructor. |
|
27
|
|
|
* |
|
28
|
|
|
* @param string $oauthEndpoint |
|
29
|
|
|
* @param string $consumerKey |
|
30
|
|
|
* @param string $consumerSecret |
|
31
|
|
|
* @param string $mediawikiWebServiceEndpoint |
|
32
|
|
|
*/ |
|
33
|
|
|
public function __construct( |
|
34
|
|
|
$oauthEndpoint, |
|
35
|
|
|
$consumerKey, |
|
36
|
|
|
$consumerSecret, |
|
37
|
|
|
$mediawikiWebServiceEndpoint |
|
38
|
|
|
) { |
|
39
|
|
|
$this->mediawikiWebServiceEndpoint = $mediawikiWebServiceEndpoint; |
|
40
|
|
|
|
|
41
|
|
|
$oauthClientConfig = new ClientConfig($oauthEndpoint); |
|
42
|
|
|
$oauthClientConfig->setConsumer(new Consumer($consumerKey, $consumerSecret)); |
|
43
|
|
|
|
|
44
|
|
|
$this->oauthClient = new Client($oauthClientConfig); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @inheritDoc |
|
49
|
|
|
*/ |
|
50
|
|
|
public function getRequestToken() |
|
51
|
|
|
{ |
|
52
|
|
|
/** @var Token $requestToken */ |
|
53
|
|
|
list($authUrl, $requestToken) = $this->oauthClient->initiate(); |
|
54
|
|
|
$this->authUrl = $authUrl; |
|
55
|
|
|
return $requestToken; |
|
|
|
|
|
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @inheritDoc |
|
60
|
|
|
*/ |
|
61
|
|
|
public function getAuthoriseUrl($requestToken) |
|
62
|
|
|
{ |
|
63
|
|
|
return $this->authUrl; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @inheritDoc |
|
68
|
|
|
*/ |
|
69
|
|
|
public function callbackCompleted($oauthRequestToken, $oauthRequestSecret, $oauthVerifier) |
|
70
|
|
|
{ |
|
71
|
|
|
$requestToken = new Token($oauthRequestToken, $oauthRequestSecret); |
|
72
|
|
|
|
|
73
|
|
|
return $this->oauthClient->complete($requestToken, $oauthVerifier); |
|
|
|
|
|
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @inheritDoc |
|
78
|
|
|
*/ |
|
79
|
|
|
public function getIdentityTicket($oauthAccessToken, $oauthAccessSecret) |
|
80
|
|
|
{ |
|
81
|
|
|
return $this->oauthClient->identify(new Token($oauthAccessToken, $oauthAccessSecret)); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @inheritDoc |
|
86
|
|
|
*/ |
|
87
|
|
|
public function apiCall($apiParams, $accessToken, $accessSecret, $method = 'GET') |
|
88
|
|
|
{ |
|
89
|
|
|
$userToken = new Token($accessToken, $accessSecret); |
|
90
|
|
|
|
|
91
|
|
|
$apiParams['format'] = 'json'; |
|
92
|
|
|
|
|
93
|
|
|
if ($apiParams === null || ! is_array($apiParams)) { |
|
94
|
|
|
throw new CurlException("Invalid API call"); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
$url = $this->mediawikiWebServiceEndpoint; |
|
98
|
|
|
$isPost = ($method === 'POST'); |
|
99
|
|
|
|
|
100
|
|
|
if ($method === 'GET') { |
|
101
|
|
|
$query = http_build_query($apiParams); |
|
102
|
|
|
$url .= '?' . $query; |
|
103
|
|
|
$apiParams = null; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
$data = $this->oauthClient->makeOAuthCall($userToken, $url, $isPost, $apiParams); |
|
107
|
|
|
|
|
108
|
|
|
return json_decode($data); |
|
109
|
|
|
} |
|
110
|
|
|
} |
In the issue above, the returned value is violating the contract defined by the mentioned interface.
Let's take a look at an example: