Debian Codegraph @

extensions/OATHAuth/src/Api/Module/ApiOATHValidate.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 MediaWiki\Api\ApiBase;
22 use MediaWiki\Api\ApiMain;
23 use MediaWiki\Api\ApiResult;
24 use MediaWiki\Extension\OATHAuth\Module\TOTP;
25 use MediaWiki\Extension\OATHAuth\OATHUserRepository;
26 use MediaWiki\Json\FormatJson;
27 use MediaWiki\Logger\LoggerFactory;
28 use MediaWiki\User\UserFactory;
29 use Wikimedia\ParamValidator\ParamValidator;
30
31 /**
32 * Validate an OATH token.
33 *
34 * @ingroup API
35 * @ingroup Extensions
36 */
37 class ApiOATHValidate extends ApiBase {
38 private OATHUserRepository $oathUserRepository;
39 private UserFactory $userFactory;
40
41 public function __construct(
42 ApiMain $mainModule,
43 string $moduleName,
44 OATHUserRepository $oathUserRepository,
45 UserFactory $userFactory
46 ) {
47 parent::__construct( $mainModule, $moduleName );
48 $this->oathUserRepository = $oathUserRepository;
49 $this->userFactory = $userFactory;
50 }
51
52 public function execute() {
53 $this->requirePostedParameters( [ 'token', 'data' ] );
54 // messages used: right-oathauth-api-all, action-oathauth-api-all,
55 $this->checkUserRightsAny( 'oathauth-api-all' );
56
57 $params = $this->extractRequestParams();
58 if ( $params['user'] === null ) {
59 $user = $this->getUser();
60 } else {
61 $user = $this->userFactory->newFromName( $params['user'] );
62 if ( $user === null ) {
63 $this->dieWithError( 'noname' );
64 }
65 }
66
67 // Don't increase pingLimiter, just check for limit exceeded.
68 if ( $user->pingLimiter( 'badoath', 0 ) ) {
69 $this->dieWithError( 'apierror-ratelimited' );
70 }
71
72 $result = [
73 ApiResult::META_BC_BOOLS => [ 'enabled', 'valid' ],
74 'enabled' => false,
75 'valid' => false,
76 ];
77
78 if ( $user->isNamed() ) {
79 $authUser = $this->oathUserRepository->findByUser( $user );
80 $keys = TOTP::getTOTPKeys( $authUser );
81 if ( $keys ) {
82 $result['enabled'] = true;
83
84 $data = [];
85 $decoded = FormatJson::decode( $params['data'], true );
86 if ( is_array( $decoded ) ) {
87 $data = $decoded;
88 }
89
90 foreach ( $keys as $key ) {
91 if ( $key->verify( $data, $authUser ) !== false ) {
92 $result['valid'] = true;
93 break;
94 }
95 }
96
97 if ( !$result['valid'] ) {
98 // Increase rate limit counter for failed request
99 $user->pingLimiter( 'badoath' );
100
101 LoggerFactory::getInstance( 'authentication' )->info(
102 'OATHAuth user {user} failed OTP token/recovery code from {clientip}',
103 [
104 'user' => $user,
105 'clientip' => $user->getRequest()->getIP(),
106 ]
107 );
108 }
109 }
110 }
111
112 $this->getResult()->addValue( null, $this->getModuleName(), $result );
113 }
114
115 /** @inheritDoc */
116 public function isInternal() {
117 return true;
118 }
119
120 /** @inheritDoc */
121 public function needsToken() {
122 return 'csrf';
123 }
124
125 /**
126 * @return array
127 */
128 public function getAllowedParams() {
129 return [
130 'user' => [
131 ParamValidator::PARAM_TYPE => 'user',
132 ],
133 'data' => [
134 ParamValidator::PARAM_TYPE => 'string',
135 ParamValidator::PARAM_REQUIRED => true,
136 ]
137 ];
138 }
139
140 /**
141 * @return array
142 */
143 protected function getExamplesMessages() {
144 return [
145 'action=oathvalidate&data={"token":"123456"}&token=123ABC'
146 => 'apihelp-oathvalidate-example-1',
147 'action=oathvalidate&user=Example&data={"token":"123456"}&token=123ABC'
148 => 'apihelp-oathvalidate-example-3',
149 ];
150 }
151 }