Skip to content

Commit 1ec9ddf

Browse files
authored
Update lcobucci/jwt to ^3.4. (#59)
1 parent 304bbb8 commit 1ec9ddf

3 files changed

Lines changed: 27 additions & 10 deletions

File tree

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"ext-json": "*",
2222
"composer/ca-bundle": "^1.2",
2323
"guzzlehttp/guzzle": "^6.4 || ^7.1",
24-
"lcobucci/jwt": "^3.3",
24+
"lcobucci/jwt": "^3.4",
2525
"ramsey/uuid": "^4.0",
2626
"tightenco/collect": "^5.8 || ^6.0 || ^7.0 || ^8.0"
2727
},

src/Resources/AccessToken.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ private function parseToken(): void
3030
{
3131
$token = (new Parser)->parse($this->token);
3232

33-
$this->expiresAt = (new DateTimeImmutable)->setTimestamp($token->getClaim('exp')) ?: new DateTimeImmutable;
34-
$this->accounts = $token->getClaim('accounts');
35-
$this->roles = $token->getClaim('roles');
33+
$this->expiresAt = $token->claims()->get('exp') ?: new DateTimeImmutable;
34+
$this->accounts = $token->claims()->get('accounts');
35+
$this->roles = $token->claims()->get('roles');
3636
}
3737

3838
public function isExpired(): bool

tests/Unit/Resources/AccessTokenTest.php

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,30 @@
22

33
namespace Mvdnbrk\DhlParcel\Tests\Unit\Resources;
44

5-
use Lcobucci\JWT\Builder;
5+
use DateInterval;
6+
use DateTimeImmutable;
7+
use Lcobucci\JWT\Configuration;
68
use Mvdnbrk\DhlParcel\Resources\AccessToken;
79
use Mvdnbrk\DhlParcel\Tests\TestCase;
810

911
class AccessTokenTest extends TestCase
1012
{
13+
/** @var \Lcobucci\JWT\Configuration */
14+
protected $config;
15+
16+
protected function setUp(): void
17+
{
18+
parent::setUp();
19+
20+
$this->config = Configuration::forUnsecuredSigner();
21+
}
22+
1123
/** @test */
1224
public function create_a_new_access_token()
1325
{
26+
$expires = (new DateTimeImmutable('1970-01-01 00:00:00'))->add(new DateInterval('PT9S'));
1427
$accessToken = new AccessToken(
15-
(new Builder)->setExpiration(9)->set('accounts', [])->set('roles', [])->getToken()->__toString()
28+
$this->config->builder()->expiresAt($expires)->withClaim('accounts', [])->withClaim('roles', [])->getToken($this->config->signer(), $this->config->signingKey())->toString()
1629
);
1730

1831
$this->assertSame('eyJ0eXAiOiJKV1QiLCJhbGciOiJub25lIn0.eyJleHAiOjksImFjY291bnRzIjpbXSwicm9sZXMiOltdfQ.', $accessToken->token);
@@ -22,14 +35,16 @@ public function create_a_new_access_token()
2235
/** @test */
2336
public function it_can_determine_if_the_access_token_has_expired()
2437
{
38+
$expires = (new DateTimeImmutable())->sub(new DateInterval('PT1S'));
2539
$accessToken = new AccessToken(
26-
(new Builder)->setExpiration(0)->set('accounts', [])->set('roles', [])->getToken()->__toString()
40+
$this->config->builder()->expiresAt($expires)->withClaim('accounts', [])->withClaim('roles', [])->getToken($this->config->signer(), $this->config->signingKey())->toString()
2741
);
2842

2943
$this->assertTrue($accessToken->isExpired());
3044

45+
$expires = (new DateTimeImmutable())->add(new DateInterval('PT9S'));
3146
$accessToken = new AccessToken(
32-
(new Builder)->setExpiration(time() + 999)->set('accounts', [])->set('roles', [])->getToken()->__toString()
47+
$this->config->builder()->expiresAt($expires)->withClaim('accounts', [])->withClaim('roles', [])->getToken($this->config->signer(), $this->config->signingKey())->toString()
3348
);
3449

3550
$this->assertFalse($accessToken->isExpired());
@@ -38,8 +53,9 @@ public function it_can_determine_if_the_access_token_has_expired()
3853
/** @test */
3954
public function it_can_retrieve_the_account_id_from_the_token()
4055
{
56+
$expires = new DateTimeImmutable();
4157
$accessToken = new AccessToken(
42-
(new Builder)->setExpiration(0)->set('accounts', ['123456'])->set('roles', [])->getToken()->__toString()
58+
$this->config->builder()->expiresAt($expires)->withClaim('accounts', ['123456'])->withClaim('roles', [])->getToken($this->config->signer(), $this->config->signingKey())->toString()
4359
);
4460

4561
$this->assertEquals('123456', $accessToken->getAccountId());
@@ -48,8 +64,9 @@ public function it_can_retrieve_the_account_id_from_the_token()
4864
/** @test */
4965
public function it_can_set_the_account_id()
5066
{
67+
$expires = new DateTimeImmutable();
5168
$accessToken = new AccessToken(
52-
(new Builder)->setExpiration(0)->set('accounts', ['1111', '2222'])->set('roles', [])->getToken()->__toString()
69+
$this->config->builder()->expiresAt($expires)->withClaim('accounts', ['1111', '2222'])->withClaim('roles', [])->getToken($this->config->signer(), $this->config->signingKey())->toString()
5370
);
5471

5572
$accessToken->setAccountId('does-not-exist');

0 commit comments

Comments
 (0)