Debian Codegraph @

extensions/OATHAuth/src/HTMLForm/TOTPDisableForm.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Extension\OATHAuth\HTMLForm;
4
5 use MediaWiki\Extension\OATHAuth\Module\TOTP;
6 use MediaWiki\Logger\LoggerFactory;
7 use MediaWiki\Message\Message;
8 use MWException;
9
10 class TOTPDisableForm extends OATHAuthOOUIHTMLForm {
11 /**
12 * Add content to output when operation was successful
13 */
14 public function onSuccess() {
15 $this->getOutput()->addWikiMsg( 'oathauth-disabledoath' );
16 }
17
18 /**
19 * @return array
20 */
21 protected function getDescriptors() {
22 return [
23 'token' => [
24 'type' => 'text',
25 'label-message' => 'oathauth-entertoken',
26 'name' => 'token',
27 'required' => true,
28 'autofocus' => true,
29 'dir' => 'ltr',
30 'autocomplete' => 'one-time-code',
31 'spellcheck' => false,
32 'help' => $this->msg( 'oathauth-hint' )->parse(),
33 ],
34 ];
35 }
36
37 /**
38 * @param array $formData
39 * @return array|bool
40 * @throws MWException
41 */
42 public function onSubmit( array $formData ) {
43 // Don't increase pingLimiter, instead check for the limit being exceeded.
44 if ( $this->getUser()->pingLimiter( 'badoath', 0 ) ) {
45 // Arbitrary duration given here
46 LoggerFactory::getInstance( 'authentication' )->info(
47 'OATHAuth {user} rate limited while disabling 2FA from {clientip}', [
48 'user' => $this->getUser()->getName(),
49 'clientip' => $this->getRequest()->getIP(),
50 ]
51 );
52 return [ 'oathauth-throttled', Message::durationParam( 60 ) ];
53 }
54
55 foreach ( TOTP::getTOTPKeys( $this->oathUser ) as $key ) {
56 if ( !$key->verify( [ 'token' => $formData['token'] ], $this->oathUser ) ) {
57 continue;
58 }
59
60 $this->oathRepo->removeKey(
61 $this->oathUser,
62 $key,
63 $this->getRequest()->getIP(),
64 true
65 );
66
67 return true;
68 }
69
70 LoggerFactory::getInstance( 'authentication' )->info(
71 'OATHAuth {user} failed to provide a correct token while disabling 2FA from {clientip}', [
72 'user' => $this->getUser()->getName(),
73 'clientip' => $this->getRequest()->getIP(),
74 ]
75 );
76
77 // Increase rate limit counter for failed request
78 $this->getUser()->pingLimiter( 'badoath' );
79
80 return [ 'oathauth-failedtovalidateoath' ];
81 }
82 }