Skip to content

Commit fd40fe0

Browse files
committed
Allow using a different Service Account ID for custom token generation
1 parent b59d0ab commit fd40fe0

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

src/Firebase/Auth/CustomTokenViaGoogleCredentials.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,11 @@ final class CustomTokenViaGoogleCredentials
2424

2525
private readonly Parser $parser;
2626

27-
public function __construct(private readonly SignBlobInterface $signer, private readonly ?string $tenantId = null)
27+
public function __construct(
28+
private readonly SignBlobInterface $signer,
29+
private readonly ?string $tenantId = null,
30+
private readonly ?string $serviceAccountIdForTokenGeneration = null,
31+
)
2832
{
2933
$this->encoder = new JoseEncoder();
3034
$this->parser = new Parser($this->encoder);
@@ -43,10 +47,12 @@ public function createCustomToken($uid, array $claims = [], ?DateTimeInterface $
4347
? DT::toUTCDateTimeImmutable($expiresAt)
4448
: $now->add(new DateInterval('PT1H'));
4549

50+
$issAndSub = $this->serviceAccountIdForTokenGeneration ?? $this->signer->getClientName();
51+
4652
$header = ['typ' => 'JWT', 'alg' => 'RS256'];
4753
$payload = [
48-
'iss' => $this->signer->getClientName(),
49-
'sub' => $this->signer->getClientName(),
54+
'iss' => $issAndSub,
55+
'sub' => $issAndSub,
5056
'aud' => 'https://identitytoolkit.googleapis.com/google.identity.identitytoolkit.v1.IdentityToolkit',
5157
'iat' => $now->getTimestamp(),
5258
'exp' => $expiresAt->getTimestamp(),

src/Firebase/Factory.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,11 @@ final class Factory
8989
*/
9090
private ?array $serviceAccount = null;
9191

92+
/**
93+
* @var non-empty-string|null
94+
*/
95+
private ?string $serviceAccountIdForCustomTokenGeneration = null;
96+
9297
private ?FetchAuthTokenInterface $googleAuthTokenCredentials = null;
9398

9499
/**
@@ -170,6 +175,17 @@ public function withServiceAccount(string|array $value): self
170175
return $factory;
171176
}
172177

178+
/**
179+
* @param non-empty-string $serviceAccountId
180+
*/
181+
public function withServiceAccountIdForCustomTokenGeneration(string $serviceAccountId): self
182+
{
183+
$factory = clone $this;
184+
$factory->serviceAccountIdForCustomTokenGeneration = $serviceAccountId;
185+
186+
return $factory;
187+
}
188+
173189
/**
174190
* @param non-empty-string $projectId
175191
*/
@@ -666,7 +682,7 @@ private function createCustomTokenGenerator(): ?CustomTokenViaGoogleCredentials
666682
$credentials = $this->getGoogleAuthTokenCredentials();
667683

668684
if ($credentials instanceof SignBlobInterface) {
669-
return new CustomTokenViaGoogleCredentials($credentials, $this->tenantId);
685+
return new CustomTokenViaGoogleCredentials($credentials, $this->tenantId, $this->serviceAccountIdForCustomTokenGeneration);
670686
}
671687

672688
return null;

0 commit comments

Comments
 (0)