Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions src/Firebase/Auth/CustomTokenViaGoogleCredentials.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ final class CustomTokenViaGoogleCredentials

private readonly Parser $parser;

public function __construct(private readonly SignBlobInterface $signer, private readonly ?string $tenantId = null)
public function __construct(
private readonly SignBlobInterface $signer,
private readonly ?string $tenantId = null,
private readonly ?string $serviceAccountIdForTokenGeneration = null,
)
{
$this->encoder = new JoseEncoder();
$this->parser = new Parser($this->encoder);
Expand All @@ -43,10 +47,12 @@ public function createCustomToken($uid, array $claims = [], ?DateTimeInterface $
? DT::toUTCDateTimeImmutable($expiresAt)
: $now->add(new DateInterval('PT1H'));

$issAndSub = $this->serviceAccountIdForTokenGeneration ?? $this->signer->getClientName();

$header = ['typ' => 'JWT', 'alg' => 'RS256'];
$payload = [
'iss' => $this->signer->getClientName(),
'sub' => $this->signer->getClientName(),
'iss' => $issAndSub,
'sub' => $issAndSub,
'aud' => 'https://identitytoolkit.googleapis.com/google.identity.identitytoolkit.v1.IdentityToolkit',
'iat' => $now->getTimestamp(),
'exp' => $expiresAt->getTimestamp(),
Expand Down
18 changes: 17 additions & 1 deletion src/Firebase/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ final class Factory

private ?ServiceAccount $serviceAccount = null;

/**
* @var non-empty-string|null
*/
private ?string $serviceAccountIdForCustomTokenGeneration = null;

private ?FetchAuthTokenInterface $googleAuthTokenCredentials = null;

/**
Expand Down Expand Up @@ -164,6 +169,17 @@ public function withServiceAccount(string|array $value): self
return $factory;
}

/**
* @param non-empty-string $serviceAccountId
*/
public function withServiceAccountIdForCustomTokenGeneration(string $serviceAccountId): self
{
$factory = clone $this;
$factory->serviceAccountIdForCustomTokenGeneration = $serviceAccountId;

return $factory;
}

/**
* @param non-empty-string $projectId
*/
Expand Down Expand Up @@ -685,7 +701,7 @@ private function createCustomTokenGenerator(): ?CustomTokenViaGoogleCredentials
$credentials = $this->getGoogleAuthTokenCredentials();

if ($credentials instanceof SignBlobInterface) {
return new CustomTokenViaGoogleCredentials($credentials, $this->tenantId);
return new CustomTokenViaGoogleCredentials($credentials, $this->tenantId, $this->serviceAccountIdForCustomTokenGeneration);
}

return null;
Expand Down
Loading