fixed to support service worker syntax
parent
af00a86827
commit
6c088c50ed
|
@ -18,6 +18,7 @@ export interface VerifyFirebaseAuthConfig {
|
||||||
keyStore?: KeyStorer;
|
keyStore?: KeyStorer;
|
||||||
keyStoreInitializer?: (c: Context) => KeyStorer;
|
keyStoreInitializer?: (c: Context) => KeyStorer;
|
||||||
disableErrorLog?: boolean;
|
disableErrorLog?: boolean;
|
||||||
|
firebaseEmulatorHost?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
const defaultKVStoreJWKCacheKey = "verify-firebase-auth-cached-public-key";
|
const defaultKVStoreJWKCacheKey = "verify-firebase-auth-cached-public-key";
|
||||||
|
@ -39,6 +40,7 @@ export const verifyFirebaseAuth = (
|
||||||
keyStoreInitializer:
|
keyStoreInitializer:
|
||||||
userConfig.keyStoreInitializer ?? defaultKeyStoreInitializer,
|
userConfig.keyStoreInitializer ?? defaultKeyStoreInitializer,
|
||||||
disableErrorLog: userConfig.disableErrorLog,
|
disableErrorLog: userConfig.disableErrorLog,
|
||||||
|
firebaseEmulatorHost: userConfig.firebaseEmulatorHost,
|
||||||
};
|
};
|
||||||
|
|
||||||
return async (c, next) => {
|
return async (c, next) => {
|
||||||
|
@ -55,7 +57,10 @@ export const verifyFirebaseAuth = (
|
||||||
);
|
);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const idToken = await auth.verifyIdToken(jwt, c.env);
|
const idToken = await auth.verifyIdToken(jwt, {
|
||||||
|
FIREBASE_AUTH_EMULATOR_HOST:
|
||||||
|
config.firebaseEmulatorHost ?? c.env.FIREBASE_AUTH_EMULATOR_HOST,
|
||||||
|
});
|
||||||
setFirebaseToken(c, idToken);
|
setFirebaseToken(c, idToken);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
if (!userConfig.disableErrorLog) {
|
if (!userConfig.disableErrorLog) {
|
||||||
|
|
|
@ -1,4 +1,8 @@
|
||||||
import { Auth, KeyStorer } from "firebase-auth-cloudflare-workers";
|
import {
|
||||||
|
Auth,
|
||||||
|
KeyStorer,
|
||||||
|
WorkersKVStoreSingle,
|
||||||
|
} from "firebase-auth-cloudflare-workers";
|
||||||
import { Hono } from "hono";
|
import { Hono } from "hono";
|
||||||
import {
|
import {
|
||||||
VerifyFirebaseAuthEnv,
|
VerifyFirebaseAuthEnv,
|
||||||
|
@ -26,6 +30,47 @@ describe("verifyFirebaseAuth middleware", () => {
|
||||||
await sleep(1000); // wait for iat
|
await sleep(1000); // wait for iat
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe("service worker syntax", () => {
|
||||||
|
test("valid case, should be 200", async () => {
|
||||||
|
const app = new Hono<VerifyFirebaseAuthEnv>();
|
||||||
|
|
||||||
|
resetAuth();
|
||||||
|
|
||||||
|
// This is assumed to be obtained from an environment variable.
|
||||||
|
const PUBLIC_JWK_CACHE_KEY = "testing-cache-key";
|
||||||
|
|
||||||
|
app.use(
|
||||||
|
"*",
|
||||||
|
verifyFirebaseAuth({
|
||||||
|
projectId: validProjectId,
|
||||||
|
keyStore: WorkersKVStoreSingle.getOrInitialize(
|
||||||
|
PUBLIC_JWK_CACHE_KEY,
|
||||||
|
PUBLIC_JWK_CACHE_KV
|
||||||
|
),
|
||||||
|
disableErrorLog: true,
|
||||||
|
firebaseEmulatorHost: emulatorHost,
|
||||||
|
})
|
||||||
|
);
|
||||||
|
app.get("/hello", (c) => c.json(getFirebaseToken(c)));
|
||||||
|
|
||||||
|
const req = new Request("http://localhost/hello", {
|
||||||
|
headers: {
|
||||||
|
Authorization: `Bearer ${user.idToken}`,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const res = await app.request(req);
|
||||||
|
|
||||||
|
expect(res).not.toBeNull();
|
||||||
|
expect(res.status).toBe(200);
|
||||||
|
|
||||||
|
const json = await res.json<{ aud: string; email: string }>();
|
||||||
|
expect(json.aud).toBe(validProjectId);
|
||||||
|
expect(json.email).toBe("codehex@hono.js");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("module worker syntax", () => {
|
||||||
test.each([
|
test.each([
|
||||||
[
|
[
|
||||||
"valid case, should be 200",
|
"valid case, should be 200",
|
||||||
|
@ -186,7 +231,8 @@ describe("verifyFirebaseAuth middleware", () => {
|
||||||
|
|
||||||
const json = await res.json<{ aud: string; email: string }>();
|
const json = await res.json<{ aud: string; email: string }>();
|
||||||
expect(json.aud).toBe(validProjectId);
|
expect(json.aud).toBe(validProjectId);
|
||||||
expect(json.email).toBe("codehex@example.com");
|
expect(json.email).toBe("codehex@hono.js");
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -220,11 +266,11 @@ const generateDummyJWT = () => {
|
||||||
sub: "t1aLdTkAs0S0J0P6TNbjwbmry5B3",
|
sub: "t1aLdTkAs0S0J0P6TNbjwbmry5B3",
|
||||||
iat: now - 1000,
|
iat: now - 1000,
|
||||||
exp: now + 3000, // + 3s
|
exp: now + 3000, // + 3s
|
||||||
email: "codehex@example.com",
|
email: "codehex@hono.js",
|
||||||
email_verified: false,
|
email_verified: false,
|
||||||
firebase: {
|
firebase: {
|
||||||
identities: {
|
identities: {
|
||||||
email: ["codehex@example.com"],
|
email: ["codehex@hono.js"],
|
||||||
},
|
},
|
||||||
sign_in_provider: "password",
|
sign_in_provider: "password",
|
||||||
},
|
},
|
||||||
|
|
Loading…
Reference in New Issue