From 1c1560e3b5058861b1651ac8692bc5380ffa60dd Mon Sep 17 00:00:00 2001 From: Kei Kamikawa Date: Thu, 28 Jul 2022 15:52:16 +0900 Subject: [PATCH] fixed documentation --- README.md | 62 +++++++++++++++++++++++++++++++--------------- test/index.test.ts | 7 ++++-- 2 files changed, 47 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index d2a7372c..03012f56 100644 --- a/README.md +++ b/README.md @@ -1,40 +1,62 @@ -# Hello middleware for Hono +# Hono Firebase Auth middleware for Cloudflare Workers. -An example project of the third-party middleware for [Hono](https://github.com/honojs/hono). -This middleware add `X-Message` header to the Response. +This is a Firebase Auth middleware library for [Hono](https://github.com/honojs/hono) which is used [firebase-auth-cloudflare-workers](https://github.com/Code-Hex/firebase-auth-cloudflare-workers). -## Usage +Currently only Cloudflare Workers are supported officially. However, it may work in other environments as well, so please let us know in an issue if it works. + +## Synopsis ```ts -import { hello } from '@honojs/hello' -import { Hono } from 'hono' +import { Hono } from "hono"; +import { VerifyFirebaseAuthConfig, VerifyFirebaseAuthEnv, verifyFirebaseAuth, getFirebaseToken } from "@honojs/firebase-auth"; -const app = new Hono() +const config: VerifyFirebaseAuthConfig = { + // specify your firebase project ID. + projectId: "your-project-id", +} -app.use('*', hello('Hello!! Hono!!')) -app.get('/', (c) => c.text('foo')) +// Or you can specify here the extended VerifyFirebaseAuthEnv type. +const app = new Hono() + +// set middleware +app.use("*", verifyFirebaseAuth(config)); +app.get("/hello", (c) => { + const idToken = getFirebaseToken(c) // get id-token object. + return c.json(idToken) +}); export default app ``` -## Deno +## Config (`VerifyFirebaseAuthConfig`) -```ts -import { serve } from 'https://deno.land/std/http/server.ts' -import { hello } from 'https://deno.land/x/hono_hello/mod.ts' -import { Hono } from 'https://deno.land/x/hono/mod.ts' +### `projectId: string` (**required**) -const app = new Hono() +This field indicates your firebase project ID. -app.use('*', hello('Hello!! Hono!!')) -app.get('/', (c) => c.text('foo')) +### `authorizationHeaderKey?: string` (optional) -serve(app.fetch) -``` +Based on this configuration, the JWT created by firebase auth is looked for in the HTTP headers. The default is "Authorization". + +### `keyStore?: KeyStorer` (optional) + +This is used to cache the public key used to validate the Firebase ID token (JWT). This KeyStorer type has been defined in [firebase-auth-cloudflare-workers](https://github.com/Code-Hex/firebase-auth-cloudflare-workers/tree/main#keystorer) library. + +If you don't specify the field, this library uses [WorkersKVStoreSingle](https://github.com/Code-Hex/firebase-auth-cloudflare-workers/tree/main#workerskvstoresinglegetorinitializecachekey-string-cfkvnamespace-kvnamespace-workerskvstoresingle) instead. You must fill in the fields defined in `VerifyFirebaseAuthEnv`. + +### `keyStoreInitializer?: (c: Context) => KeyStorer` (optional) + +Use this when initializing KeyStorer and environment variables, etc. are required. + +If you don't specify the field, this library uses [WorkersKVStoreSingle](https://github.com/Code-Hex/firebase-auth-cloudflare-workers/tree/main#workerskvstoresinglegetorinitializecachekey-string-cfkvnamespace-kvnamespace-workerskvstoresingle) instead. You must fill in the fields defined in `VerifyFirebaseAuthEnv`. + +### `disableErrorLog?: boolean` (optional) + +Throws an exception if JWT validation fails. By default, this is output to the error log, but if you don't expect it, use this. ## Author -Yusuke Wada +codehex ## License diff --git a/test/index.test.ts b/test/index.test.ts index d343ebbe..c8488c1c 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -1,7 +1,10 @@ -import { getFirebaseToken } from "./../src/index"; import { Auth, KeyStorer } from "firebase-auth-cloudflare-workers"; import { Hono } from "hono"; -import { VerifyFirebaseAuthEnv, verifyFirebaseAuth } from "../src"; +import { + VerifyFirebaseAuthEnv, + verifyFirebaseAuth, + getFirebaseToken, +} from "../src"; describe("verifyFirebaseAuth middleware", () => { const emulatorHost = "127.0.0.1:9099";