Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TOTP::expiresIn() is not correct when setting epoch #236

Open
tomme87 opened this issue Feb 25, 2025 · 1 comment
Open

TOTP::expiresIn() is not correct when setting epoch #236

tomme87 opened this issue Feb 25, 2025 · 1 comment
Assignees
Labels
Milestone

Comments

@tomme87
Copy link

tomme87 commented Feb 25, 2025

Version(s) affected

11.3.0

Description

Seams like epoch is not taken into consideration when doing TOTP::expiresIn()

How to reproduce

$clock = new MockClock('2025-02-24 15:10:33');
$now = $clock->now();
$totp = TOTP::generate($clock);
$totp->setPeriod(1000);
$totp->setEpoch($now->getTimestamp());
echo $totp->expiresIn()

This returns 167, but since i set the epoch to the current timestamp it should be 1000.

The actual expiration is correct though.

$clock->sleep(999);
echo $totp->verify($code) ? 'yes' : 'no';
$clock->sleep(1);
echo $totp->verify($code) ? 'yes' : 'no';

This returns yes and no on the second echo.

Possible Solution

I think the correct expiresIn should be (?)

$totp->getPeriod() - (($clock->now()->getTimestamp() - $totp->getEpoch()) % $totp->getPeriod())

Additional Context

No response

@Spomky Spomky self-assigned this Feb 25, 2025
@Spomky Spomky added the bug label Feb 25, 2025
@Spomky Spomky added this to the 11.2.3 milestone Feb 25, 2025
@Spomky
Copy link
Member

Spomky commented Feb 25, 2025

Hello @tomme87,

Indeed the returned value does not consider the epoc parameter.
Would you mind to create a PR to fix this (with a test if possible)?

Many thanks.
Regards.

@tomme87 tomme87 mentioned this issue Feb 26, 2025
4 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants