Security News
Node.js EOL Versions CVE Dubbed the "Worst CVE of the Year" by Security Experts
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
firebase-auth-cloudflare-workers
Advanced tools
Zero-dependencies firebase auth library for Cloudflare Workers.
Zero-dependencies firebase auth library for Cloudflare Workers.
import type { EmulatorEnv } from "firebase-auth-cloudflare-workers";
import { Auth, WorkersKVStoreSingle } from "firebase-auth-cloudflare-workers";
interface Bindings extends EmulatorEnv {
PROJECT_ID: string
PUBLIC_JWK_CACHE_KEY: string
PUBLIC_JWK_CACHE_KV: KVNamespace
FIREBASE_AUTH_EMULATOR_HOST: string
}
const verifyJWT = async (req: Request, env: Bindings): Promise<Response> => {
const authorization = req.headers.get('Authorization')
if (authorization === null) {
return new Response(null, {
status: 400,
})
}
const jwt = authorization.replace(/Bearer\s+/i, "")
const auth = Auth.getOrInitialize(
env.PROJECT_ID,
WorkersKVStoreSingle.getOrInitialize(env.PUBLIC_JWK_CACHE_KEY, env.PUBLIC_JWK_CACHE_KV)
)
const firebaseToken = await auth.verifyIdToken(jwt, env)
return new Response(JSON.stringify(firebaseToken), {
headers: {
"Content-Type": "application/json"
}
})
}
name = "firebase-auth-example"
compatibility_date = "2022-07-05"
workers_dev = true
[vars]
FIREBASE_AUTH_EMULATOR_HOST = "127.0.0.1:9099"
PROJECT_ID = "example-project12345"
# Specify cache key to store and get public jwk.
PUBLIC_JWK_CACHE_KEY = "public-jwk-cache-key"
[[kv_namespaces]]
binding = "PUBLIC_JWK_CACHE_KV"
id = ""
preview_id = "testingId"
export async function fetch(req: Request, env: Bindings) {
return await verifyJWT(req, env)
}
export default { fetch };
declare global {
const PROJECT_ID: string
const PUBLIC_JWK_CACHE_KEY: string
const PUBLIC_JWK_CACHE_KV: KVNamespace
const FIREBASE_AUTH_EMULATOR_HOST: string
}
addEventListener('fetch', (event: FetchEvent) => {
// Create env object for verifyIdToken API.
const bindings: EmulatorEnv = {
PROJECT_ID,
PUBLIC_JWK_CACHE_KEY,
PUBLIC_JWK_CACHE_KV,
FIREBASE_AUTH_EMULATOR_HOST,
}
event.respondWith(verifyJWT(event.request, bindings))
})
You can install from npm registry.
$ npm i firebase-auth-cloudflare-workers
Auth.getOrInitialize(projectId: string, keyStore: KeyStorer): Auth
Auth is created as a singleton object. This is because the Module Worker syntax only use environment variables at the time of request.
projectId
specifies the ID of the project for which firebase auth is used.keyStore
is used to cache the public key used to validate the Firebase ID token (JWT).See official document for project ID: https://firebase.google.com/docs/projects/learn-more#project-identifiers
authObj.verifyIdToken(idToken: string, env?: EmulatorEnv): Promise<FirebaseIdToken>
Verifies a Firebase ID token (JWT). If the token is valid, the promise is fulfilled with the token's decoded claims; otherwise, the promise is rejected.
See the ID Token section of the OpenID Connect spec for more information about the specific properties below.
env
is an optional parameter. but this is using to detect should use emulator or not.WorkersKVStoreSingle.getOrInitialize(cacheKey: string, cfKVNamespace: KVNamespace): WorkersKVStoreSingle
WorkersKVStoreSingle is created as a singleton object. This is because the Module Worker syntax only use environment variables at the time of request.
This caches the public key used to verify the Firebase ID token in the Workers KV.
This is implemented KeyStorer
interface.
cacheKey
specifies the key of the public key cache.cfKVNamespace
specifies the KV namespace which is bound your workers.emulatorHost(env?: EmulatorEnv): string | undefined
Returns the host of your Firebase Auth Emulator. For example, this case returns "127.0.0.1:9099"
if you configured like below.
wrangler.toml
[vars]
FIREBASE_AUTH_EMULATOR_HOST = "127.0.0.1:9099"
useEmulator(env?: EmulatorEnv): boolean
This is a wrapper emulatorHost
function.
When true the SDK should communicate with the Auth Emulator for all API calls and also produce unsigned tokens.
KeyStorer
This is an interface to cache the public key used to verify the Firebase ID token. By creating a class that implemented this interface, you can cache it in any storage of your choice.
interface KeyStorer {
get<ExpectedValue = unknown>(): Promise<ExpectedValue | null>;
put(value: string, expirationTtl: number): Promise<void>;
}
EmulatorEnv
interface EmulatorEnv {
FIREBASE_AUTH_EMULATOR_HOST: string | undefined
}
FirebaseIdToken
Interface representing a decoded Firebase ID token, returned from the authObj.verifyIdToken
method.
I put an example directory as Module Worker Syntax. this is explanation how to run the code.
yarn
command.$ yarn start-firebase-emulator
test@example.com
password: test1234
)localhost:8787
) by $ yarn start-example
$ curl -s http://localhost:8787/get-jwt | jq .idToken -r
$ curl http://localhost:8787/ -H 'Authorization: Bearer PASTE-JWT-HERE'
FAQs
Zero-dependencies firebase auth library for Cloudflare Workers.
The npm package firebase-auth-cloudflare-workers receives a total of 1,859 weekly downloads. As such, firebase-auth-cloudflare-workers popularity was classified as popular.
We found that firebase-auth-cloudflare-workers demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
Security News
cURL and Go security teams are publicly rejecting CVSS as flawed for assessing vulnerabilities and are calling for more accurate, context-aware approaches.
Security News
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.