Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 38
0.00% covered (danger)
0.00%
0 / 8
CRAP
0.00% covered (danger)
0.00%
0 / 1
ApiChangeAuthenticationData
0.00% covered (danger)
0.00%
0 / 37
0.00% covered (danger)
0.00%
0 / 8
132
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 23
0.00% covered (danger)
0.00%
0 / 1
20
 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
 dynamicParameterDocumentation
0.00% covered (danger)
0.00%
0 / 1
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 * @license GPL-2.0-or-later
6 * @file
7 */
8
9namespace MediaWiki\Api;
10
11use MediaWiki\Auth\AuthManager;
12use MediaWiki\MainConfigNames;
13use MediaWiki\Session\SessionManager;
14
15/**
16 * Change authentication data with AuthManager
17 *
18 * @ingroup API
19 */
20class ApiChangeAuthenticationData extends ApiBase {
21
22    public function __construct(
23        ApiMain $main,
24        string $action,
25        private readonly AuthManager $authManager,
26        private readonly SessionManager $sessionManager,
27    ) {
28        parent::__construct( $main, $action, 'changeauth' );
29    }
30
31    public function execute() {
32        if ( !$this->getUser()->isNamed() ) {
33            $this->dieWithError( 'apierror-mustbeloggedin-changeauthenticationdata', 'notloggedin' );
34        }
35
36        $this->checkUserRightsAny( 'editmyprivateinfo' );
37
38        $helper = new ApiAuthManagerHelper( $this, $this->authManager );
39
40        // Check security-sensitive operation status
41        $helper->securitySensitiveOperation( 'ChangeCredentials' );
42
43        // Fetch the request
44        $reqs = ApiAuthManagerHelper::blacklistAuthenticationRequests(
45            $helper->loadAuthenticationRequests( AuthManager::ACTION_CHANGE ),
46            $this->getConfig()->get( MainConfigNames::ChangeCredentialsBlacklist )
47        );
48        if ( count( $reqs ) !== 1 ) {
49            $this->dieWithError( 'apierror-changeauth-norequest', 'badrequest' );
50        }
51        $req = reset( $reqs );
52
53        // Make the change
54        $status = $this->authManager->allowsAuthenticationDataChange( $req, true );
55        $this->getHookRunner()->onChangeAuthenticationDataAudit( $req, $status );
56        if ( !$status->isGood() ) {
57            $this->dieStatus( $status );
58        }
59        $this->authManager->changeAuthenticationData( $req );
60
61        // change user token and update the session
62        $session = $this->getRequest()->getSession();
63        $user = $this->getUser();
64        $this->sessionManager->invalidateSessionsForUser( $user );
65        $session->setUser( $user );
66        $session->resetId();
67
68        $this->getResult()->addValue( null, 'changeauthenticationdata', [ 'status' => 'success' ] );
69    }
70
71    /** @inheritDoc */
72    public function isWriteMode() {
73        return true;
74    }
75
76    /** @inheritDoc */
77    public function needsToken() {
78        return 'csrf';
79    }
80
81    /** @inheritDoc */
82    public function getAllowedParams() {
83        return ApiAuthManagerHelper::getStandardParams( AuthManager::ACTION_CHANGE,
84            'request'
85        );
86    }
87
88    /** @inheritDoc */
89    public function dynamicParameterDocumentation() {
90        return [ 'api-help-authmanagerhelper-additional-params', AuthManager::ACTION_CHANGE ];
91    }
92
93    /** @inheritDoc */
94    protected function getExamplesMessages() {
95        return [
96            'action=changeauthenticationdata' .
97                '&changeauthrequest=MediaWiki%5CAuth%5CPasswordAuthenticationRequest' .
98                '&password=ExamplePassword&retype=ExamplePassword&changeauthtoken=123ABC'
99                => 'apihelp-changeauthenticationdata-example-password',
100        ];
101    }
102
103    /** @inheritDoc */
104    public function getHelpUrls() {
105        return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Manage_authentication_data';
106    }
107}
108
109/** @deprecated class alias since 1.43 */
110class_alias( ApiChangeAuthenticationData::class, 'ApiChangeAuthenticationData' );