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