Skip to content

Commit

Permalink
fix: Corrected the single use token time window
Browse files Browse the repository at this point in the history
  • Loading branch information
roelvanhintum committed Feb 8, 2024
1 parent eb93bcb commit 89d2339
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions src/services/Verify.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,9 +183,7 @@ private function getUserRecord(User $user)
*/
private function isTokenUsed($token, User $user): bool
{
$settings = TwoFactorAuth::$plugin->getSettings();
$delay = is_int($settings->totpDelay) ? $settings->totpDelay : 0;
$start = new \DateTime("-$delay seconds");
$start = $this->getTotpStartTime();

// Find the token used by user in the current window.
$userTokenRecord = UserTokenRecord::find()
Expand Down Expand Up @@ -230,9 +228,7 @@ private function insertToken($token, User $user)
*/
public function removeOldTokens(User $user)
{
$settings = TwoFactorAuth::$plugin->getSettings();
$delay = is_int($settings->totpDelay) ? $settings->totpDelay : 0;
$start = new \DateTime("-$delay seconds");
$start = $this->getTotpStartTime();

$userTokenRecords = UserTokenRecord::find()
->where([
Expand All @@ -245,4 +241,16 @@ public function removeOldTokens(User $user)
$userTokenRecord->delete();
}
}

/**
* Get TOTP start time
* @return \DateTime
*/
private function getTotpStartTime(): \DateTime
{
$settings = TwoFactorAuth::$plugin->getSettings();
$delay = is_int($settings->totpDelay) ? $settings->totpDelay : 0;
$window = 31 + $delay; // Default window is 30 seconds, but we add 1 second to be sure.
return new \DateTime("-$window seconds");
}
}

0 comments on commit 89d2339

Please sign in to comment.