Don't check namespace in SpecialWantedtemplates
[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
28 /**
29 * Unit to authenticate log-in attempts to the current wiki.
30 *
31 * @ingroup API
32 */
33 class ApiLogin extends ApiBase {
34
35 public function __construct( ApiMain $main, $action ) {
36 parent::__construct( $main, $action, 'lg' );
37 }
38
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.
47 */
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 ) );
56
57 return;
58 }
59
60 $params = $this->extractRequestParams();
61
62 $result = array();
63
64 // Init session if necessary
65 if ( session_id() == '' ) {
66 wfSetupSession();
67 }
68
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' => ''
78 )
79 ) );
80 $loginForm = new LoginForm();
81 $loginForm->setContext( $context );
82
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 );
89
90 ApiQueryInfo::resetTokenCache();
91
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 ) );
97
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;
105
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;
112
113 case LoginForm::WRONG_TOKEN:
114 $result['result'] = 'WrongToken';
115 break;
116
117 case LoginForm::NO_NAME:
118 $result['result'] = 'NoName';
119 break;
120
121 case LoginForm::ILLEGAL:
122 $result['result'] = 'Illegal';
123 break;
124
125 case LoginForm::WRONG_PLUGIN_PASS:
126 $result['result'] = 'WrongPluginPass';
127 break;
128
129 case LoginForm::NOT_EXISTS:
130 $result['result'] = 'NotExists';
131 break;
132
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;
139
140 case LoginForm::EMPTY_PASS:
141 $result['result'] = 'EmptyPass';
142 break;
143
144 case LoginForm::CREATE_BLOCKED:
145 $result['result'] = 'CreateBlocked';
146 $result['details'] = 'Your IP address is blocked from account creation';
147 break;
148
149 case LoginForm::THROTTLED:
150 $result['result'] = 'Throttled';
151 $throttle = $this->getConfig()->get( 'PasswordAttemptThrottle' );
152 $result['wait'] = intval( $throttle['seconds'] );
153 break;
154
155 case LoginForm::USER_BLOCKED:
156 $result['result'] = 'Blocked';
157 break;
158
159 case LoginForm::ABORTED:
160 $result['result'] = 'Aborted';
161 $result['reason'] = $loginForm->mAbortLoginErrorMsg;
162 break;
163
164 default:
165 ApiBase::dieDebug( __METHOD__, "Unhandled case value: {$authRes}" );
166 }
167
168 $this->getResult()->addValue( null, 'login', $result );
169 }
170
171 public function mustBePosted() {
172 return true;
173 }
174
175 public function isReadMode() {
176 return false;
177 }
178
179 public function getAllowedParams() {
180 return array(
181 'name' => null,
182 'password' => array(
183 ApiBase::PARAM_TYPE => 'password',
184 ),
185 'domain' => null,
186 'token' => null,
187 );
188 }
189
190 protected function getExamplesMessages() {
191 return array(
192 'action=login&lgname=user&lgpassword=password'
193 => 'apihelp-login-example-gettoken',
194 'action=login&lgname=user&lgpassword=password&lgtoken=123ABC'
195 => 'apihelp-login-example-login',
196 );
197 }
198
199 public function getHelpUrls() {
200 return 'https://www.mediawiki.org/wiki/API:Login';
201 }
202 }