Skip to content

Commit 7b4d6c9

Browse files
refactor(Template): align JSConfigHelper PasswordConfirmation logic w/ Middleware
Signed-off-by: Josh <josh.t.richards@gmail.com>
1 parent 14ee86f commit 7b4d6c9

1 file changed

Lines changed: 41 additions & 14 deletions

File tree

lib/private/Template/JSConfigHelper.php

Lines changed: 41 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,17 @@
4040

4141
class JSConfigHelper {
4242

43-
/** @var array user back-ends excluded from password verification */
44-
private $excludedUserBackEnds = ['user_saml' => true, 'user_globalsiteselector' => true];
43+
/**
44+
* Backends that cannot participate in password confirmation are exempt from both
45+
* strict and non-strict password confirmation checks. New backends should prefer
46+
* implementing IPasswordConfirmationBackend instead of being added here.
47+
*
48+
* @var array<string, true>
49+
*/
50+
private array $excludedUserBackEnds = [
51+
'user_saml' => true,
52+
'user_globalsiteselector' => true,
53+
];
4554

4655
public function __construct(
4756
protected ServerVersion $serverVersion,
@@ -63,18 +72,15 @@ public function __construct(
6372
}
6473

6574
public function getConfig(): string {
66-
$userBackendAllowsPasswordConfirmation = true;
75+
$userCanUsePasswordConfirmation = false;
76+
$canUserValidatePassword = $this->canUserValidatePassword();
77+
6778
if ($this->currentUser !== null) {
6879
$uid = $this->currentUser->getUID();
6980

70-
$backend = $this->currentUser->getBackend();
71-
if ($backend instanceof IPasswordConfirmationBackend) {
72-
$userBackendAllowsPasswordConfirmation = $backend->canConfirmPassword($uid) && $this->canUserValidatePassword();
73-
} elseif (isset($this->excludedUserBackEnds[$this->currentUser->getBackendClassName()])) {
74-
$userBackendAllowsPasswordConfirmation = false;
75-
} else {
76-
$userBackendAllowsPasswordConfirmation = $this->canUserValidatePassword();
77-
}
81+
$userCanUsePasswordConfirmation =
82+
$this->canBackendConfirmPassword($this->currentUser)
83+
&& $canUserValidatePassword;
7884
} else {
7985
$uid = null;
8086
}
@@ -126,7 +132,7 @@ public function getConfig(): string {
126132
}
127133

128134
if ($this->currentUser instanceof IUser) {
129-
if ($this->canUserValidatePassword()) {
135+
if ($canUserValidatePassword) {
130136
$lastConfirmTimestamp = $this->session->get('last-password-confirm');
131137
if (!is_int($lastConfirmTimestamp)) {
132138
$lastConfirmTimestamp = 0;
@@ -174,7 +180,7 @@ public function getConfig(): string {
174180
$array = [
175181
'_oc_debug' => $this->config->getSystemValue('debug', false) ? 'true' : 'false',
176182
'_oc_isadmin' => $uid !== null && $this->groupManager->isAdmin($uid) ? 'true' : 'false',
177-
'backendAllowsPasswordConfirmation' => $userBackendAllowsPasswordConfirmation ? 'true' : 'false',
183+
'backendAllowsPasswordConfirmation' => $userCanUsePasswordConfirmation ? 'true' : 'false',
178184
'oc_dataURL' => is_string($dataLocation) ? '"' . $dataLocation . '"' : 'false',
179185
'_oc_webroot' => '"' . \OC::$WEBROOT . '"',
180186
'_oc_appswebroots' => str_replace('\\/', '/', json_encode($apps_paths)), // Ugly unescape slashes waiting for better solution
@@ -304,10 +310,31 @@ protected function canUserValidatePassword(): bool {
304310
try {
305311
$token = $this->tokenProvider->getToken($this->session->getId());
306312
} catch (ExpiredTokenException|WipeTokenException|InvalidTokenException|SessionNotAvailableException) {
307-
// actually we do not know, so we fall back to this statement
313+
// If token lookup fails, fall back to allowing password validation in the UI.
308314
return true;
309315
}
310316
$scope = $token->getScopeAsArray();
311317
return !isset($scope[IToken::SCOPE_SKIP_PASSWORD_VALIDATION]) || $scope[IToken::SCOPE_SKIP_PASSWORD_VALIDATION] === false;
312318
}
319+
320+
private function canBackendConfirmPassword(?IUser $user): bool {
321+
if ($user === null) {
322+
return false;
323+
}
324+
325+
$backend = $user->getBackend();
326+
if (
327+
$backend instanceof IPasswordConfirmationBackend
328+
&& !$backend->canConfirmPassword($user->getUID())
329+
) {
330+
return false;
331+
}
332+
333+
return !$this->isLegacyBackendExcludedFromPasswordConfirmation($user);
334+
}
335+
336+
private function isLegacyBackendExcludedFromPasswordConfirmation(?IUser $user): bool {
337+
$backendClassName = $user?->getBackendClassName() ?? '';
338+
return isset($this->excludedUserBackEnds[$backendClassName]);
339+
}
313340
}

0 commit comments

Comments
 (0)