From d926f209bc3f0be2a8a57f77699bb4c2d67fab2b Mon Sep 17 00:00:00 2001 From: Fernand Couto Date: Fri, 12 May 2023 15:49:43 +0200 Subject: [PATCH] feat: Add expiration interval in jwt token to Embed --- src/Embed.php | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/Embed.php b/src/Embed.php index 4e5575e..a525cab 100644 --- a/src/Embed.php +++ b/src/Embed.php @@ -2,6 +2,8 @@ namespace Metabase; +use DateInterval; +use DateTimeImmutable; use Lcobucci\JWT\Configuration; use Lcobucci\JWT\Signer\Key\InMemory; use Lcobucci\JWT\Signer\Hmac\Sha256; @@ -24,6 +26,8 @@ class Embed private $jwtConfig; + private $expirationSeconds; + /** * Default constructor * @@ -33,8 +37,9 @@ class Embed * @param string $width Set css width of dashboard/question (default = 100%) * @param string $height Set css height of dashboard/question (default = 800) * @param bool $border Show dashboard/question border (default = true) + * @param int $expirationSeconds Set jwt token expiration in seconds (default = null) */ - public function __construct($url, $key, $title = false, $width = '100%', $height = '800', $border = true) + public function __construct($url, $key, $title = false, $width = '100%', $height = '800', $border = true, $expirationSeconds = null) { $this->url = $url; $this->key = $key; @@ -42,6 +47,7 @@ public function __construct($url, $key, $title = false, $width = '100%', $height $this->title = $title; $this->width = $width; $this->height = $height; + $this->expirationSeconds = $expirationSeconds; $this->jwtConfig = Configuration::forSymmetricSigner(new Sha256(), InMemory::plainText($this->key)); } @@ -89,6 +95,9 @@ private function encode($resource, $params) } else { $jwt->withClaim('params', $params); } + if (!is_null($this->expirationSeconds)) { + $jwt->expiresAt((new DateTimeImmutable())->modify('+' . $this->expirationSeconds . ' seconds')); + } return $jwt->getToken($this->jwtConfig->signer(), $this->jwtConfig->signingKey()); }