Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 32
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
TOTPSecondaryAuthenticationProvider
0.00% covered (danger)
0.00%
0 / 32
0.00% covered (danger)
0.00%
0 / 5
90
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getAuthenticationRequests
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 beginSecondaryAuthentication
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 continueSecondaryAuthentication
0.00% covered (danger)
0.00%
0 / 25
0.00% covered (danger)
0.00%
0 / 1
20
 beginSecondaryAccountCreation
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * @license GPL-2.0-or-later
4 */
5
6namespace MediaWiki\Extension\OATHAuth\Auth;
7
8use MediaWiki\Auth\AbstractSecondaryAuthenticationProvider;
9use MediaWiki\Auth\AuthenticationRequest;
10use MediaWiki\Auth\AuthenticationResponse;
11use MediaWiki\Auth\AuthManager;
12use MediaWiki\Extension\OATHAuth\Module\TOTP;
13use MediaWiki\Extension\OATHAuth\OATHAuthLogger;
14use MediaWiki\Extension\OATHAuth\OATHUserRepository;
15use MediaWiki\Message\Message;
16
17/**
18 * AuthManager secondary authentication provider for TOTP second-factor authentication.
19 *
20 * After a successful primary authentication, requests a time-based one-time password
21 * (typically generated by a mobile app such as Google Authenticator) from the user.
22 *
23 * @see AuthManager
24 * @see https://en.wikipedia.org/wiki/Time-based_One-time_Password_Algorithm
25 */
26class TOTPSecondaryAuthenticationProvider extends AbstractSecondaryAuthenticationProvider {
27    public function __construct(
28        private readonly TOTP $module,
29        private readonly OATHUserRepository $userRepository,
30        private readonly OATHAuthLogger $oathLogger,
31    ) {
32    }
33
34    /** @inheritDoc */
35    public function getAuthenticationRequests( $action, array $options ) {
36        // don't ask for anything initially, so the second factor is on a separate screen
37        return [];
38    }
39
40    /**
41     * If the user has enabled two-factor authentication, request a second factor.
42     *
43     * @inheritDoc
44     */
45    public function beginSecondaryAuthentication( $user, array $reqs ) {
46        $authUser = $this->userRepository->findByUser( $user );
47
48        if ( !$this->module->isEnabled( $authUser ) ) {
49            return AuthenticationResponse::newAbstain();
50        }
51
52        return AuthenticationResponse::newUI( [ new TOTPAuthenticationRequest() ] );
53    }
54
55    /** @inheritDoc */
56    public function continueSecondaryAuthentication( $user, array $reqs ) {
57        /** @var TOTPAuthenticationRequest $request */
58        $request = AuthenticationRequest::getRequestByClass( $reqs, TOTPAuthenticationRequest::class );
59        if ( !$request ) {
60            return AuthenticationResponse::newUI( [ new TOTPAuthenticationRequest() ],
61                wfMessage( 'oathauth-login-failed' ), 'error' );
62        }
63
64        // Don't increase pingLimiter, just check for limit exceeded.
65        if ( $user->pingLimiter( 'badoath', 0 ) ) {
66            return AuthenticationResponse::newUI(
67                [ new TOTPAuthenticationRequest() ],
68                new Message( 'oathauth-throttled' ),
69                'error'
70            );
71        }
72
73        $authUser = $this->userRepository->findByUser( $user );
74        $token = $request->OATHToken;
75
76        if ( $this->module->verify( $authUser, [ 'token' => $token ] ) ) {
77            return AuthenticationResponse::newPass();
78        }
79
80        // Increase rate limit counter for failed request
81        $user->pingLimiter( 'badoath' );
82
83        $this->logger->info( 'OATHAuth user {user} failed OTP token/recovery code from {clientip}', [
84            'user'     => $user->getName(),
85            'clientip' => $user->getRequest()->getIP(),
86        ] );
87
88        $this->oathLogger->logFailedVerification( $user );
89
90        return AuthenticationResponse::newUI(
91            [ new TOTPAuthenticationRequest() ],
92            wfMessage( 'oathauth-login-failed' ),
93            'error'
94        );
95    }
96
97    /** @inheritDoc */
98    public function beginSecondaryAccountCreation( $user, $creator, array $reqs ) {
99        return AuthenticationResponse::newAbstain();
100    }
101}