diff --git a/src/bundle/Resources/config/two_factor_provider_google.php b/src/bundle/Resources/config/two_factor_provider_google.php index 84fad478..c1c9bfd2 100644 --- a/src/bundle/Resources/config/two_factor_provider_google.php +++ b/src/bundle/Resources/config/two_factor_provider_google.php @@ -8,6 +8,7 @@ use Scheb\TwoFactorBundle\Security\TwoFactor\Provider\Google\GoogleAuthenticatorTwoFactorProvider; use Scheb\TwoFactorBundle\Security\TwoFactor\Provider\Google\GoogleTotpFactory; use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator; +use Symfony\Component\DependencyInjection\Loader\Configurator\ReferenceConfigurator; use function Symfony\Component\DependencyInjection\Loader\Configurator\service; return static function (ContainerConfigurator $container): void { @@ -18,6 +19,7 @@ '%scheb_two_factor.google.server_name%', '%scheb_two_factor.google.issuer%', '%scheb_two_factor.google.digits%', + (new ReferenceConfigurator('clock'))->nullOnInvalid(), ]) ->set('scheb_two_factor.security.google_authenticator', GoogleAuthenticator::class) diff --git a/src/bundle/Resources/config/two_factor_provider_totp.php b/src/bundle/Resources/config/two_factor_provider_totp.php index febf34d6..44d2801d 100644 --- a/src/bundle/Resources/config/two_factor_provider_totp.php +++ b/src/bundle/Resources/config/two_factor_provider_totp.php @@ -8,6 +8,7 @@ use Scheb\TwoFactorBundle\Security\TwoFactor\Provider\Totp\TotpAuthenticatorTwoFactorProvider; use Scheb\TwoFactorBundle\Security\TwoFactor\Provider\Totp\TotpFactory; use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator; +use Symfony\Component\DependencyInjection\Loader\Configurator\ReferenceConfigurator; use function Symfony\Component\DependencyInjection\Loader\Configurator\service; return static function (ContainerConfigurator $container): void { @@ -19,6 +20,7 @@ '%scheb_two_factor.totp.server_name%', '%scheb_two_factor.totp.issuer%', '%scheb_two_factor.totp.parameters%', + (new ReferenceConfigurator('clock'))->nullOnInvalid(), ]) ->set('scheb_two_factor.security.totp_authenticator', TotpAuthenticator::class) diff --git a/src/google-authenticator/Security/TwoFactor/Provider/Google/GoogleTotpFactory.php b/src/google-authenticator/Security/TwoFactor/Provider/Google/GoogleTotpFactory.php index b998fa65..1f06cceb 100644 --- a/src/google-authenticator/Security/TwoFactor/Provider/Google/GoogleTotpFactory.php +++ b/src/google-authenticator/Security/TwoFactor/Provider/Google/GoogleTotpFactory.php @@ -6,6 +6,7 @@ use OTPHP\TOTP; use OTPHP\TOTPInterface; +use Psr\Clock\ClockInterface; use Scheb\TwoFactorBundle\Model\Google\TwoFactorInterface; use Scheb\TwoFactorBundle\Security\TwoFactor\Provider\Exception\TwoFactorProviderLogicException; use function strlen; @@ -19,6 +20,7 @@ public function __construct( private readonly string|null $server, private readonly string|null $issuer, private readonly int $digits, + private readonly ClockInterface|null $clock = null, ) { } @@ -30,7 +32,7 @@ public function createTotpForUser(TwoFactorInterface $user): TOTPInterface } /** @psalm-suppress ArgumentTypeCoercion */ - $totp = TOTP::create($secret, 30, 'sha1', $this->digits); + $totp = TOTP::create($secret, 30, 'sha1', $this->digits, clock: $this->clock); $userAndHost = $user->getGoogleAuthenticatorUsername().(null !== $this->server && $this->server ? '@'.$this->server : ''); $totp->setLabel($userAndHost); diff --git a/src/totp/Security/TwoFactor/Provider/Totp/TotpFactory.php b/src/totp/Security/TwoFactor/Provider/Totp/TotpFactory.php index 252247f0..d9f3c193 100644 --- a/src/totp/Security/TwoFactor/Provider/Totp/TotpFactory.php +++ b/src/totp/Security/TwoFactor/Provider/Totp/TotpFactory.php @@ -6,6 +6,7 @@ use OTPHP\TOTP; use OTPHP\TOTPInterface; +use Psr\Clock\ClockInterface; use Scheb\TwoFactorBundle\Model\Totp\TwoFactorInterface; use Scheb\TwoFactorBundle\Security\TwoFactor\Provider\Exception\TwoFactorProviderLogicException; use function strlen; @@ -22,6 +23,7 @@ public function __construct( private readonly string|null $server, private readonly string|null $issuer, private readonly array $customParameters, + private readonly ClockInterface|null $clock = null, ) { } @@ -43,6 +45,7 @@ public function createTotpForUser(TwoFactorInterface $user): TOTPInterface $totpConfiguration->getPeriod(), $totpConfiguration->getAlgorithm(), $totpConfiguration->getDigits(), + clock: $this->clock, ); $userAndHost = $user->getTotpAuthenticationUsername().(null !== $this->server && $this->server ? '@'.$this->server : '');