Skip to content

Commit

Permalink
Merge pull request #5 from Fefbug/main
Browse files Browse the repository at this point in the history
feat: Add expiration time in jwt token to Embed
  • Loading branch information
ipeevski committed May 12, 2023
2 parents c173146 + d926f20 commit 19cbd94
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/Embed.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -24,6 +26,8 @@ class Embed

private $jwtConfig;

private $expirationSeconds;

/**
* Default constructor
*
Expand All @@ -33,15 +37,17 @@ 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;
$this->border = $border;
$this->title = $title;
$this->width = $width;
$this->height = $height;
$this->expirationSeconds = $expirationSeconds;

$this->jwtConfig = Configuration::forSymmetricSigner(new Sha256(), InMemory::plainText($this->key));
}
Expand Down Expand Up @@ -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());
}
Expand Down

0 comments on commit 19cbd94

Please sign in to comment.