MediaWiki  master
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 }
ApiMain
This is the main API class, used for both external and internal processing.
Definition: ApiMain.php:47
ContextSource\getConfig
getConfig()
Definition: ContextSource.php:67
ApiAuthManagerHelper\newForModule
static newForModule(ApiBase $module, AuthManager $authManager=null)
Static version of the constructor, for chaining.
Definition: ApiAuthManagerHelper.php:66
ApiRemoveAuthenticationData\needsToken
needsToken()
Returns the token type this module requires in order to execute.
Definition: ApiRemoveAuthenticationData.php:92
ApiRemoveAuthenticationData\isWriteMode
isWriteMode()
Indicates whether this module requires write mode.
Definition: ApiRemoveAuthenticationData.php:88
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:155
ApiBase\dieWithError
dieWithError( $msg, $code=null, $data=null, $httpCode=null)
Abort execution with an error.
Definition: ApiBase.php:1382
ApiRemoveAuthenticationData\$authAction
$authAction
Definition: ApiRemoveAuthenticationData.php:34
ApiBase\getResult
getResult()
Get the result object.
Definition: ApiBase.php:565
ApiRemoveAuthenticationData\__construct
__construct(ApiMain $main, $action)
Definition: ApiRemoveAuthenticationData.php:37
ApiAuthManagerHelper\getStandardParams
static getStandardParams( $action,... $wantedParams)
Fetch the standard parameters this helper recognizes.
Definition: ApiAuthManagerHelper.php:362
ContextSource\getUser
getUser()
Stable to override.
Definition: ContextSource.php:131
ApiBase
This abstract class implements many basic API functions, and is the base of all API classes.
Definition: ApiBase.php:52
ApiRemoveAuthenticationData\getExamplesMessages
getExamplesMessages()
Returns usage examples for this module.
Definition: ApiRemoveAuthenticationData.php:102
ApiBase\getModulePath
getModulePath()
Get the path to this module.
Definition: ApiBase.php:509
MediaWiki\Auth\AuthenticationRequest\getUniqueId
getUniqueId()
Supply a unique key for deduplication.
Definition: AuthenticationRequest.php:90
ApiBase\extractRequestParams
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition: ApiBase.php:717
ApiRemoveAuthenticationData\getHelpUrls
getHelpUrls()
Return links to more detailed help pages about the module.
Definition: ApiRemoveAuthenticationData.php:111
ApiRemoveAuthenticationData
Remove authentication data from AuthManager.
Definition: ApiRemoveAuthenticationData.php:32
MediaWiki\Auth\AuthManager
This serves as the entry point to the authentication system.
Definition: AuthManager.php:88
ApiRemoveAuthenticationData\$operation
$operation
Definition: ApiRemoveAuthenticationData.php:35
$path
$path
Definition: NoLocalSettings.php:25
ApiRemoveAuthenticationData\getAllowedParams
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
Definition: ApiRemoveAuthenticationData.php:96
ApiBase\dieStatus
dieStatus(StatusValue $status)
Throw an ApiUsageException based on the Status object.
Definition: ApiBase.php:1440
ApiBase\getModuleName
getModuleName()
Get the name of the module being executed by this instance.
Definition: ApiBase.php:444
ApiRemoveAuthenticationData\execute
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
Definition: ApiRemoveAuthenticationData.php:48
ApiBase\getHookRunner
getHookRunner()
Get an ApiHookRunner for running core API hooks.
Definition: ApiBase.php:662
MediaWiki\Auth\AuthenticationRequest
This is a value object for authentication requests.
Definition: AuthenticationRequest.php:38