Skip to content

Commit

Permalink
Add Factory::getDebugInfo()
Browse files Browse the repository at this point in the history
  • Loading branch information
jeromegamez committed Aug 30, 2021
1 parent 0b3ba9f commit d58d79b
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 3 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
@@ -1,6 +1,9 @@
# CHANGELOG

## [Unreleased]
### Added
* Added `Factory::getDebugInfo()` to display information about the currently configured factory.
([Documentation](https://firebase-php.readthedocs.io/en/latest/troubleshooting.html#debugging))

## [5.23.0] - 2021-08-26
### Added
Expand Down
38 changes: 38 additions & 0 deletions docs/troubleshooting.rst
Expand Up @@ -248,3 +248,41 @@ responses will then be pushed to this logger with their full headers and bodies.
.. code-block:: php
$factory = $factory->withEnabledDebug($logger);
If you want to make sure that the Factory has the configuration you expect it to have,
call the ``getDebugInfo()`` method:

.. code-block:: php
$factoryInfo = $factory->getDebugInfo();
The output will be something like this:

.. code-block::
Array
(
[credentialsType] => Google\Auth\Credentials\ServiceAccountCredentials
[databaseUrl] => https://project-id-default-rtdb.firebaseio.com
[defaultStorageBucket] =>
[projectId] => project-id
[serviceAccount] => Array
(
[type] => service_account
[project_id] => project-id
[private_key_id] => a1b2c3d4e5f6g7h8i9j0
[private_key] => {exists, redacted}
[client_email] => project-id-xyz@beste-firebase.iam.gserviceaccount.com
[client_id] => 1234567890987654321
[auth_uri] => https://accounts.google.com/o/oauth2/auth
[token_uri] => https://oauth2.googleapis.com/token
[auth_provider_x509_cert_url] => https://www.googleapis.com/oauth2/v1/certs
[client_x509_cert_url] => https://www.googleapis.com/robot/v1/metadata/x509/project-id-xyz%40beste-firebase.iam.gserviceaccount.com
)
[tenantId] =>
[tokenCacheType] => Google\Auth\Cache\MemoryCacheItemPool
[verifierCacheType] => Firebase\Auth\Token\Cache\InMemoryCache
)
The private key of a service account will be redacted.
2 changes: 1 addition & 1 deletion src/Firebase/Auth/IdTokenVerifier.php
Expand Up @@ -69,7 +69,7 @@ public function verifyIdToken($token): Token
}

return $token;
} catch (UnknownKey | InvalidSignature $e) {
} catch (UnknownKey|InvalidSignature $e) {
throw $e;
} catch (ExpiredToken $e) {
// Re-check expiry with the clock
Expand Down
47 changes: 47 additions & 0 deletions src/Firebase/Factory.php
Expand Up @@ -599,6 +599,53 @@ public function createStorage(): Contract\Storage
return new Storage($storageClient, $this->getStorageBucketName());
}

/**
* @codeCoverageIgnore
*
* @return array{
* credentialsType: class-string|null,
* databaseUrl: string,
* defaultStorageBucket: string|null,
* serviceAccount: array{
* client_email: string|null,
* private_key: string|null,
* project_id: string|null,
* type: string
* }|array<string, string|null>|null,
* projectId: string|null,
* tenantId: string|null,
* verifierCacheType: class-string|null,
* }
*/
public function getDebugInfo(): array
{
$projectId = $this->getProjectId();
$credentials = $this->getGoogleAuthTokenCredentials();

try {
$databaseUrl = (string) $this->getDatabaseUri();
} catch (Throwable $e) {
$databaseUrl = $e->getMessage();
}

$serviceAccountInfo = null;
if ($serviceAccount = $this->getServiceAccount()) {
$serviceAccountInfo = $serviceAccount->asArray();
$serviceAccountInfo['private_key'] = $serviceAccountInfo['private_key'] ? '{exists, redacted}' : '{not set}';
}

return [
'credentialsType' => $credentials ? \get_class($credentials) : null,
'databaseUrl' => $databaseUrl,
'defaultStorageBucket' => $this->defaultStorageBucket,
'projectId' => $projectId ? $projectId->value() : null,
'serviceAccount' => $serviceAccountInfo,
'tenantId' => $this->tenantId ? $this->tenantId->toString() : null,
'tokenCacheType' => \get_class($this->authTokenCache),
'verifierCacheType' => \get_class($this->verifierCache),
];
}

/**
* @internal
*
Expand Down
16 changes: 14 additions & 2 deletions src/Firebase/ServiceAccount.php
Expand Up @@ -13,7 +13,14 @@
*/
class ServiceAccount
{
/** @var array<string, string> */
/**
* @var array{
* project_id: string|null,
* client_email: string|null,
* private_key: string|null,
* type?: string|null
* }|array<string, string|null> $data
*/
private array $data = [];

public function getProjectId(): string
Expand All @@ -32,7 +39,12 @@ public function getPrivateKey(): string
}

/**
* @return array<string, string>
* @return array{
* project_id: string|null,
* client_email: string|null,
* private_key: string|null,
* type: string|null
* }|array<string, string|null>
*/
public function asArray(): array
{
Expand Down

0 comments on commit d58d79b

Please sign in to comment.