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
21 * @ingroup Maintenance
24 use MediaWiki\Auth\AuthManager
;
25 use MediaWiki\Auth\Throttler
;
26 use MediaWiki\Logger\LoggerFactory
;
28 require_once __DIR__
. '/Maintenance.php';
31 * Reset login/signup throttling for a specified user and/or IP.
33 * @ingroup Maintenance
36 class ResetAuthenticationThrottle
extends Maintenance
{
38 public function __construct() {
39 parent
::__construct();
40 $this->addDescription( 'Reset login/signup throttling for a specified user and/or IP. '
42 . 'When resetting signup only, provide the IP. When resetting login (or both), provide '
43 . 'both username (as entered in login screen) and IP. An easy way to obtain them is '
44 . "the 'throttler' log channel." );
45 $this->addOption( 'login', 'Reset login throttle' );
46 $this->addOption( 'signup', 'Reset account creation throttle' );
47 $this->addOption( 'user', 'Username to reset', false, true );
48 $this->addOption( 'ip', 'IP to reset', false, true );
51 public function execute() {
52 $forLogin = (bool)$this->getOption( 'login' );
53 $forSignup = (bool)$this->getOption( 'signup' );
54 $username = $this->getOption( 'user' );
55 $ip = $this->getOption( 'ip' );
57 if ( !$forLogin && !$forSignup ) {
58 $this->fatalError( 'At least one of --login and --signup is required!' );
59 } elseif ( $forLogin && ( $ip === null ||
$username === null ) ) {
60 $this->fatalError( '--usename and --ip are both required when using --login!' );
61 } elseif ( $forSignup && $ip === null ) {
62 $this->fatalError( '--ip is required when using --signup!' );
63 } elseif ( $ip !== null && !IP
::isValid( $ip ) ) {
64 $this->fatalError( "Not a valid IP: $ip" );
68 $this->clearLoginThrottle( $username, $ip );
71 $this->clearSignupThrottle( $ip );
74 LoggerFactory
::getInstance( 'throttler' )->notice( 'Manually cleared {type} throttle', [
75 'type' => implode( ' and ', array_filter( [
76 $forLogin ?
'login' : null,
77 $forSignup ?
'signup' : null,
79 'username' => $username,
85 * @param string|null $rawUsername
86 * @param string|null $ip
88 protected function clearLoginThrottle( $rawUsername, $ip ) {
89 $this->output( 'Clearing login throttle... ' );
91 $passwordAttemptThrottle = $this->getConfig()->get( 'PasswordAttemptThrottle' );
92 if ( !$passwordAttemptThrottle ) {
93 $this->output( "none set\n" );
97 $throttler = new Throttler( $passwordAttemptThrottle, [
99 'cache' => ObjectCache
::getLocalClusterInstance(),
101 if ( $rawUsername !== null ) {
102 $usernames = AuthManager
::singleton()->normalizeUsername( $rawUsername );
104 $this->fatalError( "Not a valid username: $rawUsername" );
107 $usernames = [ null ];
109 foreach ( $usernames as $username ) {
110 $throttler->clear( $username, $ip );
113 $botPasswordThrottler = new Throttler( $passwordAttemptThrottle, [
114 'type' => 'botpassword',
115 'cache' => ObjectCache
::getLocalClusterInstance(),
117 $botPasswordThrottler->clear( $username, $ip );
119 $this->output( "done\n" );
125 protected function clearSignupThrottle( $ip ) {
126 $this->output( 'Clearing signup throttle... ' );
128 $accountCreationThrottle = $this->getConfig()->get( 'AccountCreationThrottle' );
129 if ( !is_array( $accountCreationThrottle ) ) {
130 $accountCreationThrottle = [ [
131 'count' => $accountCreationThrottle,
135 if ( !$accountCreationThrottle ) {
136 $this->output( "none set\n" );
139 $throttler = new Throttler( $accountCreationThrottle, [
140 'type' => 'acctcreate',
141 'cache' => ObjectCache
::getLocalClusterInstance(),
144 $throttler->clear( null, $ip );
146 $this->output( "done\n" );
151 $maintClass = ResetAuthenticationThrottle
::class;
152 require_once RUN_MAINTENANCE_IF_MAIN
;