Merge "Remove old WebResponse::setCookie() calling method"
[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\AuthenticationResponse;
25 use MediaWiki\Auth\CreateFromLoginAuthenticationRequest;
26
27 /**
28 * Log in to the wiki with AuthManager
29 *
30 * @ingroup API
31 */
32 class ApiClientLogin extends ApiBase {
33
34 public function __construct( ApiMain $main, $action ) {
35 parent::__construct( $main, $action, 'login' );
36 }
37
38 public function getFinalDescription() {
39 // A bit of a hack to append 'api-help-authmanager-general-usage'
40 $msgs = parent::getFinalDescription();
41 $msgs[] = ApiBase::makeMessage( 'api-help-authmanager-general-usage', $this->getContext(), [
42 $this->getModulePrefix(),
43 $this->getModuleName(),
44 $this->getModulePath(),
45 AuthManager::ACTION_LOGIN,
46 self::needsToken(),
47 ] );
48 return $msgs;
49 }
50
51 public function execute() {
52 $params = $this->extractRequestParams();
53
54 $this->requireAtLeastOneParameter( $params, 'continue', 'returnurl' );
55
56 if ( $params['returnurl'] !== null ) {
57 $bits = wfParseUrl( $params['returnurl'] );
58 if ( !$bits || $bits['scheme'] === '' ) {
59 $encParamName = $this->encodeParamName( 'returnurl' );
60 $this->dieUsage(
61 "Invalid value '{$params['returnurl']}' for url parameter $encParamName",
62 "badurl_{$encParamName}"
63 );
64 }
65 }
66
67 $helper = new ApiAuthManagerHelper( $this );
68 $manager = AuthManager::singleton();
69
70 // Make sure it's possible to log in
71 if ( !$manager->canAuthenticateNow() ) {
72 $this->getResult()->addValue( null, 'clientlogin', $helper->formatAuthenticationResponse(
73 AuthenticationResponse::newFail( $this->msg( 'userlogin-cannot-' . AuthManager::ACTION_LOGIN ) )
74 ) );
75 return;
76 }
77
78 // Perform the login step
79 if ( $params['continue'] ) {
80 $reqs = $helper->loadAuthenticationRequests( AuthManager::ACTION_LOGIN_CONTINUE );
81 $res = $manager->continueAuthentication( $reqs );
82 } else {
83 $reqs = $helper->loadAuthenticationRequests( AuthManager::ACTION_LOGIN );
84 if ( $params['preservestate'] ) {
85 $req = $helper->getPreservedRequest();
86 if ( $req ) {
87 $reqs[] = $req;
88 }
89 }
90 $res = $manager->beginAuthentication( $reqs, $params['returnurl'] );
91 }
92
93 // Remove CreateFromLoginAuthenticationRequest from $res->neededRequests.
94 // It's there so a RESTART treated as UI will work right, but showing
95 // it to the API client is just confusing.
96 $res->neededRequests = ApiAuthManagerHelper::blacklistAuthenticationRequests(
97 $res->neededRequests, [ CreateFromLoginAuthenticationRequest::class ]
98 );
99
100 $this->getResult()->addValue( null, 'clientlogin',
101 $helper->formatAuthenticationResponse( $res ) );
102 }
103
104 public function isReadMode() {
105 return false;
106 }
107
108 public function needsToken() {
109 return 'login';
110 }
111
112 public function getAllowedParams() {
113 return ApiAuthManagerHelper::getStandardParams( AuthManager::ACTION_LOGIN,
114 'requests', 'messageformat', 'mergerequestfields', 'preservestate', 'returnurl', 'continue'
115 );
116 }
117
118 public function dynamicParameterDocumentation() {
119 return [ 'api-help-authmanagerhelper-additional-params', AuthManager::ACTION_LOGIN ];
120 }
121
122 protected function getExamplesMessages() {
123 return [
124 'action=clientlogin&username=Example&password=ExamplePassword&'
125 . 'loginreturnurl=http://example.org/&logintoken=123ABC'
126 => 'apihelp-clientlogin-example-login',
127 'action=clientlogin&logincontinue=1&OATHToken=987654&logintoken=123ABC'
128 => 'apihelp-clientlogin-example-login2',
129 ];
130 }
131
132 public function getHelpUrls() {
133 return 'https://www.mediawiki.org/wiki/API:Login';
134 }
135 }