Conversation
There was a problem hiding this comment.
Pull request overview
This PR adds an initial (proof-of-concept) OAuth 2.0 / OIDC bearer-token authentication layer to the MCP HTTP server. When configured, the server validates JWTs against a JWKS endpoint, exposes /.well-known/oauth-protected-resource metadata per RFC 9728, and challenges unauthenticated callers with a WWW-Authenticate header pointing at that metadata URL. Configuration is taken from CLI flags, environment variables, or an optional .ontap-mcp.env file.
Changes:
- New
server/oauth.gowith a JWKS-backed JWT middleware (audience/issuer/exp/scope checks) plus a request-logging middleware. server/server.goreworksrunHTTPServerto use a realhttp.ServeMux, conditionally mounting the OAuth middleware + protected-resource metadata endpoint, and adds aloadEnvhelper that reads.ontap-mcp.env.- New CLI/
Optionsfields (oauth-server-url,jwks-url,resource-url) and the corresponding dependencies (MicahParks/keyfunc/v3,golang-jwt/jwt/v5).
Reviewed changes
Copilot reviewed 4 out of 5 changed files in this pull request and generated 14 comments.
Show a summary per file
| File | Description |
|---|---|
| server/oauth.go | New OAuth/JWT validation middleware, helpers, and request-logging middleware. |
| server/server.go | Wires OAuth config + protected-resource metadata into the HTTP server; adds .ontap-mcp.env loader and new Options fields. |
| cmd/cmds.go | Adds CLI/env flags for OAuth server URL, JWKS URL, and resource URL, and passes them into server options. |
| go.mod | Adds MicahParks/keyfunc/v3 and indirect deps (MicahParks/jwkset, golang-jwt/jwt/v5, golang.org/x/time). |
| go.sum | Checksum updates for the newly added modules. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+794
to
+814
| } | ||
| defer func() { | ||
| if err := file.Close(); err != nil { | ||
| slog.Warn("failed to close file", slog.Any("error", err)) | ||
| } | ||
| }() | ||
|
|
||
| scanner := bufio.NewScanner(file) | ||
| for scanner.Scan() { | ||
| line := strings.TrimSpace(scanner.Text()) | ||
| if line == "" || strings.HasPrefix(line, "#") { | ||
| continue | ||
| } | ||
| parts := strings.SplitN(line, "=", 2) | ||
| if len(parts) == 2 { | ||
| key := strings.TrimSpace(parts[0]) | ||
| value := strings.TrimSpace(parts[1]) | ||
| if os.Getenv(key) == "" { | ||
| if err = os.Setenv(key, value); err != nil { | ||
| // Log the error and proceed further | ||
| slog.Error("Error setting environment variable", slog.String("key", key), slog.String("value", value), slog.Any("err", err)) |
| func (c *OAuthConfig) sendUnauthorized(w http.ResponseWriter, _ *http.Request) { | ||
| metadataURL := c.ResourceURL + "/.well-known/oauth-protected-resource" | ||
| w.Header().Set("WWW-Authenticate", | ||
| fmt.Sprintf(`Bearer resource_metadata="%q", scope="openid profile email"`, metadataURL)) |
Comment on lines
+294
to
+305
| mux.HandleFunc("/.well-known/oauth-protected-resource", func(w http.ResponseWriter, _ *http.Request) { | ||
| w.WriteHeader(http.StatusOK) | ||
| return | ||
| } | ||
| metadata := oauthex.ProtectedResourceMetadata{ | ||
| Resource: oauthConfig.ResourceURL, | ||
| ScopesSupported: []string{"mcp:tools"}, | ||
| AuthorizationServers: []string{oauthConfig.AuthServerURL}, | ||
| } | ||
|
|
||
| handler.ServeHTTP(w, r) | ||
| }) | ||
| w.Header().Set("Content-Type", "application/json") | ||
| if err := json.NewEncoder(w).Encode(metadata); err != nil { | ||
| a.logger.Error("metadata encoding failed", slog.Any("error", err)) | ||
| } |
Comment on lines
+144
to
+149
| exp, ok := claims["exp"].(float64) | ||
| if !ok { | ||
| return false | ||
| } | ||
| // Allow 60 seconds of clock skew | ||
| return time.Now().Unix() < int64(exp)+60 |
Comment on lines
+293
to
+329
| if oAthExist { | ||
| mux.HandleFunc("/.well-known/oauth-protected-resource", func(w http.ResponseWriter, _ *http.Request) { | ||
| w.WriteHeader(http.StatusOK) | ||
| return | ||
| } | ||
| metadata := oauthex.ProtectedResourceMetadata{ | ||
| Resource: oauthConfig.ResourceURL, | ||
| ScopesSupported: []string{"mcp:tools"}, | ||
| AuthorizationServers: []string{oauthConfig.AuthServerURL}, | ||
| } | ||
|
|
||
| handler.ServeHTTP(w, r) | ||
| }) | ||
| w.Header().Set("Content-Type", "application/json") | ||
| if err := json.NewEncoder(w).Encode(metadata); err != nil { | ||
| a.logger.Error("metadata encoding failed", slog.Any("error", err)) | ||
| } | ||
| }) | ||
|
|
||
| // MCP endpoint (OAuth authorization required, with logging) | ||
| mux.Handle("/", LoggingMiddleware(oauthConfig.OAuthMiddleware(handler))) | ||
| } else { | ||
| mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { | ||
| // Skip MCP handler for health endpoint | ||
| if r.URL.Path == "/health" { | ||
| http.DefaultServeMux.ServeHTTP(w, r) | ||
| return | ||
| } | ||
|
|
||
| w.Header().Set("Access-Control-Allow-Origin", "*") | ||
| w.Header().Set("Access-Control-Allow-Methods", "GET, POST, OPTIONS") | ||
| w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Mcp-Protocol-Version, Mcp-Session-Id") | ||
|
|
||
| if r.Method == http.MethodOptions { | ||
| w.WriteHeader(http.StatusOK) | ||
| return | ||
| } | ||
|
|
||
| handler.ServeHTTP(w, r) | ||
| }) | ||
| } |
| } | ||
|
|
||
| func (c *OAuthConfig) sendUnauthorized(w http.ResponseWriter, _ *http.Request) { | ||
| metadataURL := c.ResourceURL + "/.well-known/oauth-protected-resource" |
Comment on lines
+247
to
+249
| a.logger.Info("MCP Server started with Oauth") | ||
| } else { | ||
| a.logger.Info("MCP Server started without any Oauth") |
| Port int | ||
| ReadOnly bool | ||
| Stateless bool | ||
| OauthServerURL string |
| // Validate expiration | ||
| // Note: jwt.Parse already validates exp by default, but we explicitly check here for clarity | ||
| if !c.validateExpiration(claims) { | ||
| slog.Error("token has been expired") |
Comment on lines
+792
to
+793
| // .ontap-mcp.env file doesn't exist, that's okay | ||
| slog.Warn(".ontap-mcp.env file not exist", slog.Any("error", err)) |
# Conflicts: # cmd/cmds.go # go.mod # go.sum # server/server.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.