Skip to content

Commit

Permalink
Merge pull request #30025 from nextcloud/backport/30012/stable23
Browse files Browse the repository at this point in the history
[stable23] Only check the twofactor state once per request
  • Loading branch information
ChristophWurst authored Dec 6, 2021
2 parents af29d0b + febca70 commit f88f576
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion lib/private/Authentication/TwoFactorAuth/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ class Manager {
/** @var EventDispatcherInterface */
private $legacyDispatcher;

/** @psalm-var array<string, bool> */
private $userIsTwoFactorAuthenticated = [];

public function __construct(ProviderLoader $providerLoader,
IRegistry $providerRegistry,
MandatoryTwoFactor $mandatoryTwoFactor,
Expand Down Expand Up @@ -118,6 +121,10 @@ public function __construct(ProviderLoader $providerLoader,
* @return boolean
*/
public function isTwoFactorAuthenticated(IUser $user): bool {
if (isset($this->userIsTwoFactorAuthenticated[$user->getUID()])) {
return $this->userIsTwoFactorAuthenticated[$user->getUID()];
}

if ($this->mandatoryTwoFactor->isEnforcedFor($user)) {
return true;
}
Expand All @@ -129,7 +136,8 @@ public function isTwoFactorAuthenticated(IUser $user): bool {
$providerIds = array_keys($enabled);
$providerIdsWithoutBackupCodes = array_diff($providerIds, [self::BACKUP_CODES_PROVIDER_ID]);

return !empty($providerIdsWithoutBackupCodes);
$this->userIsTwoFactorAuthenticated[$user->getUID()] = !empty($providerIdsWithoutBackupCodes);
return $this->userIsTwoFactorAuthenticated[$user->getUID()];
}

/**
Expand Down

0 comments on commit f88f576

Please sign in to comment.