| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Extension\ConfirmEdit\Auth; |
| 4 |
|
| 5 |
use MediaWiki\Extension\ConfirmEdit\CaptchaTriggers; |
| 6 |
use MediaWiki\Extension\ConfirmEdit\SimpleCaptcha\SimpleCaptcha; |
| 7 |
use MediaWiki\MediaWikiServices; |
| 8 |
use MediaWiki\User\User; |
| 9 |
use MediaWiki\User\UserNameUtils; |
| 10 |
use Wikimedia\ObjectCache\BagOStuff; |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
class LoginAttemptCounter { |
| 18 |
private SimpleCaptcha $captcha; |
| 19 |
|
| 20 |
public function __construct( SimpleCaptcha $captcha ) { |
| 21 |
$this->captcha = $captcha; |
| 22 |
} |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
public function increaseBadLoginCounter( $username ) { |
| 31 |
global $wgCaptchaBadLoginExpiration, $wgCaptchaBadLoginPerUserExpiration; |
| 32 |
|
| 33 |
$cache = MediaWikiServices::getInstance()->getObjectCacheFactory()->getLocalClusterInstance(); |
| 34 |
|
| 35 |
if ( $this->captcha->triggersCaptcha( CaptchaTriggers::BAD_LOGIN ) ) { |
| 36 |
$key = $this->badLoginKey( $cache ); |
| 37 |
$cache->incrWithInit( $key, $wgCaptchaBadLoginExpiration ); |
| 38 |
|
| 39 |
|
| 40 |
$key = $this->badLoginKey( $cache, true ); |
| 41 |
$cache->incrWithInit( $key, $wgCaptchaBadLoginExpiration * 300 ); |
| 42 |
} |
| 43 |
|
| 44 |
if ( $this->captcha->triggersCaptcha( CaptchaTriggers::BAD_LOGIN_PER_USER ) && $username ) { |
| 45 |
$key = $this->badLoginPerUserKey( $username, $cache ); |
| 46 |
$cache->incrWithInit( $key, $wgCaptchaBadLoginPerUserExpiration ); |
| 47 |
|
| 48 |
$key = $this->badLoginPerUserKey( $username, $cache, true ); |
| 49 |
$cache->incrWithInit( $key, $wgCaptchaBadLoginPerUserExpiration * 300 ); |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
public function resetBadLoginCounter( $username ) { |
| 58 |
if ( $this->captcha->triggersCaptcha( CaptchaTriggers::BAD_LOGIN_PER_USER ) && $username ) { |
| 59 |
$cache = MediaWikiServices::getInstance()->getObjectCacheFactory()->getLocalClusterInstance(); |
| 60 |
$cache->delete( $this->badLoginPerUserKey( $username, $cache ) ); |
| 61 |
$cache->delete( $this->badLoginPerUserKey( $username, $cache, true ) ); |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
public function isBadLoginTriggered() { |
| 71 |
global $wgCaptchaBadLoginAttempts; |
| 72 |
|
| 73 |
$cache = MediaWikiServices::getInstance()->getObjectCacheFactory()->getLocalClusterInstance(); |
| 74 |
return $this->captcha->triggersCaptcha( CaptchaTriggers::BAD_LOGIN ) |
| 75 |
&& ( |
| 76 |
(int)$cache->get( $this->badLoginKey( $cache ) ) >= $wgCaptchaBadLoginAttempts || |
| 77 |
(int)$cache->get( $this->badLoginKey( $cache, true ) ) >= ( $wgCaptchaBadLoginAttempts * 30 ) |
| 78 |
); |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
public function isBadLoginPerUserTriggered( $u ) { |
| 88 |
global $wgCaptchaBadLoginPerUserAttempts; |
| 89 |
|
| 90 |
$cache = MediaWikiServices::getInstance()->getObjectCacheFactory()->getLocalClusterInstance(); |
| 91 |
|
| 92 |
if ( is_object( $u ) ) { |
| 93 |
$u = $u->getName(); |
| 94 |
} |
| 95 |
return $this->captcha->triggersCaptcha( CaptchaTriggers::BAD_LOGIN_PER_USER ) |
| 96 |
&& ( |
| 97 |
(int)$cache->get( $this->badLoginPerUserKey( $u, $cache ) ) >= $wgCaptchaBadLoginPerUserAttempts || |
| 98 |
(int)$cache->get( $this->badLoginPerUserKey( $u, $cache, true ) ) |
| 99 |
>= ( $wgCaptchaBadLoginPerUserAttempts * 30 ) |
| 100 |
); |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
private function badLoginKey( BagOStuff $cache, $long = false ) { |
| 110 |
global $wgRequest; |
| 111 |
$ip = $wgRequest->getIP(); |
| 112 |
if ( !$long ) { |
| 113 |
return $cache->makeGlobalKey( 'captcha', 'badlogin', 'ip', $ip ); |
| 114 |
} |
| 115 |
return $cache->makeGlobalKey( 'captcha', 'badlogin', 'ip', 'long', $ip ); |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
private function badLoginPerUserKey( $username, BagOStuff $cache, $long = false ) { |
| 126 |
$userNameUtils = MediaWikiServices::getInstance()->getUserNameUtils(); |
| 127 |
$username = $userNameUtils->getCanonical( $username, UserNameUtils::RIGOR_USABLE ) ?: $username; |
| 128 |
if ( !$long ) { |
| 129 |
return $cache->makeGlobalKey( |
| 130 |
'captcha', 'badlogin', 'user', md5( $username ) |
| 131 |
); |
| 132 |
} |
| 133 |
return $cache->makeGlobalKey( |
| 134 |
'captcha', 'badlogin', 'user', 'long', md5( $username ) |
| 135 |
); |
| 136 |
} |
| 137 |
} |