Debian Codegraph @

debian/go/src/github.com/modelcontextprotocol/go-sdk/oauthex/auth_meta.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 // This file implements Authorization Server Metadata.
6 // See https://www.rfc-editor.org/rfc/rfc8414.html.
7
8 package oauthex
9
10 import (
11 "context"
12 "errors"
13 "fmt"
14 "net/http"
15 )
16
17 // AuthServerMeta represents the metadata for an OAuth 2.0 authorization server,
18 // as defined in [RFC 8414].
19 //
20 // Not supported:
21 // - signed metadata
22 //
23 // Note: URL fields in this struct are validated by validateAuthServerMetaURLs to
24 // prevent XSS attacks. If you add a new URL field, you must also add it to that
25 // function.
26 //
27 // [RFC 8414]: https://tools.ietf.org/html/rfc8414)
28 type AuthServerMeta struct {
29 // Issuer is the REQUIRED URL identifying the authorization server.
30 Issuer string `json:"issuer"`
31
32 // AuthorizationEndpoint is the REQUIRED URL of the server's OAuth 2.0 authorization endpoint.
33 AuthorizationEndpoint string `json:"authorization_endpoint"`
34
35 // TokenEndpoint is the REQUIRED URL of the server's OAuth 2.0 token endpoint.
36 TokenEndpoint string `json:"token_endpoint"`
37
38 // JWKSURI is the REQUIRED URL of the server's JSON Web Key Set [JWK] document.
39 JWKSURI string `json:"jwks_uri"`
40
41 // RegistrationEndpoint is the RECOMMENDED URL of the server's OAuth 2.0 Dynamic Client Registration endpoint.
42 RegistrationEndpoint string `json:"registration_endpoint,omitempty"`
43
44 // ScopesSupported is a RECOMMENDED JSON array of strings containing a list of the OAuth 2.0
45 // "scope" values that this server supports.
46 ScopesSupported []string `json:"scopes_supported,omitempty"`
47
48 // ResponseTypesSupported is a REQUIRED JSON array of strings containing a list of the OAuth 2.0
49 // "response_type" values that this server supports.
50 ResponseTypesSupported []string `json:"response_types_supported"`
51
52 // ResponseModesSupported is a RECOMMENDED JSON array of strings containing a list of the OAuth 2.0
53 // "response_mode" values that this server supports.
54 ResponseModesSupported []string `json:"response_modes_supported,omitempty"`
55
56 // GrantTypesSupported is a RECOMMENDED JSON array of strings containing a list of the OAuth 2.0
57 // grant type values that this server supports.
58 GrantTypesSupported []string `json:"grant_types_supported,omitempty"`
59
60 // TokenEndpointAuthMethodsSupported is a RECOMMENDED JSON array of strings containing a list of
61 // client authentication methods supported by this token endpoint.
62 TokenEndpointAuthMethodsSupported []string `json:"token_endpoint_auth_methods_supported,omitempty"`
63
64 // TokenEndpointAuthSigningAlgValuesSupported is a RECOMMENDED JSON array of strings containing
65 // a list of the JWS signing algorithms ("alg" values) supported by the token endpoint for
66 // the signature on the JWT used to authenticate the client.
67 TokenEndpointAuthSigningAlgValuesSupported []string `json:"token_endpoint_auth_signing_alg_values_supported,omitempty"`
68
69 // ServiceDocumentation is a RECOMMENDED URL of a page containing human-readable documentation
70 // for the service.
71 ServiceDocumentation string `json:"service_documentation,omitempty"`
72
73 // UILocalesSupported is a RECOMMENDED JSON array of strings representing supported
74 // BCP47 [RFC5646] language tag values for display in the user interface.
75 UILocalesSupported []string `json:"ui_locales_supported,omitempty"`
76
77 // OpPolicyURI is a RECOMMENDED URL that the server provides to the person registering
78 // the client to read about the server's operator policies.
79 OpPolicyURI string `json:"op_policy_uri,omitempty"`
80
81 // OpTOSURI is a RECOMMENDED URL that the server provides to the person registering the
82 // client to read about the server's terms of service.
83 OpTOSURI string `json:"op_tos_uri,omitempty"`
84
85 // RevocationEndpoint is a RECOMMENDED URL of the server's OAuth 2.0 revocation endpoint.
86 RevocationEndpoint string `json:"revocation_endpoint,omitempty"`
87
88 // RevocationEndpointAuthMethodsSupported is a RECOMMENDED JSON array of strings containing
89 // a list of client authentication methods supported by this revocation endpoint.
90 RevocationEndpointAuthMethodsSupported []string `json:"revocation_endpoint_auth_methods_supported,omitempty"`
91
92 // RevocationEndpointAuthSigningAlgValuesSupported is a RECOMMENDED JSON array of strings
93 // containing a list of the JWS signing algorithms ("alg" values) supported by the revocation
94 // endpoint for the signature on the JWT used to authenticate the client.
95 RevocationEndpointAuthSigningAlgValuesSupported []string `json:"revocation_endpoint_auth_signing_alg_values_supported,omitempty"`
96
97 // IntrospectionEndpoint is a RECOMMENDED URL of the server's OAuth 2.0 introspection endpoint.
98 IntrospectionEndpoint string `json:"introspection_endpoint,omitempty"`
99
100 // IntrospectionEndpointAuthMethodsSupported is a RECOMMENDED JSON array of strings containing
101 // a list of client authentication methods supported by this introspection endpoint.
102 IntrospectionEndpointAuthMethodsSupported []string `json:"introspection_endpoint_auth_methods_supported,omitempty"`
103
104 // IntrospectionEndpointAuthSigningAlgValuesSupported is a RECOMMENDED JSON array of strings
105 // containing a list of the JWS signing algorithms ("alg" values) supported by the introspection
106 // endpoint for the signature on the JWT used to authenticate the client.
107 IntrospectionEndpointAuthSigningAlgValuesSupported []string `json:"introspection_endpoint_auth_signing_alg_values_supported,omitempty"`
108
109 // CodeChallengeMethodsSupported is a RECOMMENDED JSON array of strings containing a list of
110 // PKCE code challenge methods supported by this authorization server.
111 CodeChallengeMethodsSupported []string `json:"code_challenge_methods_supported,omitempty"`
112
113 // ClientIDMetadataDocumentSupported is a boolean indicating whether the authorization server
114 // supports client ID metadata documents.
115 ClientIDMetadataDocumentSupported bool `json:"client_id_metadata_document_supported,omitempty"`
116 }
117
118 // GetAuthServerMeta issues a GET request to retrieve authorization server metadata
119 // from an OAuth authorization server with the given metadataURL.
120 //
121 // It follows [RFC 8414]:
122 // - The metadataURL must use HTTPS or be a local address.
123 // - The Issuer field is checked against metadataURL.Issuer.
124 //
125 // It also verifies that the authorization server supports PKCE and that the URLs
126 // in the metadata don't use dangerous schemes.
127 //
128 // It returns an error if the request fails with a non-4xx status code or the fetched
129 // metadata doesn't pass security validations.
130 // It returns nil if the request fails with a 4xx status code.
131 //
132 // [RFC 8414]: https://tools.ietf.org/html/rfc8414
133 func GetAuthServerMeta(ctx context.Context, metadataURL, issuer string, c *http.Client) (*AuthServerMeta, error) {
134 // Only allow HTTP for local addresses (testing or development purposes).
135 if err := checkHTTPSOrLoopback(metadataURL); err != nil {
136 return nil, fmt.Errorf("metadataURL: %v", err)
137 }
138 asm, err := getJSON[AuthServerMeta](ctx, c, metadataURL, 1<<20)
139 if err != nil {
140 var httpErr *httpStatusError
141 if errors.As(err, &httpErr) {
142 if 400 <= httpErr.StatusCode && httpErr.StatusCode < 500 {
143 return nil, nil
144 }
145 }
146 return nil, fmt.Errorf("%v", err) // Do not expose error types.
147 }
148 if asm.Issuer != issuer {
149 // Validate the Issuer field (see RFC 8414, section 3.3).
150 return nil, fmt.Errorf("metadata issuer %q does not match issuer URL %q", asm.Issuer, issuer)
151 }
152
153 if len(asm.CodeChallengeMethodsSupported) == 0 {
154 return nil, fmt.Errorf("authorization server at %s does not implement PKCE", issuer)
155 }
156
157 // Validate endpoint URLs to prevent XSS attacks (see #526).
158 if err := validateAuthServerMetaURLs(asm); err != nil {
159 return nil, err
160 }
161
162 return asm, nil
163 }
164
165 // validateAuthServerMetaURLs validates all URL fields in AuthServerMeta
166 // to ensure they don't use dangerous schemes that could enable XSS attacks.
167 // It also validates that URLs likely to be called by the client use
168 // HTTPS or are loopback addresses.
169 func validateAuthServerMetaURLs(asm *AuthServerMeta) error {
170 urls := []struct {
171 name string
172 value string
173 }{
174 {"authorization_endpoint", asm.AuthorizationEndpoint},
175 {"token_endpoint", asm.TokenEndpoint},
176 {"jwks_uri", asm.JWKSURI},
177 {"registration_endpoint", asm.RegistrationEndpoint},
178 {"service_documentation", asm.ServiceDocumentation},
179 {"op_policy_uri", asm.OpPolicyURI},
180 {"op_tos_uri", asm.OpTOSURI},
181 {"revocation_endpoint", asm.RevocationEndpoint},
182 {"introspection_endpoint", asm.IntrospectionEndpoint},
183 }
184
185 for _, u := range urls {
186 if err := checkURLScheme(u.value); err != nil {
187 return fmt.Errorf("%s: %w", u.name, err)
188 }
189 }
190
191 urls = []struct {
192 name string
193 value string
194 }{
195 {"authorization_endpoint", asm.AuthorizationEndpoint},
196 {"token_endpoint", asm.TokenEndpoint},
197 {"registration_endpoint", asm.RegistrationEndpoint},
198 {"introspection_endpoint", asm.IntrospectionEndpoint},
199 }
200
201 for _, u := range urls {
202 if err := checkHTTPSOrLoopback(u.value); err != nil {
203 return fmt.Errorf("%s: %w", u.name, err)
204 }
205 }
206
207 return nil
208 }