MediaWiki  1.34.0
TOTP.php
Go to the documentation of this file.
1 <?php
2 
4 
14 use MWException;
17 
18 class TOTP implements IModule {
19  public static function factory() {
20  return new static();
21  }
22 
27  public function getName() {
28  return "totp";
29  }
30 
31  public function getDisplayName() {
32  return wfMessage( 'oathauth-module-totp-label' );
33  }
34 
41  public function newKey( array $data ) {
42  if ( !isset( $data['secret'] ) || !isset( $data['scratch_tokens'] ) ) {
43  throw new MWException( 'oathauth-invalid-data-format' );
44  }
45  if ( is_string( $data['scratch_tokens' ] ) ) {
46  $data['scratch_tokens'] = explode( ',', $data['scratch_tokens'] );
47  }
48 
49  return TOTPKey::newFromArray( $data );
50  }
51 
57  public function getDataFromUser( OATHUser $user ) {
58  $key = $user->getFirstKey();
59  if ( !( $key instanceof TOTPKey ) ) {
60  throw new MWException( 'oathauth-invalid-key-type' );
61  }
62  return [
63  'keys' => [ $key->jsonSerialize() ]
64  ];
65  }
66 
70  public function getSecondaryAuthProvider() {
72  }
73 
80  public function verify( OATHUser $user, array $data ) {
81  if ( !isset( $data['token'] ) ) {
82  return false;
83  }
84  $key = $user->getFirstKey();
85  if ( !( $key instanceof TOTPKey ) ) {
86  return false;
87  }
88  return $key->verify( $data, $user );
89  }
90 
97  public function isEnabled( OATHUser $user ) {
98  return $user->getFirstKey() instanceof TOTPKey;
99  }
100 
107  public function getManageForm( $action, OATHUser $user, OATHUserRepository $repo ) {
108  $isEnabledForUser = $user->getModule() instanceof self;
109  if ( $action === OATHManage::ACTION_ENABLE && !$isEnabledForUser ) {
110  return new TOTPEnableForm( $user, $repo, $this );
111  }
112  if ( $action === OATHManage::ACTION_DISABLE && $isEnabledForUser ) {
113  return new TOTPDisableForm( $user, $repo, $this );
114  }
115  return null;
116  }
117 
121  public function getConfig() {
122  return null;
123  }
124 
128  public function getDescriptionMessage() {
129  return wfMessage( 'oathauth-totp-description' );
130  }
131 
135  public function getDisableWarningMessage() {
136  return wfMessage( 'oathauth-totp-disable-warning' );
137  }
138 }
MediaWiki\Extension\OATHAuth\OATHUser\getModule
getModule()
Gets the module instance associated with this user.
Definition: OATHUser.php:140
MediaWiki\Extension\OATHAuth\Module\TOTP\isEnabled
isEnabled(OATHUser $user)
Is this module currently enabled for the given user.
Definition: TOTP.php:97
MediaWiki\Extension\OATHAuth\Module\TOTP\getSecondaryAuthProvider
getSecondaryAuthProvider()
Definition: TOTP.php:70
MediaWiki\Extension\OATHAuth\IAuthKey
Definition: IAuthKey.php:8
MediaWiki\Extension\OATHAuth\OATHUser
Class representing a user from OATH's perspective.
Definition: OATHUser.php:28
MediaWiki\Extension\OATHAuth\Key\TOTPKey
Class representing a two-factor key.
Definition: TOTPKey.php:41
MediaWiki\Extension\OATHAuth\Module\TOTP\getConfig
getConfig()
Definition: TOTP.php:121
MediaWiki\Extension\OATHAuth\Module\TOTP\factory
static factory()
Definition: TOTP.php:19
MediaWiki\Extension\OATHAuth\Module\TOTP\getManageForm
getManageForm( $action, OATHUser $user, OATHUserRepository $repo)
Definition: TOTP.php:107
MediaWiki\Extension\OATHAuth\Module
Definition: TOTP.php:3
MediaWiki\Extension\OATHAuth\HTMLForm\TOTPDisableForm
Definition: TOTPDisableForm.php:10
wfMessage
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
Definition: GlobalFunctions.php:1264
MediaWiki\Extension\OATHAuth\Auth\TOTPSecondaryAuthenticationProvider
AuthManager secondary authentication provider for TOTP second-factor authentication.
Definition: TOTPSecondaryAuthenticationProvider.php:39
MediaWiki\Extension\OATHAuth\Module\TOTP
Definition: TOTP.php:18
MediaWiki\Extension\OATHAuth\Special\OATHManage\ACTION_ENABLE
const ACTION_ENABLE
Definition: OATHManage.php:43
MediaWiki\Extension\OATHAuth\OATHUserRepository
Definition: OATHUserRepository.php:33
MWException
MediaWiki exception.
Definition: MWException.php:26
MediaWiki\Extension\OATHAuth\Module\TOTP\getDisableWarningMessage
getDisableWarningMessage()
Module-specific text that will be shown when user is disabling the module, to warn of data-loss....
Definition: TOTP.php:135
MediaWiki\Auth\SecondaryAuthenticationProvider
A secondary provider mostly acts when the submitted authentication data has already been associated t...
Definition: SecondaryAuthenticationProvider.php:52
MediaWiki\Extension\OATHAuth\Special\OATHManage
Definition: OATHManage.php:42
MediaWiki\Extension\OATHAuth\Module\TOTP\getDescriptionMessage
getDescriptionMessage()
Return Message object for the short text to be displayed as description.Message
Definition: TOTP.php:128
MediaWiki\Extension\OATHAuth\Module\TOTP\getDataFromUser
getDataFromUser(OATHUser $user)
Definition: TOTP.php:57
MediaWiki\Extension\OATHAuth\Special\OATHManage\ACTION_DISABLE
const ACTION_DISABLE
Definition: OATHManage.php:44
MediaWiki\Extension\OATHAuth\Key\TOTPKey\newFromArray
static newFromArray(array $data)
Definition: TOTPKey.php:93
MediaWiki\Extension\OATHAuth\Module\TOTP\getDisplayName
getDisplayName()
Definition: TOTP.php:31
MediaWiki\Extension\OATHAuth\Module\TOTP\verify
verify(OATHUser $user, array $data)
Definition: TOTP.php:80
MediaWiki\$action
string $action
Cache what action this request is.
Definition: MediaWiki.php:48
MediaWiki\Extension\OATHAuth\Module\TOTP\getName
getName()
Name of the module.
Definition: TOTP.php:27
MediaWiki\Extension\OATHAuth\HTMLForm\TOTPEnableForm
Definition: TOTPEnableForm.php:10
MediaWiki\Extension\OATHAuth\OATHUser\getFirstKey
getFirstKey()
Useful for modules that operate on single-key premise, as well as testing the key type,...
Definition: OATHUser.php:92
MediaWiki\Extension\OATHAuth\Module\TOTP\newKey
newKey(array $data)
Definition: TOTP.php:41
MediaWiki\Extension\OATHAuth\HTMLForm\IManageForm
Definition: IManageForm.php:10
MediaWiki\Extension\OATHAuth\IModule
Definition: IModule.php:9