Follow-ups to r56684: add a member function for extensions to add header text cleanly...
[lhc/web/wiklou.git] / includes / api / ApiLogin.php
1 <?php
2
3 /*
4 * Created on Sep 19, 2006
5 *
6 * API for MediaWiki 1.8+
7 *
8 * Copyright (C) 2006-2007 Yuri Astrakhan <Firstname><Lastname>@gmail.com,
9 * Daniel Cannon (cannon dot danielc at gmail dot com)
10 *
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.
15 *
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.
20 *
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
25 */
26
27 if (!defined('MEDIAWIKI')) {
28 // Eclipse helper - will be ignored in production
29 require_once ('ApiBase.php');
30 }
31
32 /**
33 * Unit to authenticate log-in attempts to the current wiki.
34 *
35 * @ingroup API
36 */
37 class ApiLogin extends ApiBase {
38
39 public function __construct($main, $action) {
40 parent :: __construct($main, $action, 'lg');
41 }
42
43 /**
44 * Executes the log-in attempt using the parameters passed. If
45 * the log-in succeeeds, it attaches a cookie to the session
46 * and outputs the user id, username, and session token. If a
47 * log-in fails, as the result of a bad password, a nonexistent
48 * user, or any other reason, the host is cached with an expiry
49 * and no log-in attempts will be accepted until that expiry
50 * is reached. The expiry is $this->mLoginThrottle.
51 *
52 * @access public
53 */
54 public function execute() {
55 $params = $this->extractRequestParams();
56
57 $result = array ();
58
59 $req = new FauxRequest(array (
60 'wpName' => $params['name'],
61 'wpPassword' => $params['password'],
62 'wpDomain' => $params['domain'],
63 'wpRemember' => '1'
64 ));
65
66 // Init session if necessary
67 if( session_id() == '' ) {
68 wfSetupSession();
69 }
70
71 $login = new Login( $req );
72 switch ( $authRes = $login->attemptLogin() ) {
73 case Login::SUCCESS :
74 global $wgUser, $wgCookiePrefix;
75
76 $result['result'] = 'Success';
77 $result['lguserid'] = intval($wgUser->getId());
78 $result['lgusername'] = $wgUser->getName();
79 $result['lgtoken'] = $wgUser->getToken();
80 $result['cookieprefix'] = $wgCookiePrefix;
81 $result['sessionid'] = session_id();
82 break;
83
84 case Login::NO_NAME :
85 $result['result'] = 'NoName';
86 break;
87 case Login::ILLEGAL :
88 $result['result'] = 'Illegal';
89 break;
90 case Login::WRONG_PLUGIN_PASS :
91 $result['result'] = 'WrongPluginPass';
92 break;
93 case Login::NOT_EXISTS :
94 $result['result'] = 'NotExists';
95 break;
96 case Login::WRONG_PASS :
97 $result['result'] = 'WrongPass';
98 break;
99 case Login::EMPTY_PASS :
100 $result['result'] = 'EmptyPass';
101 break;
102 case Login::CREATE_BLOCKED :
103 $result['result'] = 'CreateBlocked';
104 $result['details'] = 'Your IP address is blocked from account creation';
105 break;
106 case Login::THROTTLED :
107 global $wgPasswordAttemptThrottle;
108 $result['result'] = 'Throttled';
109 $result['wait'] = intval( $wgPasswordAttemptThrottle['seconds'] );
110 break;
111 default :
112 ApiBase::dieDebug( __METHOD__, "Unhandled case value: {$authRes}" );
113 }
114
115 $this->getResult()->addValue(null, 'login', $result);
116 }
117
118 public function mustBePosted() { return true; }
119
120 public function isReadMode() {
121 return false;
122 }
123
124 public function getAllowedParams() {
125 return array (
126 'name' => null,
127 'password' => null,
128 'domain' => null
129 );
130 }
131
132 public function getParamDescription() {
133 return array (
134 'name' => 'User Name',
135 'password' => 'Password',
136 'domain' => 'Domain (optional)'
137 );
138 }
139
140 public function getDescription() {
141 return array (
142 'This module is used to login and get the authentication tokens. ',
143 'In the event of a successful log-in, a cookie will be attached',
144 'to your session. In the event of a failed log-in, you will not ',
145 'be able to attempt another log-in through this method for 5 seconds.',
146 'This is to prevent password guessing by automated password crackers.'
147 );
148 }
149
150 protected function getExamples() {
151 return array(
152 'api.php?action=login&lgname=user&lgpassword=password'
153 );
154 }
155
156 public function getVersion() {
157 return __CLASS__ . ': $Id$';
158 }
159 }