Debian Codegraph @

debian/go/src/github.com/modelcontextprotocol/go-sdk/oauthex/auth_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 "context"
9 "encoding/json"
10 "net/http"
11 "net/http/httptest"
12 "net/url"
13 "os"
14 "path/filepath"
15 "strings"
16 "testing"
17 )
18
19 func TestAuthMetaParse(t *testing.T) {
20 // Verify that we parse Google's auth server metadata.
21 data, err := os.ReadFile(filepath.FromSlash("testdata/google-auth-meta.json"))
22 if err != nil {
23 t.Fatal(err)
24 }
25 var a AuthServerMeta
26 if err := json.Unmarshal(data, &a); err != nil {
27 t.Fatal(err)
28 }
29 // Spot check.
30 if g, w := a.Issuer, "https://accounts.google.com"; g != w {
31 t.Errorf("got %q, want %q", g, w)
32 }
33 }
34
35 func TestGetAuthServerMetaPKCESupport(t *testing.T) {
36 ctx := context.Background()
37 tests := []struct {
38 name string
39 hasPKCESupport bool
40 wantError string
41 }{
42 {
43 name: "server_with_pkce_support",
44 hasPKCESupport: true,
45 },
46 {
47 name: "server_without_pkce_support",
48 hasPKCESupport: false,
49 wantError: "does not implement PKCE",
50 },
51 }
52
53 for _, tt := range tests {
54 t.Run(tt.name, func(t *testing.T) {
55 // Start a fake OAuth 2.1 auth server
56 wrapper := http.NewServeMux()
57 wrapper.HandleFunc("/.well-known/oauth-authorization-server", func(w http.ResponseWriter, r *http.Request) {
58 u, _ := url.Parse("https://" + r.Host)
59 issuer := "https://localhost:" + u.Port()
60 metadata := AuthServerMeta{
61 Issuer: issuer,
62 AuthorizationEndpoint: issuer + "/authorize",
63 TokenEndpoint: issuer + "/token",
64 RegistrationEndpoint: issuer + "/register",
65 JWKSURI: issuer + "/.well-known/jwks.json",
66 ScopesSupported: []string{"openid", "profile", "email"},
67 ResponseTypesSupported: []string{"code"},
68 GrantTypesSupported: []string{"authorization_code"},
69 TokenEndpointAuthMethodsSupported: []string{"none"},
70 }
71
72 // Add PKCE support based on test case
73 if tt.hasPKCESupport {
74 metadata.CodeChallengeMethodsSupported = []string{"S256"}
75 }
76 // If hasPKCESupport is false, CodeChallengeMethodsSupported remains empty
77
78 w.Header().Set("Content-Type", "application/json")
79 json.NewEncoder(w).Encode(metadata)
80 })
81 ts := httptest.NewTLSServer(wrapper)
82 defer ts.Close()
83
84 // The fake server sets issuer to https://localhost:<port>, so compute that issuer.
85 u, _ := url.Parse(ts.URL)
86 issuer := "https://localhost:" + u.Port()
87 metadataURL := issuer + "/.well-known/oauth-authorization-server"
88
89 // The fake server presents a cert for example.com; set ServerName accordingly.
90 httpClient := ts.Client()
91 if tr, ok := httpClient.Transport.(*http.Transport); ok {
92 clone := tr.Clone()
93 clone.TLSClientConfig.ServerName = "example.com"
94 httpClient.Transport = clone
95 }
96
97 meta, err := GetAuthServerMeta(ctx, metadataURL, issuer, httpClient)
98 if tt.wantError != "" {
99 if err == nil {
100 t.Fatal("wanted error but got none")
101 }
102 if !strings.Contains(err.Error(), tt.wantError) {
103 t.Errorf("wanted error to contain %q, but got: %v", tt.wantError, err)
104 }
105 } else {
106 if err != nil {
107 t.Fatalf("unwanted error: %v", err)
108 }
109 if meta == nil {
110 t.Fatal("wanted metadata but got nil")
111 }
112 // Verify PKCE support is present
113 if len(meta.CodeChallengeMethodsSupported) == 0 {
114 t.Error("wanted PKCE support but CodeChallengeMethodsSupported is empty")
115 }
116 }
117 })
118 }
119 }