Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 142
0.00% covered (danger)
0.00%
0 / 17
CRAP
0.00% covered (danger)
0.00%
0 / 1
SpecialChangeCredentials
0.00% covered (danger)
0.00%
0 / 142
0.00% covered (danger)
0.00%
0 / 17
1892
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 getRestriction
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getGroupName
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 isListed
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 doesWrites
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getDefaultAction
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
72
 loadAuth
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
30
 onAuthChangeFormFields
0.00% covered (danger)
0.00%
0 / 24
0.00% covered (danger)
0.00%
0 / 1
6
 getAuthFormDescriptor
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 1
56
 getAuthForm
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 1
6
 needsSubmitButton
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 handleFormSubmit
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
6
 showSubpageList
0.00% covered (danger)
0.00%
0 / 20
0.00% covered (danger)
0.00%
0 / 1
30
 success
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 1
6
 getReturnUrl
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
6
 getRequestBlacklist
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace MediaWiki\Specials;
4
5use LogicException;
6use MediaWiki\Auth\AuthenticationRequest;
7use MediaWiki\Auth\AuthenticationResponse;
8use MediaWiki\Auth\AuthManager;
9use MediaWiki\Auth\PasswordAuthenticationRequest;
10use MediaWiki\Html\Html;
11use MediaWiki\MainConfigNames;
12use MediaWiki\Message\Message;
13use MediaWiki\Session\SessionManager;
14use MediaWiki\SpecialPage\AuthManagerSpecialPage;
15use MediaWiki\Status\Status;
16use MediaWiki\Title\Title;
17
18/**
19 * Change user credentials, such as the password.
20 *
21 * This is also powers most of the SpecialRemoveCredentials subclass.
22 *
23 * @see SpecialChangePassword
24 * @ingroup SpecialPage
25 * @ingroup Auth
26 */
27class SpecialChangeCredentials extends AuthManagerSpecialPage {
28    /** @inheritDoc */
29    protected static $allowedActions = [ AuthManager::ACTION_CHANGE ];
30
31    /** @var string */
32    protected static $messagePrefix = 'changecredentials';
33
34    /** @var bool Change action needs user data; remove action does not */
35    protected static $loadUserData = true;
36
37    public function __construct(
38        AuthManager $authManager,
39        private readonly SessionManager $sessionManager
40    ) {
41        parent::__construct( 'ChangeCredentials' );
42        $this->setAuthManager( $authManager );
43    }
44
45    /** @inheritDoc */
46    public function getRestriction(): string {
47        return 'editmyprivateinfo';
48    }
49
50    /** @inheritDoc */
51    protected function getGroupName() {
52        return 'login';
53    }
54
55    /** @inheritDoc */
56    public function isListed() {
57        $this->loadAuth( '' );
58        return (bool)$this->authRequests;
59    }
60
61    /** @inheritDoc */
62    public function doesWrites() {
63        return true;
64    }
65
66    /** @inheritDoc */
67    protected function getDefaultAction( $subPage ) {
68        return AuthManager::ACTION_CHANGE;
69    }
70
71    /** @inheritDoc */
72    public function execute( $subPage ) {
73        $this->setHeaders();
74        $this->outputHeader();
75
76        $this->loadAuth( $subPage );
77
78        if ( !$subPage ) {
79            $this->showSubpageList();
80            return;
81        }
82
83        if ( !$this->authRequests ) {
84            // messages used: changecredentials-invalidsubpage, removecredentials-invalidsubpage
85            $this->showSubpageList( $this->msg( static::$messagePrefix . '-invalidsubpage', $subPage ) );
86            return;
87        }
88
89        $out = $this->getOutput();
90        $out->addModules( 'mediawiki.special.changecredentials' );
91        $out->addBacklinkSubtitle( $this->getPageTitle() );
92        $status = $this->trySubmit();
93
94        if ( $status === false || !$status->isOK() ) {
95            $this->displayForm( $status );
96            return;
97        }
98
99        $response = $status->getValue();
100
101        switch ( $response->status ) {
102            case AuthenticationResponse::PASS:
103                $this->success();
104                break;
105            case AuthenticationResponse::FAIL:
106                $this->displayForm( Status::newFatal( $response->message ) );
107                break;
108            default:
109                throw new LogicException( 'invalid AuthenticationResponse' );
110        }
111    }
112
113    /** @inheritDoc */
114    protected function loadAuth( $subPage, $authAction = null, $reset = false ) {
115        parent::loadAuth( $subPage, $authAction );
116        if ( $subPage ) {
117            $foundReqs = [];
118            foreach ( $this->authRequests as $req ) {
119                if ( $req->getUniqueId() === $subPage ) {
120                    $foundReqs[] = $req;
121                }
122            }
123            if ( count( $foundReqs ) > 1 ) {
124                throw new LogicException( 'Multiple AuthenticationRequest objects with same ID!' );
125            }
126            $this->authRequests = $foundReqs;
127        }
128    }
129
130    /** @inheritDoc */
131    public function onAuthChangeFormFields(
132        array $requests, array $fieldInfo, array &$formDescriptor, $action
133    ) {
134        parent::onAuthChangeFormFields( $requests, $fieldInfo, $formDescriptor, $action );
135
136        // Add some UI flair for password changes, the most common use case for this page.
137        if ( AuthenticationRequest::getRequestByClass( $this->authRequests,
138            PasswordAuthenticationRequest::class )
139        ) {
140            $formDescriptor = self::mergeDefaultFormDescriptor( $fieldInfo, $formDescriptor, [
141                'password' => [
142                    'autocomplete' => 'new-password',
143                    'placeholder-message' => 'createacct-yourpassword-ph',
144                    'help-message' => '',
145                ],
146                'retype' => [
147                    'autocomplete' => 'new-password',
148                    'placeholder-message' => 'createacct-yourpasswordagain-ph',
149                ],
150                // T263927 - the Chromium password form guide recommends always having a username field
151                'username' => [
152                    'type' => 'text',
153                    'baseField' => 'password',
154                    'autocomplete' => 'username',
155                    'nodata' => true,
156                    'readonly' => true,
157                    'cssclass' => 'mw-htmlform-hidden-field',
158                    'label-message' => 'userlogin-yourname',
159                    'placeholder-message' => 'userlogin-yourname-ph',
160                ],
161            ] );
162        }
163    }
164
165    /** @inheritDoc */
166    protected function getAuthFormDescriptor( $requests, $action ) {
167        if ( !static::$loadUserData ) {
168            return [];
169        }
170
171        $descriptor = parent::getAuthFormDescriptor( $requests, $action );
172
173        $any = false;
174        foreach ( $descriptor as &$field ) {
175            if ( $field['type'] === 'password' && $field['name'] !== 'retype' ) {
176                $any = true;
177                if ( isset( $field['cssclass'] ) ) {
178                    $field['cssclass'] .= ' mw-changecredentials-validate-password';
179                } else {
180                    $field['cssclass'] = 'mw-changecredentials-validate-password';
181                }
182            }
183        }
184        unset( $field );
185
186        if ( $any ) {
187            $this->getOutput()->addModules( 'mediawiki.misc-authed-ooui' );
188        }
189
190        return $descriptor;
191    }
192
193    /** @inheritDoc */
194    protected function getAuthForm( array $requests, $action ) {
195        $form = parent::getAuthForm( $requests, $action );
196        $req = reset( $requests );
197        $info = $req->describeCredentials();
198
199        $form->addPreHtml(
200            Html::openElement( 'dl' )
201            . Html::element( 'dt', [], $this->msg( 'credentialsform-provider' )->text() )
202            . Html::element( 'dd', [], $info['provider']->text() )
203            . Html::element( 'dt', [], $this->msg( 'credentialsform-account' )->text() )
204            . Html::element( 'dd', [], $info['account']->text() )
205            . Html::closeElement( 'dl' )
206        );
207
208        // messages used: changecredentials-submit removecredentials-submit
209        $form->setSubmitTextMsg( static::$messagePrefix . '-submit' );
210        $form->showCancel()->setCancelTarget( $this->getReturnUrl() ?: Title::newMainPage() );
211        $form->setSubmitID( 'change_credentials_submit' );
212        return $form;
213    }
214
215    /** @inheritDoc */
216    protected function needsSubmitButton( array $requests ) {
217        // Change/remove forms show are built from a single AuthenticationRequest and do not allow
218        // for redirect flow; they always need a submit button.
219        return true;
220    }
221
222    /** @inheritDoc */
223    public function handleFormSubmit( $data ) {
224        // remove requests do not accept user input
225        $requests = $this->authRequests;
226        if ( static::$loadUserData ) {
227            $requests = AuthenticationRequest::loadRequestsFromSubmission( $this->authRequests, $data );
228        }
229
230        $response = $this->performAuthenticationStep( $this->authAction, $requests );
231
232        // we can't handle FAIL or similar as failure here since it might require changing the form
233        return Status::newGood( $response );
234    }
235
236    /**
237     * @param Message|null $error
238     */
239    protected function showSubpageList( $error = null ) {
240        $out = $this->getOutput();
241
242        if ( $error ) {
243            $out->addHTML( $error->parse() );
244        }
245
246        $groupedRequests = [];
247        foreach ( $this->authRequests as $req ) {
248            $info = $req->describeCredentials();
249            $groupedRequests[$info['provider']->text()][] = $req;
250        }
251
252        $linkRenderer = $this->getLinkRenderer();
253        $out->addHTML( Html::openElement( 'dl' ) );
254        foreach ( $groupedRequests as $group => $members ) {
255            $out->addHTML( Html::element( 'dt', [], $group ) );
256            foreach ( $members as $req ) {
257                /** @var AuthenticationRequest $req */
258                $info = $req->describeCredentials();
259                $out->addHTML( Html::rawElement( 'dd', [],
260                    $linkRenderer->makeLink(
261                        $this->getPageTitle( $req->getUniqueId() ),
262                        $info['account']->text()
263                    )
264                ) );
265            }
266        }
267        $out->addHTML( Html::closeElement( 'dl' ) );
268    }
269
270    protected function success() {
271        $session = $this->getRequest()->getSession();
272        $user = $this->getUser();
273        $out = $this->getOutput();
274        $returnUrl = $this->getReturnUrl();
275
276        // change user token and update the session
277        $this->sessionManager->invalidateSessionsForUser( $user );
278        $session->setUser( $user );
279        $session->resetId();
280
281        if ( $returnUrl ) {
282            $out->redirect( $returnUrl );
283        } else {
284            // messages used: changecredentials-success removecredentials-success
285            $out->addModuleStyles( 'mediawiki.codex.messagebox.styles' );
286            $out->addHTML(
287                Html::successBox(
288                    $out->msg( static::$messagePrefix . '-success' )->parse()
289                )
290            );
291            $out->returnToMain();
292        }
293    }
294
295    /**
296     * @return string|null
297     */
298    protected function getReturnUrl() {
299        $request = $this->getRequest();
300        $returnTo = $request->getText( 'returnto' );
301        $returnToQuery = $request->getText( 'returntoquery', '' );
302
303        if ( !$returnTo ) {
304            return null;
305        }
306
307        return Title::newFromText( $returnTo )->getFullUrlForRedirect( $returnToQuery );
308    }
309
310    /** @inheritDoc */
311    protected function getRequestBlacklist() {
312        return $this->getConfig()->get( MainConfigNames::ChangeCredentialsBlacklist );
313    }
314}
315
316// @codeCoverageIgnoreStart
317/** @deprecated class alias since 1.41 */
318class_alias( SpecialChangeCredentials::class, 'SpecialChangeCredentials' );
319// @codeCoverageIgnoreEnd