Skip to content

Commit

Permalink
Restore support for using a JSON string in the `GOOGLE_APPLICATION_CR…
Browse files Browse the repository at this point in the history
…EDENTIALS` environment variable
  • Loading branch information
jeromegamez committed Feb 11, 2023
1 parent e10fa22 commit dea8f61
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 0 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

## [Unreleased]

### Fixed

* Restored support for using a JSON string in the `GOOGLE_APPLICATION_CREDENTIALS` environment variable.
([#767](https://github.com/kreait/firebase-php/pull/767))

## [7.0.2] - 2023-01-27

### Fixed
Expand Down
6 changes: 6 additions & 0 deletions src/Firebase/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,12 @@ public function __construct()
$this->authTokenCache = new MemoryCacheItemPool();
$this->keySetCache = new MemoryCacheItemPool();
$this->httpClientOptions = HttpClientOptions::default();

$googleApplicationCredentials = Util::getenv('GOOGLE_APPLICATION_CREDENTIALS');

if ($googleApplicationCredentials !== null && str_starts_with($googleApplicationCredentials, '{')) {
$this->serviceAccount = Json::decode($googleApplicationCredentials, true);
}
}

/**
Expand Down
113 changes: 113 additions & 0 deletions tests/Integration/ServiceAccountTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<?php

declare(strict_types=1);

namespace Kreait\Firebase\Tests\Integration;

use Beste\Json;
use Kreait\Firebase\Factory;
use Kreait\Firebase\Util;
use PHPUnit\Framework\TestCase;

use function assert;

/**
* @internal
*/
final class ServiceAccountTest extends TestCase
{
/** @var non-empty-string */
private static string $credentialsPath;
private static bool $credentialsPathIsTemporary = false;

public static function setUpBeforeClass(): void
{
$credentialsFromEnvironment = Util::getenv('GOOGLE_APPLICATION_CREDENTIALS');

if ($credentialsFromEnvironment !== null && str_starts_with($credentialsFromEnvironment, '{')) {
// Don't overwrite the fixtures file
$credentialsPath = __DIR__.'/test_credentials.json';
self::$credentialsPathIsTemporary = true;

$result = file_put_contents($credentialsPath, $credentialsFromEnvironment);

if ($result === false) {
self::fail("Unable to write credentials to file `{$credentialsPath}`");
}

Util::putenv('GOOGLE_APPLICATION_CREDENTIALS', $credentialsPath);
} elseif (!file_exists($credentialsPath = __DIR__.'/../_fixtures/test_credentials.json')) {
self::markTestSkipped('The integration tests require credentials');
}

self::$credentialsPath = $credentialsPath;
}

public static function tearDownAfterClass(): void
{
if (self::$credentialsPathIsTemporary) {
unlink(self::$credentialsPath);
}
}

public function testWithPathToServiceAccount(): void
{
$factory = (new Factory())->withServiceAccount(self::$credentialsPath);

$this->assertFunctioningConnection($factory);
}

public function testWithJsonString(): void
{
$json = file_get_contents(self::$credentialsPath);
assert($json !== false && $json !== '');

$factory = (new Factory())->withServiceAccount($json);

$this->assertFunctioningConnection($factory);
}

public function testWithArray(): void
{
$json = file_get_contents(self::$credentialsPath);
assert($json !== false && $json !== '');

$array = Json::decode($json, true);

$factory = (new Factory())->withServiceAccount($array);

$this->assertFunctioningConnection($factory);
}

public function testWithGoogleApplicationCredentialsAsFilePath(): void
{
Util::putenv('GOOGLE_APPLICATION_CREDENTIALS', self::$credentialsPath);

$this->assertFunctioningConnection(new Factory());
}

public function testWithGoogleApplicationCredentialsAsJsonString(): void
{
$json = file_get_contents(self::$credentialsPath);
assert($json !== false && $json !== '');

Util::putenv('GOOGLE_APPLICATION_CREDENTIALS', $json);

$this->assertFunctioningConnection(new Factory());
}

private function assertFunctioningConnection(Factory $factory): void
{
$auth = $factory->createAuth();
$user = null;

try {
$user = $auth->createAnonymousUser();
$this->addToAssertionCount(1);
} finally {
if ($user !== null) {
$auth->deleteUser($user->uid);
}
}
}
}

0 comments on commit dea8f61

Please sign in to comment.