Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 58
0.00% covered (danger)
0.00%
0 / 10
CRAP
0.00% covered (danger)
0.00%
0 / 1
ApiClientLogin
0.00% covered (danger)
0.00%
0 / 58
0.00% covered (danger)
0.00%
0 / 10
306
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
 getFinalDescription
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 32
0.00% covered (danger)
0.00%
0 / 1
72
 isReadMode
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 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 / 7
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\AuthenticationResponse;
24use MediaWiki\Auth\AuthManager;
25use MediaWiki\Auth\CreateFromLoginAuthenticationRequest;
26
27/**
28 * Log in to the wiki with AuthManager
29 *
30 * @ingroup API
31 */
32class ApiClientLogin extends ApiBase {
33
34    private AuthManager $authManager;
35
36    /**
37     * @param ApiMain $main
38     * @param string $action
39     * @param AuthManager $authManager
40     */
41    public function __construct(
42        ApiMain $main,
43        $action,
44        AuthManager $authManager
45    ) {
46        parent::__construct( $main, $action, 'login' );
47        $this->authManager = $authManager;
48    }
49
50    public function getFinalDescription() {
51        // A bit of a hack to append 'api-help-authmanager-general-usage'
52        $msgs = parent::getFinalDescription();
53        $msgs[] = ApiBase::makeMessage( 'api-help-authmanager-general-usage', $this->getContext(), [
54            $this->getModulePrefix(),
55            $this->getModuleName(),
56            $this->getModulePath(),
57            AuthManager::ACTION_LOGIN,
58            $this->needsToken(),
59        ] );
60        return $msgs;
61    }
62
63    public function execute() {
64        $params = $this->extractRequestParams();
65
66        $this->requireAtLeastOneParameter( $params, 'continue', 'returnurl' );
67
68        if ( $params['returnurl'] !== null ) {
69            $bits = wfParseUrl( $params['returnurl'] );
70            if ( !$bits || $bits['scheme'] === '' ) {
71                $encParamName = $this->encodeParamName( 'returnurl' );
72                $this->dieWithError(
73                    [ 'apierror-badurl', $encParamName, wfEscapeWikiText( $params['returnurl'] ) ],
74                    "badurl_{$encParamName}"
75                );
76            }
77        }
78
79        $helper = new ApiAuthManagerHelper( $this, $this->authManager );
80
81        // Make sure it's possible to log in
82        if ( !$this->authManager->canAuthenticateNow() ) {
83            $res = AuthenticationResponse::newFail( $this->msg( 'userlogin-cannot-' . AuthManager::ACTION_LOGIN ) );
84            $this->getResult()->addValue( null, 'clientlogin',
85                $helper->formatAuthenticationResponse( $res ) );
86            $helper->logAuthenticationResult( 'login', $res );
87            return;
88        }
89
90        // Perform the login step
91        if ( $params['continue'] ) {
92            $reqs = $helper->loadAuthenticationRequests( AuthManager::ACTION_LOGIN_CONTINUE );
93            $res = $this->authManager->continueAuthentication( $reqs );
94        } else {
95            $reqs = $helper->loadAuthenticationRequests( AuthManager::ACTION_LOGIN );
96            if ( $params['preservestate'] ) {
97                $req = $helper->getPreservedRequest();
98                if ( $req ) {
99                    $reqs[] = $req;
100                }
101            }
102            $res = $this->authManager->beginAuthentication( $reqs, $params['returnurl'] );
103        }
104
105        // Remove CreateFromLoginAuthenticationRequest from $res->neededRequests.
106        // It's there so a RESTART treated as UI will work right, but showing
107        // it to the API client is just confusing.
108        $res->neededRequests = ApiAuthManagerHelper::blacklistAuthenticationRequests(
109            $res->neededRequests, [ CreateFromLoginAuthenticationRequest::class ]
110        );
111
112        $this->getResult()->addValue( null, 'clientlogin',
113            $helper->formatAuthenticationResponse( $res ) );
114        $helper->logAuthenticationResult( 'login', $res );
115    }
116
117    public function isReadMode() {
118        return false;
119    }
120
121    public function isWriteMode() {
122        // (T283394) Logging in triggers some database writes, so should be marked appropriately.
123        return true;
124    }
125
126    public function needsToken() {
127        return 'login';
128    }
129
130    public function getAllowedParams() {
131        return ApiAuthManagerHelper::getStandardParams( AuthManager::ACTION_LOGIN,
132            'requests', 'messageformat', 'mergerequestfields', 'preservestate', 'returnurl', 'continue'
133        );
134    }
135
136    public function dynamicParameterDocumentation() {
137        return [ 'api-help-authmanagerhelper-additional-params', AuthManager::ACTION_LOGIN ];
138    }
139
140    protected function getExamplesMessages() {
141        return [
142            'action=clientlogin&username=Example&password=ExamplePassword&'
143                . 'loginreturnurl=http://example.org/&logintoken=123ABC'
144                => 'apihelp-clientlogin-example-login',
145            'action=clientlogin&logincontinue=1&OATHToken=987654&logintoken=123ABC'
146                => 'apihelp-clientlogin-example-login2',
147        ];
148    }
149
150    public function getHelpUrls() {
151        return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Login';
152    }
153}