Skip to content

Commit 43cc138

Browse files
committed
Change TokenGenerator so Token expiration is settable from the outside.
1 parent 8f9961a commit 43cc138

2 files changed

Lines changed: 25 additions & 4 deletions

File tree

src/TokenGenerator.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,19 @@ class TokenGenerator
1818

1919
use CryptTrait;
2020

21-
/** @var Config */
22-
public $config;
21+
public Config $config;
22+
23+
private \DateInterval $validFor;
2324

2425
//////////////////////////////// PUBLIC API \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
2526

2627
final public function __construct(
27-
Config $config
28+
Config $config,
29+
\DateInterval $validFor
2830
) {
2931
$this->config = $config;
32+
$this->validFor = $validFor;
33+
3034
$this->setEncryptionKey($this->config->getKeys()->getEncryptionKey());
3135
}
3236

@@ -54,7 +58,8 @@ public function generateIdToken($accessToken, $clientId, $subject, $nonce, $priv
5458
$jwtConfig = Configuration::forSymmetricSigner(new Sha256(), InMemory::plainText($privateKey));
5559
$now = new DateTimeImmutable();
5660
$useAfter = $now->sub(new \DateInterval('PT1S'));
57-
$expire = $now->add(new \DateInterval('PT' . 14*24*60*60 . 'S'));
61+
62+
$expire = $now->add($this->validFor);
5863

5964
$token = $jwtConfig->builder()
6065
->issuedBy($issuer)

tests/unit/TokenGeneratorTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,22 @@ final public function testInstantiateWithoutConfig(): void
7878
new TokenGenerator();
7979
}
8080

81+
/**
82+
* @testdox Token Generator SHOULD complain WHEN instantiated without validity period
83+
*
84+
* @coversNothing
85+
*/
86+
final public function testInstantiateWithoutValidFor(): void
87+
{
88+
$this->expectArgumentCountError(2);
89+
90+
$mockConfig = $this->getMockBuilder(Config::class)
91+
->disableOriginalConstructor()
92+
->getMock();
93+
94+
new TokenGenerator($mockConfig);
95+
}
96+
8197
/**
8298
* @testdox Token Generator SHOULD be created WHEN instantiated with Config and validity period
8399
*

0 commit comments

Comments
 (0)