1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<?php
declare(strict_types=1);
namespace Kreait\Firebase\JWT;
use DateInterval;
use Kreait\Clock\SystemClock;
use Kreait\Firebase\JWT\Action\CreateCustomToken;
use Kreait\Firebase\JWT\Action\CreateCustomToken\Handler;
use Kreait\Firebase\JWT\Action\CreateCustomToken\WithHandlerDiscovery;
use Kreait\Firebase\JWT\Contract\Token;
use Kreait\Firebase\JWT\Error\CustomTokenCreationFailed;
use Kreait\Firebase\JWT\Value\Duration;
final class CustomTokenGenerator
{
private Handler $handler;
private ?string $tenantId = null;
public function __construct(Handler $handler)
{
$this->handler = $handler;
}
public static function withClientEmailAndPrivateKey(string $clientEmail, string $privateKey): self
{
$handler = new WithHandlerDiscovery($clientEmail, $privateKey, new SystemClock());
return new self($handler);
}
public function withTenantId(string $tenantId): self
{
$generator = clone $this;
$generator->tenantId = $tenantId;
return $generator;
}
public function execute(CreateCustomToken $action): Token
{
if ($this->tenantId) {
$action = $action->withTenantId($this->tenantId);
}
return $this->handler->handle($action);
}
/**
* @param array<string, mixed> $claims
* @param Duration|DateInterval|string|int $timeToLive
*
* @throws CustomTokenCreationFailed
*/
public function createCustomToken(string $uid, array $claims = null, $timeToLive = null): Token
{
$action = CreateCustomToken::forUid($uid);
if ($claims !== null) {
$action = $action->withCustomClaims($claims);
}
if ($timeToLive !== null) {
$action = $action->withTimeToLive($timeToLive);
}
return $this->execute($action);
}
}