Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 45
0.00% covered (danger)
0.00%
0 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
ApiRemoveAuthenticationData
0.00% covered (danger)
0.00%
0 / 45
0.00% covered (danger)
0.00%
0 / 7
210
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
12
 execute
0.00% covered (danger)
0.00%
0 / 25
0.00% covered (danger)
0.00%
0 / 1
42
 isWriteMode
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 needsToken
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getAllowedParams
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 getExamplesMessages
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
 getHelpUrls
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
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
23use MediaWiki\Auth\AuthenticationRequest;
24use MediaWiki\Auth\AuthManager;
25use MediaWiki\MainConfigNames;
26
27/**
28 * Remove authentication data from AuthManager
29 *
30 * @ingroup API
31 */
32class ApiRemoveAuthenticationData extends ApiBase {
33
34    private $authAction;
35    private $operation;
36
37    private AuthManager $authManager;
38
39    /**
40     * @param ApiMain $main
41     * @param string $action
42     * @param AuthManager $authManager
43     */
44    public function __construct(
45        ApiMain $main,
46        $action,
47        AuthManager $authManager
48    ) {
49        parent::__construct( $main, $action );
50
51        $this->authAction = $action === 'unlinkaccount'
52            ? AuthManager::ACTION_UNLINK
53            : AuthManager::ACTION_REMOVE;
54        $this->operation = $action === 'unlinkaccount'
55            ? 'UnlinkAccount'
56            : 'RemoveCredentials';
57
58        $this->authManager = $authManager;
59    }
60
61    public function execute() {
62        if ( !$this->getUser()->isNamed() ) {
63            $this->dieWithError( 'apierror-mustbeloggedin-removeauth', 'notloggedin' );
64        }
65
66        $params = $this->extractRequestParams();
67
68        // Check security-sensitive operation status
69        ApiAuthManagerHelper::newForModule( $this, $this->authManager )
70            ->securitySensitiveOperation( $this->operation );
71
72        // Fetch the request. No need to load from the request, so don't use
73        // ApiAuthManagerHelper's method.
74        $remove = $this->authAction === AuthManager::ACTION_REMOVE
75            ? array_fill_keys( $this->getConfig()->get(
76                MainConfigNames::RemoveCredentialsBlacklist ), true )
77            : [];
78        $reqs = array_filter(
79            $this->authManager->getAuthenticationRequests( $this->authAction, $this->getUser() ),
80            static function ( AuthenticationRequest $req ) use ( $params, $remove ) {
81                return $req->getUniqueId() === $params['request'] &&
82                    !isset( $remove[get_class( $req )] );
83            }
84        );
85        if ( count( $reqs ) !== 1 ) {
86            $this->dieWithError( 'apierror-changeauth-norequest', 'badrequest' );
87        }
88        $req = reset( $reqs );
89
90        // Perform the removal
91        $status = $this->authManager->allowsAuthenticationDataChange( $req, true );
92        $this->getHookRunner()->onChangeAuthenticationDataAudit( $req, $status );
93        if ( !$status->isGood() ) {
94            $this->dieStatus( $status );
95        }
96        $this->authManager->changeAuthenticationData( $req );
97
98        $this->getResult()->addValue( null, $this->getModuleName(), [ 'status' => 'success' ] );
99    }
100
101    public function isWriteMode() {
102        return true;
103    }
104
105    public function needsToken() {
106        return 'csrf';
107    }
108
109    public function getAllowedParams() {
110        return ApiAuthManagerHelper::getStandardParams( $this->authAction,
111            'request'
112        );
113    }
114
115    protected function getExamplesMessages() {
116        $path = $this->getModulePath();
117        $action = $this->getModuleName();
118        return [
119            "action={$action}&request=FooAuthenticationRequest&token=123ABC"
120                => "apihelp-{$path}-example-simple",
121        ];
122    }
123
124    public function getHelpUrls() {
125        return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Manage_authentication_data';
126    }
127}