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.
This commit is contained in:
Max 2025-07-20 19:11:16 +08:00
parent 6256e131b3
commit 458391f5b4
3 changed files with 31 additions and 25 deletions

View file

@ -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) {}

View file

@ -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)

28
openapi/well-known.go Normal file
View file

@ -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) {}