Spomky-Labs / otphp

:closed_lock_with_key: A PHP library for generating one time passwords according to RFC 4226 (HOTP) and the RFC 6238 (TOTP)
MIT License
1.31k stars 150 forks source link

PSR Clock "null" used! #228

Closed fiSCIENCES closed 3 months ago

fiSCIENCES commented 3 months ago

Version(s) affected

11.3.0

Description

Hello,

Can this be fixed soon? PHP Deprecated: Since spomky-labs/otphp 11.3.0: The parameter "$clock" will become mandatory in 12.0.0. Please set a valid PSR Clock implementation instead of "null". in /app/vendor/symfony/deprecation-contracts/function.php on line 25

Thank you.

How to reproduce

$otp = TOTP::createFromSecret($secret)

Possible Solution

No response

Additional Context

No response

Spomky commented 3 months ago

Hi @fiSCIENCES,

It is up to you to decide the clock implementation you want to use ant pass it as an argument. There is nothing to fix right now.

fiSCIENCES commented 3 months ago

OK, can't find doc on how to use it for both call (create and verify).

Thank you.

Spomky commented 3 months ago

Indeed, I missed it.

$clock = ... // Object that implements ClockInterface
$otp = TOTP::createFromSecret($secret, clock: $clock)
fiSCIENCES commented 3 months ago

Doesn't help me enough. Could you provide working exemple? Why null was ok and in release 12 it will not be ok? Default value? Thank you

Spomky commented 3 months ago

The clock object is a simple class that implements the PSR interface. It has a unique method now that returns a DateTime object. See the one used for the tests. https://github.com/Spomky-Labs/otphp/blob/11.4.x/tests/ClockMock.php

null is still valid and is the default value to prevent unwanted behaviors. It just triggers a deprecation notice to warn users that it will be mandatory for the next major release 12 (not tagged at that time).

Spomky commented 3 months ago

Basic clock implementation:

<?php

declare(strict_types=1);

namespace App;

use DateTimeImmutable;
use Psr\Clock\ClockInterface;

final readonly class Clock implements ClockInterface
{
    public function now(): DateTimeImmutable
    {
        return new DateTimeImmutable();
    }
} 
fiSCIENCES commented 3 months ago

Wow, I will wait until release 12.x is out.

Thank you.