Debian Codegraph @

includes/api/ApiRemoveAuthenticationData.php

raw symbols SCIP
1 <?php
2 /**
3 * Copyright © 2016 Wikimedia Foundation and contributors
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 */
22
23 namespace MediaWiki\Api;
24
25 use MediaWiki\Auth\AuthenticationRequest;
26 use MediaWiki\Auth\AuthManager;
27 use MediaWiki\MainConfigNames;
28
29 /**
30 * Remove authentication data from AuthManager
31 *
32 * @ingroup API
33 */
34 class ApiRemoveAuthenticationData extends ApiBase {
35
36 /** @var string */
37 private $authAction;
38 /** @var string */
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 $this->checkUserRightsAny( 'editmyprivateinfo' );
66
67 $params = $this->extractRequestParams();
68
69 // Check security-sensitive operation status
70 ApiAuthManagerHelper::newForModule( $this, $this->authManager )
71 ->securitySensitiveOperation( $this->operation );
72
73 // Fetch the request. No need to load from the request, so don't use
74 // ApiAuthManagerHelper's method.
75 $remove = $this->authAction === AuthManager::ACTION_REMOVE
76 ? array_fill_keys( $this->getConfig()->get(
77 MainConfigNames::RemoveCredentialsBlacklist ), true )
78 : [];
79 $reqs = array_filter(
80 $this->authManager->getAuthenticationRequests( $this->authAction, $this->getUser() ),
81 static function ( AuthenticationRequest $req ) use ( $params, $remove ) {
82 return $req->getUniqueId() === $params['request'] &&
83 !isset( $remove[get_class( $req )] );
84 }
85 );
86 if ( count( $reqs ) !== 1 ) {
87 $this->dieWithError( 'apierror-changeauth-norequest', 'badrequest' );
88 }
89 $req = reset( $reqs );
90
91 // Perform the removal
92 $status = $this->authManager->allowsAuthenticationDataChange( $req, true );
93 $this->getHookRunner()->onChangeAuthenticationDataAudit( $req, $status );
94 if ( !$status->isGood() ) {
95 $this->dieStatus( $status );
96 }
97 $this->authManager->changeAuthenticationData( $req );
98
99 $this->getResult()->addValue( null, $this->getModuleName(), [ 'status' => 'success' ] );
100 }
101
102 public function isWriteMode() {
103 return true;
104 }
105
106 public function needsToken() {
107 return 'csrf';
108 }
109
110 public function getAllowedParams() {
111 return ApiAuthManagerHelper::getStandardParams( $this->authAction,
112 'request'
113 );
114 }
115
116 protected function getExamplesMessages() {
117 $path = $this->getModulePath();
118 $action = $this->getModuleName();
119 return [
120 "action={$action}&request=FooAuthenticationRequest&token=123ABC"
121 => "apihelp-{$path}-example-simple",
122 ];
123 }
124
125 public function getHelpUrls() {
126 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Manage_authentication_data';
127 }
128 }
129
130 /** @deprecated class alias since 1.43 */
131 class_alias( ApiRemoveAuthenticationData::class, 'ApiRemoveAuthenticationData' );