MediaWiki master
ApiRemoveAuthenticationData.php
Go to the documentation of this file.
1<?php
23namespace MediaWiki\Api;
24
28
35
37 private $authAction;
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 $params = $this->extractRequestParams();
66
67 // Check security-sensitive operation status
68 ApiAuthManagerHelper::newForModule( $this, $this->authManager )
69 ->securitySensitiveOperation( $this->operation );
70
71 // Fetch the request. No need to load from the request, so don't use
72 // ApiAuthManagerHelper's method.
73 $remove = $this->authAction === AuthManager::ACTION_REMOVE
74 ? array_fill_keys( $this->getConfig()->get(
76 : [];
77 $reqs = array_filter(
78 $this->authManager->getAuthenticationRequests( $this->authAction, $this->getUser() ),
79 static function ( AuthenticationRequest $req ) use ( $params, $remove ) {
80 return $req->getUniqueId() === $params['request'] &&
81 !isset( $remove[get_class( $req )] );
82 }
83 );
84 if ( count( $reqs ) !== 1 ) {
85 $this->dieWithError( 'apierror-changeauth-norequest', 'badrequest' );
86 }
87 $req = reset( $reqs );
88
89 // Perform the removal
90 $status = $this->authManager->allowsAuthenticationDataChange( $req, true );
91 $this->getHookRunner()->onChangeAuthenticationDataAudit( $req, $status );
92 if ( !$status->isGood() ) {
93 $this->dieStatus( $status );
94 }
95 $this->authManager->changeAuthenticationData( $req );
96
97 $this->getResult()->addValue( null, $this->getModuleName(), [ 'status' => 'success' ] );
98 }
99
100 public function isWriteMode() {
101 return true;
102 }
103
104 public function needsToken() {
105 return 'csrf';
106 }
107
108 public function getAllowedParams() {
109 return ApiAuthManagerHelper::getStandardParams( $this->authAction,
110 'request'
111 );
112 }
113
114 protected function getExamplesMessages() {
115 $path = $this->getModulePath();
116 $action = $this->getModuleName();
117 return [
118 "action={$action}&request=FooAuthenticationRequest&token=123ABC"
119 => "apihelp-{$path}-example-simple",
120 ];
121 }
122
123 public function getHelpUrls() {
124 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Manage_authentication_data';
125 }
126}
127
129class_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:75
dieWithError( $msg, $code=null, $data=null, $httpCode=0)
Abort execution with an error.
Definition ApiBase.php:1522
getModuleName()
Get the name of the module being executed by this instance.
Definition ApiBase.php:557
getHookRunner()
Get an ApiHookRunner for running core API hooks.
Definition ApiBase.php:781
getModulePath()
Get the path to this module.
Definition ApiBase.php:636
getResult()
Get the result object.
Definition ApiBase.php:696
dieStatus(StatusValue $status)
Throw an ApiUsageException based on the Status object.
Definition ApiBase.php:1573
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:837
This is the main API class, used for both external and internal processing.
Definition ApiMain.php:78
Remove authentication data from 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.
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
getHelpUrls()
Return links to more detailed help pages about the module.
isWriteMode()
Indicates whether this module requires write access to the wiki.
__construct(ApiMain $main, string $action, AuthManager $authManager)
getExamplesMessages()
Returns usage examples for this module.
This serves as the entry point to the authentication system.
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()