From b0505826df0977c9ba0832d7a80e8e6e925402d8 Mon Sep 17 00:00:00 2001 From: Michael Babker Date: Thu, 1 Dec 2022 19:18:32 -0500 Subject: [PATCH] Fix PHP 8.2 compat issue in uses of DateTime::modify() --- Model/AbstractRefreshToken.php | 8 +++++++- .../Http/Authenticator/RefreshTokenAuthenticator.php | 9 ++++++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/Model/AbstractRefreshToken.php b/Model/AbstractRefreshToken.php index ee3d5a8f..5cfa3d4d 100644 --- a/Model/AbstractRefreshToken.php +++ b/Model/AbstractRefreshToken.php @@ -41,7 +41,13 @@ abstract class AbstractRefreshToken implements RefreshTokenInterface public static function createForUserWithTtl(string $refreshToken, UserInterface $user, int $ttl): RefreshTokenInterface { $valid = new \DateTime(); - $valid->modify('+'.$ttl.' seconds'); + + // Explicitly check for a negative number based on a behavior change in PHP 8.2, see https://github.com/php/php-src/issues/9950 + if ($ttl > 0) { + $valid->modify('+'.$ttl.' seconds'); + } elseif ($ttl < 0) { + $valid->modify($ttl.' seconds'); + } $model = new static(); $model->setRefreshToken($refreshToken); diff --git a/Security/Http/Authenticator/RefreshTokenAuthenticator.php b/Security/Http/Authenticator/RefreshTokenAuthenticator.php index bf6e0a0b..babd06b5 100644 --- a/Security/Http/Authenticator/RefreshTokenAuthenticator.php +++ b/Security/Http/Authenticator/RefreshTokenAuthenticator.php @@ -116,7 +116,14 @@ public function authenticate(Request $request): Passport if ($this->options['ttl_update']) { $expirationDate = new \DateTime(); - $expirationDate->modify(sprintf('+%d seconds', $this->options['ttl'])); + + // Explicitly check for a negative number based on a behavior change in PHP 8.2, see https://github.com/php/php-src/issues/9950 + if ($this->options['ttl'] > 0) { + $expirationDate->modify(sprintf('+%d seconds', $this->options['ttl'])); + } elseif ($this->options['ttl'] < 0) { + $expirationDate->modify(sprintf('%d seconds', $this->options['ttl'])); + } + $refreshToken->setValid($expirationDate); $this->refreshTokenManager->save($refreshToken);