From 6b3ceb05f9011911aac76b526a270489d34aade4 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Gamez?= <jerome@gamez.name>
Date: Mon, 22 Aug 2022 20:09:50 +0200
Subject: [PATCH] Ensure compatibility with `lcobucci/jwt` ^4.2

---
 CHANGELOG.md                                  |  4 ++
 src/Firebase/Auth/Token/ConvertsDates.php     | 10 ++---
 .../FetchGooglePublicKeys/WithPsr6Cache.php   |  1 +
 .../Action/VerifyIdToken/WithLcobucciJWT.php  |  2 +-
 .../Auth/Token/TenantAwareGeneratorTest.php   | 41 ++++++++++++++++++-
 5 files changed, 49 insertions(+), 9 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 8e56d4d..81d2747 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,5 +1,9 @@
 # CHANGELOG
 
+## Unreleased
+
+Ensure compatibility with `lcobucci/jwt` ^4.2
+
 ## 1.16.1 - 2021-10-03
 
 Update [lcobucci/jwt](https://github.com/lcobucci/jwt) version constraint to `^3.4.6|^4.0.4|^4.1.5` to prevent misuse
diff --git a/src/Firebase/Auth/Token/ConvertsDates.php b/src/Firebase/Auth/Token/ConvertsDates.php
index 744ab3e..7f32975 100644
--- a/src/Firebase/Auth/Token/ConvertsDates.php
+++ b/src/Firebase/Auth/Token/ConvertsDates.php
@@ -4,13 +4,15 @@
 
 namespace Firebase\Auth\Token;
 
-use DateInterval;
 use DateTime;
 use DateTimeImmutable;
 use DateTimeInterface;
 
 trait ConvertsDates
 {
+    /**
+     * @param DateTime|DateTimeImmutable $date
+     */
     protected function convertExpiryDate(DateTimeInterface $date): DateTimeImmutable
     {
         if ($date instanceof DateTimeImmutable) {
@@ -20,11 +22,5 @@ protected function convertExpiryDate(DateTimeInterface $date): DateTimeImmutable
         if ($date instanceof DateTime) {
             return DateTimeImmutable::createFromMutable($date);
         }
-
-        if ($result = DateTimeImmutable::createFromFormat('U.u', $date->format('U.u'))) {
-            return $result;
-        }
-
-        return (new DateTimeImmutable())->add(new DateInterval('PT1H'));
     }
 }
diff --git a/src/JWT/Action/FetchGooglePublicKeys/WithPsr6Cache.php b/src/JWT/Action/FetchGooglePublicKeys/WithPsr6Cache.php
index 68a952f..b79ddbb 100644
--- a/src/JWT/Action/FetchGooglePublicKeys/WithPsr6Cache.php
+++ b/src/JWT/Action/FetchGooglePublicKeys/WithPsr6Cache.php
@@ -33,6 +33,7 @@ public function handle(FetchGooglePublicKeys $action): Keys
 
         /** @noinspection PhpUnhandledExceptionInspection */
         $cacheItem = $this->cache->getItem($cacheKey);
+
         /** @var Keys|null $keys */
         $keys = $cacheItem->get();
 
diff --git a/src/JWT/Action/VerifyIdToken/WithLcobucciJWT.php b/src/JWT/Action/VerifyIdToken/WithLcobucciJWT.php
index c9457be..75b297a 100644
--- a/src/JWT/Action/VerifyIdToken/WithLcobucciJWT.php
+++ b/src/JWT/Action/VerifyIdToken/WithLcobucciJWT.php
@@ -42,7 +42,7 @@ public function __construct(string $projectId, Keys $keys, Clock $clock)
         $this->keys = $keys;
         $this->clock = $clock;
 
-        $this->config = Configuration::forSymmetricSigner(new Sha256(), InMemory::plainText(''));
+        $this->config = Configuration::forSymmetricSigner(new Sha256(), InMemory::empty());
     }
 
     public function handle(VerifyIdToken $action): Token
diff --git a/tests/Firebase/Auth/Token/TenantAwareGeneratorTest.php b/tests/Firebase/Auth/Token/TenantAwareGeneratorTest.php
index cda0434..da601a4 100644
--- a/tests/Firebase/Auth/Token/TenantAwareGeneratorTest.php
+++ b/tests/Firebase/Auth/Token/TenantAwareGeneratorTest.php
@@ -4,14 +4,16 @@
 
 namespace Firebase\Auth\Token\Tests;
 
+use DateTimeImmutable;
 use Firebase\Auth\Token\Domain;
 use Firebase\Auth\Token\TenantAwareGenerator;
+use Lcobucci\JWT\Token;
 use Lcobucci\JWT\Token\Plain;
 
 /**
  * @internal
  */
-class TenantAwareGeneratorTest extends GeneratorTest
+class TenantAwareGeneratorTest extends TestCase
 {
     /** @var TenantAwareGenerator */
     protected Domain\Generator $generator;
@@ -33,4 +35,41 @@ public function testGenerateWithTenantId(): void
 
         $this->assertSame($this->tenantId, $token->claims()->get('tenant_id'));
     }
+
+    public function testCreateCustomToken(): void
+    {
+        $token = $this->generator->createCustomToken('some-uid', ['some' => 'claim']);
+
+        $this->assertInstanceOf(Token::class, $token);
+    }
+
+    public function testCreateCustomTokenWithEmptyClaims(): void
+    {
+        $token = $this->generator->createCustomToken('some-uid');
+        $this->assertInstanceOf(Token\Plain::class, $token);
+
+        $this->assertSame('some-uid', $token->claims()->get('uid'));
+    }
+
+    public function testCreateCustomTokenWithCustomExpiration(): void
+    {
+        $expiresAt = (new DateTimeImmutable())->modify(\random_int(1, 3600).' minutes');
+
+        $token = $this->generator->createCustomToken('some-uid', [], $expiresAt);
+        $this->assertInstanceOf(Token\Plain::class, $token);
+
+        $this->assertSame($expiresAt->getTimestamp(), $token->claims()->get('exp')->getTimestamp());
+    }
+
+    public function testDontCarryStateBetweenCalls(): void
+    {
+        $token1 = $this->generator->createCustomToken('first', ['admin' => true]);
+        $token2 = $this->generator->createCustomToken('second');
+
+        $this->assertInstanceOf(Token\Plain::class, $token1);
+        $this->assertInstanceOf(Token\Plain::class, $token2);
+
+        $this->assertSame(['admin' => true], $token1->claims()->get('claims'));
+        $this->assertSame([], $token2->claims()->get('claims', []));
+    }
 }