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