MediaWiki  REL1_24
ApiLogin.php
Go to the documentation of this file.
00001 <?php
00033 class ApiLogin extends ApiBase {
00034 
00035     public function __construct( ApiMain $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 
00056             return;
00057         }
00058 
00059         $params = $this->extractRequestParams();
00060 
00061         $result = array();
00062 
00063         // Init session if necessary
00064         if ( session_id() == '' ) {
00065             wfSetupSession();
00066         }
00067 
00068         $context = new DerivativeContext( $this->getContext() );
00069         $context->setRequest( new DerivativeRequest(
00070             $this->getContext()->getRequest(),
00071             array(
00072                 'wpName' => $params['name'],
00073                 'wpPassword' => $params['password'],
00074                 'wpDomain' => $params['domain'],
00075                 'wpLoginToken' => $params['token'],
00076                 'wpRemember' => ''
00077             )
00078         ) );
00079         $loginForm = new LoginForm();
00080         $loginForm->setContext( $context );
00081 
00082         $authRes = $loginForm->authenticateUserData();
00083         switch ( $authRes ) {
00084             case LoginForm::SUCCESS:
00085                 $user = $context->getUser();
00086                 $this->getContext()->setUser( $user );
00087                 $user->setCookies( $this->getRequest(), null, true );
00088 
00089                 ApiQueryInfo::resetTokenCache();
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'] = $this->getConfig()->get( 'CookiePrefix' );
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'] = $this->getConfig()->get( 'CookiePrefix' );
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             // bug 20223 - Treat a temporary password as wrong. Per SpecialUserLogin:
00133             // The e-mailed temporary password should not be used for actual logins.
00134             case LoginForm::RESET_PASS:
00135             case LoginForm::WRONG_PASS:
00136                 $result['result'] = 'WrongPass';
00137                 break;
00138 
00139             case LoginForm::EMPTY_PASS:
00140                 $result['result'] = 'EmptyPass';
00141                 break;
00142 
00143             case LoginForm::CREATE_BLOCKED:
00144                 $result['result'] = 'CreateBlocked';
00145                 $result['details'] = 'Your IP address is blocked from account creation';
00146                 break;
00147 
00148             case LoginForm::THROTTLED:
00149                 $result['result'] = 'Throttled';
00150                 $throttle = $this->getConfig()->get( 'PasswordAttemptThrottle' );
00151                 $result['wait'] = intval( $throttle['seconds'] );
00152                 break;
00153 
00154             case LoginForm::USER_BLOCKED:
00155                 $result['result'] = 'Blocked';
00156                 break;
00157 
00158             case LoginForm::ABORTED:
00159                 $result['result'] = 'Aborted';
00160                 $result['reason'] = $loginForm->mAbortLoginErrorMsg;
00161                 break;
00162 
00163             default:
00164                 ApiBase::dieDebug( __METHOD__, "Unhandled case value: {$authRes}" );
00165         }
00166 
00167         $this->getResult()->addValue( null, 'login', $result );
00168     }
00169 
00170     public function mustBePosted() {
00171         return true;
00172     }
00173 
00174     public function isReadMode() {
00175         return false;
00176     }
00177 
00178     public function getAllowedParams() {
00179         return array(
00180             'name' => null,
00181             'password' => null,
00182             'domain' => null,
00183             'token' => null,
00184         );
00185     }
00186 
00187     public function getParamDescription() {
00188         return array(
00189             'name' => 'User Name',
00190             'password' => 'Password',
00191             'domain' => 'Domain (optional)',
00192             'token' => 'Login token obtained in first request',
00193         );
00194     }
00195 
00196     public function getDescription() {
00197         return array(
00198             'Log in and get the authentication tokens.',
00199             'In the event of a successful log-in, a cookie will be attached to your session.',
00200             'In the event of a failed log-in, you will not be able to attempt another log-in',
00201             'through this method for 5 seconds. This is to prevent password guessing by',
00202             'automated password crackers.'
00203         );
00204     }
00205 
00206     public function getExamples() {
00207         return array(
00208             'api.php?action=login&lgname=user&lgpassword=password'
00209         );
00210     }
00211 
00212     public function getHelpUrls() {
00213         return 'https://www.mediawiki.org/wiki/API:Login';
00214     }
00215 }