]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - includes/api/ApiLogin.php
MediaWiki 1.17.4
[autoinstalls/mediawiki.git] / includes / api / ApiLogin.php
1 <?php
2 /**
3  * API for MediaWiki 1.8+
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 if ( !defined( 'MEDIAWIKI' ) ) {
29         // Eclipse helper - will be ignored in production
30         require_once( 'ApiBase.php' );
31 }
32
33 /**
34  * Unit to authenticate log-in attempts to the current wiki.
35  *
36  * @ingroup API
37  */
38 class ApiLogin extends ApiBase {
39
40         public function __construct( $main, $action ) {
41                 parent::__construct( $main, $action, 'lg' );
42         }
43
44         /**
45          * Executes the log-in attempt using the parameters passed. If
46          * the log-in succeeeds, it attaches a cookie to the session
47          * and outputs the user id, username, and session token. If a
48          * log-in fails, as the result of a bad password, a nonexistent
49          * user, or any other reason, the host is cached with an expiry
50          * and no log-in attempts will be accepted until that expiry
51          * is reached. The expiry is $this->mLoginThrottle.
52          */
53         public function execute() {
54                 $params = $this->extractRequestParams();
55
56                 $result = array();
57
58                 $req = new FauxRequest( array(
59                         'wpName' => $params['name'],
60                         'wpPassword' => $params['password'],
61                         'wpDomain' => $params['domain'],
62                         'wpLoginToken' => $params['token'],
63                         'wpRemember' => ''
64                 ) );
65
66                 // Init session if necessary
67                 if ( session_id() == '' ) {
68                         wfSetupSession();
69                 }
70
71                 $loginForm = new LoginForm( $req );
72
73                 global $wgCookiePrefix, $wgUser, $wgPasswordAttemptThrottle;
74
75                 switch ( $authRes = $loginForm->authenticateUserData() ) {
76                         case LoginForm::SUCCESS:
77                                 $wgUser->setOption( 'rememberpassword', 1 );
78                                 $wgUser->setCookies();
79
80                                 // Run hooks. FIXME: split back and frontend from this hook.
81                                 // FIXME: This hook should be placed in the backend
82                                 $injected_html = '';
83                                 wfRunHooks( 'UserLoginComplete', array( &$wgUser, &$injected_html ) );
84
85                                 $result['result'] = 'Success';
86                                 $result['lguserid'] = intval( $wgUser->getId() );
87                                 $result['lgusername'] = $wgUser->getName();
88                                 $result['lgtoken'] = $wgUser->getToken();
89                                 $result['cookieprefix'] = $wgCookiePrefix;
90                                 $result['sessionid'] = session_id();
91                                 break;
92
93                         case LoginForm::NEED_TOKEN:
94                                 $result['result'] = 'NeedToken';
95                                 $result['token'] = $loginForm->getLoginToken();
96                                 $result['cookieprefix'] = $wgCookiePrefix;
97                                 $result['sessionid'] = session_id();
98                                 break;
99
100                         case LoginForm::WRONG_TOKEN:
101                                 $result['result'] = 'WrongToken';
102                                 break;
103
104                         case LoginForm::NO_NAME:
105                                 $result['result'] = 'NoName';
106                                 break;
107
108                         case LoginForm::ILLEGAL:
109                                 $result['result'] = 'Illegal';
110                                 break;
111
112                         case LoginForm::WRONG_PLUGIN_PASS:
113                                 $result['result'] = 'WrongPluginPass';
114                                 break;
115
116                         case LoginForm::NOT_EXISTS:
117                                 $result['result'] = 'NotExists';
118                                 break;
119
120                         case LoginForm::RESET_PASS: // bug 20223 - Treat a temporary password as wrong. Per SpecialUserLogin - "The e-mailed temporary password should not be used for actual logins;"
121                         case LoginForm::WRONG_PASS:
122                                 $result['result'] = 'WrongPass';
123                                 break;
124
125                         case LoginForm::EMPTY_PASS:
126                                 $result['result'] = 'EmptyPass';
127                                 break;
128
129                         case LoginForm::CREATE_BLOCKED:
130                                 $result['result'] = 'CreateBlocked';
131                                 $result['details'] = 'Your IP address is blocked from account creation';
132                                 break;
133
134                         case LoginForm::THROTTLED:
135                                 $result['result'] = 'Throttled';
136                                 $result['wait'] = intval( $wgPasswordAttemptThrottle['seconds'] );
137                                 break;
138
139                         case LoginForm::USER_BLOCKED:
140                                 $result['result'] = 'Blocked';
141                                 break;
142
143                         default:
144                                 ApiBase::dieDebug( __METHOD__, "Unhandled case value: {$authRes}" );
145                 }
146
147                 $this->getResult()->addValue( null, 'login', $result );
148         }
149
150         public function mustBePosted() {
151                 return true;
152         }
153
154         public function isReadMode() {
155                 return false;
156         }
157
158         public function getAllowedParams() {
159                 return array(
160                         'name' => null,
161                         'password' => null,
162                         'domain' => null,
163                         'token' => null,
164                 );
165         }
166
167         public function getParamDescription() {
168                 return array(
169                         'name' => 'User Name',
170                         'password' => 'Password',
171                         'domain' => 'Domain (optional)',
172                         'token' => 'Login token obtained in first request',
173                 );
174         }
175
176         public function getDescription() {
177                 return array(
178                         'This module is used to login and get the authentication tokens. ',
179                         'In the event of a successful log-in, a cookie will be attached',
180                         'to your session. In the event of a failed log-in, you will not ',
181                         'be able to attempt another log-in through this method for 5 seconds.',
182                         'This is to prevent password guessing by automated password crackers'
183                 );
184         }
185
186         public function getPossibleErrors() {
187                 return array_merge( parent::getPossibleErrors(), array(
188                         array( 'code' => 'NeedToken', 'info' => 'You need to resubmit your login with the specified token. See https://bugzilla.wikimedia.org/show_bug.cgi?id=23076' ),
189                         array( 'code' => 'WrongToken', 'info' => 'You specified an invalid token' ),
190                         array( 'code' => 'NoName', 'info' => 'You didn\'t set the lgname parameter' ),
191                         array( 'code' => 'Illegal', 'info' => ' You provided an illegal username' ),
192                         array( 'code' => 'NotExists', 'info' => ' The username you provided doesn\'t exist' ),
193                         array( 'code' => 'EmptyPass', 'info' => ' You didn\'t set the lgpassword parameter or you left it empty' ),
194                         array( 'code' => 'WrongPass', 'info' => ' The password you provided is incorrect' ),
195                         array( 'code' => 'WrongPluginPass', 'info' => 'Same as `WrongPass", returned when an authentication plugin rather than MediaWiki itself rejected the password' ),
196                         array( 'code' => 'CreateBlocked', 'info' => 'The wiki tried to automatically create a new account for you, but your IP address has been blocked from account creation' ),
197                         array( 'code' => 'Throttled', 'info' => 'You\'ve logged in too many times in a short time' ),
198                         array( 'code' => 'Blocked', 'info' => 'User is blocked' ),
199                 ) );
200         }
201
202         protected function getExamples() {
203                 return array(
204                         'api.php?action=login&lgname=user&lgpassword=password'
205                 );
206         }
207
208         public function getVersion() {
209                 return __CLASS__ . ': $Id$';
210         }
211 }