| 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 |
|
| 23 |
protected $user; |
| 24 |
|
| 25 |
protected array $params; |
| 26 |
|
| 27 |
|
| 28 |
protected array $wikis; |
| 29 |
|
| 30 |
|
| 31 |
protected $wikiParam; |
| 32 |
|
| 33 |
|
| 34 |
protected $method; |
| 35 |
|
| 36 |
|
| 37 |
protected $tokenType; |
| 38 |
|
| 39 |
|
| 40 |
protected $csrfTokens; |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 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 |
|
| 63 |
|
| 64 |
|
| 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 |
|
| 83 |
|
| 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 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 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 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 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 |
|
| 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 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 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 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
protected function getQueryParams( $wiki, ?WebRequest $originalRequest ) { |
| 221 |
$extraParams = []; |
| 222 |
if ( $this->wikiParam ) { |
| 223 |
|
| 224 |
|
| 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 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
'format' => 'json', |
| 239 |
'formatversion' => '1', |
| 240 |
'errorformat' => 'bc', |
| 241 |
] + $extraParams + $this->params; |
| 242 |
} |
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 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 |
} |