Debian Codegraph @

debian/go/src/github.com/modelcontextprotocol/go-sdk/oauthex/resource_meta_test.go

raw symbols SCIP
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 oauthex
6
7 import (
8 "encoding/json"
9 "net/http"
10 "net/http/httptest"
11 "reflect"
12 "testing"
13 )
14
15 func TestSplitChallenges(t *testing.T) {
16 tests := []struct {
17 name string
18 input string
19 want []string
20 }{
21 {
22 name: "single challenge no params",
23 input: `Basic`,
24 want: []string{`Basic`},
25 },
26 {
27 name: "single challenge with params",
28 input: `Bearer realm="example.com", error="invalid_token"`,
29 want: []string{`Bearer realm="example.com", error="invalid_token"`},
30 },
31 {
32 name: "single challenge with comma in quoted string",
33 input: `Bearer realm="example, with comma"`,
34 want: []string{`Bearer realm="example, with comma"`},
35 },
36 {
37 name: "two challenges",
38 input: `Basic, Bearer realm="example"`,
39 want: []string{`Basic`, ` Bearer realm="example"`},
40 },
41 {
42 name: "multiple challenges complex",
43 input: `Newauth realm="apps", Basic, Bearer realm="example.com", error="invalid_token"`,
44 want: []string{`Newauth realm="apps"`, ` Basic`, ` Bearer realm="example.com", error="invalid_token"`},
45 },
46 {
47 name: "challenge with escaped quote",
48 input: `Bearer realm="example \"quoted\""`,
49 want: []string{`Bearer realm="example \"quoted\""`},
50 },
51 {
52 name: "empty input",
53 input: "",
54 want: []string{""},
55 },
56 }
57
58 for _, tt := range tests {
59 t.Run(tt.name, func(t *testing.T) {
60 got, err := splitChallenges(tt.input)
61 if err != nil {
62 t.Fatal(err)
63 }
64 if !reflect.DeepEqual(got, tt.want) {
65 t.Errorf("splitChallenges() = %v, want %v", got, tt.want)
66 }
67 })
68 }
69 }
70
71 func TestSplitChallengesError(t *testing.T) {
72 if _, err := splitChallenges(`"Bearer"`); err == nil {
73 t.Fatal("got nil, want error")
74 }
75 }
76
77 func TestParseSingleChallenge(t *testing.T) {
78 tests := []struct {
79 name string
80 input string
81 want Challenge
82 wantErr bool
83 }{
84 {
85 name: "scheme only",
86 input: "Basic",
87 want: Challenge{
88 Scheme: "basic",
89 },
90 wantErr: false,
91 },
92 {
93 name: "scheme with one quoted param",
94 input: `Bearer realm="example.com"`,
95 want: Challenge{
96 Scheme: "bearer",
97 Params: map[string]string{"realm": "example.com"},
98 },
99 wantErr: false,
100 },
101 {
102 name: "scheme with one unquoted param",
103 input: `Bearer realm=example.com`,
104 want: Challenge{
105 Scheme: "bearer",
106 Params: map[string]string{"realm": "example.com"},
107 },
108 wantErr: false,
109 },
110 {
111 name: "scheme with multiple params",
112 input: `Bearer realm="example", error="invalid_token", error_description="The token expired"`,
113 want: Challenge{
114 Scheme: "bearer",
115 Params: map[string]string{
116 "realm": "example",
117 "error": "invalid_token",
118 "error_description": "The token expired",
119 },
120 },
121 wantErr: false,
122 },
123 {
124 name: "scheme with multiple unquoted params",
125 input: `Bearer realm=example, error=invalid_token, error_description=The token expired`,
126 want: Challenge{
127 Scheme: "bearer",
128 Params: map[string]string{
129 "realm": "example",
130 "error": "invalid_token",
131 "error_description": "The token expired",
132 },
133 },
134 wantErr: false,
135 },
136 {
137 name: "case-insensitive scheme and keys",
138 input: `BEARER ReAlM="example"`,
139 want: Challenge{
140 Scheme: "bearer",
141 Params: map[string]string{"realm": "example"},
142 },
143 wantErr: false,
144 },
145 {
146 name: "param with escaped quote",
147 input: `Bearer realm="example \"foo\" bar"`,
148 want: Challenge{
149 Scheme: "bearer",
150 Params: map[string]string{"realm": `example "foo" bar`},
151 },
152 wantErr: false,
153 },
154 {
155 name: "param without quotes (token)",
156 input: "Bearer realm=example.com",
157 want: Challenge{
158 Scheme: "bearer",
159 Params: map[string]string{"realm": "example.com"},
160 },
161 wantErr: false,
162 },
163 {
164 name: "malformed param - no value",
165 input: "Bearer realm=",
166 wantErr: true,
167 },
168 {
169 name: "malformed param - unterminated quote",
170 input: `Bearer realm="example`,
171 wantErr: true,
172 },
173 {
174 name: "malformed param - missing comma",
175 input: `Bearer realm="a" error="b"`,
176 wantErr: true,
177 },
178 {
179 name: "malformed param - initial equal",
180 input: `Bearer ="a"`,
181 wantErr: true,
182 },
183 {
184 name: "empty input",
185 input: "",
186 wantErr: true,
187 },
188 }
189
190 for _, tt := range tests {
191 t.Run(tt.name, func(t *testing.T) {
192 got, err := parseSingleChallenge(tt.input)
193 if (err != nil) != tt.wantErr {
194 t.Errorf("parseSingleChallenge() error = %v, wantErr %v", err, tt.wantErr)
195 return
196 }
197 if !reflect.DeepEqual(got, tt.want) {
198 t.Errorf("parseSingleChallenge() = %v, want %v", got, tt.want)
199 }
200 })
201 }
202 }
203
204 func TestGetProtectedResourceMetadata(t *testing.T) {
205 ctx := t.Context()
206 t.Run("Success", func(t *testing.T) {
207 h := &fakeResourceHandler{}
208 server := httptest.NewTLSServer(h)
209 h.installHandlers(server.URL)
210 client := server.Client()
211 metadataURL := server.URL + "/.well-known/oauth-protected-resource"
212 prm, err := GetProtectedResourceMetadata(ctx, metadataURL, server.URL, client)
213 if err != nil {
214 t.Fatal(err)
215 }
216 if prm == nil {
217 t.Fatal("nil prm")
218 }
219 })
220 t.Run("RejectsIncorrectResource", func(t *testing.T) {
221 h := &fakeResourceHandler{resourceOverride: "https://attacker.com/evil"}
222 server := httptest.NewTLSServer(h)
223 h.installHandlers(server.URL)
224 client := server.Client()
225 metadataURL := server.URL + "/.well-known/oauth-protected-resource"
226 prm, err := GetProtectedResourceMetadata(ctx, metadataURL, server.URL, client)
227 if err == nil {
228 t.Fatal("Expected validation error for mismatched resource, got nil")
229 }
230 if prm != nil {
231 t.Fatal("Expected nil prm on validation failure")
232 }
233 })
234 }
235
236 type fakeResourceHandler struct {
237 http.ServeMux
238 resourceOverride string // If set, use this instead of correct resource (for testing validation)
239 }
240
241 func (h *fakeResourceHandler) installHandlers(serverURL string) {
242 path := "/.well-known/oauth-protected-resource"
243 h.Handle("GET "+path, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
244 w.Header().Set("Content-Type", "application/json")
245 // Per RFC 9728 section 3.3, the resource field should contain the actual resource identifier,
246 // which is the URL the client uses to access the resource (serverURL + "/resource" for WWW-Authenticate case).
247 // For the well-known URL test case, it's just the serverURL.
248 resource := serverURL
249 // Allow testing with custom resource values (e.g., impersonation attacks).
250 if h.resourceOverride != "" {
251 resource = h.resourceOverride
252 }
253 prm := &ProtectedResourceMetadata{Resource: resource}
254 if err := json.NewEncoder(w).Encode(prm); err != nil {
255 panic(err)
256 }
257 }))
258 }