Debian Codegraph @

extensions/OATHAuth/src/Auth/TOTPSecondaryAuthenticationProvider.php

raw symbols SCIP
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 */
18
19 namespace MediaWiki\Extension\OATHAuth\Auth;
20
21 use MediaWiki\Auth\AbstractSecondaryAuthenticationProvider;
22 use MediaWiki\Auth\AuthenticationRequest;
23 use MediaWiki\Auth\AuthenticationResponse;
24 use MediaWiki\Auth\AuthManager;
25 use MediaWiki\Extension\OATHAuth\Module\TOTP;
26 use MediaWiki\Extension\OATHAuth\OATHUserRepository;
27 use MediaWiki\Message\Message;
28 use MediaWiki\User\User;
29
30 /**
31 * AuthManager secondary authentication provider for TOTP second-factor authentication.
32 *
33 * After a successful primary authentication, requests a time-based one-time password
34 * (typically generated by a mobile app such as Google Authenticator) from the user.
35 *
36 * @see AuthManager
37 * @see https://en.wikipedia.org/wiki/Time-based_One-time_Password_Algorithm
38 */
39 class TOTPSecondaryAuthenticationProvider extends AbstractSecondaryAuthenticationProvider {
40 private TOTP $module;
41 private OATHUserRepository $userRepository;
42
43 public function __construct( TOTP $module, OATHUserRepository $userRepository ) {
44 $this->module = $module;
45 $this->userRepository = $userRepository;
46 }
47
48 /**
49 * @param string $action
50 * @param array $options
51 *
52 * @return array
53 */
54 public function getAuthenticationRequests( $action, array $options ) {
55 // don't ask for anything initially, so the second factor is on a separate screen
56 return [];
57 }
58
59 /**
60 * If the user has enabled two-factor authentication, request a second factor.
61 *
62 * @param User $user
63 * @param array $reqs
64 *
65 * @return AuthenticationResponse
66 */
67 public function beginSecondaryAuthentication( $user, array $reqs ) {
68 $authUser = $this->userRepository->findByUser( $user );
69
70 if ( !( $authUser->getModule() instanceof TOTP ) ) {
71 return AuthenticationResponse::newAbstain();
72 }
73
74 return AuthenticationResponse::newUI(
75 [ new TOTPAuthenticationRequest() ],
76 wfMessage( 'oathauth-auth-ui' ),
77 );
78 }
79
80 /**
81 * Verify the second factor.
82 * @inheritDoc
83 */
84 public function continueSecondaryAuthentication( $user, array $reqs ) {
85 /** @var TOTPAuthenticationRequest $request */
86 $request = AuthenticationRequest::getRequestByClass( $reqs, TOTPAuthenticationRequest::class );
87 if ( !$request ) {
88 return AuthenticationResponse::newUI( [ new TOTPAuthenticationRequest() ],
89 wfMessage( 'oathauth-login-failed' ), 'error' );
90 }
91
92 $authUser = $this->userRepository->findByUser( $user );
93 $token = $request->OATHToken;
94
95 // Don't increase pingLimiter, just check for limit exceeded.
96 if ( $user->pingLimiter( 'badoath', 0 ) ) {
97 return AuthenticationResponse::newUI(
98 [ new TOTPAuthenticationRequest() ],
99 new Message( 'oathauth-throttled' ),
100 'error'
101 );
102 }
103
104 if ( $this->module->verify( $authUser, [ 'token' => $token ] ) ) {
105 return AuthenticationResponse::newPass();
106 }
107
108 // Increase rate limit counter for failed request
109 $user->pingLimiter( 'badoath' );
110
111 $this->logger->info( 'OATHAuth user {user} failed OTP token/recovery code from {clientip}', [
112 'user' => $user->getName(),
113 'clientip' => $user->getRequest()->getIP(),
114 ] );
115
116 return AuthenticationResponse::newUI(
117 [ new TOTPAuthenticationRequest() ],
118 wfMessage( 'oathauth-login-failed' ),
119 'error'
120 );
121 }
122
123 /**
124 * @param User $user
125 * @param User $creator
126 * @param array $reqs
127 *
128 * @return AuthenticationResponse
129 */
130 public function beginSecondaryAccountCreation( $user, $creator, array $reqs ) {
131 return AuthenticationResponse::newAbstain();
132 }
133 }