MediaWiki  REL1_19
ApiLogin.php
Go to the documentation of this file.
00001 <?php
00033 class ApiLogin extends ApiBase {
00034 
00035         public function __construct( $main, $action ) {
00036                 parent::__construct( $main, $action, 'lg' );
00037         }
00038 
00048         public function execute() {
00049                 // If we're in JSON callback mode, no tokens can be obtained
00050                 if ( !is_null( $this->getMain()->getRequest()->getVal( 'callback' ) ) ) {
00051                         $this->getResult()->addValue( null, 'login', array(
00052                                 'result' => 'Aborted',
00053                                 'reason' => 'Cannot log in when using a callback',
00054                         ) );
00055                         return;
00056                 }
00057 
00058                 $params = $this->extractRequestParams();
00059 
00060                 $result = array();
00061 
00062                 // Init session if necessary
00063                 if ( session_id() == '' ) {
00064                         wfSetupSession();
00065                 }
00066 
00067                 $context = new DerivativeContext( $this->getContext() );
00068                 $context->setRequest( new DerivativeRequest(
00069                         $this->getContext()->getRequest(),
00070                         array(
00071                                 'wpName' => $params['name'],
00072                                 'wpPassword' => $params['password'],
00073                                 'wpDomain' => $params['domain'],
00074                                 'wpLoginToken' => $params['token'],
00075                                 'wpRemember' => ''
00076                         )
00077                 ) );
00078                 $loginForm = new LoginForm();
00079                 $loginForm->setContext( $context );
00080 
00081                 global $wgCookiePrefix, $wgPasswordAttemptThrottle;
00082 
00083                 $authRes = $loginForm->authenticateUserData();
00084                 switch ( $authRes ) {
00085                         case LoginForm::SUCCESS:
00086                                 $user = $context->getUser();
00087                                 $this->getContext()->setUser( $user );
00088                                 $user->setOption( 'rememberpassword', 1 );
00089                                 $user->setCookies( $this->getRequest() );
00090 
00091                                 // Run hooks.
00092                                 // @todo FIXME: Split back and frontend from this hook.
00093                                 // @todo FIXME: This hook should be placed in the backend
00094                                 $injected_html = '';
00095                                 wfRunHooks( 'UserLoginComplete', array( &$user, &$injected_html ) );
00096 
00097                                 $result['result'] = 'Success';
00098                                 $result['lguserid'] = intval( $user->getId() );
00099                                 $result['lgusername'] = $user->getName();
00100                                 $result['lgtoken'] = $user->getToken();
00101                                 $result['cookieprefix'] = $wgCookiePrefix;
00102                                 $result['sessionid'] = session_id();
00103                                 break;
00104 
00105                         case LoginForm::NEED_TOKEN:
00106                                 $result['result'] = 'NeedToken';
00107                                 $result['token'] = $loginForm->getLoginToken();
00108                                 $result['cookieprefix'] = $wgCookiePrefix;
00109                                 $result['sessionid'] = session_id();
00110                                 break;
00111 
00112                         case LoginForm::WRONG_TOKEN:
00113                                 $result['result'] = 'WrongToken';
00114                                 break;
00115 
00116                         case LoginForm::NO_NAME:
00117                                 $result['result'] = 'NoName';
00118                                 break;
00119 
00120                         case LoginForm::ILLEGAL:
00121                                 $result['result'] = 'Illegal';
00122                                 break;
00123 
00124                         case LoginForm::WRONG_PLUGIN_PASS:
00125                                 $result['result'] = 'WrongPluginPass';
00126                                 break;
00127 
00128                         case LoginForm::NOT_EXISTS:
00129                                 $result['result'] = 'NotExists';
00130                                 break;
00131 
00132                         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;"
00133                         case LoginForm::WRONG_PASS:
00134                                 $result['result'] = 'WrongPass';
00135                                 break;
00136 
00137                         case LoginForm::EMPTY_PASS:
00138                                 $result['result'] = 'EmptyPass';
00139                                 break;
00140 
00141                         case LoginForm::CREATE_BLOCKED:
00142                                 $result['result'] = 'CreateBlocked';
00143                                 $result['details'] = 'Your IP address is blocked from account creation';
00144                                 break;
00145 
00146                         case LoginForm::THROTTLED:
00147                                 $result['result'] = 'Throttled';
00148                                 $result['wait'] = intval( $wgPasswordAttemptThrottle['seconds'] );
00149                                 break;
00150 
00151                         case LoginForm::USER_BLOCKED:
00152                                 $result['result'] = 'Blocked';
00153                                 break;
00154 
00155                         case LoginForm::ABORTED:
00156                                 $result['result'] = 'Aborted';
00157                                 $result['reason'] =  $loginForm->mAbortLoginErrorMsg;
00158                                 break;
00159 
00160                         default:
00161                                 ApiBase::dieDebug( __METHOD__, "Unhandled case value: {$authRes}" );
00162                 }
00163 
00164                 $this->getResult()->addValue( null, 'login', $result );
00165         }
00166 
00167         public function mustBePosted() {
00168                 return true;
00169         }
00170 
00171         public function isReadMode() {
00172                 return false;
00173         }
00174 
00175         public function getAllowedParams() {
00176                 return array(
00177                         'name' => null,
00178                         'password' => null,
00179                         'domain' => null,
00180                         'token' => null,
00181                 );
00182         }
00183 
00184         public function getParamDescription() {
00185                 return array(
00186                         'name' => 'User Name',
00187                         'password' => 'Password',
00188                         'domain' => 'Domain (optional)',
00189                         'token' => 'Login token obtained in first request',
00190                 );
00191         }
00192 
00193         public function getDescription() {
00194                 return array(
00195                         'Log in and get the authentication tokens. ',
00196                         'In the event of a successful log-in, a cookie will be attached',
00197                         'to your session. In the event of a failed log-in, you will not ',
00198                         'be able to attempt another log-in through this method for 5 seconds.',
00199                         'This is to prevent password guessing by automated password crackers'
00200                 );
00201         }
00202 
00203         public function getPossibleErrors() {
00204                 return array_merge( parent::getPossibleErrors(), array(
00205                         array( 'code' => 'NeedToken', 'info' => 'You need to resubmit your login with the specified token. See https://bugzilla.wikimedia.org/show_bug.cgi?id=23076' ),
00206                         array( 'code' => 'WrongToken', 'info' => 'You specified an invalid token' ),
00207                         array( 'code' => 'NoName', 'info' => 'You didn\'t set the lgname parameter' ),
00208                         array( 'code' => 'Illegal', 'info' => ' You provided an illegal username' ),
00209                         array( 'code' => 'NotExists', 'info' => ' The username you provided doesn\'t exist' ),
00210                         array( 'code' => 'EmptyPass', 'info' => ' You didn\'t set the lgpassword parameter or you left it empty' ),
00211                         array( 'code' => 'WrongPass', 'info' => ' The password you provided is incorrect' ),
00212                         array( 'code' => 'WrongPluginPass', 'info' => 'Same as "WrongPass", returned when an authentication plugin rather than MediaWiki itself rejected the password' ),
00213                         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' ),
00214                         array( 'code' => 'Throttled', 'info' => 'You\'ve logged in too many times in a short time' ),
00215                         array( 'code' => 'Blocked', 'info' => 'User is blocked' ),
00216                 ) );
00217         }
00218 
00219         public function getExamples() {
00220                 return array(
00221                         'api.php?action=login&lgname=user&lgpassword=password'
00222                 );
00223         }
00224 
00225         public function getHelpUrls() {
00226                 return 'https://www.mediawiki.org/wiki/API:Login';
00227         }
00228 
00229         public function getVersion() {
00230                 return __CLASS__ . ': $Id$';
00231         }
00232 }