3 * Copyright © 2016 Wikimedia Foundation and contributors
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.
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.
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
23 namespace MediaWiki\Api
;
25 use MediaWiki\Auth\AuthenticationResponse
;
26 use MediaWiki\Auth\AuthManager
;
27 use MediaWiki\Auth\CreateFromLoginAuthenticationRequest
;
28 use MediaWiki\Utils\UrlUtils
;
31 * Log in to the wiki with AuthManager
35 class ApiClientLogin
extends ApiBase
{
37 private AuthManager
$authManager;
38 private UrlUtils
$urlUtils;
40 public function __construct(
43 AuthManager
$authManager,
46 parent
::__construct( $main, $action, 'login' );
47 $this->authManager
= $authManager;
48 $this->urlUtils
= $urlUtils;
51 public function getFinalDescription() {
52 // A bit of a hack to append 'api-help-authmanager-general-usage'
53 $msgs = parent
::getFinalDescription();
54 $msgs[] = $this->msg( 'api-help-authmanager-general-usage',
55 $this->getModulePrefix(),
56 $this->getModuleName(),
57 $this->getModulePath(),
58 AuthManager
::ACTION_LOGIN
,
64 public function execute() {
65 $params = $this->extractRequestParams();
66 $performer = $this->getUser();
68 $this->requireAtLeastOneParameter( $params, 'continue', 'returnurl' );
70 if ( $params['returnurl'] !== null ) {
71 $bits = $this->urlUtils
->parse( $params['returnurl'] );
72 if ( !$bits ||
$bits['scheme'] === '' ) {
73 $encParamName = $this->encodeParamName( 'returnurl' );
75 [ 'apierror-badurl', $encParamName, wfEscapeWikiText( $params['returnurl'] ) ],
76 "badurl_{$encParamName}"
81 $helper = new ApiAuthManagerHelper( $this, $this->authManager
);
83 // Make sure it's possible to log in
84 if ( !$this->authManager
->canAuthenticateNow() ) {
85 $res = AuthenticationResponse
::newFail( $this->msg( 'userlogin-cannot-' . AuthManager
::ACTION_LOGIN
) );
86 $this->getResult()->addValue( null, 'clientlogin',
87 $helper->formatAuthenticationResponse( $res ) );
88 $helper->logAuthenticationResult( 'login', $performer, $res );
92 // Perform the login step
93 if ( $params['continue'] ) {
94 $reqs = $helper->loadAuthenticationRequests( AuthManager
::ACTION_LOGIN_CONTINUE
);
95 $res = $this->authManager
->continueAuthentication( $reqs );
97 $reqs = $helper->loadAuthenticationRequests( AuthManager
::ACTION_LOGIN
);
98 if ( $params['preservestate'] ) {
99 $req = $helper->getPreservedRequest();
104 $res = $this->authManager
->beginAuthentication( $reqs, $params['returnurl'] );
107 // Remove CreateFromLoginAuthenticationRequest from $res->neededRequests.
108 // It's there so a RESTART treated as UI will work right, but showing
109 // it to the API client is just confusing.
110 $res->neededRequests
= ApiAuthManagerHelper
::blacklistAuthenticationRequests(
111 $res->neededRequests
, [ CreateFromLoginAuthenticationRequest
::class ]
114 $this->getResult()->addValue( null, 'clientlogin',
115 $helper->formatAuthenticationResponse( $res ) );
116 $helper->logAuthenticationResult( 'login', $performer, $res );
119 public function isReadMode() {
123 public function isWriteMode() {
124 // (T283394) Logging in triggers some database writes, so should be marked appropriately.
128 public function needsToken() {
132 public function getAllowedParams() {
133 return ApiAuthManagerHelper
::getStandardParams( AuthManager
::ACTION_LOGIN
,
134 'requests', 'messageformat', 'mergerequestfields', 'preservestate', 'returnurl', 'continue'
138 public function dynamicParameterDocumentation() {
139 return [ 'api-help-authmanagerhelper-additional-params', AuthManager
::ACTION_LOGIN
];
142 protected function getExamplesMessages() {
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',
152 public function getHelpUrls() {
153 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Login';
157 /** @deprecated class alias since 1.43 */
158 class_alias( ApiClientLogin
::class, 'ApiClientLogin' );