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 private AuthManager $authManager;
28
29 public function __construct(
30 ApiMain $main,
31 string $action,
32 AuthManager $authManager
33 ) {
34 parent::__construct( $main, $action );
35
36 $this->authAction = $action === 'unlinkaccount'
37 ? AuthManager::ACTION_UNLINK
38 : AuthManager::ACTION_REMOVE;
39 $this->operation = $action === 'unlinkaccount'
40 ? 'UnlinkAccount'
41 : 'RemoveCredentials';
42
43 $this->authManager = $authManager;
44 }
45
46 public function execute() {
47 if ( !$this->getUser()->isNamed() ) {
48 $this->dieWithError( 'apierror-mustbeloggedin-removeauth', 'notloggedin' );
49 }
50
51 $params = $this->extractRequestParams();
52
53 // Check security-sensitive operation status
54 ApiAuthManagerHelper::newForModule( $this, $this->authManager )
55 ->securitySensitiveOperation( $this->operation );
56
57 // Fetch the request. No need to load from the request, so don't use
58 // ApiAuthManagerHelper's method.
59 $remove = $this->authAction === AuthManager::ACTION_REMOVE
60 ? array_fill_keys( $this->getConfig()->get(
62 : [];
63 $reqs = array_filter(
64 $this->authManager->getAuthenticationRequests( $this->authAction, $this->getUser() ),
65 static function ( AuthenticationRequest $req ) use ( $params, $remove ) {
66 return $req->getUniqueId() === $params['request'] &&
67 !isset( $remove[get_class( $req )] );
68 }
69 );
70 if ( count( $reqs ) !== 1 ) {
71 $this->dieWithError( 'apierror-changeauth-norequest', 'badrequest' );
72 }
73 $req = reset( $reqs );
74
75 // Perform the removal
76 $status = $this->authManager->allowsAuthenticationDataChange( $req, true );
77 $this->getHookRunner()->onChangeAuthenticationDataAudit( $req, $status );
78 if ( !$status->isGood() ) {
79 $this->dieStatus( $status );
80 }
81 $this->authManager->changeAuthenticationData( $req );
82
83 $this->getResult()->addValue( null, $this->getModuleName(), [ 'status' => 'success' ] );
84 }
85
87 public function isWriteMode() {
88 return true;
89 }
90
92 public function needsToken() {
93 return 'csrf';
94 }
95
97 public function getAllowedParams() {
98 return ApiAuthManagerHelper::getStandardParams( $this->authAction,
99 'request'
100 );
101 }
102
104 protected function getExamplesMessages() {
105 $path = $this->getModulePath();
106 $action = $this->getModuleName();
107 return [
108 "action={$action}&request=FooAuthenticationRequest&token=123ABC"
109 => "apihelp-{$path}-example-simple",
110 ];
111 }
112
114 public function getHelpUrls() {
115 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Manage_authentication_data';
116 }
117}
118
120class_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:61
dieWithError( $msg, $code=null, $data=null, $httpCode=0)
Abort execution with an error.
Definition ApiBase.php:1511
getModuleName()
Get the name of the module being executed by this instance.
Definition ApiBase.php:543
getHookRunner()
Get an ApiHookRunner for running core API hooks.
Definition ApiBase.php:767
getModulePath()
Get the path to this module.
Definition ApiBase.php:622
getResult()
Get the result object.
Definition ApiBase.php:682
dieStatus(StatusValue $status)
Throw an ApiUsageException based on the Status object.
Definition ApiBase.php:1562
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:823
This is the main API class, used for both external and internal processing.
Definition ApiMain.php:66
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.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...
__construct(ApiMain $main, string $action, AuthManager $authManager)
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()
Language name search API.