| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 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 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 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 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
public function getAuthenticationRequests( $action, array $options ) { |
| 55 |
|
| 56 |
return []; |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 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 |
|
| 82 |
|
| 83 |
|
| 84 |
public function continueSecondaryAuthentication( $user, array $reqs ) { |
| 85 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
public function beginSecondaryAccountCreation( $user, $creator, array $reqs ) { |
| 131 |
return AuthenticationResponse::newAbstain(); |
| 132 |
} |
| 133 |
} |