SpecialMovepage: Convert form to use OOUI controls
[lhc/web/wiklou.git] / includes / api / ApiLogin.php
1 <?php
2 /**
3 *
4 *
5 * Created on Sep 19, 2006
6 *
7 * Copyright © 2006-2007 Yuri Astrakhan "<Firstname><Lastname>@gmail.com",
8 * Daniel Cannon (cannon dot danielc at gmail dot com)
9 *
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.
14 *
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.
19 *
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
24 *
25 * @file
26 */
27 use MediaWiki\Logger\LoggerFactory;
28
29 /**
30 * Unit to authenticate log-in attempts to the current wiki.
31 *
32 * @ingroup API
33 */
34 class ApiLogin extends ApiBase {
35
36 public function __construct( ApiMain $main, $action ) {
37 parent::__construct( $main, $action, 'lg' );
38 }
39
40 /**
41 * Executes the log-in attempt using the parameters passed. If
42 * the log-in succeeds, it attaches a cookie to the session
43 * and outputs the user id, username, and session token. If a
44 * log-in fails, as the result of a bad password, a nonexistent
45 * user, or any other reason, the host is cached with an expiry
46 * and no log-in attempts will be accepted until that expiry
47 * is reached. The expiry is $this->mLoginThrottle.
48 */
49 public function execute() {
50 // If we're in a mode that breaks the same-origin policy, no tokens can
51 // be obtained
52 if ( $this->lacksSameOriginSecurity() ) {
53 $this->getResult()->addValue( null, 'login', array(
54 'result' => 'Aborted',
55 'reason' => 'Cannot log in when the same-origin policy is not applied',
56 ) );
57
58 return;
59 }
60
61 $params = $this->extractRequestParams();
62
63 $result = array();
64
65 // Init session if necessary
66 if ( session_id() == '' ) {
67 wfSetupSession();
68 }
69
70 $context = new DerivativeContext( $this->getContext() );
71 $context->setRequest( new DerivativeRequest(
72 $this->getContext()->getRequest(),
73 array(
74 'wpName' => $params['name'],
75 'wpPassword' => $params['password'],
76 'wpDomain' => $params['domain'],
77 'wpLoginToken' => $params['token'],
78 'wpRemember' => ''
79 )
80 ) );
81 $loginForm = new LoginForm();
82 $loginForm->setContext( $context );
83
84 $authRes = $loginForm->authenticateUserData();
85 switch ( $authRes ) {
86 case LoginForm::SUCCESS:
87 $user = $context->getUser();
88 $this->getContext()->setUser( $user );
89 $user->setCookies( $this->getRequest(), null, true );
90
91 ApiQueryInfo::resetTokenCache();
92
93 // Run hooks.
94 // @todo FIXME: Split back and frontend from this hook.
95 // @todo FIXME: This hook should be placed in the backend
96 $injected_html = '';
97 Hooks::run( 'UserLoginComplete', array( &$user, &$injected_html ) );
98
99 $result['result'] = 'Success';
100 $result['lguserid'] = intval( $user->getId() );
101 $result['lgusername'] = $user->getName();
102 $result['lgtoken'] = $user->getToken();
103 $result['cookieprefix'] = $this->getConfig()->get( 'CookiePrefix' );
104 $result['sessionid'] = session_id();
105 break;
106
107 case LoginForm::NEED_TOKEN:
108 $result['result'] = 'NeedToken';
109 $result['token'] = $loginForm->getLoginToken();
110 $result['cookieprefix'] = $this->getConfig()->get( 'CookiePrefix' );
111 $result['sessionid'] = session_id();
112 break;
113
114 case LoginForm::WRONG_TOKEN:
115 $result['result'] = 'WrongToken';
116 break;
117
118 case LoginForm::NO_NAME:
119 $result['result'] = 'NoName';
120 break;
121
122 case LoginForm::ILLEGAL:
123 $result['result'] = 'Illegal';
124 break;
125
126 case LoginForm::WRONG_PLUGIN_PASS:
127 $result['result'] = 'WrongPluginPass';
128 break;
129
130 case LoginForm::NOT_EXISTS:
131 $result['result'] = 'NotExists';
132 break;
133
134 // bug 20223 - Treat a temporary password as wrong. Per SpecialUserLogin:
135 // The e-mailed temporary password should not be used for actual logins.
136 case LoginForm::RESET_PASS:
137 case LoginForm::WRONG_PASS:
138 $result['result'] = 'WrongPass';
139 break;
140
141 case LoginForm::EMPTY_PASS:
142 $result['result'] = 'EmptyPass';
143 break;
144
145 case LoginForm::CREATE_BLOCKED:
146 $result['result'] = 'CreateBlocked';
147 $result['details'] = 'Your IP address is blocked from account creation';
148 $result = array_merge(
149 $result,
150 ApiQueryUserInfo::getBlockInfo( $context->getUser()->getBlock() )
151 );
152 break;
153
154 case LoginForm::THROTTLED:
155 $result['result'] = 'Throttled';
156 $throttle = $this->getConfig()->get( 'PasswordAttemptThrottle' );
157 $result['wait'] = intval( $throttle['seconds'] );
158 break;
159
160 case LoginForm::USER_BLOCKED:
161 $result['result'] = 'Blocked';
162 $result = array_merge(
163 $result,
164 ApiQueryUserInfo::getBlockInfo( User::newFromName( $params['name'] )->getBlock() )
165 );
166 break;
167
168 case LoginForm::ABORTED:
169 $result['result'] = 'Aborted';
170 $result['reason'] = $loginForm->mAbortLoginErrorMsg;
171 break;
172
173 default:
174 ApiBase::dieDebug( __METHOD__, "Unhandled case value: {$authRes}" );
175 }
176
177 $this->getResult()->addValue( null, 'login', $result );
178
179 LoggerFactory::getInstance( 'authmanager' )->info( 'Login attempt', array(
180 'event' => 'login',
181 'successful' => $authRes === LoginForm::SUCCESS,
182 'status' => LoginForm::$statusCodes[$authRes],
183 ) );
184 }
185
186 public function mustBePosted() {
187 return true;
188 }
189
190 public function isReadMode() {
191 return false;
192 }
193
194 public function getAllowedParams() {
195 return array(
196 'name' => null,
197 'password' => array(
198 ApiBase::PARAM_TYPE => 'password',
199 ),
200 'domain' => null,
201 'token' => null,
202 );
203 }
204
205 protected function getExamplesMessages() {
206 return array(
207 'action=login&lgname=user&lgpassword=password'
208 => 'apihelp-login-example-gettoken',
209 'action=login&lgname=user&lgpassword=password&lgtoken=123ABC'
210 => 'apihelp-login-example-login',
211 );
212 }
213
214 public function getHelpUrls() {
215 return 'https://www.mediawiki.org/wiki/API:Login';
216 }
217 }