MediaWiki master
ApiRemoveAuthenticationData.php
Go to the documentation of this file.
1<?php
9namespace MediaWiki\Api;
10
14
21
23 private $authAction;
25 private $operation;
26
27 public function __construct(
28 ApiMain $main,
29 string $action,
30 private readonly AuthManager $authManager
31 ) {
32 parent::__construct( $main, $action );
33
34 $this->authAction = $action === 'unlinkaccount'
35 ? AuthManager::ACTION_UNLINK
36 : AuthManager::ACTION_REMOVE;
37 $this->operation = $action === 'unlinkaccount'
38 ? 'UnlinkAccount'
39 : 'RemoveCredentials';
40 }
41
42 public function execute() {
43 if ( !$this->getUser()->isNamed() ) {
44 $this->dieWithError( 'apierror-mustbeloggedin-removeauth', 'notloggedin' );
45 }
46
47 $params = $this->extractRequestParams();
48
49 // Check security-sensitive operation status
50 ApiAuthManagerHelper::newForModule( $this, $this->authManager )
51 ->securitySensitiveOperation( $this->operation );
52
53 // Fetch the request. No need to load from the request, so don't use
54 // ApiAuthManagerHelper's method.
55 $remove = $this->authAction === AuthManager::ACTION_REMOVE
56 ? array_fill_keys( $this->getConfig()->get(
58 : [];
59 $reqs = array_filter(
60 $this->authManager->getAuthenticationRequests( $this->authAction, $this->getUser() ),
61 static function ( AuthenticationRequest $req ) use ( $params, $remove ) {
62 return $req->getUniqueId() === $params['request'] &&
63 !isset( $remove[get_class( $req )] );
64 }
65 );
66 if ( count( $reqs ) !== 1 ) {
67 $this->dieWithError( 'apierror-changeauth-norequest', 'badrequest' );
68 }
69 $req = reset( $reqs );
70
71 // Perform the removal
72 $status = $this->authManager->allowsAuthenticationDataChange( $req, true );
73 $this->getHookRunner()->onChangeAuthenticationDataAudit( $req, $status );
74 if ( !$status->isGood() ) {
75 $this->dieStatus( $status );
76 }
77 $this->authManager->changeAuthenticationData( $req );
78
79 $this->getResult()->addValue( null, $this->getModuleName(), [ 'status' => 'success' ] );
80 }
81
83 public function isWriteMode() {
84 return true;
85 }
86
88 public function needsToken() {
89 return 'csrf';
90 }
91
93 public function getAllowedParams() {
94 return ApiAuthManagerHelper::getStandardParams( $this->authAction,
95 'request'
96 );
97 }
98
100 protected function getExamplesMessages() {
101 $path = $this->getModulePath();
102 $action = $this->getModuleName();
103 return [
104 "action={$action}&request=FooAuthenticationRequest&token=123ABC"
105 => "apihelp-{$path}-example-simple",
106 ];
107 }
108
110 public function getHelpUrls() {
111 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Manage_authentication_data';
112 }
113}
114
116class_alias( ApiRemoveAuthenticationData::class, 'ApiRemoveAuthenticationData' );
static getStandardParams( $action,... $wantedParams)
Fetch the standard parameters this helper recognizes.
static newForModule(ApiBase $module, ?AuthManager $authManager=null)
Static version of the constructor, for chaining.
This abstract class implements many basic API functions, and is the base of all API classes.
Definition ApiBase.php:60
dieWithError( $msg, $code=null, $data=null, $httpCode=0)
Abort execution with an error.
Definition ApiBase.php:1506
getModuleName()
Get the name of the module being executed by this instance.
Definition ApiBase.php:542
getHookRunner()
Get an ApiHookRunner for running core API hooks.
Definition ApiBase.php:766
getModulePath()
Get the path to this module.
Definition ApiBase.php:621
getResult()
Get the result object.
Definition ApiBase.php:681
dieStatus(StatusValue $status)
Throw an ApiUsageException based on the Status object.
Definition ApiBase.php:1557
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:822
This is the main API class, used for both external and internal processing.
Definition ApiMain.php:66
Remove authentication data from AuthManager.
__construct(ApiMain $main, string $action, private readonly AuthManager $authManager)
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
needsToken()
Returns the token type this module requires in order to execute.Modules are strongly encouraged to us...
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
getHelpUrls()
Return links to more detailed help pages about the module.1.25, returning boolean false is deprecated...
isWriteMode()
Indicates whether this module requires write access to the wiki.API modules must override this method...
getExamplesMessages()
Returns usage examples for this module.Return value has query strings as keys, with values being eith...
AuthManager is the authentication system in MediaWiki and serves entry point for authentication.
This is a value object for authentication requests.
getUniqueId()
Supply a unique key for deduplication.
A class containing constants representing the names of configuration variables.
const RemoveCredentialsBlacklist
Name constant for the RemoveCredentialsBlacklist setting, for use with Config::get()