Skip to content

Commit

Permalink
Merge pull request #162 from oat-sa/feature/TR-5569/configurable-jwt-ttl
Browse files Browse the repository at this point in the history
feat: allow setting custom JWT TTL
  • Loading branch information
wazelin authored Jun 12, 2023
2 parents d525604 + 902a63a commit ee2f9d6
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 5 deletions.
6 changes: 5 additions & 1 deletion .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ jobs:
include:
- php: 8.0
coverage: "false" # PHPUnit 8.5.14 doesn't support code coverage under PHP 8
- php: 8.1
coverage: "false" # PHPUnit 8.5.14 doesn't support code coverage under PHP 8
- php: 8.2
coverage: "false" # PHPUnit 8.5.14 doesn't support code coverage under PHP 8

steps:
- name: Checkout
Expand All @@ -39,7 +43,7 @@ jobs:
- name: Psalm
run: |
./vendor/bin/psalm --shepherd
- name: Coveralls
if: ${{ matrix.coverage == 'true' }}
env:
Expand Down
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

6.8.0
-----

* Allowed setting custom JWT TTL

6.7.2
-----

Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
"license": "GPL-2.0-only",
"require": {
"php": ">=7.2.0",
"ext-openssl": "*",
"ext-json": "*",
"ext-openssl": "*",
"codercat/jwk-to-pem": "^1.0",
"guzzlehttp/guzzle": "^6.5 || ^7.0",
"lcobucci/jwt": "^3.4 || ^4.1.5",
"league/oauth2-server": "^8.2",
"nesbot/carbon": "^2.43",
"nyholm/psr7": "^1.3",
"php-http/message-factory": "^1.1",
"phpseclib/phpseclib": "^2.0.31",
"psr/cache": "^1.0 || ^2.0 || ^3.0",
"psr/http-message": "^1.0",
Expand Down
13 changes: 10 additions & 3 deletions src/Security/Jwt/Builder/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,17 @@ class Builder implements BuilderInterface
/** @var IdGeneratorInterface */
private $generator;

public function __construct(?ConfigurationFactory $factory = null, ?IdGeneratorInterface $generator = null)
{
/** @var int */
private $messageTtl;

public function __construct(
?ConfigurationFactory $factory = null,
?IdGeneratorInterface $generator = null,
int $messageTtl = MessagePayloadInterface::TTL
) {
$this->factory = $factory ?? new ConfigurationFactory();
$this->generator = $generator ?? new IdGenerator();
$this->messageTtl = $messageTtl;
}

/**
Expand All @@ -68,7 +75,7 @@ public function build(array $headers, array $claims, KeyInterface $key): TokenIn
MessagePayloadInterface::CLAIM_JTI => $this->generator->generate(),
MessagePayloadInterface::CLAIM_IAT => $now->toDateTimeImmutable(),
MessagePayloadInterface::CLAIM_NBF => $now->toDateTimeImmutable(),
MessagePayloadInterface::CLAIM_EXP => $now->addSeconds(MessagePayloadInterface::TTL)->toDateTimeImmutable(),
MessagePayloadInterface::CLAIM_EXP => $now->addSeconds($this->messageTtl)->toDateTimeImmutable(),
]
);

Expand Down

0 comments on commit ee2f9d6

Please sign in to comment.