Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 49 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 1 |
ApiQueryOATH | |
0.00% |
0 / 49 |
|
0.00% |
0 / 6 |
240 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
execute | |
0.00% |
0 / 31 |
|
0.00% |
0 / 1 |
110 | |||
getCacheMode | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
isInternal | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getAllowedParams | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
2 | |||
getExamplesMessages | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | /** |
3 | * This program is free software; you can redistribute it and/or modify |
4 | * it under the terms of the GNU General Public License as published by |
5 | * the Free Software Foundation; either version 2 of the License, or |
6 | * (at your option) any later version. |
7 | * |
8 | * This program is distributed in the hope that it will be useful, |
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
11 | * GNU General Public License for more details. |
12 | * |
13 | * You should have received a copy of the GNU General Public License along |
14 | * with this program; if not, write to the Free Software Foundation, Inc., |
15 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
16 | * http://www.gnu.org/copyleft/gpl.html |
17 | */ |
18 | |
19 | namespace MediaWiki\Extension\OATHAuth\Api\Module; |
20 | |
21 | use ManualLogEntry; |
22 | use MediaWiki\Api\ApiQuery; |
23 | use MediaWiki\Api\ApiQueryBase; |
24 | use MediaWiki\Api\ApiResult; |
25 | use MediaWiki\Extension\OATHAuth\OATHUserRepository; |
26 | use MediaWiki\MediaWikiServices; |
27 | use Wikimedia\ParamValidator\ParamValidator; |
28 | |
29 | /** |
30 | * Query module to check if a user has OATH authentication enabled. |
31 | * |
32 | * Usage requires the 'oathauth-api-all' grant which is not given to any group |
33 | * by default. Use of this API is security sensitive and should not be granted |
34 | * lightly. Configuring a special 'oathauth' user group is recommended. |
35 | * |
36 | * @ingroup API |
37 | * @ingroup Extensions |
38 | */ |
39 | class ApiQueryOATH extends ApiQueryBase { |
40 | private OATHUserRepository $oathUserRepository; |
41 | |
42 | public function __construct( |
43 | ApiQuery $query, |
44 | string $moduleName, |
45 | OATHUserRepository $oathUserRepository |
46 | ) { |
47 | parent::__construct( $query, $moduleName, 'oath' ); |
48 | $this->oathUserRepository = $oathUserRepository; |
49 | } |
50 | |
51 | public function execute() { |
52 | // messages used: right-oathauth-api-all, action-oathauth-api-all, |
53 | // right-oathauth-verify-user, action-oathauth-verify-user |
54 | $this->checkUserRightsAny( [ 'oathauth-api-all', 'oathauth-verify-user' ] ); |
55 | |
56 | $params = $this->extractRequestParams(); |
57 | |
58 | $hasOAthauthApiAll = $this->getPermissionManager() |
59 | ->userHasRight( |
60 | $this->getUser(), |
61 | 'oathauth-api-all' |
62 | ); |
63 | |
64 | $reasonProvided = $params['reason'] !== null && $params['reason'] !== ''; |
65 | if ( !$hasOAthauthApiAll && !$reasonProvided ) { |
66 | $this->dieWithError( [ 'apierror-missingparam', 'reason' ] ); |
67 | } |
68 | |
69 | if ( $params['user'] === null ) { |
70 | $user = $this->getUser(); |
71 | } else { |
72 | $user = MediaWikiServices::getInstance()->getUserFactory() |
73 | ->newFromName( $params['user'] ); |
74 | if ( $user === null ) { |
75 | $this->dieWithError( 'noname' ); |
76 | } |
77 | } |
78 | |
79 | $result = $this->getResult(); |
80 | $data = [ |
81 | ApiResult::META_BC_BOOLS => [ 'enabled' ], |
82 | 'enabled' => false, |
83 | ]; |
84 | |
85 | if ( $user->isNamed() ) { |
86 | $authUser = $this->oathUserRepository->findByUser( $user ); |
87 | $data['enabled'] = $authUser && $authUser->isTwoFactorAuthEnabled(); |
88 | |
89 | // Log if the user doesn't have oathauth-api-all or if a reason is provided |
90 | // messages used: logentry-oath-verify, log-action-oath-verify |
91 | if ( !$hasOAthauthApiAll || $reasonProvided ) { |
92 | $logEntry = new ManualLogEntry( 'oath', 'verify' ); |
93 | $logEntry->setPerformer( $this->getUser() ); |
94 | $logEntry->setTarget( $user->getUserPage() ); |
95 | $logEntry->setComment( $params['reason'] ); |
96 | $logEntry->insert(); |
97 | } |
98 | } |
99 | $result->addValue( 'query', $this->getModuleName(), $data ); |
100 | } |
101 | |
102 | /** |
103 | * @param array $params |
104 | * |
105 | * @return string |
106 | */ |
107 | public function getCacheMode( $params ) { |
108 | return 'private'; |
109 | } |
110 | |
111 | /** @inheritDoc */ |
112 | public function isInternal() { |
113 | return true; |
114 | } |
115 | |
116 | /** |
117 | * @return array |
118 | */ |
119 | public function getAllowedParams() { |
120 | return [ |
121 | 'user' => [ |
122 | ParamValidator::PARAM_TYPE => 'user', |
123 | ], |
124 | 'reason' => [ |
125 | ParamValidator::PARAM_TYPE => 'string', |
126 | ], |
127 | ]; |
128 | } |
129 | |
130 | /** |
131 | * @return array |
132 | */ |
133 | protected function getExamplesMessages() { |
134 | return [ |
135 | 'action=query&meta=oath' |
136 | => 'apihelp-query+oath-example-1', |
137 | 'action=query&meta=oath&oathuser=Example' |
138 | => 'apihelp-query+oath-example-2', |
139 | ]; |
140 | } |
141 | } |