| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
namespace MediaWiki\Extension\OATHAuth\Api\Module; |
| 20 |
|
| 21 |
use MediaWiki\Api\ApiBase; |
| 22 |
use MediaWiki\Api\ApiMain; |
| 23 |
use MediaWiki\Api\ApiResult; |
| 24 |
use MediaWiki\Extension\OATHAuth\Module\TOTP; |
| 25 |
use MediaWiki\Extension\OATHAuth\OATHUserRepository; |
| 26 |
use MediaWiki\Json\FormatJson; |
| 27 |
use MediaWiki\Logger\LoggerFactory; |
| 28 |
use MediaWiki\User\UserFactory; |
| 29 |
use Wikimedia\ParamValidator\ParamValidator; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
class ApiOATHValidate extends ApiBase { |
| 38 |
private OATHUserRepository $oathUserRepository; |
| 39 |
private UserFactory $userFactory; |
| 40 |
|
| 41 |
public function __construct( |
| 42 |
ApiMain $mainModule, |
| 43 |
string $moduleName, |
| 44 |
OATHUserRepository $oathUserRepository, |
| 45 |
UserFactory $userFactory |
| 46 |
) { |
| 47 |
parent::__construct( $mainModule, $moduleName ); |
| 48 |
$this->oathUserRepository = $oathUserRepository; |
| 49 |
$this->userFactory = $userFactory; |
| 50 |
} |
| 51 |
|
| 52 |
public function execute() { |
| 53 |
$this->requirePostedParameters( [ 'token', 'data' ] ); |
| 54 |
|
| 55 |
$this->checkUserRightsAny( 'oathauth-api-all' ); |
| 56 |
|
| 57 |
$params = $this->extractRequestParams(); |
| 58 |
if ( $params['user'] === null ) { |
| 59 |
$user = $this->getUser(); |
| 60 |
} else { |
| 61 |
$user = $this->userFactory->newFromName( $params['user'] ); |
| 62 |
if ( $user === null ) { |
| 63 |
$this->dieWithError( 'noname' ); |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
if ( $user->pingLimiter( 'badoath', 0 ) ) { |
| 69 |
$this->dieWithError( 'apierror-ratelimited' ); |
| 70 |
} |
| 71 |
|
| 72 |
$result = [ |
| 73 |
ApiResult::META_BC_BOOLS => [ 'enabled', 'valid' ], |
| 74 |
'enabled' => false, |
| 75 |
'valid' => false, |
| 76 |
]; |
| 77 |
|
| 78 |
if ( $user->isNamed() ) { |
| 79 |
$authUser = $this->oathUserRepository->findByUser( $user ); |
| 80 |
$keys = TOTP::getTOTPKeys( $authUser ); |
| 81 |
if ( $keys ) { |
| 82 |
$result['enabled'] = true; |
| 83 |
|
| 84 |
$data = []; |
| 85 |
$decoded = FormatJson::decode( $params['data'], true ); |
| 86 |
if ( is_array( $decoded ) ) { |
| 87 |
$data = $decoded; |
| 88 |
} |
| 89 |
|
| 90 |
foreach ( $keys as $key ) { |
| 91 |
if ( $key->verify( $data, $authUser ) !== false ) { |
| 92 |
$result['valid'] = true; |
| 93 |
break; |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
if ( !$result['valid'] ) { |
| 98 |
|
| 99 |
$user->pingLimiter( 'badoath' ); |
| 100 |
|
| 101 |
LoggerFactory::getInstance( 'authentication' )->info( |
| 102 |
'OATHAuth user {user} failed OTP token/recovery code from {clientip}', |
| 103 |
[ |
| 104 |
'user' => $user, |
| 105 |
'clientip' => $user->getRequest()->getIP(), |
| 106 |
] |
| 107 |
); |
| 108 |
} |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
$this->getResult()->addValue( null, $this->getModuleName(), $result ); |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
public function isInternal() { |
| 117 |
return true; |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
public function needsToken() { |
| 122 |
return 'csrf'; |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
public function getAllowedParams() { |
| 129 |
return [ |
| 130 |
'user' => [ |
| 131 |
ParamValidator::PARAM_TYPE => 'user', |
| 132 |
], |
| 133 |
'data' => [ |
| 134 |
ParamValidator::PARAM_TYPE => 'string', |
| 135 |
ParamValidator::PARAM_REQUIRED => true, |
| 136 |
] |
| 137 |
]; |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
protected function getExamplesMessages() { |
| 144 |
return [ |
| 145 |
'action=oathvalidate&data={"token":"123456"}&token=123ABC' |
| 146 |
=> 'apihelp-oathvalidate-example-1', |
| 147 |
'action=oathvalidate&user=Example&data={"token":"123456"}&token=123ABC' |
| 148 |
=> 'apihelp-oathvalidate-example-3', |
| 149 |
]; |
| 150 |
} |
| 151 |
} |