Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 23 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
ApiCentralAuthToken | |
0.00% |
0 / 23 |
|
0.00% |
0 / 4 |
90 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
execute | |
0.00% |
0 / 16 |
|
0.00% |
0 / 1 |
42 | |||
getAllowedParams | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getExamplesMessages | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | /** |
3 | * Created on Aug 09, 2014 |
4 | * |
5 | * CentralAuth extension |
6 | * |
7 | * Copyright (C) 2014 Brad Jorsch bjorsch@wikimedia.org |
8 | * |
9 | * This program is free software; you can redistribute it and/or modify |
10 | * it under the terms of the GNU General Public License as published by |
11 | * the Free Software Foundation; either version 2 of the License, or |
12 | * (at your option) any later version. |
13 | * |
14 | * This program is distributed in the hope that it will be useful, |
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
17 | * GNU General Public License for more details. |
18 | * |
19 | * You should have received a copy of the GNU General Public License along |
20 | * with this program; if not, write to the Free Software Foundation, Inc., |
21 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
22 | * http://www.gnu.org/copyleft/gpl.html |
23 | */ |
24 | |
25 | namespace MediaWiki\Extension\CentralAuth\Api; |
26 | |
27 | use CentralAuthSessionProvider; |
28 | use MediaWiki\Api\ApiBase; |
29 | use MediaWiki\Api\ApiMain; |
30 | use MediaWiki\Extension\CentralAuth\CentralAuthApiTokenGenerator; |
31 | use MediaWiki\Extension\CentralAuth\User\CentralAuthUser; |
32 | use MediaWiki\Session\SessionManager; |
33 | use MediaWiki\WikiMap\WikiMap; |
34 | |
35 | /** |
36 | * Module to fetch the centralauthtoken for cross-wiki queries. |
37 | * |
38 | * @ingroup API |
39 | * @ingroup Extensions |
40 | * @see \MediaWiki\Extension\CentralAuth\Hooks\Handlers\ApiHookHandler::onAPIGetAllowedParams |
41 | * @see \CentralAuthApiSessionProvider |
42 | * @see \CentralAuthHeaderSessionProvider |
43 | */ |
44 | class ApiCentralAuthToken extends ApiBase { |
45 | |
46 | private CentralAuthApiTokenGenerator $tokenGenerator; |
47 | |
48 | public function __construct( |
49 | ApiMain $main, |
50 | string $moduleName, |
51 | CentralAuthApiTokenGenerator $tokenGenerator |
52 | ) { |
53 | parent::__construct( $main, $moduleName ); |
54 | $this->tokenGenerator = $tokenGenerator; |
55 | } |
56 | |
57 | public function execute() { |
58 | $user = $this->getUser(); |
59 | |
60 | // If we're in JSON callback mode, no tokens can be obtained |
61 | if ( $this->lacksSameOriginSecurity() ) { |
62 | $this->dieWithError( 'apiwarn-tokens-origin', 'hascallback' ); |
63 | } |
64 | |
65 | if ( !$user->isRegistered() ) { |
66 | $this->dieWithError( 'apierror-centralauth-notloggedin', 'notloggedin' ); |
67 | } |
68 | |
69 | $session = SessionManager::getGlobalSession(); |
70 | if ( !$session->getProvider() instanceof CentralAuthSessionProvider ) { |
71 | $this->dieWithError( 'apierror-centralauth-badsession', 'badsession' ); |
72 | } |
73 | $id = $session->getId(); |
74 | |
75 | $centralUser = CentralAuthUser::getInstance( $user ); |
76 | if ( !$centralUser->exists() || !$centralUser->isAttached() ) { |
77 | $this->dieWithError( 'apierror-centralauth-notattached', 'notattached' ); |
78 | } |
79 | |
80 | $loginToken = $this->tokenGenerator->getToken( $user, $id, WikiMap::getCurrentWikiId() ); |
81 | |
82 | $this->getResult()->addValue( null, $this->getModuleName(), [ |
83 | 'centralauthtoken' => $loginToken |
84 | ] ); |
85 | } |
86 | |
87 | /** @inheritDoc */ |
88 | public function getAllowedParams() { |
89 | return []; |
90 | } |
91 | |
92 | /** @inheritDoc */ |
93 | protected function getExamplesMessages() { |
94 | return [ |
95 | 'action=centralauthtoken' |
96 | => 'apihelp-centralauthtoken-example-1', |
97 | ]; |
98 | } |
99 | } |