Debian Codegraph @

extensions/Echo/includes/ForeignWikiRequest.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Extension\Notifications;
4
5 use CentralAuthSessionProvider;
6 use Exception;
7 use MediaWiki\Api\ApiMain;
8 use MediaWiki\Context\RequestContext;
9 use MediaWiki\Logger\LoggerFactory;
10 use MediaWiki\MediaWikiServices;
11 use MediaWiki\Request\FauxRequest;
12 use MediaWiki\Request\WebRequest;
13 use MediaWiki\Session\SessionManager;
14 use MediaWiki\User\CentralId\CentralIdLookup;
15 use MediaWiki\User\User;
16 use MediaWiki\User\UserIdentity;
17 use MediaWiki\WikiMap\WikiMap;
18 use MWExceptionHandler;
19
20 class ForeignWikiRequest {
21
22 /** @var User */
23 protected $user;
24
25 protected array $params;
26
27 /** @var string[] */
28 protected array $wikis;
29
30 /** @var string|null */
31 protected $wikiParam;
32
33 /** @var string */
34 protected $method;
35
36 /** @var string|null */
37 protected $tokenType;
38
39 /** @var string[]|null */
40 protected $csrfTokens;
41
42 /**
43 * @param User $user
44 * @param array $params Request parameters
45 * @param string[] $wikis Wikis to send the request to
46 * @param string|null $wikiParam Parameter name to set to the name of the wiki
47 * @param string|null $postToken If set, use POST requests and inject a token of this type;
48 * if null, use GET requests.
49 */
50 public function __construct( User $user, array $params, array $wikis, $wikiParam = null, $postToken = null ) {
51 $this->user = $user;
52 $this->params = $params;
53 $this->wikis = $wikis;
54 $this->wikiParam = $wikiParam;
55 $this->method = $postToken === null ? 'GET' : 'POST';
56 $this->tokenType = $postToken;
57
58 $this->csrfTokens = null;
59 }
60
61 /**
62 * Execute the request
63 * @param WebRequest|null $originalRequest Original request data to be sent with these requests
64 * @return array[] [ wiki => result ]
65 */
66 public function execute( ?WebRequest $originalRequest = null ) {
67 if ( !$this->canUseCentralAuth() ) {
68 return [];
69 }
70
71 $reqs = $this->getRequestParams(
72 $this->method,
73 function ( string $wiki ) use ( $originalRequest ) {
74 return $this->getQueryParams( $wiki, $originalRequest );
75 },
76 $originalRequest
77 );
78 return $this->doRequests( $reqs );
79 }
80
81 /**
82 * @param UserIdentity $user
83 * @return int
84 */
85 protected function getCentralId( $user ) {
86 return MediaWikiServices::getInstance()
87 ->getCentralIdLookup()
88 ->centralIdFromLocalUser( $user, CentralIdLookup::AUDIENCE_RAW );
89 }
90
91 protected function canUseCentralAuth() {
92 global $wgFullyInitialised;
93
94 return $wgFullyInitialised &&
95 RequestContext::getMain()->getUser()->isSafeToLoad() &&
96 $this->user->isSafeToLoad() &&
97 SessionManager::getGlobalSession()->getProvider() instanceof CentralAuthSessionProvider &&
98 $this->getCentralId( $this->user ) !== 0;
99 }
100
101 /**
102 * Returns CentralAuth token, or null on failure.
103 *
104 * @param User $user
105 * @return string|null
106 */
107 protected function getCentralAuthToken( User $user ) {
108 $context = new RequestContext;
109 $context->setRequest( new FauxRequest( [ 'action' => 'centralauthtoken' ],
110 session: SessionManager::getGlobalSession() ) );
111 $context->setUser( $user );
112
113 $api = new ApiMain( $context );
114
115 try {
116 $api->execute();
117
118 return $api->getResult()->getResultData( [ 'centralauthtoken', 'centralauthtoken' ] );
119 } catch ( Exception $ex ) {
120 LoggerFactory::getInstance( 'Echo' )->debug(
121 'Exception when fetching CentralAuth token: wiki: {wiki}, userName: {userName}, ' .
122 'userId: {userId}, centralId: {centralId}, exception: {exception}',
123 [
124 'wiki' => WikiMap::getCurrentWikiId(),
125 'userName' => $user->getName(),
126 'userId' => $user->getId(),
127 'centralId' => $this->getCentralId( $user ),
128 'exception' => $ex,
129 ]
130 );
131
132 MWExceptionHandler::logException( $ex );
133
134 return null;
135 }
136 }
137
138 /**
139 * Get the CSRF token for a given wiki.
140 * This method fetches the tokens for all requested wikis at once and caches the result.
141 *
142 * @param string $wiki Name of the wiki to get a token for
143 * @param WebRequest|null $originalRequest Original request data to be sent with these requests
144 * @return string Token, or empty string if an unable to retrieve the token.
145 */
146 protected function getCsrfToken( $wiki, ?WebRequest $originalRequest ) {
147 if ( $this->csrfTokens === null ) {
148 $this->csrfTokens = [];
149 $reqs = $this->getRequestParams( 'GET', function ( string $wiki ) {
150 // This doesn't depend on the wiki, but 'centralauthtoken' must be different every time
151 return [
152 'action' => 'query',
153 'meta' => 'tokens',
154 'type' => $this->tokenType,
155 'format' => 'json',
156 'formatversion' => '1',
157 'errorformat' => 'bc',
158 'centralauthtoken' => $this->getCentralAuthToken( $this->user ),
159 ];
160 }, $originalRequest );
161 $responses = $this->doRequests( $reqs );
162 foreach ( $responses as $w => $response ) {
163 if ( isset( $response['query']['tokens']['csrftoken'] ) ) {
164 $this->csrfTokens[$w] = $response['query']['tokens']['csrftoken'];
165 } else {
166 LoggerFactory::getInstance( 'Echo' )->warning(
167 __METHOD__ . ': Unexpected CSRF token API response from {wiki}',
168 [
169 'wiki' => $wiki,
170 'response' => $response,
171 ]
172 );
173 }
174 }
175 }
176 return $this->csrfTokens[$wiki] ?? '';
177 }
178
179 /**
180 * @param string $method 'GET' or 'POST'
181 * @param callable $makeParams Callback that takes a wiki name and returns an associative array of
182 * query string / POST parameters
183 * @param WebRequest|null $originalRequest Original request data to be sent with these requests
184 * @return array[] Array of request parameters to pass to doRequests(), keyed by wiki name
185 */
186 protected function getRequestParams( $method, $makeParams, ?WebRequest $originalRequest ) {
187 $apis = ForeignNotifications::getApiEndpoints( $this->wikis );
188 if ( !$apis ) {
189 return [];
190 }
191
192 $reqs = [];
193 foreach ( $apis as $wiki => $api ) {
194 $queryKey = $method === 'POST' ? 'body' : 'query';
195 $reqs[$wiki] = [
196 'method' => $method,
197 'url' => $api['url'],
198 $queryKey => $makeParams( $wiki )
199 ];
200
201 if ( $originalRequest ) {
202 $reqs[$wiki]['headers'] = [
203 'X-Forwarded-For' => $originalRequest->getIP(),
204 'User-Agent' => (
205 $originalRequest->getHeader( 'User-Agent' )
206 . ' (via ForeignWikiRequest MediaWiki/' . MW_VERSION . ')'
207 ),
208 ];
209 }
210 }
211
212 return $reqs;
213 }
214
215 /**
216 * @param string $wiki Wiki name
217 * @param WebRequest|null $originalRequest Original request data to be sent with these requests
218 * @return array
219 */
220 protected function getQueryParams( $wiki, ?WebRequest $originalRequest ) {
221 $extraParams = [];
222 if ( $this->wikiParam ) {
223 // Only request data from that specific wiki, or they'd all spawn
224 // cross-wiki api requests...
225 $extraParams[$this->wikiParam] = $wiki;
226 }
227 if ( $this->method === 'POST' ) {
228 $extraParams['token'] = $this->getCsrfToken( $wiki, $originalRequest );
229 }
230
231 return [
232 'centralauthtoken' => $this->getCentralAuthToken( $this->user ),
233 // once all the results are gathered & merged, they'll be output in the
234 // user requested format
235 // but this is going to be an internal request & we don't want those
236 // results in the format the user requested but in a fixed format that
237 // we can interpret here
238 'format' => 'json',
239 'formatversion' => '1',
240 'errorformat' => 'bc',
241 ] + $extraParams + $this->params;
242 }
243
244 /**
245 * @param array $reqs API request params
246 * @return array[]
247 * @throws Exception
248 */
249 protected function doRequests( array $reqs ) {
250 $http = MediaWikiServices::getInstance()->getHttpRequestFactory()->createMultiClient();
251 $responses = $http->runMulti( $reqs );
252
253 $results = [];
254 foreach ( $responses as $wiki => $response ) {
255 $statusCode = $response['response']['code'];
256
257 if ( $statusCode >= 200 && $statusCode <= 299 ) {
258 $parsed = json_decode( $response['response']['body'], true );
259 if ( $parsed ) {
260 $results[$wiki] = $parsed;
261 }
262 }
263
264 if ( !isset( $results[$wiki] ) ) {
265 LoggerFactory::getInstance( 'Echo' )->warning(
266 'Failed to fetch API response from {wiki}. Error: {error}',
267 [
268 'wiki' => $wiki,
269 'error' => $response['response']['error'] ?? 'unknown',
270 'statusCode' => $statusCode,
271 'response' => $response['response'],
272 'request' => $reqs[$wiki],
273 ]
274 );
275 }
276 }
277
278 return $results;
279 }
280 }