Add optional API token auth and validation tests
This commit is contained in:
1 parent
f65e4d9876
commit
bfb8275b2f
5 files changed
+69
No files matched your search
@@ -10,6 +10,7 @@ const PORT = Number(process.env.PORT ?? "8787");
|
||||
const MAX_BODY_BYTES = Number(process.env.MAX_BODY_BYTES ?? 2 * 1024 * 1024);
|
||||
const CORS_ORIGIN = process.env.CORS_ORIGIN ?? "*";
|
||||
const MAX_REQUESTS_PER_MINUTE = Number(process.env.MAX_REQUESTS_PER_MINUTE ?? 120);
|
||||
const SCHEMETA_AUTH_TOKEN = String(process.env.SCHEMETA_AUTH_TOKEN ?? "").trim();
|
||||
const FRONTEND_ROOT = join(process.cwd(), "frontend");
|
||||
export const API_VERSION = "0.3.0";
|
||||
export const SCHEMA_VERSION = "1.0.0";
|
||||
@@ -47,6 +48,24 @@ export function errorEnvelope(code, message) {
|
||||
};
|
||||
}
|
||||
|
||||
export function isAuthorizedRequest(req) {
|
||||
if (!SCHEMETA_AUTH_TOKEN) {
|
||||
return true;
|
||||
}
|
||||
const auth = req.headers?.authorization;
|
||||
if (typeof auth === "string") {
|
||||
const m = /^Bearer\s+(.+)$/i.exec(auth.trim());
|
||||
if (m && m[1] === SCHEMETA_AUTH_TOKEN) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
const apiKey = req.headers?.["x-api-key"];
|
||||
if (typeof apiKey === "string" && apiKey.trim() === SCHEMETA_AUTH_TOKEN) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
function readBody(req) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const chunks = [];
|
||||
@@ -213,6 +232,10 @@ export function createRequestHandler() {
|
||||
}
|
||||
|
||||
if (req.method === "POST" && pathname === "/analyze") {
|
||||
if (!isAuthorizedRequest(req)) {
|
||||
res.setHeader("WWW-Authenticate", "Bearer");
|
||||
return json(res, 401, errorEnvelope("unauthorized", "Missing or invalid API token."));
|
||||
}
|
||||
if (!withinRateLimit(req)) {
|
||||
return json(res, 429, errorEnvelope("rate_limited", "Rate limit exceeded. Try again shortly."));
|
||||
}
|
||||
@@ -232,6 +255,10 @@ export function createRequestHandler() {
|
||||
}
|
||||
|
||||
if (req.method === "POST" && pathname === "/compile") {
|
||||
if (!isAuthorizedRequest(req)) {
|
||||
res.setHeader("WWW-Authenticate", "Bearer");
|
||||
return json(res, 401, errorEnvelope("unauthorized", "Missing or invalid API token."));
|
||||
}
|
||||
if (!withinRateLimit(req)) {
|
||||
return json(res, 429, errorEnvelope("rate_limited", "Rate limit exceeded. Try again shortly."));
|
||||
}
|
||||
@@ -251,6 +278,10 @@ export function createRequestHandler() {
|
||||
}
|
||||
|
||||
if (req.method === "POST" && pathname === "/layout/auto") {
|
||||
if (!isAuthorizedRequest(req)) {
|
||||
res.setHeader("WWW-Authenticate", "Bearer");
|
||||
return json(res, 401, errorEnvelope("unauthorized", "Missing or invalid API token."));
|
||||
}
|
||||
if (!withinRateLimit(req)) {
|
||||
return json(res, 429, errorEnvelope("rate_limited", "Rate limit exceeded. Try again shortly."));
|
||||
}
|
||||
@@ -279,6 +310,10 @@ export function createRequestHandler() {
|
||||
}
|
||||
|
||||
if (req.method === "POST" && pathname === "/layout/tidy") {
|
||||
if (!isAuthorizedRequest(req)) {
|
||||
res.setHeader("WWW-Authenticate", "Bearer");
|
||||
return json(res, 401, errorEnvelope("unauthorized", "Missing or invalid API token."));
|
||||
}
|
||||
if (!withinRateLimit(req)) {
|
||||
return json(res, 429, errorEnvelope("rate_limited", "Rate limit exceeded. Try again shortly."));
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user