Debian Codegraph @

extensions/OATHAuth/src/Api/Module/ApiQueryOATH.php

raw symbols SCIP
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 /**
43 * @param ApiQuery $query
44 * @param string $moduleName
45 * @param OATHUserRepository $oathUserRepository
46 */
47 public function __construct(
48 $query,
49 $moduleName,
50 OATHUserRepository $oathUserRepository
51 ) {
52 parent::__construct( $query, $moduleName, 'oath' );
53 $this->oathUserRepository = $oathUserRepository;
54 }
55
56 public function execute() {
57 // messages used: right-oathauth-api-all, action-oathauth-api-all,
58 // right-oathauth-verify-user, action-oathauth-verify-user
59 $this->checkUserRightsAny( [ 'oathauth-api-all', 'oathauth-verify-user' ] );
60
61 $params = $this->extractRequestParams();
62
63 $hasOAthauthApiAll = $this->getPermissionManager()
64 ->userHasRight(
65 $this->getUser(),
66 'oathauth-api-all'
67 );
68
69 $reasonProvided = $params['reason'] !== null && $params['reason'] !== '';
70 if ( !$hasOAthauthApiAll && !$reasonProvided ) {
71 $this->dieWithError( [ 'apierror-missingparam', 'reason' ] );
72 }
73
74 if ( $params['user'] === null ) {
75 $user = $this->getUser();
76 } else {
77 $user = MediaWikiServices::getInstance()->getUserFactory()
78 ->newFromName( $params['user'] );
79 if ( $user === null ) {
80 $this->dieWithError( 'noname' );
81 }
82 }
83
84 $result = $this->getResult();
85 $data = [
86 ApiResult::META_BC_BOOLS => [ 'enabled' ],
87 'enabled' => false,
88 ];
89
90 if ( $user->isNamed() ) {
91 $authUser = $this->oathUserRepository->findByUser( $user );
92 $data['enabled'] = $authUser && $authUser->isTwoFactorAuthEnabled();
93
94 // Log if the user doesn't have oathauth-api-all or if a reason is provided
95 // messages used: logentry-oath-verify, log-action-oath-verify
96 if ( !$hasOAthauthApiAll || $reasonProvided ) {
97 $logEntry = new ManualLogEntry( 'oath', 'verify' );
98 $logEntry->setPerformer( $this->getUser() );
99 $logEntry->setTarget( $user->getUserPage() );
100 $logEntry->setComment( $params['reason'] );
101 $logEntry->insert();
102 }
103 }
104 $result->addValue( 'query', $this->getModuleName(), $data );
105 }
106
107 /**
108 * @param array $params
109 *
110 * @return string
111 */
112 public function getCacheMode( $params ) {
113 return 'private';
114 }
115
116 /** @inheritDoc */
117 public function isInternal() {
118 return true;
119 }
120
121 /**
122 * @return array
123 */
124 public function getAllowedParams() {
125 return [
126 'user' => [
127 ParamValidator::PARAM_TYPE => 'user',
128 ],
129 'reason' => [
130 ParamValidator::PARAM_TYPE => 'string',
131 ],
132 ];
133 }
134
135 /**
136 * @return array
137 */
138 protected function getExamplesMessages() {
139 return [
140 'action=query&meta=oath'
141 => 'apihelp-query+oath-example-1',
142 'action=query&meta=oath&oathuser=Example'
143 => 'apihelp-query+oath-example-2',
144 ];
145 }
146 }