Skip to content

Commit 740bf17

Browse files
committed
feat(mcp): oauth-authorization-server
1 parent 32b7e07 commit 740bf17

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-2
lines changed

doit-mcp-server/src/app.ts

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { decodeJWT } from "../../src/utils/util";
1515

1616
export type Bindings = Env & {
1717
OAUTH_PROVIDER: OAuthHelpers;
18+
OAUTH_KV: KVNamespace;
1819
};
1920

2021
const app = new Hono<{
@@ -74,16 +75,23 @@ async function handleApprove(c: any) {
7475
await parseApproveFormBody(await c.req.parseBody());
7576

7677
if (!oauthReqInfo) {
77-
return c.html("INVALID LOGIN", 401);
78+
// Add WWW-Authenticate header with resource_metadata
79+
const url = new URL(c.req.url);
80+
const base = url.origin;
81+
return c.html("INVALID LOGIN", 401, {
82+
"WWW-Authenticate": `Bearer resource_metadata=\"${base}/.well-known/oauth-authorization-server\"`,
83+
});
7884
}
7985

86+
const jwtInfo = decodeJWT(apiKey);
87+
8088
// The user must be successfully logged in and have approved the scopes, so we
8189
// can complete the authorization request
8290
const { redirectTo } = await c.env.OAUTH_PROVIDER.completeAuthorization({
8391
request: oauthReqInfo,
8492
userId: apiKey,
8593
metadata: {
86-
label: "User label",
94+
label: jwtInfo?.payload?.sub || "User label",
8795
},
8896
scope: oauthReqInfo.scope,
8997
props: {
@@ -172,4 +180,19 @@ app.post("/customer-context", async (c) => {
172180
// then completing the authorization request with the OAUTH_PROVIDER
173181
app.post("/approve", handleApprove);
174182

183+
// Add /.well-known/oauth-authorization-server endpoint
184+
app.get("/.well-known/oauth-authorization-server", (c) => {
185+
// Extract base URL (protocol + host)
186+
const url = new URL(c.req.url);
187+
const base = url.origin;
188+
return c.json({
189+
issuer: base,
190+
authorization_endpoint: `${base}/authorize`,
191+
token_endpoint: `${base}/token`,
192+
registration_endpoint: `${base}/register`,
193+
scopes_supported: ["*"],
194+
code_challenge_methods_supported: ["S256"],
195+
});
196+
});
197+
175198
export default app;

0 commit comments

Comments
 (0)