Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
CentralAuthTokenProvider | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
6 | |
0.00% |
0 / 1 |
getToken | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
6 |
1 | <?php |
2 | |
3 | namespace FileImporter\Remote\MediaWiki; |
4 | |
5 | use MediaWiki\Api\ApiMain; |
6 | use MediaWiki\Api\ApiUsageException; |
7 | use MediaWiki\Context\RequestContext; |
8 | use MediaWiki\Request\FauxRequest; |
9 | use MediaWiki\User\User; |
10 | use RuntimeException; |
11 | |
12 | /** |
13 | * @license GPL-2.0-or-later |
14 | */ |
15 | class CentralAuthTokenProvider { |
16 | |
17 | /** |
18 | * @return string |
19 | * @throws ApiUsageException e.g. when CentralAuth is not available locally |
20 | * @throws RuntimeException when there is an unexpected API result |
21 | */ |
22 | public function getToken( User $user ) { |
23 | $context = new RequestContext; |
24 | $context->setRequest( new FauxRequest( [ 'action' => 'centralauthtoken' ] ) ); |
25 | $context->setUser( $user ); |
26 | |
27 | $api = new ApiMain( $context ); |
28 | |
29 | $api->execute(); |
30 | $token = $api->getResult()->getResultData( [ 'centralauthtoken', 'centralauthtoken' ] ); |
31 | if ( !$token ) { |
32 | // This should be unreachable, because execute() takes care of all error handling |
33 | throw new RuntimeException( 'Unexpected return value from the centralauthtoken API' ); |
34 | } |
35 | |
36 | return $token; |
37 | } |
38 | |
39 | } |