Use TOCData methods to process new headings
[mediawiki.git] / maintenance / resetAuthenticationThrottle.php
blobd1f7bff13bc613e6392f1117a68a43f6d5ea7f40
1 <?php
2 /**
3 * Reset login/signup throttling for a specified user and/or IP.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
20 * @file
21 * @ingroup Maintenance
24 use MediaWiki\Auth\Throttler;
25 use MediaWiki\Logger\LoggerFactory;
26 use MediaWiki\MainConfigNames;
27 use MediaWiki\MediaWikiServices;
28 use Wikimedia\IPUtils;
30 require_once __DIR__ . '/Maintenance.php';
32 /**
33 * Reset login/signup throttling for a specified user and/or IP.
35 * @ingroup Maintenance
36 * @since 1.32
38 class ResetAuthenticationThrottle extends Maintenance {
40 public function __construct() {
41 parent::__construct();
42 $this->addDescription( 'Reset login/signup throttling for a specified user and/or IP. '
43 . "\n\n"
44 . 'When resetting signup only, provide the IP. When resetting login (or both), provide '
45 . 'both username (as entered in login screen) and IP. An easy way to obtain them is '
46 . "the 'throttler' log channel." );
47 $this->addOption( 'login', 'Reset login throttle' );
48 $this->addOption( 'signup', 'Reset account creation throttle' );
49 $this->addOption( 'user', 'Username to reset', false, true );
50 $this->addOption( 'ip', 'IP to reset', false, true );
53 public function execute() {
54 $forLogin = (bool)$this->getOption( 'login' );
55 $forSignup = (bool)$this->getOption( 'signup' );
56 $username = $this->getOption( 'user' );
57 $ip = $this->getOption( 'ip' );
59 if ( !$forLogin && !$forSignup ) {
60 $this->fatalError( 'At least one of --login and --signup is required!' );
61 } elseif ( $forLogin && ( $ip === null || $username === null ) ) {
62 $this->fatalError( '--user and --ip are both required when using --login!' );
63 } elseif ( $forSignup && $ip === null ) {
64 $this->fatalError( '--ip is required when using --signup!' );
65 } elseif ( $ip !== null && !IPUtils::isValid( $ip ) ) {
66 $this->fatalError( "Not a valid IP: $ip" );
69 if ( $forLogin ) {
70 $this->clearLoginThrottle( $username, $ip );
72 if ( $forSignup ) {
73 $this->clearSignupThrottle( $ip );
76 LoggerFactory::getInstance( 'throttler' )->info( 'Manually cleared {type} throttle', [
77 'type' => implode( ' and ', array_filter( [
78 $forLogin ? 'login' : null,
79 $forSignup ? 'signup' : null,
80 ] ) ),
81 'username' => $username,
82 'ipKey' => $ip,
83 ] );
86 /**
87 * @param string|null $rawUsername
88 * @param string|null $ip
90 protected function clearLoginThrottle( $rawUsername, $ip ) {
91 $this->output( 'Clearing login throttle...' );
93 $passwordAttemptThrottle = $this->getConfig()->get( MainConfigNames::PasswordAttemptThrottle );
94 if ( !$passwordAttemptThrottle ) {
95 $this->output( "none set\n" );
96 return;
99 $throttler = new Throttler( $passwordAttemptThrottle, [
100 'type' => 'password',
101 'cache' => ObjectCache::getLocalClusterInstance(),
102 ] );
103 if ( $rawUsername !== null ) {
104 $usernames = MediaWikiServices::getInstance()->getAuthManager()
105 ->normalizeUsername( $rawUsername );
106 if ( !$usernames ) {
107 $this->fatalError( "Not a valid username: $rawUsername" );
109 } else {
110 $usernames = [ null ];
112 foreach ( $usernames as $username ) {
113 $throttler->clear( $username, $ip );
116 $botPasswordThrottler = new Throttler( $passwordAttemptThrottle, [
117 'type' => 'botpassword',
118 'cache' => ObjectCache::getLocalClusterInstance(),
119 ] );
120 // @phan-suppress-next-line PhanPossiblyUndeclaredVariable T240141
121 $botPasswordThrottler->clear( $username, $ip );
123 $this->output( "done\n" );
127 * @param string $ip
129 protected function clearSignupThrottle( $ip ) {
130 $this->output( 'Clearing signup throttle...' );
132 $accountCreationThrottle = $this->getConfig()->get( MainConfigNames::AccountCreationThrottle );
133 if ( !is_array( $accountCreationThrottle ) ) {
134 $accountCreationThrottle = [ [
135 'count' => $accountCreationThrottle,
136 'seconds' => 86400,
137 ] ];
139 if ( !$accountCreationThrottle ) {
140 $this->output( "none set\n" );
141 return;
143 $throttler = new Throttler( $accountCreationThrottle, [
144 'type' => 'acctcreate',
145 'cache' => ObjectCache::getLocalClusterInstance(),
146 ] );
148 $throttler->clear( null, $ip );
150 $this->output( "done\n" );
155 $maintClass = ResetAuthenticationThrottle::class;
156 require_once RUN_MAINTENANCE_IF_MAIN;