From 21099f1aff81706781578a19335d8a4c7c96422a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jacob=20M=C3=BCller?= Date: Thu, 21 Mar 2024 19:44:57 +0100 Subject: [PATCH] feat: allow token "expires in" to be defined as date interval (#1733) --- src/Passport.php | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/src/Passport.php b/src/Passport.php index 97757cbe..ccfe6455 100644 --- a/src/Passport.php +++ b/src/Passport.php @@ -288,16 +288,18 @@ public static function tokensCan(array $scopes) /** * Get or set when access tokens expire. * - * @param \DateTimeInterface|null $date + * @param \DateTimeInterface|\DateInterval|null $date * @return \DateInterval|static */ - public static function tokensExpireIn(DateTimeInterface $date = null) + public static function tokensExpireIn(DateTimeInterface|DateInterval $date = null) { if (is_null($date)) { return static::$tokensExpireIn ?? new DateInterval('P1Y'); } - static::$tokensExpireIn = Carbon::now()->diff($date); + static::$tokensExpireIn = $date instanceof DateTimeInterface + ? Carbon::now()->diff($date) + : $date; return new static; } @@ -305,16 +307,18 @@ public static function tokensExpireIn(DateTimeInterface $date = null) /** * Get or set when refresh tokens expire. * - * @param \DateTimeInterface|null $date + * @param \DateTimeInterface|\DateInterval|null $date * @return \DateInterval|static */ - public static function refreshTokensExpireIn(DateTimeInterface $date = null) + public static function refreshTokensExpireIn(DateTimeInterface|DateInterval $date = null) { if (is_null($date)) { return static::$refreshTokensExpireIn ?? new DateInterval('P1Y'); } - static::$refreshTokensExpireIn = Carbon::now()->diff($date); + static::$refreshTokensExpireIn = $date instanceof DateTimeInterface + ? Carbon::now()->diff($date) + : $date; return new static; } @@ -322,16 +326,18 @@ public static function refreshTokensExpireIn(DateTimeInterface $date = null) /** * Get or set when personal access tokens expire. * - * @param \DateTimeInterface|null $date + * @param \DateTimeInterface|\DateInterval|null $date * @return \DateInterval|static */ - public static function personalAccessTokensExpireIn(DateTimeInterface $date = null) + public static function personalAccessTokensExpireIn(DateTimeInterface|DateInterval $date = null) { if (is_null($date)) { return static::$personalAccessTokensExpireIn ?? new DateInterval('P1Y'); } - static::$personalAccessTokensExpireIn = Carbon::now()->diff($date); + static::$personalAccessTokensExpireIn = $date instanceof DateTimeInterface + ? Carbon::now()->diff($date) + : $date; return new static; }