-
Notifications
You must be signed in to change notification settings - Fork 436
Expand file tree
/
Copy pathauthorization_code.go
More file actions
336 lines (299 loc) · 11.6 KB
/
authorization_code.go
File metadata and controls
336 lines (299 loc) · 11.6 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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
// Copyright 2026 The Go MCP SDK Authors. All rights reserved.
// Use of this source code is governed by the license
// that can be found in the LICENSE file.
//go:build mcp_go_client_oauth
package auth
import (
"context"
"crypto/rand"
"errors"
"fmt"
"log"
"net/http"
"net/url"
"slices"
"github.com/modelcontextprotocol/go-sdk/oauthex"
"golang.org/x/oauth2"
)
// ErrRedirected is returned when the user was redirected to the authorization server.
var ErrRedirected = errors.New("redirected")
// ClientIDMetadataDocumentConfig is used to configure the Client ID Metadata Document
// based client registration per
// https://modelcontextprotocol.io/specification/2025-11-25/basic/authorization#client-id-metadata-documents.
// See https://client.dev/ for more information.
type ClientIDMetadataDocumentConfig struct {
// URL is the client identifier URL as per
// https://datatracker.ietf.org/doc/html/draft-ietf-oauth-client-id-metadata-document-00#section-3.
URL string
}
// PreregisteredClientConfig is used to configure a pre-registered client per
// https://modelcontextprotocol.io/specification/2025-11-25/basic/authorization#preregistration.
type PreregisteredClientConfig struct {
ClientID string
ClientSecret string
AuthStyle oauth2.AuthStyle
}
// DynamicClientRegistrationConfig is used to configure dynamic client registration per
// https://modelcontextprotocol.io/specification/2025-11-25/basic/authorization#dynamic-client-registration.
type DynamicClientRegistrationConfig struct {
// Metadata to be used in dynamic client registration request as per
// https://datatracker.ietf.org/doc/html/rfc7591#section-2.
Metadata *oauthex.ClientRegistrationMetadata
}
type registrationType int
const (
registrationTypeClientIDMetadataDocument registrationType = iota
registrationTypePreregistered
registrationTypeDynamic
)
type resolvedClientConfig struct {
registrationType registrationType
clientID string
clientSecret string
authStyle oauth2.AuthStyle
}
type AuthorizationCodeOAuthHandler struct {
// Client registration configuration.
// It is attempted in the following order:
// 1. Client ID Metadata Document
// 2. Preregistration
// 3. Dynamic Client Registration
ClientIDMetadataDocumentConfig *ClientIDMetadataDocumentConfig
PreregisteredClientConfig *PreregisteredClientConfig
DynamicClientRegistrationConfig *DynamicClientRegistrationConfig
// RedirectURL is the URL to redirect to after authorization.
// If Dynamic Client Registration is used, the RedirectURL must be consistent
// with [DynamicClientRegistrationConfig.Metadata.RedirectURIs].
RedirectURL string
// AuthorizationURLHandler is called to handle the authorization URL.
// It is responsible for opening the URL in a browser.
// It should return once the redirect has been issued.
// The redirect callback should be handled by the caller and the authorization code
// should be set by calling [SetAuthorizationCode] before retrying the request.
AuthorizationURLHandler func(ctx context.Context, authorizationURL string) error
// StateProvider is an optional function to generate a state string for authorization
// requests. If not provided, a random string will be generated.
// The state should be validated on the redirect callback.
StateProvider func() string
// TokenStore is an optional object that allows persistent storage of tokens.
TokenStore TokenStore
// resolvedClientConfig used during the authorization flow.
resolvedClientConfig *resolvedClientConfig
// tokenSource is the token source to use for authorization.
// It can be prepopulated by calling [SetTokenSource].
tokenSource oauth2.TokenSource
// codeVerifier is the PKCE code verifier.
codeVerifier string
// authorizationCode is the authorization code obtained from the authorization server.
authorizationCode string
// state is the state string used in the authorization request.
state string
}
func (h *AuthorizationCodeOAuthHandler) TokenSource(ctx context.Context) (oauth2.TokenSource, error) {
return h.tokenSource, nil
}
// TODO: extract some logic into helper functions.
// TODO: validate required args
func (h *AuthorizationCodeOAuthHandler) Authorize(ctx context.Context, req *http.Request, resp *http.Response) error {
defer resp.Body.Close()
log.Printf("Authorize: %s %s", req.Method, req.URL)
if h.resolvedClientConfig == nil && h.authorizationCode != "" {
return fmt.Errorf("exchanging authorization code with unregistered client is not allowed")
}
resourceURL := req.URL.String()
challenges, err := oauthex.ParseWWWAuthenticate(resp.Header[http.CanonicalHeaderKey("WWW-Authenticate")])
if err != nil {
return fmt.Errorf("failed to parse WWW-Authenticate header: %v", err)
}
log.Printf("WWW-Authenticate header: %v", challenges)
var prm *oauthex.ProtectedResourceMetadata
for _, url := range oauthex.ProtectedResourceMetadataURLs(oauthex.ResourceMetadataURL(challenges), resourceURL) {
var err error
log.Printf("Getting protected resource metadata from %q", url)
prm, err = oauthex.GetProtectedResourceMetadata(ctx, url, http.DefaultClient)
if err == nil {
break
}
log.Printf("Failed to get protected resource metadata from %q: %v", url, err)
}
var authServerURL string
if prm != nil && len(prm.AuthorizationServers) > 0 {
// Use the first authorization server, similarly to other SDKs.
authServerURL = prm.AuthorizationServers[0]
} else {
// Fallback to 2025-03-26 spec: MCP server base URL acts as Authorization Server.
authURL, err := url.Parse(resourceURL)
if err != nil {
return fmt.Errorf("failed to parse resource URL: %v", err)
}
authURL.Path = ""
authServerURL = authURL.String()
}
log.Printf("Authorization server URL: %s", authServerURL)
asm, err := oauthex.GetAuthServerMeta(ctx, authServerURL, http.DefaultClient)
if err != nil {
return fmt.Errorf("failed to get authorization server metadata: %w", err)
}
log.Print("Authorization server medatada fetched")
if err := h.handleRegistration(ctx, authServerURL, asm); err != nil {
return err
}
scopes := oauthex.Scopes(challenges)
if len(scopes) == 0 && prm != nil && len(prm.ScopesSupported) > 0 {
scopes = prm.ScopesSupported
}
var authorizationEndpoint, tokenEndpoint string
if asm != nil {
authorizationEndpoint = asm.AuthorizationEndpoint
tokenEndpoint = asm.TokenEndpoint
} else {
// Fallback to 2025-03-26 spec: predefined endpoints if not provided by AS.
authorizationEndpoint = authServerURL + "/authorize"
tokenEndpoint = authServerURL + "/token"
}
cfg := &oauth2.Config{
ClientID: h.resolvedClientConfig.clientID,
ClientSecret: h.resolvedClientConfig.clientSecret,
Endpoint: oauth2.Endpoint{
AuthURL: authorizationEndpoint,
TokenURL: tokenEndpoint,
// TODO: validate if the auth style is supported by the AS.
AuthStyle: h.resolvedClientConfig.authStyle,
},
RedirectURL: h.RedirectURL,
Scopes: scopes,
}
if h.authorizationCode != "" {
log.Print("Authorization code is available, exchanging for token")
opts := []oauth2.AuthCodeOption{
oauth2.VerifierOption(h.codeVerifier),
oauth2.SetAuthURLParam("resource", req.URL.String()),
}
token, err := cfg.Exchange(ctx, h.authorizationCode, opts...)
defer func() {
// Authorization code has been consumed, clear it.
h.authorizationCode = ""
}()
if err != nil {
return fmt.Errorf("token exchange failed: %w", err)
}
ts := cfg.TokenSource(ctx, token)
if h.TokenStore != nil {
// Persist the returned tokens to the store if requested.
ts = NewPersistentTokenSource(ctx, ts, h.TokenStore)
}
h.tokenSource = ts
return nil
}
h.codeVerifier = oauth2.GenerateVerifier()
h.state = rand.Text()
if h.StateProvider != nil {
h.state = h.StateProvider()
}
authURL := cfg.AuthCodeURL(h.state,
oauth2.S256ChallengeOption(h.codeVerifier),
oauth2.SetAuthURLParam("resource", req.URL.String()),
)
log.Print("No authorization code available, opening authorization URL")
if h.AuthorizationURLHandler != nil {
if err := h.AuthorizationURLHandler(ctx, authURL); err != nil {
return fmt.Errorf("authorization URL handler failed: %w", err)
}
}
return ErrRedirected
}
func (h *AuthorizationCodeOAuthHandler) SetTokenSource(ts oauth2.TokenSource) {
h.tokenSource = ts
}
func (h *AuthorizationCodeOAuthHandler) FinalizeAuthorization(code, state string) error {
defer func() {
// State has been used for validation, clear it.
h.state = ""
}()
if state != h.state {
return fmt.Errorf("state mismatch: expected %q, got %q", h.state, state)
}
h.authorizationCode = code
return nil
}
func (h *AuthorizationCodeOAuthHandler) handleRegistration(ctx context.Context, authServerURL string, asm *oauthex.AuthServerMeta) error {
// 1. Attempt to use Client ID Metadata Document (SEP-991).
cimdCfg := h.ClientIDMetadataDocumentConfig
if cimdCfg != nil {
supportsCIMD := asm != nil && asm.ClientIDMetadataDocumentSupported
if supportsCIMD {
if !isNonRootHTTPSURL(cimdCfg.URL) {
return fmt.Errorf("client ID metadata document URL is not a non-root HTTPS URL")
}
h.resolvedClientConfig = &resolvedClientConfig{
registrationType: registrationTypeClientIDMetadataDocument,
clientID: cimdCfg.URL,
}
return nil
}
}
// 2. Attempt to use pre-registered client ID.
pCfg := h.PreregisteredClientConfig
if pCfg != nil {
if pCfg.ClientID == "" || pCfg.ClientSecret == "" {
return fmt.Errorf("pre-registered client ID or secret is empty")
}
h.resolvedClientConfig = &resolvedClientConfig{
registrationType: registrationTypePreregistered,
clientID: pCfg.ClientID,
clientSecret: pCfg.ClientSecret,
authStyle: pCfg.AuthStyle,
}
return nil
}
// 3. Attempt to use dynamic client registration.
dcrCfg := h.DynamicClientRegistrationConfig
if dcrCfg != nil {
if !slices.Contains(dcrCfg.Metadata.RedirectURIs, h.RedirectURL) {
return fmt.Errorf("redirect URI %q is not in the list of allowed redirect URIs for dynamic client registration", h.RedirectURL)
}
var registrationEndpoint string
if asm != nil {
if asm.RegistrationEndpoint == "" {
return fmt.Errorf("authorization server does not support dynamic client registration")
}
registrationEndpoint = asm.RegistrationEndpoint
} else {
// Fallback to 2025-03-26 spec: predefined endpoints if not provided by AS.
registrationEndpoint = authServerURL + "/register"
}
log.Printf("Attempting dynamic client registration at %v", registrationEndpoint)
regResp, err := oauthex.RegisterClient(ctx, registrationEndpoint, dcrCfg.Metadata, http.DefaultClient)
if err != nil {
return fmt.Errorf("failed to register client: %w", err)
}
h.resolvedClientConfig = &resolvedClientConfig{
registrationType: registrationTypeDynamic,
clientID: regResp.ClientID,
clientSecret: regResp.ClientSecret,
}
switch regResp.TokenEndpointAuthMethod {
case "client_secret_post":
h.resolvedClientConfig.authStyle = oauth2.AuthStyleInParams
case "client_secret_basic":
h.resolvedClientConfig.authStyle = oauth2.AuthStyleInHeader
case "none":
// "none" is equivalent to "client_secret_post" but without sending client secret.
h.resolvedClientConfig.authStyle = oauth2.AuthStyleInParams
h.resolvedClientConfig.clientSecret = ""
default:
// We leave the AuthStyle set to zero value, which is auto-detection.
}
log.Printf("Client registered with client ID: %s", regResp.ClientID)
return nil
}
return fmt.Errorf("no client registration method configured")
}
func isNonRootHTTPSURL(u string) bool {
pu, err := url.Parse(u)
if err != nil {
return false
}
return pu.Scheme == "https" && pu.Path != ""
}
var _ OAuthHandler = (*AuthorizationCodeOAuthHandler)(nil)