honojs-middleware/packages/firebase-auth/test/index.test.ts

690 lines
19 KiB
TypeScript
Raw Normal View History

import type { Credential, KeyStorer } from 'firebase-auth-cloudflare-workers'
import { AdminAuthApiClient, Auth, WorkersKVStoreSingle } from 'firebase-auth-cloudflare-workers'
import type { GoogleOAuthAccessToken } from 'firebase-auth-cloudflare-workers/dist/main/credential'
2023-02-04 19:20:42 +08:00
import { Hono } from 'hono'
import { setCookie } from 'hono/cookie'
import { HTTPException } from 'hono/http-exception'
import { Miniflare } from 'miniflare'
import { describe, it, expect, beforeAll, vi } from 'vitest'
2023-02-04 19:20:42 +08:00
import type { VerifyFirebaseAuthEnv } from '../src'
import { verifyFirebaseAuth, getFirebaseToken, verifySessionCookieFirebaseAuth } from '../src'
2022-07-28 13:58:54 +08:00
2023-02-04 19:20:42 +08:00
describe('verifyFirebaseAuth middleware', () => {
const emulatorHost = '127.0.0.1:9099'
const validProjectId = 'example-project12345' // see package.json
const nullScript = 'export default { fetch: () => new Response(null, { status: 404 }) };'
const mf = new Miniflare({
modules: true,
script: nullScript,
kvNamespaces: ['PUBLIC_JWK_CACHE_KV'],
})
2022-07-28 13:58:54 +08:00
2023-02-04 19:20:42 +08:00
let user: signUpResponse
2022-07-28 13:58:54 +08:00
beforeAll(async () => {
2023-02-04 19:20:42 +08:00
await deleteAccountEmulator(emulatorHost, validProjectId)
2022-07-28 13:58:54 +08:00
user = await signUpEmulator(emulatorHost, {
2023-02-04 19:20:42 +08:00
email: 'codehex@hono.js',
password: 'honojs',
})
2022-07-28 13:58:54 +08:00
2023-02-04 19:20:42 +08:00
await sleep(1000) // wait for iat
})
2022-07-28 13:58:54 +08:00
2023-02-04 19:20:42 +08:00
describe('service worker syntax', () => {
it('valid case, should be 200', async () => {
2023-02-04 19:20:42 +08:00
const app = new Hono()
2022-07-29 22:03:09 +08:00
2023-02-04 19:20:42 +08:00
resetAuth()
2022-07-29 22:03:09 +08:00
// This is assumed to be obtained from an environment variable.
2023-02-04 19:20:42 +08:00
const PUBLIC_JWK_CACHE_KEY = 'testing-cache-key'
const PUBLIC_JWK_CACHE_KV = (await mf.getKVNamespace(
'PUBLIC_JWK_CACHE_KV'
)) as unknown as KVNamespace
2022-07-29 22:03:09 +08:00
app.use(
2023-02-04 19:20:42 +08:00
'*',
2022-07-29 22:03:09 +08:00
verifyFirebaseAuth({
2022-07-28 13:58:54 +08:00
projectId: validProjectId,
2023-02-04 19:20:42 +08:00
keyStore: WorkersKVStoreSingle.getOrInitialize(PUBLIC_JWK_CACHE_KEY, PUBLIC_JWK_CACHE_KV),
2022-07-29 22:03:09 +08:00
disableErrorLog: true,
firebaseEmulatorHost: emulatorHost,
})
2023-02-04 19:20:42 +08:00
)
app.get('/hello', (c) => c.json(getFirebaseToken(c)))
2022-07-29 22:03:09 +08:00
2023-02-04 19:20:42 +08:00
const req = new Request('http://localhost/hello', {
2022-07-29 22:03:09 +08:00
headers: {
Authorization: `Bearer ${user.idToken}`,
2022-07-28 13:58:54 +08:00
},
2023-02-04 19:20:42 +08:00
})
2022-07-29 22:03:09 +08:00
2023-02-04 19:20:42 +08:00
const res = await app.request(req)
2022-07-29 22:03:09 +08:00
2023-02-04 19:20:42 +08:00
expect(res).not.toBeNull()
expect(res.status).toBe(200)
2022-07-29 22:03:09 +08:00
2023-02-04 19:20:42 +08:00
const json = await res.json<{ aud: string; email: string }>()
expect(json.aud).toBe(validProjectId)
expect(json.email).toBe('codehex@hono.js')
})
})
2022-07-29 22:03:09 +08:00
2023-02-04 19:20:42 +08:00
describe('module worker syntax', () => {
it.each([
2022-07-29 22:03:09 +08:00
[
2023-02-04 19:20:42 +08:00
'valid case, should be 200',
2022-07-29 22:03:09 +08:00
{
2023-02-04 19:20:42 +08:00
headerKey: 'Authorization',
2022-07-29 22:03:09 +08:00
config: {
projectId: validProjectId,
},
wantStatus: 200,
2022-07-28 13:58:54 +08:00
},
2022-07-29 22:03:09 +08:00
],
[
2023-02-04 19:20:42 +08:00
'valid specified headerKey, should be 200',
2022-07-29 22:03:09 +08:00
{
2023-02-04 19:20:42 +08:00
headerKey: 'X-Authorization',
2022-07-29 22:03:09 +08:00
config: {
projectId: validProjectId,
2023-02-04 19:20:42 +08:00
authorizationHeaderKey: 'X-Authorization',
2022-07-29 22:03:09 +08:00
},
wantStatus: 200,
2022-07-28 13:58:54 +08:00
},
2022-07-29 22:03:09 +08:00
],
[
2023-02-04 19:20:42 +08:00
'invalid authorization header, should be 400',
2022-07-29 22:03:09 +08:00
{
2023-02-04 19:20:42 +08:00
headerKey: 'X-Authorization',
2022-07-29 22:03:09 +08:00
config: {
projectId: validProjectId, // see package.json
// No specified header key.
},
wantStatus: 400,
2022-07-28 13:58:54 +08:00
},
2022-07-29 22:03:09 +08:00
],
[
2023-02-04 19:20:42 +08:00
'invalid project ID, should be 401',
2022-07-29 22:03:09 +08:00
{
2023-02-04 19:20:42 +08:00
headerKey: 'Authorization',
2022-07-29 22:03:09 +08:00
config: {
2023-02-04 19:20:42 +08:00
projectId: 'invalid-projectId',
2022-07-29 22:03:09 +08:00
},
wantStatus: 401,
2022-07-28 13:58:54 +08:00
},
2022-07-29 22:03:09 +08:00
],
])('%s', async (_, { headerKey, config, wantStatus }) => {
2023-02-04 19:20:42 +08:00
const app = new Hono<{ Bindings: VerifyFirebaseAuthEnv }>()
2022-07-29 22:03:09 +08:00
2023-02-04 19:20:42 +08:00
resetAuth()
2022-07-29 22:03:09 +08:00
const PUBLIC_JWK_CACHE_KV = (await mf.getKVNamespace(
'PUBLIC_JWK_CACHE_KV'
)) as unknown as KVNamespace
const env: VerifyFirebaseAuthEnv = {
FIREBASE_AUTH_EMULATOR_HOST: 'localhost:9099',
PUBLIC_JWK_CACHE_KEY: 'testing-cache-key',
PUBLIC_JWK_CACHE_KV,
}
2022-07-29 22:03:09 +08:00
app.use(
2023-02-04 19:20:42 +08:00
'*',
2022-07-29 22:03:09 +08:00
verifyFirebaseAuth({
...config,
disableErrorLog: true,
})
2023-02-04 19:20:42 +08:00
)
app.get('/hello', (c) => c.text('OK'))
2022-07-29 22:03:09 +08:00
2023-02-04 19:20:42 +08:00
const req = new Request('http://localhost/hello', {
2022-07-29 22:03:09 +08:00
headers: {
[headerKey]: `Bearer ${user.idToken}`,
2022-07-28 13:58:54 +08:00
},
2023-02-04 19:20:42 +08:00
})
2022-07-28 13:58:54 +08:00
2023-02-04 19:20:42 +08:00
const res = await app.fetch(req, env)
2022-07-28 13:58:54 +08:00
2023-02-04 19:20:42 +08:00
expect(res).not.toBeNull()
expect(res.status).toBe(wantStatus)
})
2022-07-28 13:58:54 +08:00
it('specified keyStore is used', async () => {
2023-02-04 19:20:42 +08:00
const testingJWT = generateDummyJWT()
2022-07-28 13:58:54 +08:00
2023-02-04 19:20:42 +08:00
const nopKeyStore = new NopKeyStore()
const getSpy = vi.spyOn(nopKeyStore, 'get')
const putSpy = vi.spyOn(nopKeyStore, 'put')
2022-07-28 13:58:54 +08:00
2023-02-04 19:20:42 +08:00
const app = new Hono<{ Bindings: VerifyFirebaseAuthEnv }>()
2022-07-28 13:58:54 +08:00
2023-02-04 19:20:42 +08:00
resetAuth()
2022-07-28 13:58:54 +08:00
2022-07-29 22:03:09 +08:00
app.use(
2023-02-04 19:20:42 +08:00
'*',
2022-07-29 22:03:09 +08:00
verifyFirebaseAuth({
projectId: validProjectId,
keyStore: nopKeyStore,
disableErrorLog: true,
})
2023-02-04 19:20:42 +08:00
)
app.get('/hello', (c) => c.text('OK'))
2022-07-29 22:03:09 +08:00
2023-02-04 19:20:42 +08:00
const req = new Request('http://localhost/hello', {
2022-07-29 22:03:09 +08:00
headers: {
Authorization: `Bearer ${testingJWT}`,
},
2023-02-04 19:20:42 +08:00
})
2022-07-28 13:58:54 +08:00
2022-07-29 22:03:09 +08:00
// not use firebase emulator to check using key store
const res = await app.fetch(req, {
FIREBASE_AUTH_EMULATOR_HOST: undefined,
2023-02-04 19:20:42 +08:00
})
2022-07-28 13:58:54 +08:00
2023-02-04 19:20:42 +08:00
expect(res).not.toBeNull()
expect(res.status).toBe(401)
expect(getSpy).toHaveBeenCalled()
expect(putSpy).toHaveBeenCalled()
})
2022-07-28 13:58:54 +08:00
it('usable id-token in main handler', async () => {
2023-02-04 19:20:42 +08:00
const testingJWT = generateDummyJWT()
2022-07-28 13:58:54 +08:00
2023-02-04 19:20:42 +08:00
const nopKeyStore = new NopKeyStore()
const app = new Hono<{ Bindings: VerifyFirebaseAuthEnv }>()
2022-07-28 13:58:54 +08:00
2023-02-04 19:20:42 +08:00
resetAuth()
2022-07-28 13:58:54 +08:00
2022-07-29 22:03:09 +08:00
app.use(
2023-02-04 19:20:42 +08:00
'*',
2022-07-29 22:03:09 +08:00
verifyFirebaseAuth({
projectId: validProjectId,
keyStore: nopKeyStore,
disableErrorLog: true,
})
2023-02-04 19:20:42 +08:00
)
app.get('/hello', (c) => c.json(getFirebaseToken(c)))
2022-07-29 22:03:09 +08:00
2023-02-04 19:20:42 +08:00
const req = new Request('http://localhost/hello', {
2022-07-29 22:03:09 +08:00
headers: {
Authorization: `Bearer ${testingJWT}`,
},
2023-02-04 19:20:42 +08:00
})
2022-07-28 13:58:54 +08:00
2022-07-29 22:03:09 +08:00
const res = await app.fetch(req, {
FIREBASE_AUTH_EMULATOR_HOST: emulatorHost,
2023-02-04 19:20:42 +08:00
})
2022-07-28 13:58:54 +08:00
2023-02-04 19:20:42 +08:00
expect(res).not.toBeNull()
expect(res.status).toBe(200)
2022-07-28 13:58:54 +08:00
2023-02-04 19:20:42 +08:00
const json = await res.json<{ aud: string; email: string }>()
expect(json.aud).toBe(validProjectId)
expect(json.email).toBe('codehex@hono.js')
})
it('invalid PUBLIC_JWK_CACHE_KV is undefined, should be 501', async () => {
const app = new Hono<{ Bindings: VerifyFirebaseAuthEnv }>()
resetAuth()
let gotError: Error | undefined
app.use(
'*',
async (c, next) => {
await next()
gotError = c.error
},
verifyFirebaseAuth({
projectId: validProjectId,
disableErrorLog: true,
})
)
app.get('/hello', (c) => c.text('OK'))
const req = new Request('http://localhost/hello', {
headers: {
Authorization: `Bearer ${user.idToken}`,
},
})
const env: VerifyFirebaseAuthEnv = {
FIREBASE_AUTH_EMULATOR_HOST: emulatorHost,
PUBLIC_JWK_CACHE_KV: undefined,
}
const res = await app.fetch(req, env)
expect(gotError instanceof HTTPException).toBeTruthy()
expect(res).not.toBeNull()
expect(res.status).toBe(501)
})
2023-02-04 19:20:42 +08:00
})
})
2022-07-28 13:58:54 +08:00
describe('verifySessionCookieFirebaseAuth middleware', () => {
const emulatorHost = '127.0.0.1:9099'
const validProjectId = 'example-project12345' // see package.json
const nullScript = 'export default { fetch: () => new Response(null, { status: 404 }) };'
const mf = new Miniflare({
modules: true,
script: nullScript,
kvNamespaces: ['PUBLIC_JWK_CACHE_KV'],
})
let user: signUpResponse
const env: VerifyFirebaseAuthEnv = {
FIREBASE_AUTH_EMULATOR_HOST: emulatorHost,
}
const expiresIn = 24 * 60 * 60 * 1000
beforeAll(async () => {
await deleteAccountEmulator(emulatorHost, validProjectId)
user = await signUpEmulator(emulatorHost, {
email: 'codehex@hono.js',
password: 'honojs',
})
await sleep(1000) // wait for iat
})
describe('service worker syntax', () => {
it('valid case, should be 200', async () => {
const app = new Hono()
resetAuth()
// This is assumed to be obtained from an environment variable.
const PUBLIC_JWK_CACHE_KEY = 'testing-cache-key'
const PUBLIC_JWK_CACHE_KV = (await mf.getKVNamespace(
'PUBLIC_JWK_CACHE_KV'
)) as unknown as KVNamespace
const cookieName = 'session-key'
app.get(
'/hello',
verifySessionCookieFirebaseAuth({
projectId: validProjectId,
cookieName,
keyStore: WorkersKVStoreSingle.getOrInitialize(PUBLIC_JWK_CACHE_KEY, PUBLIC_JWK_CACHE_KV),
firebaseEmulatorHost: emulatorHost,
redirects: {
signIn: '/login',
},
}),
(c) => c.json(getFirebaseToken(c))
)
app.post('/create-session', async (c) => {
const { idToken } = await c.req.json<{ idToken: string }>()
const adminAuthClient = AdminAuthApiClient.getOrInitialize(
validProjectId,
new NopCredential()
)
const sessionCookie = await adminAuthClient.createSessionCookie(idToken, expiresIn, env)
setCookie(c, cookieName, sessionCookie, {
httpOnly: true,
})
return c.newResponse(null, 200)
})
const req1 = new Request('http://localhost/create-session', {
method: 'POST',
body: JSON.stringify({
idToken: user.idToken,
}),
headers: {
'Content-Type': 'application/json',
},
})
const res1 = await app.request(req1)
expect(res1).not.toBeNull()
expect(res1.status).toBe(200)
const gotSetCookie = res1.headers.get('Set-Cookie')
expect(gotSetCookie).not.toBeNull()
const req2 = new Request('http://localhost/hello', {
method: 'GET',
headers: {
Cookie: gotSetCookie!,
'Content-Type': 'application/json',
},
})
const res2 = await app.request(req2)
expect(res2.status).toBe(200)
const json = await res2.json<{ aud: string; email: string }>()
expect(json.aud).toBe(validProjectId)
expect(json.email).toBe('codehex@hono.js')
})
})
describe('module worker syntax', () => {
const signInPath = '/login'
const adminAuthClient = AdminAuthApiClient.getOrInitialize(validProjectId, new NopCredential())
it.each([
[
'valid case, should be 200',
{
cookieName: 'session',
config: {
projectId: validProjectId,
},
wantStatus: 200,
},
],
[
'valid specified cookie name, should be 200',
{
cookieName: 'x-cookie',
config: {
projectId: validProjectId,
cookieName: 'x-cookie',
},
wantStatus: 200,
},
],
[
'mismatched cookie name, should be 302',
{
cookieName: 'x-cookie',
config: {
projectId: validProjectId, // see package.json
// No specified cookie name.
},
wantStatus: 302,
},
],
[
'invalid project ID, should be 302',
{
cookieName: 'session',
config: {
projectId: 'invalid-projectId',
},
wantStatus: 302,
},
],
])('%s', async (_, { cookieName, config, wantStatus }) => {
const app = new Hono<{ Bindings: VerifyFirebaseAuthEnv }>()
resetAuth()
const PUBLIC_JWK_CACHE_KV = (await mf.getKVNamespace(
'PUBLIC_JWK_CACHE_KV'
)) as unknown as KVNamespace
const env: VerifyFirebaseAuthEnv = {
FIREBASE_AUTH_EMULATOR_HOST: emulatorHost,
PUBLIC_JWK_CACHE_KEY: 'testing-cache-key',
PUBLIC_JWK_CACHE_KV,
}
let gotError: Error | undefined
app.use(
'*',
async (c, next) => {
await next()
gotError = c.error
},
verifySessionCookieFirebaseAuth({
...config,
redirects: {
signIn: signInPath,
},
})
)
app.get('/hello', (c) => c.text('OK'))
const sessionCookie = await adminAuthClient.createSessionCookie(user.idToken, expiresIn, env)
const req = new Request('http://localhost/hello', {
headers: {
Cookie: `${cookieName}=${sessionCookie}; Path=/; HttpOnly`,
},
})
const res = await app.fetch(req, env)
expect(res).not.toBeNull()
expect(res.status).toBe(wantStatus)
if (wantStatus === 302) {
expect(gotError instanceof HTTPException).toBeTruthy()
expect(res.headers.get('location')).toBe(signInPath)
}
})
// NOTE(codehex): We can't check this test because https://identitytoolkit.googleapis.com/v1/sessionCookiePublicKeys endpoint
// responds with `cache-control: no-cache, no-store, max-age=0, must-revalidate`
//
// it('specified keyStore is used', async () => {
// const nopKeyStore = new NopKeyStore()
// const getSpy = vi.spyOn(nopKeyStore, 'get')
// const putSpy = vi.spyOn(nopKeyStore, 'put')
// const app = new Hono<{ Bindings: VerifyFirebaseAuthEnv }>()
// resetAuth()
// const signInPath = '/login'
// app.use(
// '*',
// verifySessionCookieFirebaseAuth({
// projectId: validProjectId,
// keyStore: nopKeyStore,
// redirects: {
// signIn: signInPath,
// },
// })
// )
// app.get('/hello', (c) => c.text('OK'))
// const sessionCookie = await adminAuthClient.createSessionCookie(user.idToken, expiresIn, env)
// const req = new Request('http://localhost/hello', {
// headers: {
// Cookie: `session=${sessionCookie}; Path=/; HttpOnly`,
// },
// })
// // not use firebase emulator to check using key store
// const res = await app.fetch(req, {
// FIREBASE_AUTH_EMULATOR_HOST: undefined,
// })
// expect(res).not.toBeNull()
// expect(res.status).toBe(302)
// expect(res.headers.get('location')).toBe(signInPath)
// expect(getSpy).toHaveBeenCalled()
// expect(putSpy).toHaveBeenCalled()
// })
it('usable id-token in main handler', async () => {
const nopKeyStore = new NopKeyStore()
const app = new Hono<{ Bindings: VerifyFirebaseAuthEnv }>()
resetAuth()
app.use(
'*',
verifySessionCookieFirebaseAuth({
projectId: validProjectId,
keyStore: nopKeyStore,
redirects: {
signIn: signInPath,
},
})
)
app.get('/hello', (c) => c.json(getFirebaseToken(c)))
const sessionCookie = await adminAuthClient.createSessionCookie(user.idToken, expiresIn, env)
const req = new Request('http://localhost/hello', {
headers: {
Cookie: `session=${sessionCookie}; Path=/; HttpOnly`,
},
})
const res = await app.fetch(req, {
FIREBASE_AUTH_EMULATOR_HOST: emulatorHost,
})
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')
})
it('invalid PUBLIC_JWK_CACHE_KV is undefined, should be 501', async () => {
const app = new Hono<{ Bindings: VerifyFirebaseAuthEnv }>()
resetAuth()
let gotError: Error | undefined
app.use(
'*',
async (c, next) => {
await next()
gotError = c.error
},
verifySessionCookieFirebaseAuth({
projectId: validProjectId,
redirects: {
signIn: signInPath,
},
})
)
app.get('/hello', (c) => c.text('OK'))
const sessionCookie = await adminAuthClient.createSessionCookie(user.idToken, expiresIn, env)
const req = new Request('http://localhost/hello', {
headers: {
Cookie: `session=${sessionCookie}; Path=/; HttpOnly`,
},
})
const res = await app.fetch(req, {
FIREBASE_AUTH_EMULATOR_HOST: emulatorHost,
PUBLIC_JWK_CACHE_KV: undefined,
})
expect(gotError instanceof HTTPException).toBeTruthy()
expect(res).not.toBeNull()
expect(res.status).toBe(501)
})
})
})
2022-07-28 13:58:54 +08:00
class NopKeyStore implements KeyStorer {
2023-02-04 19:20:42 +08:00
// eslint-disable-next-line @typescript-eslint/no-empty-function
2022-07-28 13:58:54 +08:00
constructor() {}
get(): Promise<null> {
2023-02-04 19:20:42 +08:00
return new Promise((resolve) => resolve(null))
2022-07-28 13:58:54 +08:00
}
put(): Promise<void> {
2023-02-04 19:20:42 +08:00
return new Promise((resolve) => resolve())
2022-07-28 13:58:54 +08:00
}
}
2023-02-04 19:20:42 +08:00
const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms))
2022-07-28 13:58:54 +08:00
// magic to reset state of static object for "firebase-auth-cloudflare-workers"
2023-02-04 19:20:42 +08:00
const resetAuth = () => delete Auth['instance']
2022-07-28 13:58:54 +08:00
const generateDummyJWT = () => {
const header = JSON.stringify({
2023-02-04 19:20:42 +08:00
alg: 'RS256',
kid: 'kid',
typ: 'JWT',
})
const now = Math.floor(Date.now() / 1000)
2022-07-28 13:58:54 +08:00
const payload = JSON.stringify({
2023-02-04 19:20:42 +08:00
iss: 'https://securetoken.google.com/example-project12345',
aud: 'example-project12345',
2022-07-28 13:58:54 +08:00
auth_time: now - 1000,
2023-02-04 19:20:42 +08:00
user_id: 't1aLdTkAs0S0J0P6TNbjwbmry5B3',
sub: 't1aLdTkAs0S0J0P6TNbjwbmry5B3',
2022-07-28 13:58:54 +08:00
iat: now - 1000,
exp: now + 3000, // + 3s
2023-02-04 19:20:42 +08:00
email: 'codehex@hono.js',
2022-07-28 13:58:54 +08:00
email_verified: false,
firebase: {
identities: {
2023-02-04 19:20:42 +08:00
email: ['codehex@hono.js'],
2022-07-28 13:58:54 +08:00
},
2023-02-04 19:20:42 +08:00
sign_in_provider: 'password',
2022-07-28 13:58:54 +08:00
},
2023-02-04 19:20:42 +08:00
})
return `${btoa(header)}.${btoa(payload)}.`
}
2022-07-28 13:58:54 +08:00
interface EmailPassword {
2023-02-04 19:20:42 +08:00
email: string
password: string
2022-07-28 13:58:54 +08:00
}
export interface signUpResponse {
2023-02-04 19:20:42 +08:00
kind: string
localId: string
email: string
idToken: string
refreshToken: string
expiresIn: string
2022-07-28 13:58:54 +08:00
}
const signUpEmulator = async (
emulatorHost: string,
body: EmailPassword
): Promise<signUpResponse> => {
// http://localhost:9099/identitytoolkit.googleapis.com/v1/accounts:signUp?key=dummy
2023-02-04 19:20:42 +08:00
const url = `http://${emulatorHost}/identitytoolkit.googleapis.com/v1/accounts:signUp?key=dummy`
2022-07-28 13:58:54 +08:00
const resp = await fetch(url, {
2023-02-04 19:20:42 +08:00
method: 'POST',
2022-07-28 13:58:54 +08:00
headers: {
2023-02-04 19:20:42 +08:00
'Content-Type': 'application/json',
2022-07-28 13:58:54 +08:00
},
body: JSON.stringify({
...body,
returnSecureToken: true,
}),
2023-02-04 19:20:42 +08:00
})
2022-07-28 13:58:54 +08:00
if (resp.status !== 200) {
2023-02-04 19:20:42 +08:00
console.log({ status: resp.status })
throw new Error('error')
2022-07-28 13:58:54 +08:00
}
2023-02-04 19:20:42 +08:00
return await resp.json()
}
2022-07-28 13:58:54 +08:00
2023-02-04 19:20:42 +08:00
const deleteAccountEmulator = async (emulatorHost: string, projectId: string): Promise<void> => {
2022-07-28 13:58:54 +08:00
// https://firebase.google.com/docs/reference/rest/auth#section-auth-emulator-clearaccounts
2023-02-04 19:20:42 +08:00
const url = `http://${emulatorHost}/emulator/v1/projects/${projectId}/accounts`
2022-07-28 13:58:54 +08:00
const resp = await fetch(url, {
2023-02-04 19:20:42 +08:00
method: 'DELETE',
})
2022-07-28 13:58:54 +08:00
if (resp.status !== 200) {
2023-02-04 19:20:42 +08:00
console.log({ status: resp.status })
throw new Error('error when clear accounts')
2022-07-28 13:58:54 +08:00
}
2023-02-04 19:20:42 +08:00
return
}
export class NopCredential implements Credential {
getAccessToken(): Promise<GoogleOAuthAccessToken> {
return Promise.resolve({
access_token: 'owner',
expires_in: 9 * 3600,
})
}
}