| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Extension\OATHAuth\Special; |
| 4 |
|
| 5 |
use ManualLogEntry; |
| 6 |
use MediaWiki\CheckUser\Hooks as CheckUserHooks; |
| 7 |
use MediaWiki\Extension\OATHAuth\OATHUserRepository; |
| 8 |
use MediaWiki\HTMLForm\HTMLForm; |
| 9 |
use MediaWiki\Logger\LoggerFactory; |
| 10 |
use MediaWiki\Registration\ExtensionRegistry; |
| 11 |
use MediaWiki\SpecialPage\FormSpecialPage; |
| 12 |
use MediaWiki\Status\Status; |
| 13 |
use MediaWiki\User\CentralId\CentralIdLookup; |
| 14 |
use MediaWiki\User\User; |
| 15 |
use MediaWiki\User\UserFactory; |
| 16 |
use MWException; |
| 17 |
use UserBlockedError; |
| 18 |
use UserNotLoggedIn; |
| 19 |
|
| 20 |
class VerifyOATHForUser extends FormSpecialPage { |
| 21 |
|
| 22 |
private const OATHAUTH_IS_ENABLED = 'enabled'; |
| 23 |
private const OATHAUTH_NOT_ENABLED = 'disabled'; |
| 24 |
private OATHUserRepository $userRepo; |
| 25 |
private UserFactory $userFactory; |
| 26 |
private CentralIdLookup $centralIdLookup; |
| 27 |
|
| 28 |
|
| 29 |
private $enabledStatus; |
| 30 |
|
| 31 |
|
| 32 |
private $targetUser; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
public function __construct( $userRepo, $userFactory, $centralIdLookup ) { |
| 40 |
|
| 41 |
|
| 42 |
parent::__construct( 'VerifyOATHForUser', 'oathauth-verify-user' ); |
| 43 |
$this->userRepo = $userRepo; |
| 44 |
$this->userFactory = $userFactory; |
| 45 |
$this->centralIdLookup = $centralIdLookup; |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
protected function getGroupName() { |
| 52 |
return 'users'; |
| 53 |
} |
| 54 |
|
| 55 |
public function doesWrites() { |
| 56 |
return true; |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
protected function getLoginSecurityLevel() { |
| 63 |
return $this->getName(); |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
public function alterForm( HTMLForm $form ) { |
| 70 |
$form->setMessagePrefix( 'oathauth' ); |
| 71 |
$form->getOutput()->setPageTitleMsg( $this->msg( 'oathauth-verify-for-user' ) ); |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
protected function getDisplayFormat() { |
| 78 |
return 'ooui'; |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
public function requiresUnblock() { |
| 85 |
return true; |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
protected function checkExecutePermissions( User $user ) { |
| 94 |
$this->requireNamedUser(); |
| 95 |
|
| 96 |
parent::checkExecutePermissions( $user ); |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
public function execute( $par ) { |
| 103 |
$this->getOutput()->disallowUserJs(); |
| 104 |
parent::execute( $par ); |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
protected function getFormFields() { |
| 111 |
return [ |
| 112 |
'user' => [ |
| 113 |
'type' => 'user', |
| 114 |
'default' => '', |
| 115 |
'label-message' => 'oathauth-enteruser', |
| 116 |
'name' => 'user', |
| 117 |
'required' => true, |
| 118 |
], |
| 119 |
'reason' => [ |
| 120 |
'type' => 'text', |
| 121 |
'default' => '', |
| 122 |
'label-message' => 'oathauth-enterverifyreason', |
| 123 |
'name' => 'reason', |
| 124 |
'required' => true, |
| 125 |
], |
| 126 |
]; |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
public function onSubmit( array $formData ) { |
| 131 |
$this->targetUser = $formData['user']; |
| 132 |
$user = $this->userFactory->newFromName( $this->targetUser ); |
| 133 |
|
| 134 |
|
| 135 |
if ( !$user || $this->centralIdLookup->centralIdFromName( $formData['user'] ) === 0 ) { |
| 136 |
return [ 'oathauth-user-not-found' ]; |
| 137 |
} |
| 138 |
|
| 139 |
if ( $this->getUser()->pingLimiter( 'verify-2fa' ) ) { |
| 140 |
return Status::newFatal( 'oathauth-throttled' ); |
| 141 |
} |
| 142 |
|
| 143 |
$oathUser = $this->userRepo->findByUser( $user ); |
| 144 |
|
| 145 |
$this->enabledStatus = $oathUser->isTwoFactorAuthEnabled() |
| 146 |
? self::OATHAUTH_IS_ENABLED |
| 147 |
: self::OATHAUTH_NOT_ENABLED; |
| 148 |
|
| 149 |
|
| 150 |
$logEntry = new ManualLogEntry( 'oath', 'verify' ); |
| 151 |
$logEntry->setPerformer( $this->getUser() ); |
| 152 |
$logEntry->setTarget( $user->getUserPage() ); |
| 153 |
$logEntry->setComment( $formData['reason'] ); |
| 154 |
$logEntry->insert(); |
| 155 |
|
| 156 |
if ( ExtensionRegistry::getInstance()->isLoaded( 'CheckUser' ) ) { |
| 157 |
CheckUserHooks::updateCheckUserData( $logEntry->getRecentChange() ); |
| 158 |
} |
| 159 |
|
| 160 |
LoggerFactory::getInstance( 'authentication' )->info( |
| 161 |
'OATHAuth status checked for {usertarget} by {user} from {clientip}', [ |
| 162 |
'user' => $this->getUser()->getName(), |
| 163 |
'usertarget' => $this->targetUser, |
| 164 |
'clientip' => $this->getRequest()->getIP(), |
| 165 |
] |
| 166 |
); |
| 167 |
|
| 168 |
return true; |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
public function onSuccess() { |
| 175 |
switch ( $this->enabledStatus ) { |
| 176 |
case self::OATHAUTH_IS_ENABLED: |
| 177 |
$msg = 'oathauth-verify-enabled'; |
| 178 |
break; |
| 179 |
case self::OATHAUTH_NOT_ENABLED: |
| 180 |
$msg = 'oathauth-verify-disabled'; |
| 181 |
break; |
| 182 |
default: |
| 183 |
throw new MWException( |
| 184 |
'Verification was successful but status is unknown' |
| 185 |
); |
| 186 |
} |
| 187 |
|
| 188 |
$out = $this->getOutput(); |
| 189 |
$out->addBacklinkSubtitle( $this->getPageTitle() ); |
| 190 |
$out->addWikiMsg( $msg, $this->targetUser ); |
| 191 |
} |
| 192 |
|
| 193 |
} |