| 1 |
<?php |
| 2 |
|
| 3 |
s |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
namespace MediaWiki\Api; |
| 24 |
|
| 25 |
use MediaWiki\Auth\AuthenticationRequest; |
| 26 |
use MediaWiki\Auth\AuthManager; |
| 27 |
use MediaWiki\MainConfigNames; |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
class ApiRemoveAuthenticationData extends ApiBase { |
| 35 |
|
| 36 |
|
| 37 |
private $authAction; |
| 38 |
|
| 39 |
private $operation; |
| 40 |
|
| 41 |
private AuthManager $authManager; |
| 42 |
|
| 43 |
public function __construct( |
| 44 |
ApiMain $main, |
| 45 |
string $action, |
| 46 |
AuthManager $authManager |
| 47 |
) { |
| 48 |
parent::__construct( $main, $action ); |
| 49 |
|
| 50 |
$this->authAction = $action === 'unlinkaccount' |
| 51 |
? AuthManager::ACTION_UNLINK |
| 52 |
: AuthManager::ACTION_REMOVE; |
| 53 |
$this->operation = $action === 'unlinkaccount' |
| 54 |
? 'UnlinkAccount' |
| 55 |
: 'RemoveCredentials'; |
| 56 |
|
| 57 |
$this->authManager = $authManager; |
| 58 |
} |
| 59 |
|
| 60 |
public function execute() { |
| 61 |
if ( !$this->getUser()->isNamed() ) { |
| 62 |
$this->dieWithError( 'apierror-mustbeloggedin-removeauth', 'notloggedin' ); |
| 63 |
} |
| 64 |
|
| 65 |
$this->checkUserRightsAny( 'editmyprivateinfo' ); |
| 66 |
|
| 67 |
$params = $this->extractRequestParams(); |
| 68 |
|
| 69 |
|
| 70 |
ApiAuthManagerHelper::newForModule( $this, $this->authManager ) |
| 71 |
->securitySensitiveOperation( $this->operation ); |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
$remove = $this->authAction === AuthManager::ACTION_REMOVE |
| 76 |
? array_fill_keys( $this->getConfig()->get( |
| 77 |
MainConfigNames::RemoveCredentialsBlacklist ), true ) |
| 78 |
: []; |
| 79 |
$reqs = array_filter( |
| 80 |
$this->authManager->getAuthenticationRequests( $this->authAction, $this->getUser() ), |
| 81 |
static function ( AuthenticationRequest $req ) use ( $params, $remove ) { |
| 82 |
return $req->getUniqueId() === $params['request'] && |
| 83 |
!isset( $remove[get_class( $req )] ); |
| 84 |
} |
| 85 |
); |
| 86 |
if ( count( $reqs ) !== 1 ) { |
| 87 |
$this->dieWithError( 'apierror-changeauth-norequest', 'badrequest' ); |
| 88 |
} |
| 89 |
$req = reset( $reqs ); |
| 90 |
|
| 91 |
|
| 92 |
$status = $this->authManager->allowsAuthenticationDataChange( $req, true ); |
| 93 |
$this->getHookRunner()->onChangeAuthenticationDataAudit( $req, $status ); |
| 94 |
if ( !$status->isGood() ) { |
| 95 |
$this->dieStatus( $status ); |
| 96 |
} |
| 97 |
$this->authManager->changeAuthenticationData( $req ); |
| 98 |
|
| 99 |
$this->getResult()->addValue( null, $this->getModuleName(), [ 'status' => 'success' ] ); |
| 100 |
} |
| 101 |
|
| 102 |
public function isWriteMode() { |
| 103 |
return true; |
| 104 |
} |
| 105 |
|
| 106 |
public function needsToken() { |
| 107 |
return 'csrf'; |
| 108 |
} |
| 109 |
|
| 110 |
public function getAllowedParams() { |
| 111 |
return ApiAuthManagerHelper::getStandardParams( $this->authAction, |
| 112 |
'request' |
| 113 |
); |
| 114 |
} |
| 115 |
|
| 116 |
protected function getExamplesMessages() { |
| 117 |
$path = $this->getModulePath(); |
| 118 |
$action = $this->getModuleName(); |
| 119 |
return [ |
| 120 |
"action={$action}&request=FooAuthenticationRequest&token=123ABC" |
| 121 |
=> "apihelp-{$path}-example-simple", |
| 122 |
]; |
| 123 |
} |
| 124 |
|
| 125 |
public function getHelpUrls() { |
| 126 |
return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Manage_authentication_data'; |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
class_alias( ApiRemoveAuthenticationData::class, 'ApiRemoveAuthenticationData' ); |