From 38c2c9fb6da44e0205e5dc65cbd787246b25784e Mon Sep 17 00:00:00 2001 From: Leo dev Date: Sat, 27 Dec 2025 02:28:47 +0100 Subject: [PATCH] Node Auth #4 --- package-lock.json | 7 +++++++ package.json | 1 + src/app/api/auth.ts | 18 ++++++++++++++++++ src/app/api/auth/route.ts | 21 +++++++++++++++++++++ src/app/api/route.ts | 6 +++--- 5 files changed, 50 insertions(+), 3 deletions(-) create mode 100644 src/app/api/auth.ts create mode 100644 src/app/api/auth/route.ts diff --git a/package-lock.json b/package-lock.json index fb776f4..f45733d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -18,6 +18,7 @@ "class-variance-authority": "^0.7.1", "clsx": "^2.1.1", "dockerode": "^4.0.9", + "http-status-codes": "^2.3.0", "js-cookie": "^3.0.5", "lucide-react": "^0.562.0", "next": "16.1.1", @@ -2732,6 +2733,12 @@ "dev": true, "license": "ISC" }, + "node_modules/http-status-codes": { + "version": "2.3.0", + "resolved": "https://registry.npmjs.org/http-status-codes/-/http-status-codes-2.3.0.tgz", + "integrity": "sha512-RJ8XvFvpPM/Dmc5SV+dC4y5PCeOhT3x1Hq0NU3rjGeg5a/CqlhZ7uudknPwZFz4aeAXDcbAyaeP7GAo9lvngtA==", + "license": "MIT" + }, "node_modules/ieee754": { "version": "1.2.1", "resolved": "https://registry.npmjs.org/ieee754/-/ieee754-1.2.1.tgz", diff --git a/package.json b/package.json index 3942165..2721444 100644 --- a/package.json +++ b/package.json @@ -18,6 +18,7 @@ "class-variance-authority": "^0.7.1", "clsx": "^2.1.1", "dockerode": "^4.0.9", + "http-status-codes": "^2.3.0", "js-cookie": "^3.0.5", "lucide-react": "^0.562.0", "next": "16.1.1", diff --git a/src/app/api/auth.ts b/src/app/api/auth.ts new file mode 100644 index 0000000..92d9f32 --- /dev/null +++ b/src/app/api/auth.ts @@ -0,0 +1,18 @@ +import { read } from "@/app/api/file"; +import { NodeJsonTemplate } from "@/app/api/types"; +import { NextRequest } from "next/server"; + +export default async function testAuth( + req: NextRequest +): Promise { + const auth = req.headers.get("Authorization")?.replaceAll("ApiKey", ""); + + if (!auth) + return "Missing Authorization header (Authorization: ApiKey )"; + + const { key } = await read(NodeJsonTemplate, "data", "node.json"); + + if (key !== auth) return "Invalid key"; + + return null; +} diff --git a/src/app/api/auth/route.ts b/src/app/api/auth/route.ts new file mode 100644 index 0000000..b2a97fe --- /dev/null +++ b/src/app/api/auth/route.ts @@ -0,0 +1,21 @@ +import { NextRequest, NextResponse } from "next/server"; +import { version } from "@/../package.json"; +import testAuth from "../auth"; +import { StatusCodes } from "http-status-codes"; + +export async function GET(req: NextRequest): Promise { + const auth = testAuth(req); + + if (auth) { + return NextResponse.json( + { message: auth }, + { status: StatusCodes.UNAUTHORIZED } + ); + } + + return NextResponse.json({ + message: "ok", + app: "dockship", + version, + }); +} diff --git a/src/app/api/route.ts b/src/app/api/route.ts index 210379b..bd46b5c 100644 --- a/src/app/api/route.ts +++ b/src/app/api/route.ts @@ -1,7 +1,7 @@ import { NextRequest, NextResponse } from "next/server"; -import { version } from "../../../package.json"; -import { read } from "./file"; -import { NodeJsonTemplate } from "./types"; +import { version } from "@/../package.json"; +import { read } from "@/app/api/file"; +import { NodeJsonTemplate } from "@/app/api/types"; export async function GET(req: NextRequest): Promise { const { node_id } = await read(NodeJsonTemplate, "data", "node.json");