Fix Turnstile enabled, remove TODO and log, add warning to readme

This commit is contained in:
William Oldham
2023-12-20 15:14:49 +00:00
parent ea88ca7cc2
commit 1d35c0fa81
3 changed files with 11 additions and 13 deletions

View File

@@ -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

View File

@@ -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<Env>) {
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) {

View File

@@ -7,13 +7,10 @@ export async function validateTurnstile(context: Context<Env>) {
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,