feat(mcp): add OAuth2 authorization for MCP services
Support connecting to MCP servers that require OAuth2, with a Cursor-like zero-config flow: the user provides only a URL and the system performs metadata discovery, dynamic client registration (DCR), and the authorization code flow with PKCE on first connect. - Persist per-user access/refresh tokens and per-service DCR client credentials, encrypted at rest (AES-256-GCM) - Store the PKCE verifier/state during the flow via Redis with an in-memory fallback for Lite mode - Cache MCP clients per (service, user) so tokens stay user-isolated - Add endpoints for authorize-url, callback, status, and token revoke; the callback is allowlisted for unauthenticated access - Surface a structured "authorization required" hint when a tool call hits an unauthorized MCP service - Frontend: add an auth-type selector with OAuth config, inline authorization status/actions, and render the connection test result inline in the editor drawer instead of a stacked centered dialog
This commit is contained in:
@@ -67,6 +67,7 @@ type RouterParams struct {
|
||||
SystemHandler *handler.SystemHandler
|
||||
MCPServiceHandler *handler.MCPServiceHandler
|
||||
MCPCredentialsHandler *handler.MCPCredentialsHandler
|
||||
MCPOAuthHandler *handler.MCPOAuthHandler
|
||||
WebSearchHandler *handler.WebSearchHandler
|
||||
WebSearchProviderHandler *handler.WebSearchProviderHandler
|
||||
WebSearchCredentialsHandler *handler.WebSearchProviderCredentialsHandler
|
||||
@@ -216,7 +217,7 @@ func NewRouter(params RouterParams) *gin.Engine {
|
||||
RegisterInitializationRoutes(v1, params.InitializationHandler, rbacGuards)
|
||||
RegisterSystemRoutes(v1, params.SystemHandler, rbacGuards)
|
||||
RegisterSystemAdminRoutes(v1, params.SystemHandler, params.AuditLogHandler, rbacGuards)
|
||||
RegisterMCPServiceRoutes(v1, params.MCPServiceHandler, params.MCPCredentialsHandler, rbacGuards)
|
||||
RegisterMCPServiceRoutes(v1, params.MCPServiceHandler, params.MCPCredentialsHandler, params.MCPOAuthHandler, rbacGuards)
|
||||
RegisterWebSearchRoutes(v1, params.WebSearchHandler, rbacGuards)
|
||||
RegisterWebSearchProviderRoutes(v1, params.WebSearchProviderHandler, params.WebSearchCredentialsHandler, rbacGuards)
|
||||
RegisterVectorStoreRoutes(v1, params.VectorStoreHandler, rbacGuards)
|
||||
@@ -833,8 +834,15 @@ func RegisterMCPServiceRoutes(
|
||||
r *gin.RouterGroup,
|
||||
handler *handler.MCPServiceHandler,
|
||||
credHandler *handler.MCPCredentialsHandler,
|
||||
oauthHandler *handler.MCPOAuthHandler,
|
||||
g *rbacGuards,
|
||||
) {
|
||||
// MCP OAuth provider redirect. Registered OUTSIDE the /mcp-services group
|
||||
// to avoid a static-vs-":id" route conflict, and left unauthenticated
|
||||
// (allow-listed in middleware/auth.go) because the third-party browser
|
||||
// redirect carries no WeKnora bearer — the single-use state authenticates.
|
||||
r.GET("/mcp-oauth/callback", oauthHandler.Callback)
|
||||
|
||||
mcpServices := r.Group("/mcp-services")
|
||||
{
|
||||
// Create MCP service — Admin+
|
||||
@@ -860,6 +868,12 @@ func RegisterMCPServiceRoutes(
|
||||
// MCP tool human approval (issue #1173) — Viewer+ to read, Admin+ to set policy
|
||||
mcpServices.GET("/:id/tool-approvals", g.Viewer(), handler.ListMCPToolApprovals)
|
||||
mcpServices.PUT("/:id/tool-approvals/:tool_name", g.Admin(), handler.SetMCPToolApproval)
|
||||
// Per-user OAuth authorization flow. Viewer+ may authorize/inspect/
|
||||
// revoke their own token; the callback is the separate public route
|
||||
// registered above.
|
||||
mcpServices.POST("/:id/oauth/authorize-url", g.Viewer(), oauthHandler.AuthorizeURL)
|
||||
mcpServices.GET("/:id/oauth/status", g.Viewer(), oauthHandler.Status)
|
||||
mcpServices.DELETE("/:id/oauth/token", g.Viewer(), oauthHandler.Revoke)
|
||||
}
|
||||
|
||||
agentTool := r.Group("/agent")
|
||||
|
||||
Reference in New Issue
Block a user