Debian Codegraph @

extensions/OATHAuth/src/Hook/HookHandler.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Extension\OATHAuth\Hook;
4
5 use BadMethodCallException;
6 use MediaWiki\Auth\AuthenticationRequest;
7 use MediaWiki\Config\Config;
8 use MediaWiki\Context\RequestContext;
9 use MediaWiki\Extension\OATHAuth\OATHAuth;
10 use MediaWiki\Extension\OATHAuth\OATHUserRepository;
11 use MediaWiki\Message\Message;
12 use MediaWiki\Permissions\Hook\GetUserPermissionsErrorsHook;
13 use MediaWiki\Permissions\Hook\UserGetRightsHook;
14 use MediaWiki\Permissions\PermissionManager;
15 use MediaWiki\Preferences\Hook\GetPreferencesHook;
16 use MediaWiki\SpecialPage\Hook\AuthChangeFormFieldsHook;
17 use MediaWiki\SpecialPage\SpecialPage;
18 use MediaWiki\Title\Title;
19 use MediaWiki\User\Hook\UserEffectiveGroupsHook;
20 use MediaWiki\User\User;
21 use MediaWiki\User\UserGroupManager;
22 use MediaWiki\User\UserGroupMembership;
23 use OOUI\ButtonWidget;
24 use OOUI\HorizontalLayout;
25 use OOUI\LabelWidget;
26
27 class HookHandler implements
28 AuthChangeFormFieldsHook,
29 GetPreferencesHook,
30 getUserPermissionsErrorsHook,
31 UserEffectiveGroupsHook,
32 UserGetRightsHook
33 {
34 /**
35 * @var OATHUserRepository
36 */
37 private $userRepo;
38
39 /**
40 * @var PermissionManager
41 */
42 private $permissionManager;
43
44 /**
45 * @var UserGroupManager
46 */
47 private $userGroupManager;
48
49 /**
50 * @var Config
51 */
52 private $config;
53
54 /**
55 * @param OATHUserRepository $userRepo
56 * @param PermissionManager $permissionManager
57 * @param Config $config
58 * @param UserGroupManager $userGroupManager
59 */
60 public function __construct( $userRepo, $permissionManager, $config, $userGroupManager ) {
61 $this->userRepo = $userRepo;
62 $this->permissionManager = $permissionManager;
63 $this->config = $config;
64 $this->userGroupManager = $userGroupManager;
65 }
66
67 /**
68 * @param AuthenticationRequest[] $requests
69 * @param array $fieldInfo
70 * @param array &$formDescriptor
71 * @param string $action
72 *
73 * @return bool
74 */
75 public function onAuthChangeFormFields( $requests, $fieldInfo, &$formDescriptor, $action ) {
76 if ( !isset( $fieldInfo['OATHToken'] ) ) {
77 return true;
78 }
79
80 $formDescriptor['OATHToken'] += [
81 'cssClass' => 'loginText',
82 'id' => 'wpOATHToken',
83 'size' => 20,
84 'dir' => 'ltr',
85 'autofocus' => true,
86 'persistent' => false,
87 'autocomplete' => 'one-time-code',
88 'spellcheck' => false,
89 'help-message' => 'oathauth-auth-token-help-ui',
90 ];
91 return true;
92 }
93
94 /**
95 * @param User $user
96 * @param array &$preferences
97 *
98 * @return bool
99 */
100 public function onGetPreferences( $user, &$preferences ) {
101 $oathUser = $this->userRepo->findByUser( $user );
102
103 // If there is no existing module for the user, and the user is not allowed to enable it,
104 // we have nothing to show.
105 if (
106 !$oathUser->isTwoFactorAuthEnabled() &&
107 !$this->permissionManager->userHasRight( $user, 'oathauth-enable' )
108 ) {
109 return true;
110 }
111
112 $module = $oathUser->getModule();
113
114 $moduleLabel = $module === null ?
115 wfMessage( 'oathauth-ui-no-module' ) :
116 $module->getDisplayName();
117
118 $manageButton = new ButtonWidget( [
119 'href' => SpecialPage::getTitleFor( 'OATHManage' )->getLocalURL(),
120 'label' => wfMessage( 'oathauth-ui-manage' )->text()
121 ] );
122
123 $currentModuleLabel = new LabelWidget( [
124 'label' => $moduleLabel->text()
125 ] );
126
127 $control = new HorizontalLayout( [
128 'items' => [
129 $currentModuleLabel,
130 $manageButton
131 ]
132 ] );
133
134 $preferences['oathauth-module'] = [
135 'type' => 'info',
136 'raw' => true,
137 'default' => (string)$control,
138 'label-message' => 'oathauth-prefs-label',
139 'section' => 'personal/info',
140 ];
141
142 $dbGroups = $this->userGroupManager->getUserGroups( $user );
143 $disabledGroups = $this->getDisabledGroups( $user, $dbGroups );
144 if ( !$oathUser->isTwoFactorAuthEnabled() && $disabledGroups ) {
145 $context = RequestContext::getMain();
146 $list = [];
147 foreach ( $disabledGroups as $disabledGroup ) {
148 $list[] = UserGroupMembership::getLinkHTML( $disabledGroup, $context );
149 }
150 $info = $context->getLanguage()->commaList( $list );
151 $disabledInfo = [ 'oathauth-disabledgroups' => [
152 'type' => 'info',
153 'label-message' => [ 'oathauth-prefs-disabledgroups',
154 Message::numParam( count( $disabledGroups ) ) ],
155 'help-message' => [ 'oathauth-prefs-disabledgroups-help',
156 Message::numParam( count( $disabledGroups ) ), $user->getName() ],
157 'default' => $info,
158 'raw' => true,
159 'section' => 'personal/info',
160 ] ];
161 // Insert right after "Member of groups"
162 $preferences = wfArrayInsertAfter( $preferences, $disabledInfo, 'usergroups' );
163 }
164
165 return true;
166 }
167
168 /**
169 * Return the groups that this user is supposed to be in, but are disabled
170 * because 2FA isn't enabled
171 *
172 * @param User $user
173 * @param string[] $groups All groups the user is supposed to be in
174 * @return string[] Groups the user should be disabled in
175 */
176 private function getDisabledGroups( User $user, array $groups ): array {
177 $requiredGroups = $this->config->get( 'OATHRequiredForGroups' );
178 // Bail early if:
179 // * No configured restricted groups
180 // * The user is not in any of the restricted groups
181 $intersect = array_intersect( $groups, $requiredGroups );
182 if ( !$requiredGroups || !$intersect ) {
183 return [];
184 }
185
186 $oathUser = $this->userRepo->findByUser( $user );
187 if ( !$oathUser->isTwoFactorAuthEnabled() ) {
188 // Not enabled, strip the groups
189 return $intersect;
190 }
191
192 return [];
193 }
194
195 /**
196 * Remove groups if 2FA is required for them and it's not enabled
197 *
198 * @param User $user User to get groups for
199 * @param string[] &$groups Current effective groups
200 */
201 public function onUserEffectiveGroups( $user, &$groups ) {
202 // If the user has 2FA disabled, don't leak that information to other users (T412061)
203 try {
204 if ( !$user->equals( RequestContext::getMain()->getUser() ) ) {
205 return;
206 }
207 } catch ( BadMethodCallException ) {
208 // If we got this exception, it means we are in a session-less entry point.
209 // Treat this as if the current user is not the same as $user, and don't expose
210 // $user's potential lack of 2FA
211 return;
212 }
213
214 $disabledGroups = $this->getDisabledGroups( $user, $groups );
215 if ( $disabledGroups ) {
216 $groups = array_diff( $groups, $disabledGroups );
217 }
218 }
219
220 /**
221 * @param Title $title
222 * @param User $user
223 * @param string $action
224 * @param string &$result
225 *
226 * @return bool
227 */
228 public function onGetUserPermissionsErrors( $title, $user, $action, &$result ) {
229 if ( !$this->config->has( 'OATHExclusiveRights' ) ) {
230 return true;
231 }
232
233 // TODO: Get the session from somewhere more... sane?
234 $session = $user->getRequest()->getSession();
235 if (
236 !(bool)$session->get( OATHAuth::AUTHENTICATED_OVER_2FA, false ) &&
237 in_array( $action, $this->config->get( 'OATHExclusiveRights' ) )
238 ) {
239 $result = 'oathauth-action-exclusive-to-2fa';
240 return false;
241 }
242 return true;
243 }
244
245 /**
246 * If a user has groups disabled for not having 2FA enabled, make sure they
247 * have "oathauth-enable" so they can turn it on
248 *
249 * @param User $user User to get rights for
250 * @param string[] &$rights Current rights
251 */
252 public function onUserGetRights( $user, &$rights ) {
253 if ( in_array( 'oathauth-enable', $rights ) ) {
254 return;
255 }
256
257 $dbGroups = $this->userGroupManager->getUserGroups( $user );
258 if ( $this->getDisabledGroups( $user, $dbGroups ) ) {
259 // User has some disabled groups, add oathauth-enable
260 $rights[] = 'oathauth-enable';
261 }
262 }
263 }