nathanfeitoza / firebase-tokens-php-5.6
用于处理 Firebase 令牌的库
1.9.1
2019-09-12 19:54 UTC
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-09-13 07:51:18 UTC
README
一个用于处理 Google Firebase 令牌的库。您可以使用它来 创建自定义令牌 和 验证 ID 令牌。
通过使用针对 PHP 5.6 的 Firebase Admin SDK (该 SDK 使用此库) 来实现更多功能。
安装
composer require nathanfeitoza/firebase-tokens-php-5.6
简单用法
创建自定义令牌
<?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...
验证 ID 令牌
<?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; }
令牌
生成器和验证器返回的令牌是 Kreait\Firebase\JWT\Token
的实例。
$tokenHeaders = $token->getHeaders(); // array $tokenPayload = token->getPayload(); // array $tokenString = $token->toString(); $tokenString = (string) $token;
高级用法
缓存 Google 安全令牌存储的结果
为了验证 ID 令牌,验证器会调用获取 Firebase 当前可用的公钥。默认情况下,这些密钥在内存中缓存。
如果您想更有效地缓存公钥,您可以使用 psr/simple-cache 或 psr/cache 的实现来包装
使用 Symfony Cache Component 的示例
use Kreait\Firebase\JWT\IdTokenVerifier; use Symfony\Component\Cache\Simple\FilesystemCache; $cache = new FilesystemCache(); $verifier = IdTokenVerifier::createWithProjectIdAndCache($projectId, $cache);
许可协议
MIT 许可协议 (MIT)。请参阅 许可文件 以获取更多信息。