PageRenderTime 40ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/mediawiki-1.21.2/includes/api/ApiLogin.php

https://gitlab.com/mcepl/dumpathome
PHP | 290 lines | 211 code | 35 blank | 44 comment | 4 complexity | 5c8adffffeee0d971ab5282adab4b97d MD5 | raw file
Possible License(s): GPL-2.0, Apache-2.0, LGPL-3.0
  1. <?php
  2. /**
  3. *
  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. * Unit to authenticate log-in attempts to the current wiki.
  29. *
  30. * @ingroup API
  31. */
  32. class ApiLogin extends ApiBase {
  33. public function __construct( $main, $action ) {
  34. parent::__construct( $main, $action, 'lg' );
  35. }
  36. /**
  37. * Executes the log-in attempt using the parameters passed. If
  38. * the log-in succeeds, it attaches a cookie to the session
  39. * and outputs the user id, username, and session token. If a
  40. * log-in fails, as the result of a bad password, a nonexistent
  41. * user, or any other reason, the host is cached with an expiry
  42. * and no log-in attempts will be accepted until that expiry
  43. * is reached. The expiry is $this->mLoginThrottle.
  44. */
  45. public function execute() {
  46. // If we're in JSON callback mode, no tokens can be obtained
  47. if ( !is_null( $this->getMain()->getRequest()->getVal( 'callback' ) ) ) {
  48. $this->getResult()->addValue( null, 'login', array(
  49. 'result' => 'Aborted',
  50. 'reason' => 'Cannot log in when using a callback',
  51. ) );
  52. return;
  53. }
  54. $params = $this->extractRequestParams();
  55. $result = array();
  56. // Init session if necessary
  57. if ( session_id() == '' ) {
  58. wfSetupSession();
  59. }
  60. $context = new DerivativeContext( $this->getContext() );
  61. $context->setRequest( new DerivativeRequest(
  62. $this->getContext()->getRequest(),
  63. array(
  64. 'wpName' => $params['name'],
  65. 'wpPassword' => $params['password'],
  66. 'wpDomain' => $params['domain'],
  67. 'wpLoginToken' => $params['token'],
  68. 'wpRemember' => ''
  69. )
  70. ) );
  71. $loginForm = new LoginForm();
  72. $loginForm->setContext( $context );
  73. global $wgCookiePrefix, $wgPasswordAttemptThrottle;
  74. $authRes = $loginForm->authenticateUserData();
  75. switch ( $authRes ) {
  76. case LoginForm::SUCCESS:
  77. $user = $context->getUser();
  78. $this->getContext()->setUser( $user );
  79. $user->setOption( 'rememberpassword', 1 );
  80. $user->setCookies( $this->getRequest() );
  81. ApiQueryInfo::resetTokenCache();
  82. // Run hooks.
  83. // @todo FIXME: Split back and frontend from this hook.
  84. // @todo FIXME: This hook should be placed in the backend
  85. $injected_html = '';
  86. wfRunHooks( 'UserLoginComplete', array( &$user, &$injected_html ) );
  87. $result['result'] = 'Success';
  88. $result['lguserid'] = intval( $user->getId() );
  89. $result['lgusername'] = $user->getName();
  90. $result['lgtoken'] = $user->getToken();
  91. $result['cookieprefix'] = $wgCookiePrefix;
  92. $result['sessionid'] = session_id();
  93. break;
  94. case LoginForm::NEED_TOKEN:
  95. $result['result'] = 'NeedToken';
  96. $result['token'] = $loginForm->getLoginToken();
  97. $result['cookieprefix'] = $wgCookiePrefix;
  98. $result['sessionid'] = session_id();
  99. break;
  100. case LoginForm::WRONG_TOKEN:
  101. $result['result'] = 'WrongToken';
  102. break;
  103. case LoginForm::NO_NAME:
  104. $result['result'] = 'NoName';
  105. break;
  106. case LoginForm::ILLEGAL:
  107. $result['result'] = 'Illegal';
  108. break;
  109. case LoginForm::WRONG_PLUGIN_PASS:
  110. $result['result'] = 'WrongPluginPass';
  111. break;
  112. case LoginForm::NOT_EXISTS:
  113. $result['result'] = 'NotExists';
  114. break;
  115. 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;"
  116. case LoginForm::WRONG_PASS:
  117. $result['result'] = 'WrongPass';
  118. break;
  119. case LoginForm::EMPTY_PASS:
  120. $result['result'] = 'EmptyPass';
  121. break;
  122. case LoginForm::CREATE_BLOCKED:
  123. $result['result'] = 'CreateBlocked';
  124. $result['details'] = 'Your IP address is blocked from account creation';
  125. break;
  126. case LoginForm::THROTTLED:
  127. $result['result'] = 'Throttled';
  128. $result['wait'] = intval( $wgPasswordAttemptThrottle['seconds'] );
  129. break;
  130. case LoginForm::USER_BLOCKED:
  131. $result['result'] = 'Blocked';
  132. break;
  133. case LoginForm::ABORTED:
  134. $result['result'] = 'Aborted';
  135. $result['reason'] = $loginForm->mAbortLoginErrorMsg;
  136. break;
  137. default:
  138. ApiBase::dieDebug( __METHOD__, "Unhandled case value: {$authRes}" );
  139. }
  140. $this->getResult()->addValue( null, 'login', $result );
  141. }
  142. public function mustBePosted() {
  143. return true;
  144. }
  145. public function isReadMode() {
  146. return false;
  147. }
  148. public function getAllowedParams() {
  149. return array(
  150. 'name' => null,
  151. 'password' => null,
  152. 'domain' => null,
  153. 'token' => null,
  154. );
  155. }
  156. public function getParamDescription() {
  157. return array(
  158. 'name' => 'User Name',
  159. 'password' => 'Password',
  160. 'domain' => 'Domain (optional)',
  161. 'token' => 'Login token obtained in first request',
  162. );
  163. }
  164. public function getResultProperties() {
  165. return array(
  166. '' => array(
  167. 'result' => array(
  168. ApiBase::PROP_TYPE => array(
  169. 'Success',
  170. 'NeedToken',
  171. 'WrongToken',
  172. 'NoName',
  173. 'Illegal',
  174. 'WrongPluginPass',
  175. 'NotExists',
  176. 'WrongPass',
  177. 'EmptyPass',
  178. 'CreateBlocked',
  179. 'Throttled',
  180. 'Blocked',
  181. 'Aborted'
  182. )
  183. ),
  184. 'lguserid' => array(
  185. ApiBase::PROP_TYPE => 'integer',
  186. ApiBase::PROP_NULLABLE => true
  187. ),
  188. 'lgusername' => array(
  189. ApiBase::PROP_TYPE => 'string',
  190. ApiBase::PROP_NULLABLE => true
  191. ),
  192. 'lgtoken' => array(
  193. ApiBase::PROP_TYPE => 'string',
  194. ApiBase::PROP_NULLABLE => true
  195. ),
  196. 'cookieprefix' => array(
  197. ApiBase::PROP_TYPE => 'string',
  198. ApiBase::PROP_NULLABLE => true
  199. ),
  200. 'sessionid' => array(
  201. ApiBase::PROP_TYPE => 'string',
  202. ApiBase::PROP_NULLABLE => true
  203. ),
  204. 'token' => array(
  205. ApiBase::PROP_TYPE => 'string',
  206. ApiBase::PROP_NULLABLE => true
  207. ),
  208. 'details' => array(
  209. ApiBase::PROP_TYPE => 'string',
  210. ApiBase::PROP_NULLABLE => true
  211. ),
  212. 'wait' => array(
  213. ApiBase::PROP_TYPE => 'integer',
  214. ApiBase::PROP_NULLABLE => true
  215. ),
  216. 'reason' => array(
  217. ApiBase::PROP_TYPE => 'string',
  218. ApiBase::PROP_NULLABLE => true
  219. )
  220. )
  221. );
  222. }
  223. public function getDescription() {
  224. return array(
  225. 'Log in and get the authentication tokens. ',
  226. 'In the event of a successful log-in, a cookie will be attached',
  227. 'to your session. In the event of a failed log-in, you will not ',
  228. 'be able to attempt another log-in through this method for 5 seconds.',
  229. 'This is to prevent password guessing by automated password crackers'
  230. );
  231. }
  232. public function getPossibleErrors() {
  233. return array_merge( parent::getPossibleErrors(), array(
  234. array( 'code' => 'NeedToken', 'info' => 'You need to resubmit your login with the specified token. See https://bugzilla.wikimedia.org/show_bug.cgi?id=23076' ),
  235. array( 'code' => 'WrongToken', 'info' => 'You specified an invalid token' ),
  236. array( 'code' => 'NoName', 'info' => 'You didn\'t set the lgname parameter' ),
  237. array( 'code' => 'Illegal', 'info' => ' You provided an illegal username' ),
  238. array( 'code' => 'NotExists', 'info' => ' The username you provided doesn\'t exist' ),
  239. array( 'code' => 'EmptyPass', 'info' => ' You didn\'t set the lgpassword parameter or you left it empty' ),
  240. array( 'code' => 'WrongPass', 'info' => ' The password you provided is incorrect' ),
  241. array( 'code' => 'WrongPluginPass', 'info' => 'Same as "WrongPass", returned when an authentication plugin rather than MediaWiki itself rejected the password' ),
  242. 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' ),
  243. array( 'code' => 'Throttled', 'info' => 'You\'ve logged in too many times in a short time' ),
  244. array( 'code' => 'Blocked', 'info' => 'User is blocked' ),
  245. ) );
  246. }
  247. public function getExamples() {
  248. return array(
  249. 'api.php?action=login&lgname=user&lgpassword=password'
  250. );
  251. }
  252. public function getHelpUrls() {
  253. return 'https://www.mediawiki.org/wiki/API:Login';
  254. }
  255. }