nathanfeitoza / firebase-tokens-php-5.6
A library to work with Firebase tokens
Requires
- ext-json: *
- ext-openssl: *
- fig/http-message-util: ^1.1
- guzzlehttp/guzzle: ^6.2.1
- lcobucci/jwt: ^3.2
- nathanfeitoza/clock-php5.6: ^1.0.1
- psr/cache: ^1.0
- psr/simple-cache: ^1.0
Requires (Dev)
- firebase/php-jwt: ^5.0
- friendsofphp/php-cs-fixer: ^2.0
- phpstan/phpstan-phpunit: ^0.9.3
- phpunit/phpunit: ^6.0
- symfony/cache: ^3.4
- symfony/var-dumper: ^3.4
Suggests
- firebase/php-jwt: ^5.0 can be used to create and parse tokens
- guzzlehttp/guzzle: ^6.2.1 can be used as an HTTP handler
- lcobucci/jwt: ^3.2 can be used to create and parse tokens
- psr/cache-implementation: to cache fetched remote public keys
- psr/simple-cache-implementation: to cache fetched remote public keys
This package is auto-updated.
Last update: 2024-10-13 08:00:44 UTC
README
A library to work with Google Firebase tokens. You can use it to create custom tokens and verify ID Tokens.
Achieve more with the Firebase Admin SDK for PHP 5.6 (which uses this library).
Installation
composer require nathanfeitoza/firebase-tokens-php-5.6
Simple usage
Create a custom token
<?php use Kreait\Firebase\JWT\CustomTokenGenerator; $clientEmail = '...'; $privateKey = '...'; $generator = CustomTokenGenerator::withClientEmailAndPrivateKey($clientEmail, $privateKey); $token = $generator->createCustomToken('uid', ['first_claim' => 'first_value' /* ... */]); echo $token; // Output: eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.e...
Verify an ID token
<?php use Kreait\Firebase\JWT\Error\IdTokenVerificationFailed; use Kreait\Firebase\JWT\IdTokenVerifier; $projectId = '...'; $idToken = '...'; $verifier = IdTokenVerifier::createWithProjectId($projectId); try { $token = $verifier->verifyIdToken($idToken); } catch (IdTokenVerificationFailed $e) { echo $e->getMessage(); // Example Output: // The value 'eyJhbGciOiJSUzI...' is not a verified ID token: // - The token is expired. exit; } try { $token = $verifier->verifyIdTokenWithLeeway($idToken, $leewayInSeconds = 10000000); } catch (IdTokenVerificationFailed $e) { print $e->getMessage(); exit; }
Tokens
Tokens returned from the Generator and Verifier are instances of Kreait\Firebase\JWT\Token
.
$tokenHeaders = $token->getHeaders(); // array $tokenPayload = token->getPayload(); // array $tokenString = $token->toString(); $tokenString = (string) $token;
Advanced usage
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 an implementation of psr/simple-cache or psr/cache to wrap the
Example using the Symfony Cache Component
use Kreait\Firebase\JWT\IdTokenVerifier; use Symfony\Component\Cache\Simple\FilesystemCache; $cache = new FilesystemCache(); $verifier = IdTokenVerifier::createWithProjectIdAndCache($projectId, $cache);
License
The MIT License (MIT). Please see License File for more information.