All Projects → kreait → Firebase Tokens Php

kreait / Firebase Tokens Php

Licence: mit
A PHP library to work with Firebase tokens

Projects that are alternatives of or similar to Firebase Tokens Php

Firebase Esp8266
ESP8266 Firebase RTDB Arduino Library
Stars: ✭ 228 (+60.56%)
Mutual labels:  firebase, jwt
Firebase Esp32
ESP32 Firebase RTDB Arduino Library
Stars: ✭ 204 (+43.66%)
Mutual labels:  firebase, jwt
Spring Boot Jwt
a simple Demo of securing Spring boot rest endpoints using JWT
Stars: ✭ 138 (-2.82%)
Mutual labels:  jwt
Gotrue
An SWT based API for managing users and issuing SWT tokens
Stars: ✭ 2,493 (+1655.63%)
Mutual labels:  jwt
Go Fcm
Firebase Cloud Messaging Library for Golang
Stars: ✭ 138 (-2.82%)
Mutual labels:  firebase
Layuiadminprophp
针对LayuiAdmin后台模板使用ThinkPHP5开发的基础版本
Stars: ✭ 139 (-2.11%)
Mutual labels:  jwt
Httpie Jwt Auth
JWTAuth (JSON Web Tokens) auth plugin for HTTPie
Stars: ✭ 140 (-1.41%)
Mutual labels:  jwt
Cli
🧰 A zero trust swiss army knife for working with X509, OAuth, JWT, OATH OTP, etc.
Stars: ✭ 2,151 (+1414.79%)
Mutual labels:  jwt
Tihom Security
基于SpringBoot+SpringSecurity+SpringSocial+JWT等的第三方登录(微信QQ)和安全认证框架
Stars: ✭ 143 (+0.7%)
Mutual labels:  jwt
React Native Firebase Chat
This repository contains the source code for a simple chat application built with React Native (frontend) and Firebase (backend).
Stars: ✭ 140 (-1.41%)
Mutual labels:  firebase
React Native Fcm
react native module for firebase cloud messaging and local notification
Stars: ✭ 1,729 (+1117.61%)
Mutual labels:  firebase
Jwtdemo
gin基于JWT实现token令牌功能demo
Stars: ✭ 140 (-1.41%)
Mutual labels:  jwt
Reactjs Authentication Tutorial
Chuck Norris World App - A sample app that shows how to add authentication to a ReactJS app
Stars: ✭ 139 (-2.11%)
Mutual labels:  jwt
Ssm
👅基于RESTful风格的前后端分离的SSM框架,集成了shiro和swagger等框架
Stars: ✭ 141 (-0.7%)
Mutual labels:  jwt
Smart Industry
🏭 Open Source Manufacturing Execution System for JobShop type manufacturer.
Stars: ✭ 138 (-2.82%)
Mutual labels:  firebase
Go Jose
An implementation of JOSE standards (JWE, JWS, JWT) in Go
Stars: ✭ 1,849 (+1202.11%)
Mutual labels:  jwt
React Firebase Hooks
React Hooks for Firebase.
Stars: ✭ 2,227 (+1468.31%)
Mutual labels:  firebase
Next Firebase Auth
Simple Firebase authentication for all Next.js rendering strategies
Stars: ✭ 135 (-4.93%)
Mutual labels:  firebase
Angular9 Example App
Angular 13 Example App + Angular CLI + i18n + GraphQL
Stars: ✭ 1,769 (+1145.77%)
Mutual labels:  firebase
Vue Comment Grid
💬 Responsive Vue.js comment system plugin that built with CSS Grid and Firebase REST API + Authentication. https://tugayyaldiz.github.io/vue-comment-grid
Stars: ✭ 143 (+0.7%)
Mutual labels:  firebase

Firebase Tokens

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 (which uses this library).

Current version Supported PHP version Monthly Downloads Total Downloads Tests Discord Sponsor

Installation

composer require kreait/firebase-tokens

Simple usage

Create a custom token

More information on what a custom token is and how it can be used can be found in Google's official documentation.

<?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

The ID token verification methods included in the Firebase Admin SDKs are meant to verify ID tokens that come from the client SDKs, not the custom tokens that you create with the Admin SDKs. See Auth tokens for more information.

<?php

use Kreait\Firebase\JWT\Error\IdTokenVerificationFailed;
use Kreait\Firebase\JWT\IdTokenVerifier;

$projectId = '...';
$idToken = 'eyJhb...'; // An ID token given to your backend by a Client application

$verifier = IdTokenVerifier::createWithProjectId($projectId);

try {
    $token = $verifier->verifyIdToken($idToken);
} catch (IdTokenVerificationFailed $e) {
    echo $e->getMessage();
    // Example Output:
    // The value 'eyJhb...' 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 and represent a JWT. The displayed outputs are examples and vary depending on the information associated with the given user in your project's auth database.

According to the JWT specification, you can expect the following payload fields to be always available: iss, aud, auth_time, sub, iat, exp. Other fields depend on the authentication method of the given account and the information stored in your project's Auth database.

$token = $verifier->verifyIdToken('eyJhb...'); // An ID token given to your backend by a Client application

echo json_encode($token->headers(), JSON_PRETTY_PRINT);
// {
//     "alg": "RS256",
//     "kid": "e5a91d9f39fa4de254a1e89df00f05b7e248b985",
//     "typ": "JWT"
// }                                                   

echo json_encode($token->payload(), JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
// {
//     "name": "Jane Doe",
//     "picture": "https://domain.tld/picture.jpg",
//     "iss": "https://securetoken.google.com/your-project-id",
//     "aud": "your-project-id",
//     "auth_time": 1580063945,
//     "user_id": "W0IturDwy4TYTmX6ilkd2ZbAXRp2",
//     "sub": "W0IturDwy4TYTmX6ilkd2ZbAXRp2",
//     "iat": 1580063945,
//     "exp": 1580067545,
//     "email": "[email protected]",
//     "email_verified": true,
//     "phone_number": "+1234567890",
//     "firebase": {
//         "identities": {
//             "phone": [
//                 "+1234567890"
//             ],
//             "email": [
//                 "[email protected]"
//             ]
//         },
//         "sign_in_provider": "custom"
//     }
// }

echo $token->toString();
// eyJhb...

$tokenString = (string) $token; // string
// eyJhb...

Tenant Awareness

You can create custom tokens that are scoped to a given tenant:

<?php

use Kreait\Firebase\JWT\CustomTokenGenerator;

$generator = CustomTokenGenerator::withClientEmailAndPrivateKey('...', '...');

$tenantAwareGenerator = $generator->withTenantId('my-tenant-id');

Similarly, you can verify that ID tokens were issued in the scope of a given tenant:

<?php

use Kreait\Firebase\JWT\IdTokenVerifier;

$verifier = IdTokenVerifier::createWithProjectId('my-project-id');

$tenantAwareVerifier = $verifier->withExpectedTenantId('my-tenant-id');

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 initialize the verifier with an implementation of psr/simple-cache or psr/cache to reduce the amount of HTTP requests to Google's servers.

Here's an 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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].