-
Notifications
You must be signed in to change notification settings - Fork 419
Expand file tree
/
Copy pathauth_test.go
More file actions
190 lines (181 loc) · 4.75 KB
/
auth_test.go
File metadata and controls
190 lines (181 loc) · 4.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
// Copyright 2025 The Go MCP SDK Authors. All rights reserved.
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
package auth
import (
"context"
"errors"
"net/http"
"net/http/httptest"
"testing"
"time"
)
func TestVerify(t *testing.T) {
verifier := func(_ context.Context, token string, _ *http.Request) (*TokenInfo, error) {
switch token {
case "valid":
return &TokenInfo{Expiration: time.Now().Add(time.Hour)}, nil
case "invalid":
return nil, ErrInvalidToken
case "oauth":
return nil, ErrOAuth
case "noexp":
return &TokenInfo{}, nil
case "expired":
return &TokenInfo{Expiration: time.Now().Add(-time.Hour)}, nil
default:
return nil, errors.New("unknown")
}
}
for _, tt := range []struct {
name string
opts *RequireBearerTokenOptions
header string
wantMsg string
wantCode int
}{
{
"valid", nil, "Bearer valid",
"", 0,
},
{
"bad header", nil, "Barer valid",
"no bearer token", 401,
},
{
"invalid", nil, "bearer invalid",
"invalid token", 401,
},
{
"oauth error", nil, "Bearer oauth",
"oauth error", 400,
},
{
"no expiration", nil, "Bearer noexp",
"token missing expiration", 401,
},
{
"expired", nil, "Bearer expired",
"token expired", 401,
},
{
"missing scope", &RequireBearerTokenOptions{Scopes: []string{"s1"}}, "Bearer valid",
"insufficient scope", 403,
},
} {
t.Run(tt.name, func(t *testing.T) {
_, gotMsg, gotCode := verify(&http.Request{
Header: http.Header{"Authorization": {tt.header}},
}, verifier, tt.opts)
if gotMsg != tt.wantMsg || gotCode != tt.wantCode {
t.Errorf("got (%q, %d), want (%q, %d)", gotMsg, gotCode, tt.wantMsg, tt.wantCode)
}
})
}
}
func TestRequireBearerToken_ClaimsTable(t *testing.T) {
issuedAt := time.Unix(1730000000, 0).UTC()
notBefore := issuedAt.Add(-time.Minute)
verifier := func(_ context.Context, token string, _ *http.Request) (*TokenInfo, error) {
switch token {
case "claims":
return &TokenInfo{
Scopes: []string{"s1"},
Expiration: time.Now().Add(time.Hour),
Issuer: "https://issuer.example",
Subject: "user-123",
Audience: []string{"aud1", "aud2"},
NotBefore: notBefore,
IssuedAt: issuedAt,
JWTID: "jwt-id-abc",
}, nil
case "claims-zero":
return &TokenInfo{Expiration: time.Now().Add(time.Hour)}, nil
default:
return nil, ErrInvalidToken
}
}
for _, tt := range []struct {
name string
header string
checkFunc func(t *testing.T, ti *TokenInfo)
}{
{
name: "claims present",
header: "Bearer claims",
checkFunc: func(t *testing.T, ti *TokenInfo) {
if ti == nil {
t.Fatalf("TokenInfo missing in context")
}
if ti.Issuer != "https://issuer.example" {
t.Fatalf("iss got %q", ti.Issuer)
}
if ti.Subject != "user-123" {
t.Fatalf("sub got %q", ti.Subject)
}
if len(ti.Audience) != 2 || ti.Audience[0] != "aud1" || ti.Audience[1] != "aud2" {
t.Fatalf("aud got %v", ti.Audience)
}
if ti.NotBefore.IsZero() {
t.Fatalf("nbf is zero")
}
if ti.IssuedAt.IsZero() {
t.Fatalf("iat is zero")
}
if ti.JWTID != "jwt-id-abc" {
t.Fatalf("jti got %q", ti.JWTID)
}
if ti.Expiration.IsZero() {
t.Fatalf("exp is zero")
}
},
},
{
name: "claims zero values (except exp)",
header: "Bearer claims-zero",
checkFunc: func(t *testing.T, ti *TokenInfo) {
if ti == nil {
t.Fatalf("TokenInfo missing in context")
}
if ti.Issuer != "" {
t.Fatalf("iss expected empty, got %q", ti.Issuer)
}
if ti.Subject != "" {
t.Fatalf("sub expected empty, got %q", ti.Subject)
}
if len(ti.Audience) != 0 {
t.Fatalf("aud expected empty, got %v", ti.Audience)
}
if !ti.NotBefore.IsZero() {
t.Fatalf("nbf expected zero, got %v", ti.NotBefore)
}
if !ti.IssuedAt.IsZero() {
t.Fatalf("iat expected zero, got %v", ti.IssuedAt)
}
if ti.JWTID != "" {
t.Fatalf("jti expected empty, got %q", ti.JWTID)
}
if ti.Expiration.IsZero() {
t.Fatalf("exp should be set for middleware to pass")
}
},
},
} {
t.Run(tt.name, func(t *testing.T) {
req := httptest.NewRequest("GET", "/", nil)
req.Header.Set("Authorization", tt.header)
rw := httptest.NewRecorder()
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ti := TokenInfoFromContext(r.Context())
// Run the provided check against the token info in context
tt.checkFunc(t, ti)
w.WriteHeader(http.StatusOK)
})
wrapped := RequireBearerToken(verifier, nil)(handler)
wrapped.ServeHTTP(rw, req)
if rw.Result().StatusCode != http.StatusOK {
t.Fatalf("unexpected status: %d", rw.Result().StatusCode)
}
})
}
}