MediaWiki REL1_34
TOTP.php
Go to the documentation of this file.
1<?php
2
4
14use MWException;
17
18class 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}
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
MediaWiki exception.
AuthManager secondary authentication provider for TOTP second-factor authentication.
Class representing a two-factor key.
Definition TOTPKey.php:42
isEnabled(OATHUser $user)
Is this module currently enabled for the given user.
Definition TOTP.php:97
verify(OATHUser $user, array $data)
Definition TOTP.php:80
getDescriptionMessage()
Return Message object for the short text to be displayed as description.Message
Definition TOTP.php:128
getManageForm( $action, OATHUser $user, OATHUserRepository $repo)
Definition TOTP.php:107
getDisableWarningMessage()
Module-specific text that will be shown when user is disabling the module, to warn of data-loss....
Definition TOTP.php:135
Class representing a user from OATH's perspective.
Definition OATHUser.php:28
getModule()
Gets the module instance associated with this user.
Definition OATHUser.php:140
getFirstKey()
Useful for modules that operate on single-key premise, as well as testing the key type,...
Definition OATHUser.php:92
A secondary provider mostly acts when the submitted authentication data has already been associated t...