Build & Publish (dsh-academy) / End-to-end inside dsh (latest, advisory) (push) Skipped
CLI / test (macos-latest / go 1.26) (push) Canceled after 0s
CLI / test (ubuntu-latest / go 1.26) (push) Canceled after 0s
CLI / test (windows-latest / go 1.26) (push) Canceled after 0s
Build and Push Docker Image / build-ui (push) Canceled after 0s
Build and Push Docker Image / build-docreader (push) Canceled after 0s
Build and Push Docker Image / build-sandbox (push) Canceled after 0s
Build and Push Docker Image / build-app (amd64, linux/amd64, ubuntu-latest) (push) Canceled after 0s
Build and Push Docker Image / build-app (arm64, linux/arm64, ubuntu-24.04-arm) (push) Canceled after 0s
Build and Push Docker Image / merge (push) Canceled after 0s
Frontend / Test, type-check, and build (push) Canceled after 0s
Frontend / Docker multi-stage image (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.10) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.11) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.12) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.13) (push) Canceled after 0s
Build & Publish (mcp-server) / Build sdist + wheel (push) Canceled after 0s
Build & Publish (mcp-server) / Publish to PyPI (push) Canceled after 0s
Anydoc / anydoc parser engine (push) Canceled after 0s
App / Format, vet, test, and build (push) Canceled after 0s
DocReader / Python tests and Go client tests (push) Canceled after 0s
Build & Publish (dsh-academy) / Typecheck, test, pack (push) Canceled after 0s
Build & Publish (dsh-academy) / End-to-end inside dsh (pinned) (push) Canceled after 0s
Build & Publish (dsh-academy) / Publish to npm (push) Canceled after 0s
Go Lint Cache / warm lint cache (.) (push) Canceled after 0s
Go Lint Cache / warm lint cache (cli) (push) Canceled after 0s
Go Lint Cache / warm lint cache (client) (push) Canceled after 0s
Helm / chart (push) Canceled after 0s
- 全量替换代码/配置/文档中的项目名(保留上游 GitHub 链接、weixin 文档域名、Docker Hub 镜像名) - 重命名 32 个含项目名的文件/目录;Go module 路径改为 academy(含 client/cli 子模块) - docreader proto 描述符采用等长替换,保持序列化字节布局有效 - Dockerfile.app 构建网络修复: browserskill 离线产物通道、PyPI/apt 国内镜像 - 运行验证: go build ✓ docker compose up ✓ 健康检查 ✓
97 lines
3.4 KiB
Go
97 lines
3.4 KiB
Go
package middleware
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"net/http"
|
|
"strings"
|
|
|
|
"academy/internal/application/service"
|
|
"academy/internal/logger"
|
|
"academy/internal/types"
|
|
"academy/internal/types/interfaces"
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
// MCPEndpointAuth authenticates calls on the workspace MCP server surface
|
|
// (/mcp/:endpoint_id). The client presents the endpoint's bearer token; on
|
|
// success the request carries the endpoint's workspace, a synthetic machine
|
|
// user, an MCP-endpoint principal and an API-key style scope derived from the
|
|
// endpoint's tool allowlist and knowledge-base scope, so every downstream
|
|
// service applies the same checks it applies to a scoped API key.
|
|
func MCPEndpointAuth(svc interfaces.MCPEndpointService, tenantSvc interfaces.TenantService) gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
endpointID := strings.TrimSpace(c.Param("endpoint_id"))
|
|
token := extractMCPEndpointToken(c)
|
|
if endpointID == "" || token == "" {
|
|
c.Header("WWW-Authenticate", `Bearer realm="academy-mcp"`)
|
|
c.JSON(http.StatusUnauthorized, gin.H{"error": "unauthorized: missing bearer token"})
|
|
c.Abort()
|
|
return
|
|
}
|
|
ep, err := svc.Authenticate(c.Request.Context(), endpointID, token)
|
|
if err != nil {
|
|
switch {
|
|
case errors.Is(err, service.ErrMCPEndpointDisabled):
|
|
c.JSON(http.StatusForbidden, gin.H{"error": "mcp endpoint disabled"})
|
|
case errors.Is(err, service.ErrMCPEndpointTokenInvalid), errors.Is(err, service.ErrMCPEndpointNotFound):
|
|
c.Header("WWW-Authenticate", `Bearer realm="academy-mcp"`)
|
|
c.JSON(http.StatusUnauthorized, gin.H{"error": "unauthorized: invalid token"})
|
|
default:
|
|
logger.Warnf(c.Request.Context(), "[mcp-endpoint] auth lookup failed: %v", err)
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "authentication unavailable"})
|
|
}
|
|
c.Abort()
|
|
return
|
|
}
|
|
tenant, err := tenantSvc.GetTenantByID(c.Request.Context(), ep.TenantID)
|
|
if err != nil || tenant == nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "workspace unavailable"})
|
|
c.Abort()
|
|
return
|
|
}
|
|
if strings.EqualFold(tenant.Status, "inactive") || strings.EqualFold(tenant.Status, "disabled") {
|
|
c.JSON(http.StatusForbidden, gin.H{"error": "workspace inactive"})
|
|
c.Abort()
|
|
return
|
|
}
|
|
|
|
scope := types.MCPEndpointScope(ep)
|
|
user := &types.User{
|
|
ID: fmt.Sprintf("mcp-%s", ep.ID),
|
|
Username: fmt.Sprintf("mcp-%s", ep.ID),
|
|
Email: fmt.Sprintf("mcp-%s@mcp.local", ep.ID),
|
|
TenantID: ep.TenantID,
|
|
IsActive: true,
|
|
}
|
|
applyAuthSession(c, authSession{
|
|
User: user,
|
|
Principal: types.MCPEndpointPrincipal(ep.TenantID, ep.ID),
|
|
TenantID: ep.TenantID,
|
|
Tenant: tenant,
|
|
Role: types.TenantRoleViewer,
|
|
APIKeyScope: &scope,
|
|
Extra: map[types.ContextKey]any{types.MCPEndpointContextKey: ep},
|
|
AuthSource: types.AuthSourceMCPEndpoint,
|
|
})
|
|
c.Next()
|
|
}
|
|
}
|
|
|
|
// extractMCPEndpointToken reads "Authorization: Bearer mcp_..." only. Query
|
|
// string tokens are rejected so they never land in access logs.
|
|
func extractMCPEndpointToken(c *gin.Context) string {
|
|
auth := strings.TrimSpace(c.GetHeader("Authorization"))
|
|
if len(auth) > 7 && strings.EqualFold(auth[:7], "Bearer ") {
|
|
return strings.TrimSpace(auth[7:])
|
|
}
|
|
return ""
|
|
}
|
|
|
|
// MCPEndpointFromContext returns the endpoint authenticated for this request.
|
|
func MCPEndpointFromContext(ctx context.Context) (*types.MCPEndpoint, bool) {
|
|
ep, ok := ctx.Value(types.MCPEndpointContextKey).(*types.MCPEndpoint)
|
|
return ep, ok && ep != nil
|
|
}
|