3 * Copyright © 2016 Wikimedia Foundation and contributors
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
23 namespace MediaWiki\Api
;
25 use MediaWiki\Auth\AuthenticationRequest
;
26 use MediaWiki\Auth\AuthManager
;
27 use MediaWiki\MainConfigNames
;
30 * Remove authentication data from AuthManager
34 class ApiRemoveAuthenticationData
extends ApiBase
{
41 private AuthManager
$authManager;
43 public function __construct(
46 AuthManager
$authManager
48 parent
::__construct( $main, $action );
50 $this->authAction
= $action === 'unlinkaccount'
51 ? AuthManager
::ACTION_UNLINK
52 : AuthManager
::ACTION_REMOVE
;
53 $this->operation
= $action === 'unlinkaccount'
55 : 'RemoveCredentials';
57 $this->authManager
= $authManager;
60 public function execute() {
61 if ( !$this->getUser()->isNamed() ) {
62 $this->dieWithError( 'apierror-mustbeloggedin-removeauth', 'notloggedin' );
65 $params = $this->extractRequestParams();
67 // Check security-sensitive operation status
68 ApiAuthManagerHelper
::newForModule( $this, $this->authManager
)
69 ->securitySensitiveOperation( $this->operation
);
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(
75 MainConfigNames
::RemoveCredentialsBlacklist
), true )
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 )] );
84 if ( count( $reqs ) !== 1 ) {
85 $this->dieWithError( 'apierror-changeauth-norequest', 'badrequest' );
87 $req = reset( $reqs );
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 );
95 $this->authManager
->changeAuthenticationData( $req );
97 $this->getResult()->addValue( null, $this->getModuleName(), [ 'status' => 'success' ] );
100 public function isWriteMode() {
104 public function needsToken() {
108 public function getAllowedParams() {
109 return ApiAuthManagerHelper
::getStandardParams( $this->authAction
,
114 protected function getExamplesMessages() {
115 $path = $this->getModulePath();
116 $action = $this->getModuleName();
118 "action={$action}&request=FooAuthenticationRequest&token=123ABC"
119 => "apihelp-{$path}-example-simple",
123 public function getHelpUrls() {
124 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Manage_authentication_data';
128 /** @deprecated class alias since 1.43 */
129 class_alias( ApiRemoveAuthenticationData
::class, 'ApiRemoveAuthenticationData' );