Completed
Pull Request — master (#526)
by Michael
01:57
created

OAuthUtility   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 179
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 5
Bugs 0 Features 0
Metric Value
eloc 106
dl 0
loc 179
ccs 0
cts 104
cp 0
rs 10
c 5
b 0
f 0
wmc 15

6 Methods

Rating   Name   Duplication   Size   Complexity  
A apiCall() 0 46 4
A getRequestToken() 0 33 4
A getAuthoriseUrl() 0 3 1
A getIdentity() 0 40 3
A callbackCompleted() 0 34 2
A __construct() 0 6 1
1
<?php
2
3
function wfDebugLog($section, $message)
0 ignored issues
show
Unused Code introduced by
The parameter $message is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

3
function wfDebugLog($section, /** @scrutinizer ignore-unused */ $message)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $section is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

3
function wfDebugLog(/** @scrutinizer ignore-unused */ $section, $message)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
4
{
5
	// no op, needed by the mediawiki oauth library.
6
}
7
8
class OAuthUtility
9
{
10
	private $consumerToken;
11
	private $consumerSecret;
12
	private $baseUrl;
13
	private $baseUrlInternal;
14
15
	public function __construct($consumerToken, $consumerSecret, $baseUrl, $baseUrlInternal)
16
	{
17
		$this->consumerSecret = $consumerSecret;
18
		$this->consumerToken = $consumerToken;
19
		$this->baseUrl = $baseUrl;
20
		$this->baseUrlInternal = $baseUrlInternal;
21
	}
22
23
	public function getRequestToken()
24
	{
25
		global $toolUserAgent;
26
27
		$endpoint = $this->baseUrlInternal . '/initiate&format=json&oauth_callback=oob';
28
29
		$c = new OAuthConsumer($this->consumerToken, $this->consumerSecret);
0 ignored issues
show
Bug introduced by
The type OAuthConsumer was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
30
		$parsed = parse_url($endpoint);
31
		$params = array();
32
		parse_str($parsed['query'], $params);
33
		$req_req = OAuthRequest::from_consumer_and_token($c, null, "GET", $endpoint, $params);
0 ignored issues
show
Bug introduced by
The type OAuthRequest was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
34
		$hmac_method = new OAuthSignatureMethod_HMAC_SHA1();
0 ignored issues
show
Bug introduced by
The type OAuthSignatureMethod_HMAC_SHA1 was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
35
		$req_req->sign_request($hmac_method, $c, null);
36
37
		$ch = curl_init();
38
		curl_setopt($ch, CURLOPT_URL, (string)$req_req);
0 ignored issues
show
Bug introduced by
It seems like $ch can also be of type false; however, parameter $ch of curl_setopt() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

38
		curl_setopt(/** @scrutinizer ignore-type */ $ch, CURLOPT_URL, (string)$req_req);
Loading history...
39
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
40
		curl_setopt($ch, CURLOPT_HEADER, 0);
41
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
42
		curl_setopt($ch, CURLOPT_USERAGENT, $toolUserAgent);
43
		$data = curl_exec($ch);
0 ignored issues
show
Bug introduced by
It seems like $ch can also be of type false; however, parameter $ch of curl_exec() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

43
		$data = curl_exec(/** @scrutinizer ignore-type */ $ch);
Loading history...
44
45
		if (!$data) {
46
			throw new Exception('Curl error: ' . curl_error($ch));
0 ignored issues
show
Bug introduced by
It seems like $ch can also be of type false; however, parameter $ch of curl_error() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

46
			throw new Exception('Curl error: ' . curl_error(/** @scrutinizer ignore-type */ $ch));
Loading history...
47
		}
48
49
		$token = json_decode($data);
0 ignored issues
show
Bug introduced by
It seems like $data can also be of type true; however, parameter $json of json_decode() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

49
		$token = json_decode(/** @scrutinizer ignore-type */ $data);
Loading history...
50
51
		if (!isset($token) || isset($token->error)) {
52
			throw new Exception("Error encountered while getting token.");
53
		}
54
55
		return $token;
56
	}
57
58
	public function getAuthoriseUrl($requestToken)
59
	{
60
		return "{$this->baseUrl}/authorize&oauth_token={$requestToken->key}&oauth_consumer_key={$this->consumerToken}";
61
	}
62
63
	public function callbackCompleted($requestToken, $requestSecret, $verifyToken)
64
	{
65
		global $toolUserAgent;
66
67
		$endpoint = $this->baseUrlInternal . '/token&format=json';
68
69
		$c = new OAuthConsumer($this->consumerToken, $this->consumerSecret);
70
		$rc = new OAuthConsumer($requestToken, $requestSecret);
71
		$parsed = parse_url($endpoint);
72
		parse_str($parsed['query'], $params);
73
		$params['oauth_verifier'] = trim($verifyToken);
74
75
		$acc_req = OAuthRequest::from_consumer_and_token($c, $rc, "GET", $endpoint, $params);
76
77
		$hmac_method = new OAuthSignatureMethod_HMAC_SHA1();
78
		$acc_req->sign_request($hmac_method, $c, $rc);
79
80
		unset($ch);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $ch seems to be never defined.
Loading history...
81
		$ch = curl_init();
82
		curl_setopt($ch, CURLOPT_URL, (string)$acc_req);
0 ignored issues
show
Bug introduced by
It seems like $ch can also be of type false; however, parameter $ch of curl_setopt() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

82
		curl_setopt(/** @scrutinizer ignore-type */ $ch, CURLOPT_URL, (string)$acc_req);
Loading history...
83
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
84
		curl_setopt($ch, CURLOPT_HEADER, 0);
85
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
86
		curl_setopt($ch, CURLOPT_USERAGENT, $toolUserAgent);
87
88
		$data = curl_exec($ch);
0 ignored issues
show
Bug introduced by
It seems like $ch can also be of type false; however, parameter $ch of curl_exec() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

88
		$data = curl_exec(/** @scrutinizer ignore-type */ $ch);
Loading history...
89
90
		if (!$data) {
91
			throw new Exception('Curl error: ' . curl_error($ch));
0 ignored issues
show
Bug introduced by
It seems like $ch can also be of type false; however, parameter $ch of curl_error() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

91
			throw new Exception('Curl error: ' . curl_error(/** @scrutinizer ignore-type */ $ch));
Loading history...
92
		}
93
94
		$token = json_decode($data);
0 ignored issues
show
Bug introduced by
It seems like $data can also be of type true; however, parameter $json of json_decode() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

94
		$token = json_decode(/** @scrutinizer ignore-type */ $data);
Loading history...
95
96
		return $token;
97
	}
98
99
	public function apiCall($apiParams, $accessToken, $accessSecret, $method = "GET")
100
	{
101
		global $mediawikiWebServiceEndpoint, $toolUserAgent;
102
103
		$userToken = new OAuthToken($accessToken, $accessSecret);
0 ignored issues
show
Bug introduced by
The type OAuthToken was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
104
105
		$apiParams['format'] = 'json';
106
107
		$c = new OAuthConsumer($this->consumerToken, $this->consumerSecret);
108
109
		$api_req = OAuthRequest::from_consumer_and_token(
110
			$c, // Consumer
111
			$userToken, // User Access Token
112
			$method, // HTTP Method
113
			$mediawikiWebServiceEndpoint, // Endpoint url
114
			$apiParams    // Extra signed parameters
115
		);
116
117
		$hmac_method = new OAuthSignatureMethod_HMAC_SHA1();
118
119
		$api_req->sign_request($hmac_method, $c, $userToken);
120
121
		$ch = curl_init();
122
		if ($method == "GET") {
123
			curl_setopt($ch, CURLOPT_URL, $mediawikiWebServiceEndpoint . "?" . http_build_query($apiParams));
0 ignored issues
show
Bug introduced by
It seems like $ch can also be of type false; however, parameter $ch of curl_setopt() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

123
			curl_setopt(/** @scrutinizer ignore-type */ $ch, CURLOPT_URL, $mediawikiWebServiceEndpoint . "?" . http_build_query($apiParams));
Loading history...
124
		}
125
        
126
		if ($method == "POST") {
127
			curl_setopt($ch, CURLOPT_URL, $mediawikiWebServiceEndpoint);
128
			curl_setopt($ch, CURLOPT_POST, count($apiParams));
129
			curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($apiParams));   
130
		}
131
        
132
		curl_setopt($ch, CURLOPT_HEADER, 0);
133
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
134
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
135
		curl_setopt($ch, CURLOPT_HTTPHEADER, array($api_req->to_header())); // Authorization header required for api
136
		curl_setopt($ch, CURLOPT_USERAGENT, $toolUserAgent);
137
138
		$data = curl_exec($ch);
0 ignored issues
show
Bug introduced by
It seems like $ch can also be of type false; however, parameter $ch of curl_exec() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

138
		$data = curl_exec(/** @scrutinizer ignore-type */ $ch);
Loading history...
139
140
		if (!$data) {
141
			throw new Exception('Curl error: ' . curl_error($ch));
0 ignored issues
show
Bug introduced by
It seems like $ch can also be of type false; however, parameter $ch of curl_error() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

141
			throw new Exception('Curl error: ' . curl_error(/** @scrutinizer ignore-type */ $ch));
Loading history...
142
		}
143
144
		return json_decode($data);
0 ignored issues
show
Bug introduced by
It seems like $data can also be of type true; however, parameter $json of json_decode() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

144
		return json_decode(/** @scrutinizer ignore-type */ $data);
Loading history...
145
	}
146
147
	public function getIdentity($accessToken, $accessSecret)
148
	{
149
150
		global $toolUserAgent;
151
152
		$endpoint = $this->baseUrlInternal . '/identify&format=json';
153
154
		$c = new OAuthConsumer($this->consumerToken, $this->consumerSecret);
155
		$rc = new OAuthToken($accessToken, $accessSecret);
156
		$parsed = parse_url($endpoint);
157
		parse_str($parsed['query'], $params);
158
159
		$acc_req = OAuthRequest::from_consumer_and_token($c, $rc, "GET", $endpoint, $params);
160
161
		$hmac_method = new OAuthSignatureMethod_HMAC_SHA1();
162
		$acc_req->sign_request($hmac_method, $c, $rc);
163
164
		unset($ch);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $ch seems to be never defined.
Loading history...
165
		$ch = curl_init();
166
		curl_setopt($ch, CURLOPT_URL, (string)$acc_req);
0 ignored issues
show
Bug introduced by
It seems like $ch can also be of type false; however, parameter $ch of curl_setopt() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

166
		curl_setopt(/** @scrutinizer ignore-type */ $ch, CURLOPT_URL, (string)$acc_req);
Loading history...
167
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
168
		curl_setopt($ch, CURLOPT_HEADER, 0);
169
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
170
		curl_setopt($ch, CURLOPT_USERAGENT, $toolUserAgent);
171
172
		$data = curl_exec($ch);
0 ignored issues
show
Bug introduced by
It seems like $ch can also be of type false; however, parameter $ch of curl_exec() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

172
		$data = curl_exec(/** @scrutinizer ignore-type */ $ch);
Loading history...
173
174
		if (!$data) {
175
			throw new Exception('Curl error: ' . curl_error($ch));
0 ignored issues
show
Bug introduced by
It seems like $ch can also be of type false; however, parameter $ch of curl_error() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

175
			throw new Exception('Curl error: ' . curl_error(/** @scrutinizer ignore-type */ $ch));
Loading history...
176
		}
177
178
		$decodedData = json_decode($data);
0 ignored issues
show
Bug introduced by
It seems like $data can also be of type true; however, parameter $json of json_decode() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

178
		$decodedData = json_decode(/** @scrutinizer ignore-type */ $data);
Loading history...
179
180
		if (isset($decodedData->error)) {
181
			throw new TransactionException($decodedData->error);
182
		}
183
184
		$identity = JWT::decode($data, $this->consumerSecret);
0 ignored issues
show
Bug introduced by
The type JWT was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
185
186
		return $identity;
187
	}
188
}
189