10c31d0997154e623bf01b095204e505f55311e5
4 * Created on Sep 19, 2006
6 * API for MediaWiki 1.8+
8 * Copyright (C) 2006-2007 Yuri Astrakhan <Firstname><Lastname>@gmail.com,
9 * Daniel Cannon (cannon dot danielc at gmail dot com)
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 * http://www.gnu.org/copyleft/gpl.html
27 if (!defined('MEDIAWIKI')) {
28 // Eclipse helper - will be ignored in production
29 require_once ('ApiBase.php');
33 * Unit to authenticate log-in attempts to the current wiki.
37 class ApiLogin
extends ApiBase
{
40 * The amount of time a user must wait after submitting
41 * a bad login (will be multiplied by the THROTTLE_FACTOR for each bad attempt)
43 const THROTTLE_TIME
= 10;
46 * The factor by which the wait-time in between authentication
47 * attempts is increased every failed attempt.
49 const THROTTLE_FACTOR
= 1.5;
52 * The maximum number of failed logins after which the wait increase stops.
54 const THOTTLE_MAX_COUNT
= 10;
56 public function __construct($main, $action) {
57 parent
:: __construct($main, $action, 'lg');
61 * Executes the log-in attempt using the parameters passed. If
62 * the log-in succeeeds, it attaches a cookie to the session
63 * and outputs the user id, username, and session token. If a
64 * log-in fails, as the result of a bad password, a nonexistant
65 * user, or any other reason, the host is cached with an expiry
66 * and no log-in attempts will be accepted until that expiry
67 * is reached. The expiry is $this->mLoginThrottle.
71 public function execute() {
72 $name = $password = $domain = null;
73 extract($this->extractRequestParams());
77 // Make sure noone is trying to guess the password brut-force
78 $nextLoginIn = $this->getNextLoginTimeout();
79 if ($nextLoginIn > 0) {
80 $result['result'] = 'NeedToWait';
81 $result['details'] = "Please wait $nextLoginIn seconds before next log-in attempt";
82 $result['wait'] = $nextLoginIn;
83 $this->getResult()->addValue(null, 'login', $result);
87 $params = new FauxRequest(array (
89 'wpPassword' => $password,
90 'wpDomain' => $domain,
94 $loginForm = new LoginForm($params);
95 switch ($loginForm->authenticateUserData()) {
96 case LoginForm
:: SUCCESS
:
99 $wgUser->setOption('rememberpassword', 1);
100 $wgUser->setCookies();
102 $result['result'] = 'Success';
103 $result['lguserid'] = $_SESSION['wsUserID'];
104 $result['lgusername'] = $_SESSION['wsUserName'];
105 $result['lgtoken'] = $_SESSION['wsToken'];
108 case LoginForm
:: NO_NAME
:
109 $result['result'] = 'NoName';
111 case LoginForm
:: ILLEGAL
:
112 $result['result'] = 'Illegal';
114 case LoginForm
:: WRONG_PLUGIN_PASS
:
115 $result['result'] = 'WrongPluginPass';
117 case LoginForm
:: NOT_EXISTS
:
118 $result['result'] = 'NotExists';
120 case LoginForm
:: WRONG_PASS
:
121 $result['result'] = 'WrongPass';
123 case LoginForm
:: EMPTY_PASS
:
124 $result['result'] = 'EmptyPass';
127 ApiBase
:: dieDebug(__METHOD__
, 'Unhandled case value');
130 if ($result['result'] != 'Success') {
131 $result['wait'] = $this->cacheBadLogin();
133 // if we were allowed to try to login, memcache is fine
135 $this->getResult()->addValue(null, 'login', $result);
140 * Caches a bad-login attempt associated with the host and with an
141 * expiry of $this->mLoginThrottle. These are cached by a key
142 * separate from that used by the captcha system--as such, logging
143 * in through the standard interface will get you a legal session
144 * and cookies to prove it, but will not remove this entry.
146 * Returns the number of seconds until next login attempt will be allowed.
150 private function cacheBadLogin() {
153 $key = $this->getMemCacheKey();
154 $val = $wgMemc->get( $key );
156 $val['lastReqTime'] = time();
157 if (!isset($val['count'])) {
160 $val['count'] = 1 +
$val['count'];
163 $delay = ApiLogin
::calculateDelay($val);
165 $wgMemc->delete($key);
166 $wgMemc->add( $key, $val, $delay );
172 * How much time the client must wait before it will be
173 * allowed to try to log-in next.
174 * The return value is 0 if no wait is required.
176 private function getNextLoginTimeout() {
179 $val = $wgMemc->get($this->getMemCacheKey());
181 $elapse = (time() - $val['lastReqTime']) / 1000; // in seconds
182 $canRetryIn = ApiLogin
::calculateDelay($val) - $elapse;
184 return $canRetryIn < 0 ?
0 : $canRetryIn;
188 * Based on the number of previously attempted logins, returns
189 * the delay (in seconds) when the next login attempt will be allowed.
191 private static function calculateDelay($val) {
192 // Defensive programming
193 $count = $val['count'];
194 $count = $count < 1 ?
1 : $count;
195 $count = $count > self
::THOTTLE_MAX_COUNT ? self
::THOTTLE_MAX_COUNT
: $count;
197 return self
::THROTTLE_TIME + self
::THROTTLE_TIME
* ($count - 1) * self
::THROTTLE_FACTOR
;
201 * Internal cache key for badlogin checks. Robbed from the
202 * ConfirmEdit extension and modified to use a key unique to the
208 private function getMemCacheKey() {
209 return wfMemcKey( 'apilogin', 'badlogin', 'ip', wfGetIP() );
212 protected function getAllowedParams() {
220 protected function getParamDescription() {
222 'name' => 'User Name',
223 'password' => 'Password',
224 'domain' => 'Domain (optional)'
228 protected function getDescription() {
230 'This module is used to login and get the authentication tokens. ',
231 'In the event of a successful log-in, a cookie will be attached',
232 'to your session. In the event of a failed log-in, you will not ',
233 'be able to attempt another log-in through this method for 60 seconds.',
234 'This is to prevent password guessing by automated password crackers.'
238 protected function getExamples() {
240 'api.php?action=login&lgname=user&lgpassword=password'
244 public function getVersion() {
245 return __CLASS__
. ': $Id$';