Skip to content

Commit

Permalink
Add support for caching Firebase authentication tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
jeromegamez committed Dec 4, 2021
1 parent 52b29c7 commit fdbcb97
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# CHANGELOG

## [Unreleased]
### Added
* Added support for caching the authentication tokens used for connecting to the Firebase servers.

## [3.0.0] - 2021-11-30
### Changed
Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,10 @@ kreait_firebase:
tenant_id: 'tenant-id'
# Optional: Default domain for Dynamic Links
default_dynamic_links_domain: 'https://my_project.page.link'
# Optional: Used to cache Google's public keys. Must implement
# \Psr\SimpleCache\CacheInterface (PSR-16)
# Optional: Used to cache Google's public keys.
verifier_cache: null # Example: cache.app
# Optional: Used to cache the authentication tokens for connecting to the Firebase servers.
auth_token_cache: null # Example: cache.app
# If set, logs simple HTTP request and response statuses
http_request_logger: null # Example: monolog.logger.firebase
# If set, logs detailed HTTP request and response statuses
Expand Down
5 changes: 5 additions & 0 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ public function getConfigTreeBuilder(): TreeBuilder
->example('cache.app')
->info('Used to cache Google\'s public keys.')
->end()
->scalarNode('auth_token_cache')
->defaultNull()
->example('cache.app')
->info('Used to cache the authentication tokens for connecting to the Firebase servers.')
->end()
->scalarNode('http_request_logger')
->defaultNull()
->example('monolog.logger.firebase')
Expand Down
18 changes: 18 additions & 0 deletions src/DependencyInjection/Factory/ProjectFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@
use Psr\Cache\CacheItemPoolInterface;
use Psr\Log\LoggerInterface;
use Psr\SimpleCache\CacheInterface;
use Symfony\Component\Cache\Adapter\Psr16Adapter;
use Symfony\Component\Cache\Psr16Cache;

class ProjectFactory
{
private Factory $firebaseFactory;
private ?CacheInterface $verifierCache = null;
private ?CacheItemPoolInterface $authTokenCache = null;
private ?LoggerInterface $httpRequestLogger = null;
private ?LoggerInterface $httpRequestDebugLogger = null;

Expand All @@ -35,6 +37,18 @@ public function setVerifierCache($verifierCache = null): void
$this->verifierCache = $verifierCache;
}

/**
* @param CacheInterface|CacheItemPoolInterface|null $authTokenCache
*/
public function setAuthTokenCache($authTokenCache = null): void
{
if ($authTokenCache instanceof CacheInterface) {
$authTokenCache = new Psr16Adapter($authTokenCache);
}

$this->authTokenCache = $authTokenCache;
}

public function setHttpRequestLogger(?LoggerInterface $logger = null): void
{
$this->httpRequestLogger = $logger;
Expand Down Expand Up @@ -72,6 +86,10 @@ public function createFactory(array $config = []): Factory
$factory = $factory->withVerifierCache($this->verifierCache);
}

if ($this->authTokenCache) {
$factory = $factory->withAuthTokenCache($this->authTokenCache);
}

if ($this->httpRequestLogger) {
$factory = $factory->withHttpLogger($this->httpRequestLogger);
}
Expand Down
4 changes: 4 additions & 0 deletions src/DependencyInjection/FirebaseExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ private function registerService(string $name, array $config, string $class, str
$factory->addMethodCall('setVerifierCache', [new Reference($config['verifier_cache'])]);
}

if ($config['auth_token_cache'] ?? null) {
$factory->addMethodCall('setAuthTokenCache', [new Reference($config['auth_token_cache'])]);
}

if ($config['http_request_logger'] ?? null) {
$factory->addMethodCall('setHttpRequestLogger', [new Reference($config['http_request_logger'])]);
}
Expand Down
32 changes: 32 additions & 0 deletions tests/DependencyInjection/Factory/ProjectFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,36 @@ public function it_accepts_a_PSR6_verifier_cache(): void
$this->factory->setVerifierCache($cache);
$this->factory->createAuth();
}

/**
* @test
*/
public function it_accepts_a_PSR16_auth_token_cache(): void
{
$cache = $this->createMock(CacheInterface::class);

$this->firebaseFactory
->expects($this->once())
->method('withAuthTokenCache')
->with($this->isInstanceOf(CacheItemPoolInterface::class));

$this->factory->setAuthTokenCache($cache);
$this->factory->createAuth();
}

/**
* @test
*/
public function it_accepts_a_PSR6_auth_token_cache(): void
{
$cache = $this->createMock(CacheItemPoolInterface::class);

$this->firebaseFactory
->expects($this->once())
->method('withAuthTokenCache')
->with($cache);

$this->factory->setAuthTokenCache($cache);
$this->factory->createAuth();
}
}

0 comments on commit fdbcb97

Please sign in to comment.