|
| 1 | +/** |
| 2 | + * Auth0 authentication configuration for Express MCP server. |
| 3 | + */ |
| 4 | + |
| 5 | +import { type RequestHandler, type Router } from "express"; |
| 6 | +import { AUTH0_DOMAIN, AUDIENCE, MCP_SERVER_URL } from "./config.js"; |
| 7 | +import { AccessTokenClaims, Auth } from "./types.js"; |
| 8 | +import { SCOPES_SUPPORTED } from "./tools.js"; |
| 9 | +import { ApiClient, VerifyAccessTokenError } from "@auth0/auth0-api-js"; |
| 10 | +import { |
| 11 | + getOAuthProtectedResourceMetadataUrl, |
| 12 | + mcpAuthMetadataRouter, |
| 13 | +} from "@modelcontextprotocol/sdk/server/auth/router.js"; |
| 14 | +import { requireBearerAuth } from "@modelcontextprotocol/sdk/server/auth/middleware/bearerAuth.js"; |
| 15 | +import { discoverAuthorizationServerMetadata } from "@modelcontextprotocol/sdk/client/auth.js"; |
| 16 | +import { InvalidTokenError } from "@modelcontextprotocol/sdk/server/auth/errors.js"; |
| 17 | + |
| 18 | +/** |
| 19 | + * Returns a router that includes MCP authorization metadata endpoints. |
| 20 | + */ |
| 21 | +export async function authMetadataRouter(): Promise<Router> { |
| 22 | + const oauthMetadata = await discoverAuthorizationServerMetadata( |
| 23 | + new URL(`https://${AUTH0_DOMAIN}`) |
| 24 | + ); |
| 25 | + |
| 26 | + if (!oauthMetadata) { |
| 27 | + throw new Error(`Failed to fetch OAuth metadata from ${AUTH0_DOMAIN}`); |
| 28 | + } |
| 29 | + |
| 30 | + return mcpAuthMetadataRouter({ |
| 31 | + resourceServerUrl: new URL(MCP_SERVER_URL), |
| 32 | + scopesSupported: SCOPES_SUPPORTED, |
| 33 | + oauthMetadata, |
| 34 | + }); |
| 35 | +} |
| 36 | + |
| 37 | +/** |
| 38 | + * Returns a middleware that verifies the access token and extracts user information. |
| 39 | + */ |
| 40 | +export function authMiddleware(): RequestHandler { |
| 41 | + const apiClient = new ApiClient({ |
| 42 | + domain: AUTH0_DOMAIN, |
| 43 | + audience: AUDIENCE, |
| 44 | + }); |
| 45 | + |
| 46 | + const verify = async (token: string): Promise<Auth> => { |
| 47 | + try { |
| 48 | + const decoded = (await apiClient.verifyAccessToken({ |
| 49 | + accessToken: token, |
| 50 | + })) as AccessTokenClaims; |
| 51 | + |
| 52 | + const clientId = decoded.client_id ?? decoded.azp; |
| 53 | + if (!clientId) { |
| 54 | + throw new Error( |
| 55 | + "Token is missing required client identification (client_id or azp claim)." |
| 56 | + ); |
| 57 | + } |
| 58 | + |
| 59 | + return { |
| 60 | + token, |
| 61 | + clientId, |
| 62 | + scopes: |
| 63 | + typeof decoded.scope === "string" |
| 64 | + ? decoded.scope.split(" ").filter(Boolean) |
| 65 | + : [], |
| 66 | + ...(decoded.exp && { expiresAt: decoded.exp }), |
| 67 | + extra: { |
| 68 | + sub: decoded.sub, |
| 69 | + ...(decoded.client_id && { client_id: decoded.client_id }), |
| 70 | + ...(decoded.azp && { azp: decoded.azp }), |
| 71 | + ...(decoded.name && { name: decoded.name }), |
| 72 | + ...(decoded.email && { email: decoded.email }), |
| 73 | + }, |
| 74 | + }; |
| 75 | + } catch (error) { |
| 76 | + if (error instanceof VerifyAccessTokenError) { |
| 77 | + throw new InvalidTokenError(error.message); |
| 78 | + } |
| 79 | + throw error; |
| 80 | + } |
| 81 | + }; |
| 82 | + |
| 83 | + return requireBearerAuth({ |
| 84 | + resourceMetadataUrl: getOAuthProtectedResourceMetadataUrl( |
| 85 | + new URL(MCP_SERVER_URL) |
| 86 | + ), |
| 87 | + verifier: { verifyAccessToken: verify }, |
| 88 | + }); |
| 89 | +} |
0 commit comments