MediaWiki REL1_35
ApiRemoveAuthenticationData.php
Go to the documentation of this file.
1<?php
26
33
34 private $authAction;
35 private $operation;
36
37 public function __construct( ApiMain $main, $action ) {
38 parent::__construct( $main, $action );
39
40 $this->authAction = $action === 'unlinkaccount'
41 ? AuthManager::ACTION_UNLINK
42 : AuthManager::ACTION_REMOVE;
43 $this->operation = $action === 'unlinkaccount'
44 ? 'UnlinkAccount'
45 : 'RemoveCredentials';
46 }
47
48 public function execute() {
49 if ( !$this->getUser()->isLoggedIn() ) {
50 $this->dieWithError( 'apierror-mustbeloggedin-removeauth', 'notloggedin' );
51 }
52
53 $params = $this->extractRequestParams();
54 $manager = MediaWikiServices::getInstance()->getAuthManager();
55
56 // Check security-sensitive operation status
57 ApiAuthManagerHelper::newForModule( $this, $manager )
58 ->securitySensitiveOperation( $this->operation );
59
60 // Fetch the request. No need to load from the request, so don't use
61 // ApiAuthManagerHelper's method.
62 $blacklist = $this->authAction === AuthManager::ACTION_REMOVE
63 ? array_flip( $this->getConfig()->get( 'RemoveCredentialsBlacklist' ) )
64 : [];
65 $reqs = array_filter(
66 $manager->getAuthenticationRequests( $this->authAction, $this->getUser() ),
67 function ( AuthenticationRequest $req ) use ( $params, $blacklist ) {
68 return $req->getUniqueId() === $params['request'] &&
69 !isset( $blacklist[get_class( $req )] );
70 }
71 );
72 if ( count( $reqs ) !== 1 ) {
73 $this->dieWithError( 'apierror-changeauth-norequest', 'badrequest' );
74 }
75 $req = reset( $reqs );
76
77 // Perform the removal
78 $status = $manager->allowsAuthenticationDataChange( $req, true );
79 $this->getHookRunner()->onChangeAuthenticationDataAudit( $req, $status );
80 if ( !$status->isGood() ) {
81 $this->dieStatus( $status );
82 }
83 $manager->changeAuthenticationData( $req );
84
85 $this->getResult()->addValue( null, $this->getModuleName(), [ 'status' => 'success' ] );
86 }
87
88 public function isWriteMode() {
89 return true;
90 }
91
92 public function needsToken() {
93 return 'csrf';
94 }
95
96 public function getAllowedParams() {
97 return ApiAuthManagerHelper::getStandardParams( $this->authAction,
98 'request'
99 );
100 }
101
102 protected function getExamplesMessages() {
103 $path = $this->getModulePath();
104 $action = $this->getModuleName();
105 return [
106 "action={$action}&request=FooAuthenticationRequest&token=123ABC"
107 => "apihelp-{$path}-example-simple",
108 ];
109 }
110
111 public function getHelpUrls() {
112 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Manage_authentication_data';
113 }
114}
static newForModule(ApiBase $module, AuthManager $authManager=null)
Static version of the constructor, for chaining.
static getStandardParams( $action,... $wantedParams)
Fetch the standard parameters this helper recognizes.
This abstract class implements many basic API functions, and is the base of all API classes.
Definition ApiBase.php:52
dieWithError( $msg, $code=null, $data=null, $httpCode=0)
Abort execution with an error.
Definition ApiBase.php:1437
getResult()
Get the result object.
Definition ApiBase.php:620
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:772
getModulePath()
Get the path to this module.
Definition ApiBase.php:564
getModuleName()
Get the name of the module being executed by this instance.
Definition ApiBase.php:499
dieStatus(StatusValue $status)
Throw an ApiUsageException based on the Status object.
Definition ApiBase.php:1495
getHookRunner()
Get an ApiHookRunner for running core API hooks.
Definition ApiBase.php:717
This is the main API class, used for both external and internal processing.
Definition ApiMain.php:47
Remove authentication data from AuthManager.
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
isWriteMode()
Indicates whether this module requires write mode.
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
getHelpUrls()
Return links to more detailed help pages about the module.
getExamplesMessages()
Returns usage examples for this module.
needsToken()
Returns the token type this module requires in order to execute.
getUser()
Stable to override.
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.
MediaWikiServices is the service locator for the application scope of MediaWiki.