Merge "Allow minor edits to be filtered out of Special:Contributions"
[lhc/web/wiklou.git] / includes / api / ApiClientLogin.php
1 <?php
2 /**
3 * Copyright © 2016 Brad Jorsch <bjorsch@wikimedia.org>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 */
22
23 use MediaWiki\Auth\AuthManager;
24 use MediaWiki\Auth\AuthenticationRequest;
25 use MediaWiki\Auth\AuthenticationResponse;
26 use MediaWiki\Auth\CreateFromLoginAuthenticationRequest;
27
28 /**
29 * Log in to the wiki with AuthManager
30 *
31 * @ingroup API
32 */
33 class ApiClientLogin extends ApiBase {
34
35 public function __construct( ApiMain $main, $action ) {
36 parent::__construct( $main, $action, 'login' );
37 }
38
39 public function getFinalDescription() {
40 // A bit of a hack to append 'api-help-authmanager-general-usage'
41 $msgs = parent::getFinalDescription();
42 $msgs[] = ApiBase::makeMessage( 'api-help-authmanager-general-usage', $this->getContext(), [
43 $this->getModulePrefix(),
44 $this->getModuleName(),
45 $this->getModulePath(),
46 AuthManager::ACTION_LOGIN,
47 self::needsToken(),
48 ] );
49 return $msgs;
50 }
51
52 public function execute() {
53 $params = $this->extractRequestParams();
54
55 $this->requireAtLeastOneParameter( $params, 'continue', 'returnurl' );
56
57 if ( $params['returnurl'] !== null ) {
58 $bits = wfParseUrl( $params['returnurl'] );
59 if ( !$bits || $bits['scheme'] === '' ) {
60 $encParamName = $this->encodeParamName( 'returnurl' );
61 $this->dieUsage(
62 "Invalid value '{$params['returnurl']}' for url parameter $encParamName",
63 "badurl_{$encParamName}"
64 );
65 }
66 }
67
68 $helper = new ApiAuthManagerHelper( $this );
69 $manager = AuthManager::singleton();
70
71 // Make sure it's possible to log in
72 if ( !$manager->canAuthenticateNow() ) {
73 $this->getResult()->addValue( null, 'clientlogin', $helper->formatAuthenticationResponse(
74 AuthenticationResponse::newFail( $this->msg( 'userlogin-cannot-' . AuthManager::ACTION_LOGIN ) )
75 ) );
76 return;
77 }
78
79 // Perform the login step
80 if ( $params['continue'] ) {
81 $reqs = $helper->loadAuthenticationRequests( AuthManager::ACTION_LOGIN_CONTINUE );
82 $res = $manager->continueAuthentication( $reqs );
83 } else {
84 $reqs = $helper->loadAuthenticationRequests( AuthManager::ACTION_LOGIN );
85 if ( $params['preservestate'] ) {
86 $req = $helper->getPreservedRequest();
87 if ( $req ) {
88 $reqs[] = $req;
89 }
90 }
91 $res = $manager->beginAuthentication( $reqs, $params['returnurl'] );
92 }
93
94 // Remove CreateFromLoginAuthenticationRequest from $res->neededRequests.
95 // It's there so a RESTART treated as UI will work right, but showing
96 // it to the API client is just confusing.
97 $res->neededRequests = ApiAuthManagerHelper::blacklistAuthenticationRequests(
98 $res->neededRequests, [ CreateFromLoginAuthenticationRequest::class ]
99 );
100
101 $this->getResult()->addValue( null, 'clientlogin',
102 $helper->formatAuthenticationResponse( $res ) );
103 }
104
105 public function isReadMode() {
106 return false;
107 }
108
109 public function needsToken() {
110 return 'login';
111 }
112
113 public function getAllowedParams() {
114 return ApiAuthManagerHelper::getStandardParams( AuthManager::ACTION_LOGIN,
115 'requests', 'messageformat', 'mergerequestfields', 'preservestate', 'returnurl', 'continue'
116 );
117 }
118
119 public function dynamicParameterDocumentation() {
120 return [ 'api-help-authmanagerhelper-additional-params', AuthManager::ACTION_LOGIN ];
121 }
122
123 protected function getExamplesMessages() {
124 return [
125 'action=clientlogin&username=Example&password=ExamplePassword&'
126 . 'loginreturnurl=http://example.org/&logintoken=123ABC'
127 => 'apihelp-clientlogin-example-login',
128 'action=clientlogin&logincontinue=1&OATHToken=987654&logintoken=123ABC'
129 => 'apihelp-clientlogin-example-login2',
130 ];
131 }
132
133 public function getHelpUrls() {
134 return 'https://www.mediawiki.org/wiki/API:Login';
135 }
136 }