Skip to content
Open
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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ Please update your remote URL if you have forked or cloned the repository.

## Unreleased

### App Check

* Added support for managing App Check debug tokens: creating, listing, getting, updating and deleting
debug tokens. The methods are exposed through the transitional
`Kreait\Firebase\Contract\AppCheckWithDebugTokens` contract.

## 8.7.0 - 2026-11-21

* Added Firebase Emulator support
Expand Down
6 changes: 6 additions & 0 deletions NEXT_MAJOR_TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,9 @@
- Fold replay protection into `Kreait\Firebase\Contract\AppCheck::verifyToken()` (e.g. argument/options),
and remove the transitional `Kreait\Firebase\Contract\AppCheckWithReplayProtection` contract.
- Update docs and integration tests accordingly after the API consolidation.

## App Check Debug Tokens

- Context: `Kreait\Firebase\Contract\AppCheckWithDebugTokens` was introduced as a transitional contract
to preserve backwards compatibility in the current major release.
- Move its methods into `Kreait\Firebase\Contract\AppCheck` and remove the transitional contract.
36 changes: 36 additions & 0 deletions docs/app-check.rst
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,39 @@ See https://firebase.google.com/docs/app-check/custom-provider for more informat
.. code-block:: php

$token = $appCheck->createToken("com.example.app-id");

.. _debug-tokens:

************
Debug Tokens
************

Debug tokens let apps running in debug environments, emulators or CI obtain valid App Check tokens without
a real attestation provider. You can register and manage debug tokens with the SDK; the app then uses the
secret with the App Check debug provider. See https://firebase.google.com/docs/app-check/web/debug-provider
for more information.

.. note::
Debug token support is exposed through ``Kreait\Firebase\Contract\AppCheckWithDebugTokens`` as a
transitional API to preserve backwards compatibility in the current major version.

.. code-block:: php

use Kreait\Firebase\Contract\AppCheck;
use Kreait\Firebase\Contract\AppCheckWithDebugTokens;

/** @var AppCheck&AppCheckWithDebugTokens $appCheck */
$appId = '1:123456789:ios:abcdef';

// Register a debug token. If no secret is given, a random UUID4 is generated.
// The secret is only available on the returned object; the API never returns it again.
// Display names can be at most 50 characters long.
$debugToken = $appCheck->createDebugToken($appId, 'CI');
$secret = $debugToken->token; // Hand this to the app's App Check debug provider

// Manage registered debug tokens (an app can have at most 20)
$tokens = $appCheck->listDebugTokens($appId);
$debugToken = $appCheck->getDebugToken($appId, $debugToken->id);
$debugToken = $appCheck->updateDebugToken($appId, $debugToken->id, 'CI (renamed)');
$appCheck->deleteDebugToken($appId, $debugToken->id);

62 changes: 61 additions & 1 deletion src/AppCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,24 @@
use Kreait\Firebase\AppCheck\AppCheckTokenGenerator;
use Kreait\Firebase\AppCheck\AppCheckTokenOptions;
use Kreait\Firebase\AppCheck\AppCheckTokenVerifier;
use Kreait\Firebase\AppCheck\DebugToken;
use Kreait\Firebase\AppCheck\VerifyAppCheckTokenResponse;
use Kreait\Firebase\Contract\AppCheckWithDebugTokens;
use Kreait\Firebase\Contract\AppCheckWithReplayProtection;
use SensitiveParameter;

use function bin2hex;
use function chr;
use function is_array;
use function ord;
use function random_bytes;
use function str_split;
use function vsprintf;

/**
* @internal
*/
final readonly class AppCheck implements Contract\AppCheck, AppCheckWithReplayProtection
final readonly class AppCheck implements Contract\AppCheck, AppCheckWithReplayProtection, AppCheckWithDebugTokens
{
public function __construct(
private ApiClient $client,
Expand Down Expand Up @@ -48,4 +56,56 @@ public function verifyTokenWithReplayProtection(#[SensitiveParameter] string $ap
{
return $this->tokenVerifier->verifyToken($appCheckToken, true);
}

public function createDebugToken(string $appId, string $displayName, #[SensitiveParameter] ?string $token = null): DebugToken
{
$token ??= $this->generateUuid4();

return DebugToken::fromArray($this->client->createDebugToken($appId, $displayName, $token), $token);
}

public function getDebugToken(string $appId, string $debugTokenId): DebugToken
{
return DebugToken::fromArray($this->client->getDebugToken($appId, $debugTokenId));
}

public function listDebugTokens(string $appId): array
{
$tokens = [];
$pageToken = null;

do {
$result = $this->client->listDebugTokens($appId, $pageToken);

foreach ($result['debugTokens'] ?? [] as $data) {
$tokens[] = DebugToken::fromArray($data);
}

$pageToken = $result['nextPageToken'] ?? null;
} while ($pageToken !== null && $pageToken !== '');

return $tokens;
}

public function updateDebugToken(string $appId, string $debugTokenId, string $displayName): DebugToken
{
return DebugToken::fromArray($this->client->updateDebugToken($appId, $debugTokenId, $displayName));
}

public function deleteDebugToken(string $appId, string $debugTokenId): void
{
$this->client->deleteDebugToken($appId, $debugTokenId);
}

/**
* @return non-empty-string
*/
private function generateUuid4(): string
{
$bytes = random_bytes(16);
$bytes[6] = chr((ord($bytes[6]) & 0x0F) | 0x40);
$bytes[8] = chr((ord($bytes[8]) & 0x3F) | 0x80);

return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($bytes), 4));
}
}
102 changes: 101 additions & 1 deletion src/AppCheck/ApiClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
* @internal
*
* @phpstan-import-type AppCheckTokenShape from AppCheckToken
* @phpstan-import-type DebugTokenShape from DebugToken
*/
final readonly class ApiClient
{
Expand Down Expand Up @@ -49,6 +50,94 @@ public function exchangeCustomToken(string $appId, string $customToken): array
return $decoded;
}

/**
* @throws AppCheckException
*
* @return DebugTokenShape
*/
public function createDebugToken(string $appId, string $displayName, #[SensitiveParameter] string $token): array
{
$response = $this->post('apps/'.$appId.'/debugTokens', [
'headers' => [
'Content-Type' => 'application/json; UTF-8',
],
'body' => Json::encode([
'displayName' => $displayName,
'token' => $token,
]),
]);

/** @var DebugTokenShape $decoded */
$decoded = Json::decode((string) $response->getBody(), true);

return $decoded;
}

/**
* @throws AppCheckException
*
* @return DebugTokenShape
*/
public function getDebugToken(string $appId, string $debugTokenId): array
{
$response = $this->request('GET', 'apps/'.$appId.'/debugTokens/'.$debugTokenId);

/** @var DebugTokenShape $decoded */
$decoded = Json::decode((string) $response->getBody(), true);

return $decoded;
}

/**
* @throws AppCheckException
*
* @return array{debugTokens?: list<DebugTokenShape>, nextPageToken?: string}
*/
public function listDebugTokens(string $appId, ?string $pageToken = null): array
{
$options = $pageToken !== null && $pageToken !== '' ? ['query' => ['pageToken' => $pageToken]] : [];

$response = $this->request('GET', 'apps/'.$appId.'/debugTokens', $options);

/** @var array{debugTokens?: list<DebugTokenShape>, nextPageToken?: string} $decoded */
$decoded = Json::decode((string) $response->getBody(), true);

return $decoded;
}

/**
* @throws AppCheckException
*
* @return DebugTokenShape
*/
public function updateDebugToken(string $appId, string $debugTokenId, string $displayName): array
{
$response = $this->request('PATCH', 'apps/'.$appId.'/debugTokens/'.$debugTokenId, [
'headers' => [
'Content-Type' => 'application/json; UTF-8',
],
'query' => [
'updateMask' => 'displayName',
],
'body' => Json::encode([
'displayName' => $displayName,
]),
]);

/** @var DebugTokenShape $decoded */
$decoded = Json::decode((string) $response->getBody(), true);

return $decoded;
}

/**
* @throws AppCheckException
*/
public function deleteDebugToken(string $appId, string $debugTokenId): void
{
$this->request('DELETE', 'apps/'.$appId.'/debugTokens/'.$debugTokenId);
}

/**
* @param non-empty-string $projectId
*
Expand Down Expand Up @@ -86,11 +175,22 @@ public function verifyReplayProtection(#[SensitiveParameter] string $token, stri
* @throws AppCheckException
*/
private function post(string $path, ?array $options = null): ResponseInterface
{
return $this->request('POST', $path, $options);
}

/**
* @param non-empty-string $method
* @param non-empty-string $path
* @param array<string, mixed>|null $options
* @throws AppCheckException
*/
private function request(string $method, string $path, ?array $options = null): ResponseInterface
{
$options ??= [];

try {
return $this->client->request('POST', $path, $options);
return $this->client->request($method, $path, $options);
} catch (Throwable $e) {
throw $this->errorHandler->convertException($e);
}
Expand Down
65 changes: 65 additions & 0 deletions src/AppCheck/DebugToken.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

declare(strict_types=1);

namespace Kreait\Firebase\AppCheck;

use DateTimeImmutable;
use Kreait\Firebase\Util\DT;
use SensitiveParameter;

use function strrpos;
use function substr;

/**
* A debug token registered for a Firebase App.
*
* The secret token value is only known when the debug token is created, the App Check API never returns it.
*
* @see https://firebase.google.com/docs/reference/appcheck/rest/v1/projects.apps.debugTokens
*
* @phpstan-type DebugTokenShape array{
* name: non-empty-string,
* displayName: string,
* updateTime?: string,
* etag?: string
* }
*/
final readonly class DebugToken
{
/**
* @param non-empty-string $name
* @param non-empty-string $id
* @param non-empty-string|null $token
*/
private function __construct(
public string $name,
public string $id,
public string $displayName,
public ?DateTimeImmutable $updateTime,
#[SensitiveParameter]
public ?string $token,
) {
}

/**
* @param DebugTokenShape $data
* @param non-empty-string|null $token
*/
public static function fromArray(array $data, #[SensitiveParameter] ?string $token = null): self
{
$name = $data['name'];
$lastSlash = strrpos($name, '/');
$id = $lastSlash === false ? $name : substr($name, $lastSlash + 1);

$updateTime = isset($data['updateTime']) ? DT::toUTCDateTimeImmutable($data['updateTime']) : null;

return new self(
$name,
$id !== '' ? $id : $name,
$data['displayName'],
$updateTime,
$token,
);
}
}
Loading