Skip to content

Commit be23dcf

Browse files
committed
feat: add handler for OAuth authorization server metadata requests
1 parent 191c45c commit be23dcf

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

exercises/99.finished/99.solution/src/auth.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,22 @@ export async function handleOAuthProtectedResourceRequest(request: Request) {
102102
scopes_supported: ['read', 'write'],
103103
})
104104
}
105+
106+
/**
107+
* Handles requests for OAuth authorization server metadata.
108+
* Fetches the metadata from the auth server and forwards it to the client.
109+
* This should only be used for backwards compatibility. Newer clients should
110+
* use `/.well-known/oauth-protected-resource/mcp` to discover the authorization
111+
* server and make this request directly to the authorization server instead.
112+
*/
113+
export async function handleOAuthAuthorizationServerRequest() {
114+
const metadataUrl = new URL(
115+
'/.well-known/oauth-authorization-server',
116+
EPIC_ME_AUTH_SERVER_URL,
117+
)
118+
119+
const response = await fetch(metadataUrl.toString())
120+
const data = await response.json()
121+
122+
return Response.json(data)
123+
}

exercises/99.finished/99.solution/src/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
type AuthInfo,
1111
getAuthInfo,
1212
handleInsufficientScope,
13+
handleOAuthAuthorizationServerRequest,
1314
handleOAuthProtectedResourceRequest,
1415
handleUnauthorized,
1516
validateScopes,
@@ -96,6 +97,11 @@ export default {
9697
return handleOAuthProtectedResourceRequest(request)
9798
}
9899

100+
// for backwards compatibility with old clients that think we're the authorization server
101+
if (url.pathname === '/.well-known/oauth-authorization-server') {
102+
return handleOAuthAuthorizationServerRequest()
103+
}
104+
99105
if (url.pathname === '/mcp') {
100106
const authInfo = await getAuthInfo(request)
101107
if (!authInfo) return handleUnauthorized(request)

0 commit comments

Comments
 (0)