MediaWiki REL1_34
TOTPSecondaryAuthenticationProvider.php
Go to the documentation of this file.
1<?php
20
27use Message;
28use User;
29
40
47 public function getAuthenticationRequests( $action, array $options ) {
48 switch ( $action ) {
49 case AuthManager::ACTION_LOGIN:
50 // don't ask for anything initially so the second factor is on a separate screen
51 return [];
52 default:
53 return [];
54 }
55 }
56
65 public function beginSecondaryAuthentication( $user, array $reqs ) {
66 $userRepo = MediaWikiServices::getInstance()->getService( 'OATHUserRepository' );
67 $authUser = $userRepo->findByUser( $user );
68
69 if ( !( $authUser->getModule() instanceof TOTP ) ) {
70 return AuthenticationResponse::newAbstain();
71 } else {
72 return AuthenticationResponse::newUI( [ new TOTPAuthenticationRequest() ],
73 wfMessage( 'oathauth-auth-ui' ), 'warning' );
74 }
75 }
76
81 public function continueSecondaryAuthentication( $user, array $reqs ) {
83 $request = AuthenticationRequest::getRequestByClass( $reqs, TOTPAuthenticationRequest::class );
84 if ( !$request ) {
85 return AuthenticationResponse::newUI( [ new TOTPAuthenticationRequest() ],
86 wfMessage( 'oathauth-login-failed' ), 'error' );
87 }
88
89 $userRepo = MediaWikiServices::getInstance()->getService( 'OATHUserRepository' );
90 $authUser = $userRepo->findByUser( $user );
91 $token = $request->OATHToken;
92
93 if ( !( $authUser->getModule() instanceof TOTP ) ) {
94 $this->logger->warning( 'Two-factor authentication was disabled mid-authentication for '
95 . $user->getName() );
96 return AuthenticationResponse::newAbstain();
97 }
98
99 // Don't increase pingLimiter, just check for limit exceeded.
100 if ( $user->pingLimiter( 'badoath', 0 ) ) {
101 return AuthenticationResponse::newUI(
103 new Message(
104 'oathauth-throttled',
105 // Arbitrary duration given here
106 [ Message::durationParam( 60 ) ]
107 ), 'error' );
108 }
109
110 if ( $authUser->getModule()->verify( $authUser, [ 'token' => $token ] ) ) {
111 return AuthenticationResponse::newPass();
112 } else {
113 return AuthenticationResponse::newUI( [ new TOTPAuthenticationRequest() ],
114 wfMessage( 'oathauth-login-failed' ), 'error' );
115 }
116 }
117
125 public function beginSecondaryAccountCreation( $user, $creator, array $reqs ) {
126 return AuthenticationResponse::newAbstain();
127 }
128}
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
A base class that implements some of the boilerplate for a SecondaryAuthenticationProvider.
This serves as the entry point to the authentication system.
This is a value object for authentication requests.
This is a value object to hold authentication response data.
AuthManager value object for the TOTP second factor of an authentication: a pseudorandom token that i...
AuthManager secondary authentication provider for TOTP second-factor authentication.
beginSecondaryAuthentication( $user, array $reqs)
If the user has enabled two-factor authentication, request a second factor.
MediaWikiServices is the service locator for the application scope of MediaWiki.
static getInstance()
Returns the global default instance of the top level service locator.
The Message class provides methods which fulfil two basic services:
Definition Message.php:162
static durationParam( $duration)
Definition Message.php:1049
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition User.php:51
This program is free software; you can redistribute it and/or modify it under the terms of the GNU Ge...