Skip to content

DoomMortal/firebase-tokens-php

 
 

Repository files navigation

Firebase Tokens

Latest Stable Version Total Downloads Build Status Scrutinizer Code Quality Code Coverage

A library to work with Google Firebase tokens. You can use it to create custom tokens and verify ID Tokens.

Installation

composer require kreait/firebase-tokens

Create a custom token

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..."

Verify an ID token

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();
}

Cache results from the Google Secure Token Store

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); 

About

A PHP library to work with Firebase tokens

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 96.6%
  • Makefile 3.4%