feat: add api key check in api route
This commit is contained in:
parent
72d0364fee
commit
9aa2484872
@ -3,6 +3,7 @@ import { createRun } from "../../../server/createRun";
|
|||||||
import { db } from "@/db/db";
|
import { db } from "@/db/db";
|
||||||
import { deploymentsTable } from "@/db/schema";
|
import { deploymentsTable } from "@/db/schema";
|
||||||
import { getRunsData } from "@/server/getRunsOutput";
|
import { getRunsData } from "@/server/getRunsOutput";
|
||||||
|
import { parseJWT } from "@/server/parseJWT";
|
||||||
import { replaceCDNUrl } from "@/server/resource";
|
import { replaceCDNUrl } from "@/server/resource";
|
||||||
import { eq } from "drizzle-orm";
|
import { eq } from "drizzle-orm";
|
||||||
import { NextResponse } from "next/server";
|
import { NextResponse } from "next/server";
|
||||||
@ -18,6 +19,14 @@ const Request2 = z.object({
|
|||||||
});
|
});
|
||||||
|
|
||||||
export async function GET(request: Request) {
|
export async function GET(request: Request) {
|
||||||
|
const token = request.headers.get("Authorization")?.split(" ")?.[1]; // Assuming token is sent as "Bearer your_token"
|
||||||
|
const userData = token ? parseJWT(token) : undefined;
|
||||||
|
if (!userData) {
|
||||||
|
return new NextResponse("Invalid or expired token", {
|
||||||
|
status: 401,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
const [data, error] = await parseDataSafe(Request2, request);
|
const [data, error] = await parseDataSafe(Request2, request);
|
||||||
if (!data || error) return error;
|
if (!data || error) return error;
|
||||||
|
|
||||||
@ -44,6 +53,14 @@ export async function GET(request: Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function POST(request: Request) {
|
export async function POST(request: Request) {
|
||||||
|
const token = request.headers.get("Authorization")?.split(" ")?.[1]; // Assuming token is sent as "Bearer your_token"
|
||||||
|
const userData = token ? parseJWT(token) : undefined;
|
||||||
|
if (!userData) {
|
||||||
|
return new NextResponse("Invalid or expired token", {
|
||||||
|
status: 401,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
const [data, error] = await parseDataSafe(Request, request);
|
const [data, error] = await parseDataSafe(Request, request);
|
||||||
if (!data || error) return error;
|
if (!data || error) return error;
|
||||||
|
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
import { parseJWT } from "../../../server/parseJWT";
|
||||||
import { db } from "@/db/db";
|
import { db } from "@/db/db";
|
||||||
import {
|
import {
|
||||||
workflowAPIType,
|
workflowAPIType,
|
||||||
@ -7,7 +8,6 @@ import {
|
|||||||
} from "@/db/schema";
|
} from "@/db/schema";
|
||||||
import { parseDataSafe } from "@/lib/parseDataSafe";
|
import { parseDataSafe } from "@/lib/parseDataSafe";
|
||||||
import { sql } from "drizzle-orm";
|
import { sql } from "drizzle-orm";
|
||||||
import jwt from "jsonwebtoken";
|
|
||||||
import { NextResponse } from "next/server";
|
import { NextResponse } from "next/server";
|
||||||
import { z } from "zod";
|
import { z } from "zod";
|
||||||
|
|
||||||
@ -36,24 +36,6 @@ export async function OPTIONS(request: Request) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
const APIKeyBodyRequest = z.object({
|
|
||||||
user_id: z.string().optional(),
|
|
||||||
org_id: z.string().optional(),
|
|
||||||
iat: z.number(),
|
|
||||||
});
|
|
||||||
|
|
||||||
function parseJWT(token: string) {
|
|
||||||
try {
|
|
||||||
// Verify the token - this also decodes it
|
|
||||||
const decoded = jwt.verify(token, process.env.JWT_SECRET!);
|
|
||||||
return APIKeyBodyRequest.parse(decoded);
|
|
||||||
} catch (err) {
|
|
||||||
// Handle error (token is invalid, expired, etc.)
|
|
||||||
console.error(err);
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function POST(request: Request) {
|
export async function POST(request: Request) {
|
||||||
const token = request.headers.get("Authorization")?.split(" ")?.[1]; // Assuming token is sent as "Bearer your_token"
|
const token = request.headers.get("Authorization")?.split(" ")?.[1]; // Assuming token is sent as "Bearer your_token"
|
||||||
const userData = token ? parseJWT(token) : undefined;
|
const userData = token ? parseJWT(token) : undefined;
|
||||||
@ -64,8 +46,6 @@ export async function POST(request: Request) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log(userData);
|
|
||||||
|
|
||||||
const { user_id, org_id } = userData;
|
const { user_id, org_id } = userData;
|
||||||
|
|
||||||
if (!user_id) return new NextResponse("Invalid user_id", { status: 401 });
|
if (!user_id) return new NextResponse("Invalid user_id", { status: 401 });
|
||||||
|
7
web/src/server/APIKeyBodyRequest.ts
Normal file
7
web/src/server/APIKeyBodyRequest.ts
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
import { z } from "zod";
|
||||||
|
|
||||||
|
export const APIKeyBodyRequest = z.object({
|
||||||
|
user_id: z.string().optional(),
|
||||||
|
org_id: z.string().optional(),
|
||||||
|
iat: z.number(),
|
||||||
|
});
|
14
web/src/server/parseJWT.ts
Normal file
14
web/src/server/parseJWT.ts
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
import { APIKeyBodyRequest } from "@/server/APIKeyBodyRequest";
|
||||||
|
import jwt from "jsonwebtoken";
|
||||||
|
|
||||||
|
export function parseJWT(token: string) {
|
||||||
|
try {
|
||||||
|
// Verify the token - this also decodes it
|
||||||
|
const decoded = jwt.verify(token, process.env.JWT_SECRET!);
|
||||||
|
return APIKeyBodyRequest.parse(decoded);
|
||||||
|
} catch (err) {
|
||||||
|
// Handle error (token is invalid, expired, etc.)
|
||||||
|
console.error(err);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user