Skip to content

Commit f61d015

Browse files
committed
Support CORS & Auth
Added CORS check back in and updated JWKS logic to support auth requests with JWT
1 parent c2c3f0f commit f61d015

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

src/index.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { StarbaseDB, StarbaseDBConfiguration } from "./handler";
33
import { DataSource, RegionLocationHint } from "./types";
44
import { createRemoteJWKSet, jwtVerify } from "jose";
55
import { handleStudioRequest } from "./studio";
6+
import { corsPreflight } from "./cors";
67

78
export { StarbaseDBDurableObject } from "./do";
89

@@ -43,7 +44,6 @@ export interface Env {
4344
EXTERNAL_DB_CLOUDFLARE_DATABASE_ID?: string;
4445

4546
AUTH_ALGORITHM?: string;
46-
AUTH_JWT_SECRET?: string;
4747
AUTH_JWKS_ENDPOINT?: string;
4848

4949
// ## DO NOT REMOVE: TEMPLATE INTERFACE ##
@@ -66,6 +66,15 @@ export default {
6666
let role: StarbaseDBConfiguration["role"] = "client";
6767
let context = {};
6868

69+
// Authorize the request with CORS rules before proceeding.
70+
if (request.method === "OPTIONS") {
71+
const preflightResponse = corsPreflight();
72+
73+
if (preflightResponse) {
74+
return preflightResponse;
75+
}
76+
}
77+
6978
// Handle Studio requests before auth checks in the worker.
7079
// StarbaseDB can handle this for us, but we need to handle it
7180
// here before auth checks.
@@ -88,7 +97,7 @@ export default {
8897

8998
// If not admin or client auth, check if JWT auth is available
9099
if (!isAdminAuthorization && !isClientAuthorization) {
91-
if (env.AUTH_JWT_SECRET && env.AUTH_JWKS_ENDPOINT) {
100+
if (env.AUTH_JWKS_ENDPOINT) {
92101
const { payload } = await jwtVerify(
93102
token,
94103
createRemoteJWKSet(new URL(env.AUTH_JWKS_ENDPOINT)),

0 commit comments

Comments
 (0)