summaryrefslogtreecommitdiff
path: root/www/wiki/includes/auth/LegacyHookPreAuthenticationProvider.php
blob: 95fe3ab85283d3362e331247d1693dd2695a2fa6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<?php
/**
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 * http://www.gnu.org/copyleft/gpl.html
 *
 * @file
 * @ingroup Auth
 */

namespace MediaWiki\Auth;

use LoginForm;
use StatusValue;
use User;

/**
 * A pre-authentication provider to call some legacy hooks.
 * @ingroup Auth
 * @since 1.27
 * @deprecated since 1.27
 */
class LegacyHookPreAuthenticationProvider extends AbstractPreAuthenticationProvider {

	public function testForAuthentication( array $reqs ) {
		$req = AuthenticationRequest::getRequestByClass( $reqs, PasswordAuthenticationRequest::class );
		if ( $req ) {
			$user = User::newFromName( $req->username );
			$password = $req->password;
		} else {
			$user = null;
			foreach ( $reqs as $req ) {
				if ( $req->username !== null ) {
					$user = User::newFromName( $req->username );
					break;
				}
			}
			if ( !$user ) {
				$this->logger->debug( __METHOD__ . ': No username in $reqs, skipping hooks' );
				return StatusValue::newGood();
			}

			// Something random for the 'AbortLogin' hook.
			$password = wfRandomString( 32 );
		}

		$msg = null;
		if ( !\Hooks::run( 'LoginUserMigrated', [ $user, &$msg ] ) ) {
			return $this->makeFailResponse(
				$user, LoginForm::USER_MIGRATED, $msg, 'LoginUserMigrated'
			);
		}

		$abort = LoginForm::ABORTED;
		$msg = null;
		if ( !\Hooks::run( 'AbortLogin', [ $user, $password, &$abort, &$msg ] ) ) {
			return $this->makeFailResponse( $user, $abort, $msg, 'AbortLogin' );
		}

		return StatusValue::newGood();
	}

	public function testForAccountCreation( $user, $creator, array $reqs ) {
		$abortError = '';
		$abortStatus = null;
		if ( !\Hooks::run( 'AbortNewAccount', [ $user, &$abortError, &$abortStatus ] ) ) {
			// Hook point to add extra creation throttles and blocks
			$this->logger->debug( __METHOD__ . ': a hook blocked creation' );
			if ( $abortStatus === null ) {
				// Report back the old string as a raw message status.
				// This will report the error back as 'createaccount-hook-aborted'
				// with the given string as the message.
				// To return a different error code, return a StatusValue object.
				$msg = wfMessage( 'createaccount-hook-aborted' )->rawParams( $abortError );
				return StatusValue::newFatal( $msg );
			} else {
				// For MediaWiki 1.23+ and updated hooks, return the Status object
				// returned from the hook.
				$ret = StatusValue::newGood();
				$ret->merge( $abortStatus );
				return $ret;
			}
		}

		return StatusValue::newGood();
	}

	public function testUserForCreation( $user, $autocreate, array $options = [] ) {
		if ( $autocreate !== false ) {
			$abortError = '';
			if ( !\Hooks::run( 'AbortAutoAccount', [ $user, &$abortError ] ) ) {
				// Hook point to add extra creation throttles and blocks
				$this->logger->debug( __METHOD__ . ": a hook blocked auto-creation: $abortError\n" );
				return $this->makeFailResponse(
					$user, LoginForm::ABORTED, $abortError, 'AbortAutoAccount'
				);
			}
		}

		return StatusValue::newGood();
	}

	/**
	 * Construct an appropriate failure response
	 * @param User $user
	 * @param int $constant One of the LoginForm::… constants
	 * @param string|null $msg Optional message key, will be derived from $constant otherwise
	 * @param string $hook Name of the hook for error logging and exception messages
	 * @return StatusValue
	 */
	private function makeFailResponse( User $user, $constant, $msg, $hook ) {
		switch ( $constant ) {
			case LoginForm::SUCCESS:
				// WTF?
				$this->logger->debug( "$hook is SUCCESS?!" );
				return StatusValue::newGood();

			case LoginForm::NEED_TOKEN:
				return StatusValue::newFatal( $msg ?: 'nocookiesforlogin' );

			case LoginForm::WRONG_TOKEN:
				return StatusValue::newFatal( $msg ?: 'sessionfailure' );

			case LoginForm::NO_NAME:
			case LoginForm::ILLEGAL:
				return StatusValue::newFatal( $msg ?: 'noname' );

			case LoginForm::WRONG_PLUGIN_PASS:
			case LoginForm::WRONG_PASS:
				return StatusValue::newFatal( $msg ?: 'wrongpassword' );

			case LoginForm::NOT_EXISTS:
				return StatusValue::newFatal( $msg ?: 'nosuchusershort', wfEscapeWikiText( $user->getName() ) );

			case LoginForm::EMPTY_PASS:
				return StatusValue::newFatal( $msg ?: 'wrongpasswordempty' );

			case LoginForm::RESET_PASS:
				return StatusValue::newFatal( $msg ?: 'resetpass_announce' );

			case LoginForm::THROTTLED:
				$throttle = $this->config->get( 'PasswordAttemptThrottle' );
				return StatusValue::newFatal(
					$msg ?: 'login-throttled',
					\Message::durationParam( $throttle['seconds'] )
				);

			case LoginForm::USER_BLOCKED:
				return StatusValue::newFatal(
					$msg ?: 'login-userblocked', wfEscapeWikiText( $user->getName() )
				);

			case LoginForm::ABORTED:
				return StatusValue::newFatal(
					$msg ?: 'login-abort-generic', wfEscapeWikiText( $user->getName() )
				);

			case LoginForm::USER_MIGRATED:
				$error = $msg ?: 'login-migrated-generic';
				return call_user_func_array( 'StatusValue::newFatal', (array)$error );

			// @codeCoverageIgnoreStart
			case LoginForm::CREATE_BLOCKED: // Can never happen
			default:
				throw new \DomainException( __METHOD__ . ": Unhandled case value from $hook" );
		}
			// @codeCoverageIgnoreEnd
	}
}