feat: migrate MCP server to v2

This commit is contained in:
ajnart committed 2026-08-14 11:32:10 +02:00
1 parent 4ed1eb5d59
commit e4bf2aa13d
17 files changed
+720 -469

No files matched your search

@@ -15,6 +15,7 @@ interface AuthCode {
codeChallenge: string;
codeChallengeMethod: string;
redirectUri: string;
resource: string | null;
expiresAt: number;
}
@@ -24,6 +25,7 @@ interface PendingAuth {
codeChallenge: string;
codeChallengeMethod: string;
state: string | null;
resource: string | null;
expiresAt: number;
}
@@ -102,6 +104,7 @@ export function createAuthCode(
codeChallenge: string,
codeChallengeMethod: string,
redirectUri: string,
resource: string | null,
) {
if (authCodes.size >= MAX_AUTH_CODES) {
return null;
@@ -115,6 +118,7 @@ export function createAuthCode(
codeChallenge,
codeChallengeMethod,
redirectUri,
resource,
expiresAt: Date.now() + AUTH_CODE_TTL_MS,
});
return code;
@@ -1,6 +1,7 @@
import type { NextRequest } from "next/server";
import { auth } from "@homarr/auth/next";
import { extractBaseUrlFromHeaders } from "@homarr/common";
import { consumePendingAuth, createAuthCode, getClient, storePendingAuth } from "../_store";
@@ -37,6 +38,7 @@ export async function GET(req: NextRequest) {
pending.codeChallenge,
pending.codeChallengeMethod,
pending.redirectUri,
pending.resource,
);
if (!code) {
return Response.json(
@@ -59,6 +61,7 @@ export async function GET(req: NextRequest) {
const codeChallenge = url.searchParams.get("code_challenge");
const codeChallengeMethod = url.searchParams.get("code_challenge_method") ?? "S256";
const state = url.searchParams.get("state");
const resource = url.searchParams.get("resource");
if (!clientId || !redirectUri || !codeChallenge) {
return Response.json(
@@ -80,6 +83,17 @@ export async function GET(req: NextRequest) {
);
}
const expectedResource = `${extractBaseUrlFromHeaders(req.headers)}/api/mcp`;
if (resource && resource !== expectedResource) {
return Response.json(
{
error: "invalid_target",
error_description: "The resource must be the canonical Homarr MCP endpoint",
},
{ status: 400 },
);
}
const client = getClient(clientId);
if (!client) {
return Response.json(
@@ -103,7 +117,7 @@ export async function GET(req: NextRequest) {
}
if (session?.user) {
const code = createAuthCode(clientId, session.user.id, codeChallenge, codeChallengeMethod, redirectUri);
const code = createAuthCode(clientId, session.user.id, codeChallenge, codeChallengeMethod, redirectUri, resource);
if (!code) {
return Response.json(
{
@@ -126,6 +140,7 @@ export async function GET(req: NextRequest) {
codeChallenge,
codeChallengeMethod,
state,
resource,
});
if (!id) {
return Response.json(
@@ -80,7 +80,7 @@ export async function POST(req: Request) {
);
}
const { grant_type, code, code_verifier, redirect_uri } = params;
const { grant_type, code, code_verifier, redirect_uri, resource } = params;
if (grant_type !== "authorization_code") {
return Response.json({ error: "unsupported_grant_type" }, { status: 400 });
@@ -111,6 +111,11 @@ export async function POST(req: Request) {
return Response.json({ error: "invalid_grant", error_description: "redirect_uri mismatch" }, { status: 400 });
}
if (authCode.resource && resource !== authCode.resource) {
recordTokenFailure(ip);
return Response.json({ error: "invalid_target", error_description: "resource mismatch" }, { status: 400 });
}
if (!verifyPkce(code_verifier, authCode.codeChallenge, authCode.codeChallengeMethod)) {
recordTokenFailure(ip);
return Response.json({ error: "invalid_grant", error_description: "PKCE verification failed" }, { status: 400 });