Debian Codegraph @

includes/api/ApiClientLogin.php

raw symbols SCIP
1 <?php
2 /**
3 * Copyright © 2016 Wikimedia Foundation and contributors
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 namespace MediaWiki\Api;
24
25 use MediaWiki\Auth\AuthenticationResponse;
26 use MediaWiki\Auth\AuthManager;
27 use MediaWiki\Auth\CreateFromLoginAuthenticationRequest;
28 use MediaWiki\Utils\UrlUtils;
29
30 /**
31 * Log in to the wiki with AuthManager
32 *
33 * @ingroup API
34 */
35 class ApiClientLogin extends ApiBase {
36
37 private AuthManager $authManager;
38 private UrlUtils $urlUtils;
39
40 public function __construct(
41 ApiMain $main,
42 string $action,
43 AuthManager $authManager,
44 UrlUtils $urlUtils
45 ) {
46 parent::__construct( $main, $action, 'login' );
47 $this->authManager = $authManager;
48 $this->urlUtils = $urlUtils;
49 }
50
51 public function getFinalDescription() {
52 // A bit of a hack to append 'api-help-authmanager-general-usage'
53 $msgs = parent::getFinalDescription();
54 $msgs[] = $this->msg( 'api-help-authmanager-general-usage',
55 $this->getModulePrefix(),
56 $this->getModuleName(),
57 $this->getModulePath(),
58 AuthManager::ACTION_LOGIN,
59 $this->needsToken(),
60 );
61 return $msgs;
62 }
63
64 public function execute() {
65 $params = $this->extractRequestParams();
66
67 $this->requireAtLeastOneParameter( $params, 'continue', 'returnurl' );
68
69 if ( $params['returnurl'] !== null ) {
70 $bits = $this->urlUtils->parse( $params['returnurl'] );
71 if ( !$bits || $bits['scheme'] === '' ) {
72 $encParamName = $this->encodeParamName( 'returnurl' );
73 $this->dieWithError(
74 [ 'apierror-badurl', $encParamName, wfEscapeWikiText( $params['returnurl'] ) ],
75 "badurl_{$encParamName}"
76 );
77 }
78 }
79
80 $helper = new ApiAuthManagerHelper( $this, $this->authManager );
81
82 // Make sure it's possible to log in
83 if ( !$this->authManager->canAuthenticateNow() ) {
84 $res = AuthenticationResponse::newFail( $this->msg( 'userlogin-cannot-' . AuthManager::ACTION_LOGIN ) );
85 $this->getResult()->addValue( null, 'clientlogin',
86 $helper->formatAuthenticationResponse( $res ) );
87 $helper->logAuthenticationResult( 'login', $res );
88 return;
89 }
90
91 // Perform the login step
92 if ( $params['continue'] ) {
93 $reqs = $helper->loadAuthenticationRequests( AuthManager::ACTION_LOGIN_CONTINUE );
94 $res = $this->authManager->continueAuthentication( $reqs );
95 } else {
96 $reqs = $helper->loadAuthenticationRequests( AuthManager::ACTION_LOGIN );
97 if ( $params['preservestate'] ) {
98 $req = $helper->getPreservedRequest();
99 if ( $req ) {
100 $reqs[] = $req;
101 }
102 }
103 $res = $this->authManager->beginAuthentication( $reqs, $params['returnurl'] );
104 }
105
106 // Remove CreateFromLoginAuthenticationRequest from $res->neededRequests.
107 // It's there so a RESTART treated as UI will work right, but showing
108 // it to the API client is just confusing.
109 $res->neededRequests = ApiAuthManagerHelper::blacklistAuthenticationRequests(
110 $res->neededRequests, [ CreateFromLoginAuthenticationRequest::class ]
111 );
112
113 $this->getResult()->addValue( null, 'clientlogin',
114 $helper->formatAuthenticationResponse( $res ) );
115 $helper->logAuthenticationResult( 'login', $res );
116 }
117
118 public function isReadMode() {
119 return false;
120 }
121
122 public function isWriteMode() {
123 // (T283394) Logging in triggers some database writes, so should be marked appropriately.
124 return true;
125 }
126
127 public function needsToken() {
128 return 'login';
129 }
130
131 public function getAllowedParams() {
132 return ApiAuthManagerHelper::getStandardParams( AuthManager::ACTION_LOGIN,
133 'requests', 'messageformat', 'mergerequestfields', 'preservestate', 'returnurl', 'continue'
134 );
135 }
136
137 public function dynamicParameterDocumentation() {
138 return [ 'api-help-authmanagerhelper-additional-params', AuthManager::ACTION_LOGIN ];
139 }
140
141 protected function getExamplesMessages() {
142 return [
143 'action=clientlogin&username=Example&password=ExamplePassword&'
144 . 'loginreturnurl=http://example.org/&logintoken=123ABC'
145 => 'apihelp-clientlogin-example-login',
146 'action=clientlogin&logincontinue=1&OATHToken=987654&logintoken=123ABC'
147 => 'apihelp-clientlogin-example-login2',
148 ];
149 }
150
151 public function getHelpUrls() {
152 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Login';
153 }
154 }
155
156 /** @deprecated class alias since 1.43 */
157 class_alias( ApiClientLogin::class, 'ApiClientLogin' );