Compare commits
6
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9486518832 | ||
|
|
b35a465303 | ||
|
|
54c29d182d | ||
|
|
1728752ce1 | ||
|
|
1fed65a187 | ||
|
|
7d4e8daaaf |
@@ -31,6 +31,11 @@ ACME_EMAIL=you@example.com
|
|||||||
# resource server). See README.md for exact provider settings.
|
# resource server). See README.md for exact provider settings.
|
||||||
# -----------------------------------------------------------------------------
|
# -----------------------------------------------------------------------------
|
||||||
OIDC_ISSUER=https://auth.example.com/application/o/shared-memory/
|
OIDC_ISSUER=https://auth.example.com/application/o/shared-memory/
|
||||||
|
# Issuer of MCP access tokens, when the MCP endpoint is a separate application
|
||||||
|
# in your IdP (it usually is). Authentik stamps each token with its own app
|
||||||
|
# slug, so verifying MCP tokens against OIDC_ISSUER fails with
|
||||||
|
# "claim invalid: iss". Defaults to OIDC_ISSUER.
|
||||||
|
OIDC_ISSUER_MCP=https://auth.example.com/application/o/shared-memory-mcp/
|
||||||
OIDC_CLIENT_ID_WEB=replace-me
|
OIDC_CLIENT_ID_WEB=replace-me
|
||||||
OIDC_CLIENT_SECRET_WEB=replace-me
|
OIDC_CLIENT_SECRET_WEB=replace-me
|
||||||
OIDC_CLIENT_ID_MCP=replace-me
|
OIDC_CLIENT_ID_MCP=replace-me
|
||||||
|
|||||||
@@ -144,6 +144,7 @@ Copy `.env.example` and fill in the values below.
|
|||||||
| `OIDC_CLIENT_SECRET_WEB` | both | Client secret of the Web-UI client. |
|
| `OIDC_CLIENT_SECRET_WEB` | both | Client secret of the Web-UI client. |
|
||||||
| `OIDC_CLIENT_ID_MCP` | both | Client ID of the MCP resource-server client in your IdP. |
|
| `OIDC_CLIENT_ID_MCP` | both | Client ID of the MCP resource-server client in your IdP. |
|
||||||
| `OIDC_AUDIENCE` | both | Audience string the MCP access token must carry in its `aud` claim. Recommended: `shared-memory`. |
|
| `OIDC_AUDIENCE` | both | Audience string the MCP access token must carry in its `aud` claim. Recommended: `shared-memory`. |
|
||||||
|
| `OIDC_ISSUER_MCP` | optional | Issuer of MCP access tokens when the MCP endpoint is a separate IdP application (Authentik stamps each app's tokens with its own slug). Defaults to `OIDC_ISSUER`. |
|
||||||
| `OIDC_AUDIENCE_SCOPE` | optional | Name of the IdP scope whose mapping emits that `aud` claim. Advertised in `scopes_supported` so clients request it. Defaults to `aud-<OIDC_AUDIENCE>`. |
|
| `OIDC_AUDIENCE_SCOPE` | optional | Name of the IdP scope whose mapping emits that `aud` claim. Advertised in `scopes_supported` so clients request it. Defaults to `aud-<OIDC_AUDIENCE>`. |
|
||||||
| `POSTGRES_USER` / `POSTGRES_PASSWORD` / `POSTGRES_DB` | both | Local Postgres credentials. |
|
| `POSTGRES_USER` / `POSTGRES_PASSWORD` / `POSTGRES_DB` | both | Local Postgres credentials. |
|
||||||
| `NEXTAUTH_SECRET` | both | Session-cookie signing key. Generate with `openssl rand -base64 32`. |
|
| `NEXTAUTH_SECRET` | both | Session-cookie signing key. Generate with `openssl rand -base64 32`. |
|
||||||
@@ -322,6 +323,25 @@ reliable pattern:
|
|||||||
> | jq .scopes_supported # must include aud-<your audience>
|
> | jq .scopes_supported # must include aud-<your audience>
|
||||||
> ```
|
> ```
|
||||||
>
|
>
|
||||||
|
> **Second trap, same failure surface:** the MCP endpoint is a *separate
|
||||||
|
> application* from the Web UI, and Authentik's default `per_provider` issuer
|
||||||
|
> mode stamps each token with its own application slug. So MCP tokens carry
|
||||||
|
> `iss: .../application/o/shared-memory-mcp/` while `OIDC_ISSUER` points at
|
||||||
|
> `.../application/o/shared-memory/`, and verification fails with
|
||||||
|
> `claim invalid: iss` even once `aud` is correct. Set `OIDC_ISSUER_MCP` to the
|
||||||
|
> MCP application's issuer. Confirm which one your tokens actually carry:
|
||||||
|
>
|
||||||
|
> ```bash
|
||||||
|
> curl -s https://auth.example.com/application/o/shared-memory-mcp/.well-known/openid-configuration | jq .issuer
|
||||||
|
> ```
|
||||||
|
>
|
||||||
|
> **Identity note:** the app verifies MCP tokens against `OIDC_ISSUER_MCP` but
|
||||||
|
> keys the user record on `OIDC_ISSUER`. Authentik's `sub` is `user.uid`, which
|
||||||
|
> is stable across providers, so the same person resolves to the same row
|
||||||
|
> whether they arrive via the Web UI or the MCP endpoint. Without that
|
||||||
|
> normalization the MCP path silently creates a second, empty account instead
|
||||||
|
> of failing visibly.
|
||||||
|
>
|
||||||
> Note also that Claude Code sends an RFC 8707 `resource` parameter on the
|
> Note also that Claude Code sends an RFC 8707 `resource` parameter on the
|
||||||
> authorize request; Authentik 2026.5 ignores it, so it cannot be relied on
|
> authorize request; Authentik 2026.5 ignores it, so it cannot be relied on
|
||||||
> for audience binding. The scope mapping is what sets `aud`.
|
> for audience binding. The scope mapping is what sets `aud`.
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import { NextResponse } from "next/server";
|
import { NextResponse } from "next/server";
|
||||||
import { env } from "@/lib/env";
|
import { env } from "@/lib/env";
|
||||||
|
import { mcpIssuer } from "@/lib/auth/jwt";
|
||||||
|
|
||||||
export const runtime = "nodejs";
|
export const runtime = "nodejs";
|
||||||
export const dynamic = "force-dynamic";
|
export const dynamic = "force-dynamic";
|
||||||
@@ -22,7 +23,10 @@ export function GET() {
|
|||||||
|
|
||||||
return NextResponse.json({
|
return NextResponse.json({
|
||||||
resource,
|
resource,
|
||||||
authorization_servers: [env().OIDC_ISSUER],
|
// The MCP application's issuer, which is not necessarily the Web UI's —
|
||||||
|
// see mcpIssuer(). Advertising the wrong one sends clients to a discovery
|
||||||
|
// document whose tokens this endpoint will then reject on `iss`.
|
||||||
|
authorization_servers: [mcpIssuer()],
|
||||||
scopes_supported: ["openid", "profile", "email", audienceScope],
|
scopes_supported: ["openid", "profile", "email", audienceScope],
|
||||||
bearer_methods_supported: ["header"],
|
bearer_methods_supported: ["header"],
|
||||||
resource_documentation: `${resource}/`,
|
resource_documentation: `${resource}/`,
|
||||||
|
|||||||
@@ -23,13 +23,21 @@ type GlobalWithJwks = typeof globalThis & {
|
|||||||
};
|
};
|
||||||
const g = globalThis as GlobalWithJwks;
|
const g = globalThis as GlobalWithJwks;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Issuer of MCP access tokens. The MCP endpoint is a separate application in
|
||||||
|
* the IdP from the Web UI, and Authentik stamps each token with its own
|
||||||
|
* application slug, so this is NOT interchangeable with OIDC_ISSUER.
|
||||||
|
*/
|
||||||
|
export function mcpIssuer(): string {
|
||||||
|
return (env().OIDC_ISSUER_MCP ?? env().OIDC_ISSUER).replace(/\/$/, "");
|
||||||
|
}
|
||||||
|
|
||||||
function jwks() {
|
function jwks() {
|
||||||
if (g.__sharedMemoryJwks) return g.__sharedMemoryJwks;
|
if (g.__sharedMemoryJwks) return g.__sharedMemoryJwks;
|
||||||
// Authentik discovery is at `${issuer}/.well-known/openid-configuration`;
|
// Authentik discovery is at `${issuer}/.well-known/openid-configuration`;
|
||||||
// the JWKS URI is normally `${issuer}/jwks/` or `${issuer}/.well-known/jwks.json`.
|
// the JWKS URI is normally `${issuer}/jwks/` or `${issuer}/.well-known/jwks.json`.
|
||||||
// Authentik canonically serves `${issuer}/jwks/`.
|
// Authentik canonically serves `${issuer}/jwks/`.
|
||||||
const issuer = env().OIDC_ISSUER.replace(/\/$/, "");
|
const url = new URL(`${mcpIssuer()}/jwks/`);
|
||||||
const url = new URL(`${issuer}/jwks/`);
|
|
||||||
g.__sharedMemoryJwks = createRemoteJWKSet(url, {
|
g.__sharedMemoryJwks = createRemoteJWKSet(url, {
|
||||||
cacheMaxAge: 10 * 60 * 1000, // 10 min
|
cacheMaxAge: 10 * 60 * 1000, // 10 min
|
||||||
cooldownDuration: 30 * 1000,
|
cooldownDuration: 30 * 1000,
|
||||||
@@ -118,7 +126,7 @@ export async function authenticateBearer(authHeader: string | null): Promise<Aut
|
|||||||
}
|
}
|
||||||
|
|
||||||
const { payload } = await jwtVerify(token, jwks(), {
|
const { payload } = await jwtVerify(token, jwks(), {
|
||||||
issuer: env().OIDC_ISSUER,
|
issuer: mcpIssuer(),
|
||||||
audience: env().OIDC_AUDIENCE,
|
audience: env().OIDC_AUDIENCE,
|
||||||
});
|
});
|
||||||
if (!payload.sub) {
|
if (!payload.sub) {
|
||||||
@@ -127,7 +135,24 @@ export async function authenticateBearer(authHeader: string | null): Promise<Aut
|
|||||||
buildWwwAuthenticate("invalid_token", "missing sub"),
|
buildWwwAuthenticate("invalid_token", "missing sub"),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
return { ...payload, groups: extractGroupsClaim(payload) } as AuthenticatedClaims;
|
// Normalize the issuer for identity purposes.
|
||||||
|
//
|
||||||
|
// The token was just verified against mcpIssuer() — that check is done.
|
||||||
|
// But identity is keyed on (oidc_iss, oidc_sub), and the Web UI signs
|
||||||
|
// people in through a DIFFERENT application whose tokens carry
|
||||||
|
// OIDC_ISSUER. Authentik's `sub` is stable across providers (it is
|
||||||
|
// `user.uid`, a user-level value), so the only thing that differs is the
|
||||||
|
// issuer.
|
||||||
|
//
|
||||||
|
// Leave it un-normalized and userContextFromClaims — which UPSERTS rather
|
||||||
|
// than failing — quietly creates a SECOND user row for the same human:
|
||||||
|
// MCP writes would land in an account with none of their memories, and
|
||||||
|
// nothing would look broken. Pin identity to the canonical issuer.
|
||||||
|
return {
|
||||||
|
...payload,
|
||||||
|
iss: env().OIDC_ISSUER,
|
||||||
|
groups: extractGroupsClaim(payload),
|
||||||
|
} as AuthenticatedClaims;
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
if (err instanceof UnauthorizedError) throw err;
|
if (err instanceof UnauthorizedError) throw err;
|
||||||
const desc =
|
const desc =
|
||||||
|
|||||||
+24
-1
@@ -4,6 +4,16 @@ const Bool = z
|
|||||||
.union([z.boolean(), z.enum(["true", "false", "1", "0"])])
|
.union([z.boolean(), z.enum(["true", "false", "1", "0"])])
|
||||||
.transform((v) => v === true || v === "true" || v === "1");
|
.transform((v) => v === true || v === "true" || v === "1");
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Treat an empty string as "not set".
|
||||||
|
*
|
||||||
|
* docker-compose renders `${VAR:-}` as an empty string rather than omitting
|
||||||
|
* the key, so an unset optional var arrives as "" and would otherwise fail
|
||||||
|
* `.url()` / `.min(1)` validation and take the whole app down at boot.
|
||||||
|
*/
|
||||||
|
const optional = <T extends z.ZodTypeAny>(schema: T) =>
|
||||||
|
z.preprocess((v) => (v === "" ? undefined : v), schema.optional());
|
||||||
|
|
||||||
const envSchema = z.object({
|
const envSchema = z.object({
|
||||||
NODE_ENV: z.enum(["development", "test", "production"]).default("development"),
|
NODE_ENV: z.enum(["development", "test", "production"]).default("development"),
|
||||||
LOG_LEVEL: z.enum(["debug", "info", "warn", "error"]).default("info"),
|
LOG_LEVEL: z.enum(["debug", "info", "warn", "error"]).default("info"),
|
||||||
@@ -13,6 +23,19 @@ const envSchema = z.object({
|
|||||||
|
|
||||||
// Authentik OIDC
|
// Authentik OIDC
|
||||||
OIDC_ISSUER: z.string().url(),
|
OIDC_ISSUER: z.string().url(),
|
||||||
|
|
||||||
|
// Issuer of MCP *access tokens*, when it differs from OIDC_ISSUER.
|
||||||
|
//
|
||||||
|
// The Web UI and the MCP endpoint are two separate applications in the IdP,
|
||||||
|
// and Authentik's default `per_provider` issuer mode stamps each token with
|
||||||
|
// its own application slug. So the web app issues
|
||||||
|
// `.../application/o/<slug>/` while the MCP provider issues
|
||||||
|
// `.../application/o/<slug>-mcp/`, and verifying MCP tokens against
|
||||||
|
// OIDC_ISSUER fails with "claim invalid: iss".
|
||||||
|
//
|
||||||
|
// Set this to the MCP application's issuer. Defaults to OIDC_ISSUER for
|
||||||
|
// single-application setups.
|
||||||
|
OIDC_ISSUER_MCP: optional(z.string().url()),
|
||||||
OIDC_CLIENT_ID_WEB: z.string().min(1),
|
OIDC_CLIENT_ID_WEB: z.string().min(1),
|
||||||
OIDC_CLIENT_SECRET_WEB: z.string().min(1),
|
OIDC_CLIENT_SECRET_WEB: z.string().min(1),
|
||||||
OIDC_CLIENT_ID_MCP: z.string().min(1),
|
OIDC_CLIENT_ID_MCP: z.string().min(1),
|
||||||
@@ -27,7 +50,7 @@ const envSchema = z.object({
|
|||||||
// every token arrives without an `aud` claim (-> 401 "claim invalid: aud").
|
// every token arrives without an `aud` claim (-> 401 "claim invalid: aud").
|
||||||
//
|
//
|
||||||
// Defaults to the `aud-<audience>` convention used in the README setup.
|
// Defaults to the `aud-<audience>` convention used in the README setup.
|
||||||
OIDC_AUDIENCE_SCOPE: z.string().min(1).optional(),
|
OIDC_AUDIENCE_SCOPE: optional(z.string().min(1)),
|
||||||
|
|
||||||
// Database
|
// Database
|
||||||
DATABASE_URL: z.string().url(),
|
DATABASE_URL: z.string().url(),
|
||||||
|
|||||||
@@ -113,6 +113,10 @@ services:
|
|||||||
OIDC_CLIENT_SECRET_WEB: ${OIDC_CLIENT_SECRET_WEB:?required}
|
OIDC_CLIENT_SECRET_WEB: ${OIDC_CLIENT_SECRET_WEB:?required}
|
||||||
OIDC_CLIENT_ID_MCP: ${OIDC_CLIENT_ID_MCP:?required}
|
OIDC_CLIENT_ID_MCP: ${OIDC_CLIENT_ID_MCP:?required}
|
||||||
OIDC_AUDIENCE: ${OIDC_AUDIENCE:?required}
|
OIDC_AUDIENCE: ${OIDC_AUDIENCE:?required}
|
||||||
|
# Optional. This block is an explicit allow-list, not env_file — a var
|
||||||
|
# added to .env but not listed here never reaches the container.
|
||||||
|
OIDC_ISSUER_MCP: ${OIDC_ISSUER_MCP:-}
|
||||||
|
OIDC_AUDIENCE_SCOPE: ${OIDC_AUDIENCE_SCOPE:-}
|
||||||
|
|
||||||
DATABASE_URL: postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@db:5432/${POSTGRES_DB}
|
DATABASE_URL: postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@db:5432/${POSTGRES_DB}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user