From 1d35c0fa81a636679ce289804f86e3cb4c3d707e Mon Sep 17 00:00:00 2001 From: William Oldham Date: Wed, 20 Dec 2023 15:14:49 +0000 Subject: [PATCH] Fix Turnstile enabled, remove TODO and log, add warning to readme --- README.md | 3 +++ src/index.ts | 18 ++++++++---------- src/turnstile.ts | 3 --- 3 files changed, 11 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index fa40ae6..c07b246 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,5 @@ # provider-api Our provider package exposed as a HTTP API + +> **Warning** +> This only works on Cloudflare due to CloudFlare specific logic and build processes diff --git a/src/index.ts b/src/index.ts index e9f0912..767cf8e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,4 +1,4 @@ -import { Hono } from 'hono'; +import { Context, Env, Hono } from 'hono'; import { streamSSE } from 'hono/streaming'; import { cors } from 'hono/cors'; import { @@ -23,6 +23,10 @@ const providers = makeProviders({ const app = new Hono(); +function isTurnstileEnabled(context: Context) { + return (context.env?.TURNSTILE_ENABLED ?? "true") === "true" +} + app.use('*', (context, next) => { const allowedCorsHosts = ((context.env?.CORS_ALLOWED as string) ?? '').split( ',', @@ -60,11 +64,9 @@ async function writeSSEEvent( app.get('/scrape', async (context) => { const queryParams = context.req.query(); - const turnstileEnabled = Boolean(context.env?.TURNSTILE_ENABLED); - let jwtResponse: string | undefined = undefined; - if (turnstileEnabled) { + if (isTurnstileEnabled(context)) { const turnstileResponse = await validateTurnstile(context); if (!turnstileResponse.success) { @@ -136,11 +138,9 @@ app.get('/scrape', async (context) => { app.get('/scrape/embed', async (context) => { const queryParams = context.req.query(); - const turnstileEnabled = Boolean(context.env?.TURNSTILE_ENABLED); - let jwtResponse: string | undefined = undefined; - if (turnstileEnabled) { + if (isTurnstileEnabled(context)) { const turnstileResponse = await validateTurnstile(context); if (!turnstileResponse.success) { @@ -203,11 +203,9 @@ app.get('/scrape/embed', async (context) => { app.get('/scrape/source', async (context) => { const queryParams = context.req.query(); - const turnstileEnabled = Boolean(context.env?.TURNSTILE_ENABLED); - let jwtResponse: string | undefined = undefined; - if (turnstileEnabled) { + if (isTurnstileEnabled(context)) { const turnstileResponse = await validateTurnstile(context); if (!turnstileResponse.success) { diff --git a/src/turnstile.ts b/src/turnstile.ts index e149ebe..abe028f 100644 --- a/src/turnstile.ts +++ b/src/turnstile.ts @@ -7,13 +7,10 @@ export async function validateTurnstile(context: Context) { const token = context.req.query('token') || ''; - // TODO: Make this cross platform const ip = context.req.header('CF-Connecting-IP') || ''; if (token.startsWith('jwt|')) { try { - console.log(token, token.slice('jwt|'.length)); - const isValid = await jsonwebtoken.verify( token.slice('jwt|'.length), jwtSecret,