A library to work with Google Firebase tokens. You can use it to create custom tokens and verify ID Tokens.
composer require kreait/firebase-tokens
use Firebase\Auth\Token\Generator;
$generator = new Generator($clientEmail, $privateKey);
$uid = 'a-uid';
$claims = ['foo' => 'bar'];
$token = $generator->createCustomToken($uid, $claims); // Returns a Lcobucci\JWT\Token instance
echo $token; // "eyJ0eXAiOiJKV1..."
use Firebase\Auth\Token\Verifier;
$verifier = new Verifier($projectId);
$idTokenString = 'eyJhbGciOiJSUzI1...';
try {
$verifiedIdToken = $verifier->verifyIdToken($idTokenString);
echo $verifiedIdToken->getClaim('sub'); // "a-uid"
} catch (\Firebase\Auth\Token\Exception\ExpiredToken $e) {
echo $e->getMessage();
} catch (\Firebase\Auth\Token\Exception\IssuedInTheFuture $e) {
echo $e->getMessage();
} catch (\Firebase\Auth\Token\Exception\InvalidToken $e) {
echo $e->getMessage();
}
In order to verify ID tokens, the verifier makes a call to fetch Firebase's currently available public keys. The keys are cached in memory by default.
If you want to cache the public keys more effectively, you can use any implementation of psr/simple-cache.
Example using the Symfony Cache Component
use Firebase\Auth\Token\HttpKeyStore;
use Firebase\Auth\Token\Verifier;
use Symfony\Component\Cache\Simple\FilesystemCache;
$cache = new FilesystemCache();
$keyStore = new HttpKeyStore(null, $cache);
$verifier = new Verifier($projectId, $keyStore);