Skip to content

Commit e2cf95c

Browse files
authored
auth: add OAuth authenticating middleware for server (#261)
Add auth.RequireBearerToken and associated types. This piece of middleware authenticates clients using OAuth 2.0, as specified in the MCP spec. For #237. Usage: ``` st := mcp.NewStreamableServerTransport(...) http.Handle(path, auth.RequireBearerToken(verifier, nil)(st.ServeHTTP)) ```
1 parent 8186bf3 commit e2cf95c

File tree

2 files changed

+166
-0
lines changed

2 files changed

+166
-0
lines changed

auth/auth.go

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
// Copyright 2025 The Go MCP SDK Authors. All rights reserved.
2+
// Use of this source code is governed by an MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package auth
6+
7+
import (
8+
"context"
9+
"errors"
10+
"net/http"
11+
"slices"
12+
"strings"
13+
"time"
14+
)
15+
16+
type TokenInfo struct {
17+
Scopes []string
18+
Expiration time.Time
19+
}
20+
21+
type TokenVerifier func(ctx context.Context, token string) (*TokenInfo, error)
22+
23+
type RequireBearerTokenOptions struct {
24+
Scopes []string
25+
ResourceMetadataURL string
26+
}
27+
28+
var ErrInvalidToken = errors.New("invalid token")
29+
30+
type tokenInfoKey struct{}
31+
32+
// RequireBearerToken returns a piece of middleware that verifies a bearer token using the verifier.
33+
// If verification succeeds, the [TokenInfo] is added to the request's context and the request proceeds.
34+
// If verification fails, the request fails with a 401 Unauthenticated, and the WWW-Authenticate header
35+
// is populated to enable [protected resource metadata].
36+
//
37+
// [protected resource metadata]: https://datatracker.ietf.org/doc/rfc9728
38+
func RequireBearerToken(verifier TokenVerifier, opts *RequireBearerTokenOptions) func(http.Handler) http.Handler {
39+
// Based on typescript-sdk/src/server/auth/middleware/bearerAuth.ts.
40+
41+
return func(handler http.Handler) http.Handler {
42+
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
43+
tokenInfo, errmsg, code := verify(r.Context(), verifier, opts, r.Header.Get("Authorization"))
44+
if code != 0 {
45+
if code == http.StatusUnauthorized || code == http.StatusForbidden {
46+
if opts != nil && opts.ResourceMetadataURL != "" {
47+
w.Header().Add("WWW-Authenticate", "Bearer resource_metadata="+opts.ResourceMetadataURL)
48+
}
49+
}
50+
http.Error(w, errmsg, code)
51+
return
52+
}
53+
r = r.WithContext(context.WithValue(r.Context(), tokenInfoKey{}, tokenInfo))
54+
handler.ServeHTTP(w, r)
55+
})
56+
}
57+
}
58+
59+
func verify(ctx context.Context, verifier TokenVerifier, opts *RequireBearerTokenOptions, authHeader string) (_ *TokenInfo, errmsg string, code int) {
60+
// Extract bearer token.
61+
fields := strings.Fields(authHeader)
62+
if len(fields) != 2 || strings.ToLower(fields[0]) != "bearer" {
63+
return nil, "no bearer token", http.StatusUnauthorized
64+
}
65+
66+
// Verify the token and get information from it.
67+
tokenInfo, err := verifier(ctx, fields[1])
68+
if err != nil {
69+
if errors.Is(err, ErrInvalidToken) {
70+
return nil, err.Error(), http.StatusUnauthorized
71+
}
72+
// TODO: the TS SDK distinguishes another error, OAuthError, and returns a 400.
73+
// Investigate how that works.
74+
// See typescript-sdk/src/server/auth/middleware/bearerAuth.ts.
75+
return nil, err.Error(), http.StatusInternalServerError
76+
}
77+
78+
// Check scopes.
79+
if opts != nil {
80+
// Note: quadratic, but N is small.
81+
for _, s := range opts.Scopes {
82+
if !slices.Contains(tokenInfo.Scopes, s) {
83+
return nil, "insufficient scope", http.StatusForbidden
84+
}
85+
}
86+
}
87+
88+
// Check expiration.
89+
if tokenInfo.Expiration.IsZero() {
90+
return nil, "token missing expiration", http.StatusUnauthorized
91+
}
92+
if tokenInfo.Expiration.Before(time.Now()) {
93+
return nil, "token expired", http.StatusUnauthorized
94+
}
95+
return tokenInfo, "", 0
96+
}

auth/auth_test.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// Copyright 2025 The Go MCP SDK Authors. All rights reserved.
2+
// Use of this source code is governed by an MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package auth
6+
7+
import (
8+
"context"
9+
"errors"
10+
"testing"
11+
"time"
12+
)
13+
14+
func TestVerify(t *testing.T) {
15+
ctx := context.Background()
16+
verifier := func(_ context.Context, token string) (*TokenInfo, error) {
17+
switch token {
18+
case "valid":
19+
return &TokenInfo{Expiration: time.Now().Add(time.Hour)}, nil
20+
case "invalid":
21+
return nil, ErrInvalidToken
22+
case "noexp":
23+
return &TokenInfo{}, nil
24+
case "expired":
25+
return &TokenInfo{Expiration: time.Now().Add(-time.Hour)}, nil
26+
default:
27+
return nil, errors.New("unknown")
28+
}
29+
}
30+
31+
for _, tt := range []struct {
32+
name string
33+
opts *RequireBearerTokenOptions
34+
header string
35+
wantMsg string
36+
wantCode int
37+
}{
38+
{
39+
"valid", nil, "Bearer valid",
40+
"", 0,
41+
},
42+
{
43+
"bad header", nil, "Barer valid",
44+
"no bearer token", 401,
45+
},
46+
{
47+
"invalid", nil, "bearer invalid",
48+
"invalid token", 401,
49+
},
50+
{
51+
"no expiration", nil, "Bearer noexp",
52+
"token missing expiration", 401,
53+
},
54+
{
55+
"expired", nil, "Bearer expired",
56+
"token expired", 401,
57+
},
58+
{
59+
"missing scope", &RequireBearerTokenOptions{Scopes: []string{"s1"}}, "Bearer valid",
60+
"insufficient scope", 403,
61+
},
62+
} {
63+
t.Run(tt.name, func(t *testing.T) {
64+
_, gotMsg, gotCode := verify(ctx, verifier, tt.opts, tt.header)
65+
if gotMsg != tt.wantMsg || gotCode != tt.wantCode {
66+
t.Errorf("got (%q, %d), want (%q, %d)", gotMsg, gotCode, tt.wantMsg, tt.wantCode)
67+
}
68+
})
69+
}
70+
}

0 commit comments

Comments
 (0)