Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 37
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
LoginAttempt
0.00% covered (danger)
0.00%
0 / 31
0.00% covered (danger)
0.00%
0 / 2
42
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 23
0.00% covered (danger)
0.00%
0 / 1
30
1<?php
2
3namespace LoginNotify\Maintenance;
4
5use LoginNotify\Hooks\HookRunner;
6use Maintenance;
7use MediaWiki\Auth\AuthenticationResponse;
8use MediaWiki\Language\RawMessage;
9use MediaWiki\MediaWikiServices;
10use MediaWiki\Request\FauxRequest;
11use MediaWiki\User\UserFactory;
12
13$IP = getenv( 'MW_INSTALL_PATH' );
14if ( $IP === false ) {
15    $IP = __DIR__ . '/../../..';
16}
17
18require_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 */
26class 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;
85require_once RUN_MAINTENANCE_IF_MAIN;