Skip to content

Commit

Permalink
Ensure compatibility with lcobucci/jwt ^4.2
Browse files Browse the repository at this point in the history
  • Loading branch information
jeromegamez committed Aug 22, 2022
1 parent ffea731 commit 6b3ceb0
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 9 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
10 changes: 3 additions & 7 deletions src/Firebase/Auth/Token/ConvertsDates.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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'));
}
}
1 change: 1 addition & 0 deletions src/JWT/Action/FetchGooglePublicKeys/WithPsr6Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public function handle(FetchGooglePublicKeys $action): Keys

/** @noinspection PhpUnhandledExceptionInspection */
$cacheItem = $this->cache->getItem($cacheKey);

/** @var Keys|null $keys */
$keys = $cacheItem->get();

Expand Down
2 changes: 1 addition & 1 deletion src/JWT/Action/VerifyIdToken/WithLcobucciJWT.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
41 changes: 40 additions & 1 deletion tests/Firebase/Auth/Token/TenantAwareGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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', []));
}
}

0 comments on commit 6b3ceb0

Please sign in to comment.