Debian Codegraph @

extensions/OATHAuth/src/Module/TOTP.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Extension\OATHAuth\Module;
4
5 use MediaWiki\Context\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 const MODULE_NAME = "totp";
19
20 private OATHUserRepository $userRepository;
21
22 /**
23 * @return TOTPKey[]
24 */
25 public static function getTOTPKeys( OATHUser $user ): array {
26 // @phan-suppress-next-line PhanTypeMismatchReturn
27 return $user->getKeysForModule( self::MODULE_NAME );
28 }
29
30 public function __construct( OATHUserRepository $userRepository ) {
31 $this->userRepository = $userRepository;
32 }
33
34 /** @inheritDoc */
35 public function getName() {
36 return self::MODULE_NAME;
37 }
38
39 /** @inheritDoc */
40 public function getDisplayName() {
41 return wfMessage( 'oathauth-module-totp-label' );
42 }
43
44 /**
45 * @inheritDoc
46 * @throws MWException
47 */
48 public function newKey( array $data ) {
49 if ( !isset( $data['secret'] ) || !isset( $data['scratch_tokens'] ) ) {
50 throw new MWException( 'oathauth-invalid-data-format' );
51 }
52 if ( is_string( $data['scratch_tokens' ] ) ) {
53 $data['scratch_tokens'] = explode( ',', $data['scratch_tokens'] );
54 }
55
56 return TOTPKey::newFromArray( $data );
57 }
58
59 /**
60 * @return TOTPSecondaryAuthenticationProvider
61 */
62 public function getSecondaryAuthProvider() {
63 return new TOTPSecondaryAuthenticationProvider(
64 $this,
65 $this->userRepository
66 );
67 }
68
69 /**
70 * @param OATHUser $user
71 * @param array $data
72 * @return bool
73 * @throws MWException
74 */
75 public function verify( OATHUser $user, array $data ): bool {
76 if ( !isset( $data['token'] ) ) {
77 return false;
78 }
79
80 foreach ( self::getTOTPKeys( $user ) as $key ) {
81 if ( $key->verify( $data, $user ) ) {
82 return true;
83 }
84 }
85
86 return false;
87 }
88
89 /**
90 * Is this module currently enabled for the given user?
91 *
92 * @param OATHUser $user
93 * @return bool
94 */
95 public function isEnabled( OATHUser $user ): bool {
96 return (bool)self::getTOTPKeys( $user );
97 }
98
99 /**
100 * @param string $action
101 * @param OATHUser $user
102 * @param OATHUserRepository $repo
103 * @param IContextSource $context
104 * @return IManageForm|null
105 */
106 public function getManageForm(
107 $action,
108 OATHUser $user,
109 OATHUserRepository $repo,
110 IContextSource $context
111 ): ?IManageForm {
112 $hasTOTPKey = $this->isEnabled( $user );
113 if ( $action === OATHManage::ACTION_ENABLE && !$hasTOTPKey ) {
114 return new TOTPEnableForm( $user, $repo, $this, $context );
115 }
116 if ( $action === OATHManage::ACTION_DISABLE && $hasTOTPKey ) {
117 return new TOTPDisableForm( $user, $repo, $this, $context );
118 }
119 return null;
120 }
121
122 /**
123 * @inheritDoc
124 */
125 public function getDescriptionMessage() {
126 return wfMessage( 'oathauth-totp-description' );
127 }
128
129 /**
130 * @inheritDoc
131 */
132 public function getDisableWarningMessage() {
133 return wfMessage( 'oathauth-totp-disable-warning' );
134 }
135 }