Localisation updates from https://translatewiki.net.
[mediawiki.git] / includes / api / ApiLogin.php
blob5480d9400b405a8d92177fe411345311fea04d23
1 <?php
2 /**
5 * Created on Sep 19, 2006
7 * Copyright © 2006-2007 Yuri Astrakhan "<Firstname><Lastname>@gmail.com",
8 * Daniel Cannon (cannon dot danielc at gmail dot com)
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 * http://www.gnu.org/copyleft/gpl.html
25 * @file
28 /**
29 * Unit to authenticate log-in attempts to the current wiki.
31 * @ingroup API
33 class ApiLogin extends ApiBase {
35 public function __construct( ApiMain $main, $action ) {
36 parent::__construct( $main, $action, 'lg' );
39 /**
40 * Executes the log-in attempt using the parameters passed. If
41 * the log-in succeeds, it attaches a cookie to the session
42 * and outputs the user id, username, and session token. If a
43 * log-in fails, as the result of a bad password, a nonexistent
44 * user, or any other reason, the host is cached with an expiry
45 * and no log-in attempts will be accepted until that expiry
46 * is reached. The expiry is $this->mLoginThrottle.
48 public function execute() {
49 // If we're in a mode that breaks the same-origin policy, no tokens can
50 // be obtained
51 if ( $this->lacksSameOriginSecurity() ) {
52 $this->getResult()->addValue( null, 'login', array(
53 'result' => 'Aborted',
54 'reason' => 'Cannot log in when the same-origin policy is not applied',
55 ) );
57 return;
60 $params = $this->extractRequestParams();
62 $result = array();
64 // Init session if necessary
65 if ( session_id() == '' ) {
66 wfSetupSession();
69 $context = new DerivativeContext( $this->getContext() );
70 $context->setRequest( new DerivativeRequest(
71 $this->getContext()->getRequest(),
72 array(
73 'wpName' => $params['name'],
74 'wpPassword' => $params['password'],
75 'wpDomain' => $params['domain'],
76 'wpLoginToken' => $params['token'],
77 'wpRemember' => ''
79 ) );
80 $loginForm = new LoginForm();
81 $loginForm->setContext( $context );
83 $authRes = $loginForm->authenticateUserData();
84 switch ( $authRes ) {
85 case LoginForm::SUCCESS:
86 $user = $context->getUser();
87 $this->getContext()->setUser( $user );
88 $user->setCookies( $this->getRequest(), null, true );
90 ApiQueryInfo::resetTokenCache();
92 // Run hooks.
93 // @todo FIXME: Split back and frontend from this hook.
94 // @todo FIXME: This hook should be placed in the backend
95 $injected_html = '';
96 Hooks::run( 'UserLoginComplete', array( &$user, &$injected_html ) );
98 $result['result'] = 'Success';
99 $result['lguserid'] = intval( $user->getId() );
100 $result['lgusername'] = $user->getName();
101 $result['lgtoken'] = $user->getToken();
102 $result['cookieprefix'] = $this->getConfig()->get( 'CookiePrefix' );
103 $result['sessionid'] = session_id();
104 break;
106 case LoginForm::NEED_TOKEN:
107 $result['result'] = 'NeedToken';
108 $result['token'] = $loginForm->getLoginToken();
109 $result['cookieprefix'] = $this->getConfig()->get( 'CookiePrefix' );
110 $result['sessionid'] = session_id();
111 break;
113 case LoginForm::WRONG_TOKEN:
114 $result['result'] = 'WrongToken';
115 break;
117 case LoginForm::NO_NAME:
118 $result['result'] = 'NoName';
119 break;
121 case LoginForm::ILLEGAL:
122 $result['result'] = 'Illegal';
123 break;
125 case LoginForm::WRONG_PLUGIN_PASS:
126 $result['result'] = 'WrongPluginPass';
127 break;
129 case LoginForm::NOT_EXISTS:
130 $result['result'] = 'NotExists';
131 break;
133 // bug 20223 - Treat a temporary password as wrong. Per SpecialUserLogin:
134 // The e-mailed temporary password should not be used for actual logins.
135 case LoginForm::RESET_PASS:
136 case LoginForm::WRONG_PASS:
137 $result['result'] = 'WrongPass';
138 break;
140 case LoginForm::EMPTY_PASS:
141 $result['result'] = 'EmptyPass';
142 break;
144 case LoginForm::CREATE_BLOCKED:
145 $result['result'] = 'CreateBlocked';
146 $result['details'] = 'Your IP address is blocked from account creation';
147 break;
149 case LoginForm::THROTTLED:
150 $result['result'] = 'Throttled';
151 $throttle = $this->getConfig()->get( 'PasswordAttemptThrottle' );
152 $result['wait'] = intval( $throttle['seconds'] );
153 break;
155 case LoginForm::USER_BLOCKED:
156 $result['result'] = 'Blocked';
157 break;
159 case LoginForm::ABORTED:
160 $result['result'] = 'Aborted';
161 $result['reason'] = $loginForm->mAbortLoginErrorMsg;
162 break;
164 default:
165 ApiBase::dieDebug( __METHOD__, "Unhandled case value: {$authRes}" );
168 $this->getResult()->addValue( null, 'login', $result );
171 public function mustBePosted() {
172 return true;
175 public function isReadMode() {
176 return false;
179 public function getAllowedParams() {
180 return array(
181 'name' => null,
182 'password' => null,
183 'domain' => null,
184 'token' => null,
188 protected function getExamplesMessages() {
189 return array(
190 'action=login&lgname=user&lgpassword=password'
191 => 'apihelp-login-example-gettoken',
192 'action=login&lgname=user&lgpassword=password&lgtoken=123ABC'
193 => 'apihelp-login-example-login',
197 public function getHelpUrls() {
198 return 'https://www.mediawiki.org/wiki/API:Login';