Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 27 |
|
0.00% |
0 / 10 |
CRAP | |
0.00% |
0 / 1 |
TOTP | |
0.00% |
0 / 27 |
|
0.00% |
0 / 10 |
552 | |
0.00% |
0 / 1 |
factory | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getName | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getDisplayName | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
newKey | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
20 | |||
getSecondaryAuthProvider | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
verify | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
30 | |||
isEnabled | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
12 | |||
getManageForm | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
30 | |||
getDescriptionMessage | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getDisableWarningMessage | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | |
3 | namespace MediaWiki\Extension\OATHAuth\Module; |
4 | |
5 | use IContextSource; |
6 | use MediaWiki\Extension\OATHAuth\Auth\TOTPSecondaryAuthenticationProvider; |
7 | use MediaWiki\Extension\OATHAuth\HTMLForm\IManageForm; |
8 | use MediaWiki\Extension\OATHAuth\HTMLForm\TOTPDisableForm; |
9 | use MediaWiki\Extension\OATHAuth\HTMLForm\TOTPEnableForm; |
10 | use MediaWiki\Extension\OATHAuth\IModule; |
11 | use MediaWiki\Extension\OATHAuth\Key\TOTPKey; |
12 | use MediaWiki\Extension\OATHAuth\OATHUser; |
13 | use MediaWiki\Extension\OATHAuth\OATHUserRepository; |
14 | use MediaWiki\Extension\OATHAuth\Special\OATHManage; |
15 | use MWException; |
16 | |
17 | class TOTP implements IModule { |
18 | public static function factory() { |
19 | return new static(); |
20 | } |
21 | |
22 | /** @inheritDoc */ |
23 | public function getName() { |
24 | return "totp"; |
25 | } |
26 | |
27 | /** @inheritDoc */ |
28 | public function getDisplayName() { |
29 | return wfMessage( 'oathauth-module-totp-label' ); |
30 | } |
31 | |
32 | /** |
33 | * @inheritDoc |
34 | * @throws MWException |
35 | */ |
36 | public function newKey( array $data ) { |
37 | if ( !isset( $data['secret'] ) || !isset( $data['scratch_tokens'] ) ) { |
38 | throw new MWException( 'oathauth-invalid-data-format' ); |
39 | } |
40 | if ( is_string( $data['scratch_tokens' ] ) ) { |
41 | $data['scratch_tokens'] = explode( ',', $data['scratch_tokens'] ); |
42 | } |
43 | |
44 | return TOTPKey::newFromArray( $data ); |
45 | } |
46 | |
47 | /** |
48 | * @return TOTPSecondaryAuthenticationProvider |
49 | */ |
50 | public function getSecondaryAuthProvider() { |
51 | return new TOTPSecondaryAuthenticationProvider(); |
52 | } |
53 | |
54 | /** |
55 | * @param OATHUser $user |
56 | * @param array $data |
57 | * @return bool |
58 | * @throws MWException |
59 | */ |
60 | public function verify( OATHUser $user, array $data ): bool { |
61 | if ( !isset( $data['token'] ) ) { |
62 | return false; |
63 | } |
64 | |
65 | foreach ( $user->getKeys() as $key ) { |
66 | if ( $key instanceof TOTPKey && $key->verify( $data, $user ) ) { |
67 | return true; |
68 | } |
69 | } |
70 | |
71 | return false; |
72 | } |
73 | |
74 | /** |
75 | * Is this module currently enabled for the given user? |
76 | * |
77 | * @param OATHUser $user |
78 | * @return bool |
79 | */ |
80 | public function isEnabled( OATHUser $user ): bool { |
81 | foreach ( $user->getKeys() as $key ) { |
82 | if ( $key instanceof TOTPKey ) { |
83 | return true; |
84 | } |
85 | } |
86 | |
87 | return false; |
88 | } |
89 | |
90 | /** |
91 | * @param string $action |
92 | * @param OATHUser $user |
93 | * @param OATHUserRepository $repo |
94 | * @param IContextSource $context |
95 | * @return IManageForm|null |
96 | */ |
97 | public function getManageForm( |
98 | $action, |
99 | OATHUser $user, |
100 | OATHUserRepository $repo, |
101 | IContextSource $context |
102 | ): ?IManageForm { |
103 | $hasTOTPKey = $this->isEnabled( $user ); |
104 | if ( $action === OATHManage::ACTION_ENABLE && !$hasTOTPKey ) { |
105 | return new TOTPEnableForm( $user, $repo, $this, $context ); |
106 | } |
107 | if ( $action === OATHManage::ACTION_DISABLE && $hasTOTPKey ) { |
108 | return new TOTPDisableForm( $user, $repo, $this, $context ); |
109 | } |
110 | return null; |
111 | } |
112 | |
113 | /** |
114 | * @inheritDoc |
115 | */ |
116 | public function getDescriptionMessage() { |
117 | return wfMessage( 'oathauth-totp-description' ); |
118 | } |
119 | |
120 | /** |
121 | * @inheritDoc |
122 | */ |
123 | public function getDisableWarningMessage() { |
124 | return wfMessage( 'oathauth-totp-disable-warning' ); |
125 | } |
126 | } |