From 458391f5b4c7a2e6693b702a672b88e64d3eded9 Mon Sep 17 00:00:00 2001 From: Max Date: Sun, 20 Jul 2025 19:11:16 +0800 Subject: [PATCH] Refactor OAuth endpoint structure and add well-known handlers - Removed OAuth discovery and metadata endpoints from the attachOAuth function to streamline the code. - Introduced a new method to handle well-known endpoints, improving organization and clarity in the OAuth implementation. - Updated the Attach method to include the new well-known handlers, ensuring proper routing for OAuth-related metadata. --- openapi/oauth.go | 25 ------------------------- openapi/openapi.go | 3 +++ openapi/well-known.go | 28 ++++++++++++++++++++++++++++ 3 files changed, 31 insertions(+), 25 deletions(-) create mode 100644 openapi/well-known.go diff --git a/openapi/oauth.go b/openapi/oauth.go index a7fa9684..dc9fd240 100644 --- a/openapi/oauth.go +++ b/openapi/oauth.go @@ -56,20 +56,6 @@ func (openapi *OpenAPI) attachOAuth(base *gin.RouterGroup) { // Token Exchange - RFC 8693 oauth.POST("/token_exchange", openapi.oauthTokenExchange) - // OAuth Discovery and Metadata Endpoints - // IMPORTANT: These should be at the root level for proper MCP discovery - // If base is /v1, consider mounting these at the application root instead - wellKnown := base.Group("/.well-known") - - // OAuth Authorization Server Metadata - RFC 8414 (Required by MCP) - wellKnown.GET("/oauth-authorization-server", openapi.oauthServerMetadata) - - // OpenID Connect Discovery - OpenID Connect Discovery 1.0 - wellKnown.GET("/openid_configuration", openapi.oauthOpenIDConfiguration) - - // OAuth Protected Resource Metadata - RFC 9728 (Required by MCP) - wellKnown.GET("/oauth-protected-resource", openapi.oauthProtectedResourceMetadata) - } // OAuth Core Endpoints Implementation @@ -114,14 +100,3 @@ func (openapi *OpenAPI) oauthPushedAuthorizationRequest(c *gin.Context) {} // oauthTokenExchange handles token exchange - RFC 8693 func (openapi *OpenAPI) oauthTokenExchange(c *gin.Context) {} - -// OAuth Discovery and Metadata Endpoints Implementation - -// oauthServerMetadata returns authorization server metadata - RFC 8414 -func (openapi *OpenAPI) oauthServerMetadata(c *gin.Context) {} - -// oauthOpenIDConfiguration returns OpenID Connect configuration -func (openapi *OpenAPI) oauthOpenIDConfiguration(c *gin.Context) {} - -// oauthProtectedResourceMetadata returns protected resource metadata - RFC 9728 -func (openapi *OpenAPI) oauthProtectedResourceMetadata(c *gin.Context) {} diff --git a/openapi/openapi.go b/openapi/openapi.go index f64384c0..025abeef 100644 --- a/openapi/openapi.go +++ b/openapi/openapi.go @@ -64,6 +64,9 @@ func (openapi *OpenAPI) Attach(router *gin.Engine) { baseURL := openapi.Config.BaseURL group := router.Group(baseURL) + // Well-known handlers + openapi.attachWellKnown(router) + // OAuth handlers openapi.attachOAuth(group) diff --git a/openapi/well-known.go b/openapi/well-known.go new file mode 100644 index 00000000..8a6a26af --- /dev/null +++ b/openapi/well-known.go @@ -0,0 +1,28 @@ +package openapi + +import "github.com/gin-gonic/gin" + +// attachWellKnown attaches the well-known handlers to the router +func (openapi *OpenAPI) attachWellKnown(router *gin.Engine) { + + // OAuth Discovery and Metadata Endpoints + wellKnown := router.Group("/.well-known") + + // OAuth Authorization Server Metadata - RFC 8414 (Required by MCP) + wellKnown.GET("/oauth-authorization-server", openapi.oauthServerMetadata) + + // OpenID Connect Discovery - OpenID Connect Discovery 1.0 + wellKnown.GET("/openid_configuration", openapi.oauthOpenIDConfiguration) + + // OAuth Protected Resource Metadata - RFC 9728 (Required by MCP) + wellKnown.GET("/oauth-protected-resource", openapi.oauthProtectedResourceMetadata) +} + +// oauthServerMetadata returns authorization server metadata - RFC 8414 +func (openapi *OpenAPI) oauthServerMetadata(c *gin.Context) {} + +// oauthOpenIDConfiguration returns OpenID Connect configuration +func (openapi *OpenAPI) oauthOpenIDConfiguration(c *gin.Context) {} + +// oauthProtectedResourceMetadata returns protected resource metadata - RFC 9728 +func (openapi *OpenAPI) oauthProtectedResourceMetadata(c *gin.Context) {}