Debian Codegraph @

extensions/LoginNotify/maintenance/loginAttempt.php

raw symbols SCIP
1 <?php
2
3 namespace LoginNotify\Maintenance;
4
5 use LoginNotify\Hooks\HookRunner;
6 use MediaWiki\Auth\AuthenticationResponse;
7 use MediaWiki\Language\RawMessage;
8 use MediaWiki\Maintenance\Maintenance;
9 use MediaWiki\MediaWikiServices;
10 use MediaWiki\Request\FauxRequest;
11 use MediaWiki\User\UserFactory;
12
13 $IP = getenv( 'MW_INSTALL_PATH' );
14 if ( $IP === false ) {
15 $IP = __DIR__ . '/../../..';
16 }
17
18 require_once "$IP/maintenance/Maintenance.php";
19
20 /**
21 * A maintenance script to programatically generate successful or failed login attempts for any
22 * user, from any given IP address and with any given user-agent string. This script makes testing
23 * LoginNotify and its interaction with other extensions (such as CheckUser) much easier for the
24 * developers.
25 */
26 class LoginAttempt extends Maintenance {
27 /**
28 * Constructor
29 *
30 * Retrieves the arguments from the command line
31 */
32 public function __construct() {
33 parent::__construct();
34 $this->addDescription( 'Registers a login attempt for a given user' );
35 $this->addArg( 'user', 'Target user', true );
36 $this->addArg( 'success', 'Whether login attempt was successful (true/false)', false );
37 $this->addArg( 'ip', 'IP address of the login attempt', false );
38 $this->addArg( 'ua', 'User-agent string of the login attempt', false );
39 $this->addArg( 'repetitions', 'How many times the attempt should be made', false );
40
41 $this->requireExtension( 'LoginNotify' );
42 }
43
44 /**
45 * Main function
46 *
47 * Registers a failed or successful login attempt for a given user
48 */
49 public function execute() {
50 global $wgRequest;
51
52 $username = $this->getArg( 0 );
53 $success = $this->getArg( 1, false ) === 'true';
54 $ip = $this->getArg( 2, '127.0.0.1' );
55 $ua = $this->getArg( 3, 'Login attempt by LoginNotify maintenance script' );
56 $reps = intval( $this->getArg( 4, 1 ) );
57
58 $wgRequest = new FauxRequest();
59 $wgRequest->setIP( $ip );
60 $wgRequest->setHeader( 'User-Agent', $ua );
61
62 $user = $this->getServiceContainer()->getUserFactory()
63 ->newFromName( $username, UserFactory::RIGOR_USABLE );
64 if ( !$user || !$user->isRegistered() ) {
65 $this->output( "User {$username} does not exist!\n" );
66 return;
67 }
68
69 $hookRunner = new HookRunner( MediaWikiServices::getInstance()->getHookContainer() );
70 for ( $i = 0; $i < $reps; $i++ ) {
71 if ( $success ) {
72 $res = AuthenticationResponse::newPass( $username );
73 $hookRunner->onAuthManagerLoginAuthenticateAudit( $res, $user, $username, [] );
74 $this->output( "A successful login attempt was registered!\n" );
75 } else {
76 $res = AuthenticationResponse::newFail( new RawMessage( 'Well, it failed' ) );
77 $hookRunner->onAuthManagerLoginAuthenticateAudit( $res, null, $username, [] );
78 $this->output( "A failed login attempt was registered!\n" );
79 }
80 }
81 }
82 }
83
84 $maintClass = LoginAttempt::class;
85 require_once RUN_MAINTENANCE_IF_MAIN;