Skip to content

Commit a4fd173

Browse files
committed
refactor: use oauth2 package and remove unused functions
1 parent 0649bcd commit a4fd173

10 files changed

Lines changed: 395 additions & 749 deletions

auth/authorization_code.go

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -395,40 +395,6 @@ func (h *AuthorizationCodeHandler) getAuthServerMetadata(ctx context.Context, pr
395395
return asm, nil
396396
}
397397

398-
// authorizationServerMetadataURLs returns a list of URLs to try when looking for
399-
// authorization server metadata as mandated by the MCP specification:
400-
// https://modelcontextprotocol.io/specification/2025-11-25/basic/authorization#authorization-server-metadata-discovery.
401-
func authorizationServerMetadataURLs(issuerURL string) []string {
402-
var urls []string
403-
404-
baseURL, err := url.Parse(issuerURL)
405-
if err != nil {
406-
return nil
407-
}
408-
409-
if baseURL.Path == "" {
410-
// "OAuth 2.0 Authorization Server Metadata".
411-
baseURL.Path = "/.well-known/oauth-authorization-server"
412-
urls = append(urls, baseURL.String())
413-
// "OpenID Connect Discovery 1.0".
414-
baseURL.Path = "/.well-known/openid-configuration"
415-
urls = append(urls, baseURL.String())
416-
return urls
417-
}
418-
419-
originalPath := baseURL.Path
420-
// "OAuth 2.0 Authorization Server Metadata with path insertion".
421-
baseURL.Path = "/.well-known/oauth-authorization-server/" + strings.TrimLeft(originalPath, "/")
422-
urls = append(urls, baseURL.String())
423-
// "OpenID Connect Discovery 1.0 with path insertion".
424-
baseURL.Path = "/.well-known/openid-configuration/" + strings.TrimLeft(originalPath, "/")
425-
urls = append(urls, baseURL.String())
426-
// "OpenID Connect Discovery 1.0 with path appending".
427-
baseURL.Path = "/" + strings.Trim(originalPath, "/") + "/.well-known/openid-configuration"
428-
urls = append(urls, baseURL.String())
429-
return urls
430-
}
431-
432398
type registrationType int
433399

434400
const (

auth/enterprise_auth.go

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ func EnterpriseAuthFlow(
133133
}
134134

135135
// Step 1: Discover IdP token endpoint via OIDC discovery
136-
idpMeta, err := GetAuthServerMetadatForIssuer(ctx, config.IdPIssuerURL, httpClient)
136+
idpMeta, err := GetAuthServerMetadataForIssuer(ctx, config.IdPIssuerURL, httpClient)
137137
if err != nil {
138138
return nil, fmt.Errorf("failed to discover IdP metadata: %w", err)
139139
}
@@ -161,7 +161,7 @@ func EnterpriseAuthFlow(
161161
}
162162

163163
// Step 3: JWT Bearer Grant (ID-JAG → Access Token)
164-
mcpMeta, err := GetAuthServerMetadatForIssuer(ctx, config.MCPAuthServerURL, httpClient)
164+
mcpMeta, err := GetAuthServerMetadataForIssuer(ctx, config.MCPAuthServerURL, httpClient)
165165
if err != nil {
166166
return nil, fmt.Errorf("failed to discover MCP auth server metadata: %w", err)
167167
}
@@ -179,18 +179,3 @@ func EnterpriseAuthFlow(
179179
}
180180
return accessToken, nil
181181
}
182-
183-
// GetAuthServerMetadatForIssuer fetches authorization server metadata for the given issuer URL.
184-
// It tries standard well-known endpoints (OAuth 2.0 and OIDC) and returns the first successful result.
185-
func GetAuthServerMetadatForIssuer(ctx context.Context, IssuerURL string, httpClient *httpClient) (*oauthex.AuthServerMeta, error) {
186-
for _, metadataURL := range authorizationServerMetadataURLs(issuerURL) {
187-
asm, err := oauthex.GetAuthServerMeta(ctx, metadataURL, issuerURL, httpClient)
188-
if err != nil {
189-
return nil, fmt.Errorf("failed to get authorization server metadata: %w", err)
190-
}
191-
if asm != nil {
192-
return asm, nil
193-
}
194-
}
195-
return nil, fmt.Errorf("no authorization server metadata found for %s", issuerURL)
196-
}

auth/extauth/enterprise_handler.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,40 +32,50 @@ type EnterpriseHandlerConfig struct {
3232

3333
// IdPIssuerURL is the enterprise IdP's issuer URL (e.g., "https://acme.okta.com").
3434
// Used for OIDC discovery to find the token endpoint.
35+
// REQUIRED.
3536
IdPIssuerURL string
3637

3738
// IdPClientID is the MCP Client's ID registered at the IdP.
39+
// OPTIONAL. Required if the IdP requires client authentication for token exchange.
3840
IdPClientID string
3941

4042
// IdPClientSecret is the MCP Client's secret registered at the IdP.
43+
// OPTIONAL. Required if the IdP requires client authentication for token exchange.
4144
IdPClientSecret string
4245

4346
// MCP Server configuration (the resource being accessed)
4447

4548
// MCPAuthServerURL is the MCP Server's authorization server issuer URL.
4649
// Used as the audience for token exchange and for metadata discovery.
50+
// REQUIRED.
4751
MCPAuthServerURL string
4852

4953
// MCPResourceURI is the MCP Server's resource identifier (RFC 9728).
5054
// Used as the resource parameter in token exchange.
55+
// REQUIRED.
5156
MCPResourceURI string
5257

5358
// MCPClientID is the MCP Client's ID registered at the MCP Server.
59+
// OPTIONAL. Required if the MCP Server requires client authentication.
5460
MCPClientID string
5561

5662
// MCPClientSecret is the MCP Client's secret registered at the MCP Server.
63+
// OPTIONAL. Required if the MCP Server requires client authentication.
5764
MCPClientSecret string
5865

5966
// MCPScopes is the list of scopes to request at the MCP Server.
67+
// OPTIONAL.
6068
MCPScopes []string
6169

6270
// IDTokenFetcher is called to obtain an ID Token when authorization is needed.
6371
// The implementation should handle the OIDC login flow (e.g., browser redirect,
6472
// callback handling) and return the ID token.
73+
// REQUIRED.
6574
IDTokenFetcher IDTokenFetcher
6675

6776
// HTTPClient is an optional HTTP client for customization.
6877
// If nil, http.DefaultClient is used.
78+
// OPTIONAL.
6979
HTTPClient *http.Client
7080
}
7181

@@ -117,12 +127,8 @@ func (h *EnterpriseHandler) TokenSource(ctx context.Context) (oauth2.TokenSource
117127
// Authorize performs the Enterprise Managed Authorization flow.
118128
// It is called when a request fails with 401 or 403.
119129
func (h *EnterpriseHandler) Authorize(ctx context.Context, req *http.Request, resp *http.Response) error {
120-
defer func() {
121-
if resp != nil && resp.Body != nil {
122-
io.Copy(io.Discard, resp.Body)
123-
resp.Body.Close()
124-
}
125-
}()
130+
defer resp.Body.Close()
131+
defer io.Copy(io.Discard, resp.Body)
126132

127133
httpClient := h.config.HTTPClient
128134
if httpClient == nil {

0 commit comments

Comments
 (0)