-
Notifications
You must be signed in to change notification settings - Fork 377
Enterprise managed authorization #770
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
radar07
wants to merge
14
commits into
modelcontextprotocol:main
Choose a base branch
from
radar07:enterprise-managed-authorization
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
160a55f
feat: add token exchange flow
radar07 6b57384
feat: add jwt bearer flow with id-jag
radar07 adaaecb
feat: add id-jag+jwt parsing and enterprise auth
radar07 5a52c5e
chore: use auth server metadata for token endpoints
radar07 e070a87
feat: fetch jwks for jwt signature verification
radar07 a0e9031
feat: add id-jag validation for mcp servers
radar07 35c0565
feat: add oidc login for sso
radar07 8b7ba7a
docs: add docs for enterprise authorization
radar07 b7433c3
chore: remove id jag verification
radar07 6b8bfdc
chore: rename MCPResourceURL to MCPResourceURI
radar07 897455b
feat: use oauth2 for token structs
radar07 b6161af
chore: update auth ext package
radar07 0649bcd
chore: update header files with current year
radar07 d5b2a5d
refactor: use oauth2 package and remove unused functions
radar07 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,181 @@ | ||
| // Copyright 2026 The Go MCP SDK Authors. All rights reserved. | ||
| // Use of this source code is governed by an MIT-style | ||
| // license that can be found in the LICENSE file. | ||
|
|
||
| // This file implements the client-side Enterprise Managed Authorization flow | ||
| // for MCP as specified in SEP-990. | ||
|
|
||
| //go:build mcp_go_client_oauth | ||
|
|
||
| package auth | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "net/http" | ||
|
|
||
| "github.com/modelcontextprotocol/go-sdk/oauthex" | ||
| "golang.org/x/oauth2" | ||
| ) | ||
|
|
||
| // EnterpriseAuthConfig contains configuration for Enterprise Managed Authorization | ||
| // (SEP-990). This configures both the IdP (for token exchange) and the MCP Server | ||
| // (for JWT Bearer grant). | ||
| type EnterpriseAuthConfig struct { | ||
| // IdP configuration (where the user authenticates) | ||
| IdPIssuerURL string // e.g., "https://acme.okta.com" | ||
| IdPClientID string // MCP Client's ID at the IdP | ||
| IdPClientSecret string // MCP Client's secret at the IdP | ||
|
|
||
| // MCP Server configuration (the resource being accessed) | ||
| MCPAuthServerURL string // MCP Server's auth server issuer URL | ||
| MCPResourceURI string // MCP Server's resource identifier | ||
| MCPClientID string // MCP Client's ID at the MCP Server | ||
| MCPClientSecret string // MCP Client's secret at the MCP Server | ||
| MCPScopes []string // Requested scopes at the MCP Server | ||
|
|
||
| // Optional HTTP client for customization | ||
| HTTPClient *http.Client | ||
| } | ||
|
|
||
| // EnterpriseAuthFlow performs the complete Enterprise Managed Authorization flow: | ||
| // 1. Token Exchange: ID Token → ID-JAG at IdP | ||
| // 2. JWT Bearer: ID-JAG → Access Token at MCP Server | ||
| // | ||
| // This function takes an ID Token that was obtained via SSO (e.g., OIDC login) | ||
| // and exchanges it for an access token that can be used to call the MCP Server. | ||
| // | ||
| // There are two ways to obtain an ID Token for use with this function: | ||
| // | ||
| // Option 1: Use the OIDC login helper functions (full flow with SSO): | ||
| // | ||
| // // Step 1: Initiate OIDC login | ||
| // oidcConfig := &OIDCLoginConfig{ | ||
| // IssuerURL: "https://acme.okta.com", | ||
| // ClientID: "client-id", | ||
| // RedirectURL: "http://localhost:8080/callback", | ||
| // Scopes: []string{"openid", "profile", "email"}, | ||
| // } | ||
| // authReq, err := InitiateOIDCLogin(ctx, oidcConfig) | ||
| // if err != nil { | ||
| // log.Fatal(err) | ||
| // } | ||
| // | ||
| // // Step 2: Direct user to authReq.AuthURL for authentication | ||
| // fmt.Printf("Visit: %s\n", authReq.AuthURL) | ||
| // | ||
| // // Step 3: After redirect, complete login with authorization code | ||
| // tokens, err := CompleteOIDCLogin(ctx, oidcConfig, authCode, authReq.CodeVerifier) | ||
| // if err != nil { | ||
| // log.Fatal(err) | ||
| // } | ||
| // | ||
| // // Step 4: Use ID token for enterprise auth | ||
| // enterpriseConfig := &EnterpriseAuthConfig{ | ||
| // IdPIssuerURL: "https://acme.okta.com", | ||
| // IdPClientID: "client-id-at-idp", | ||
| // IdPClientSecret: "secret-at-idp", | ||
| // MCPAuthServerURL: "https://auth.mcpserver.example", | ||
| // MCPResourceURI: "https://mcp.mcpserver.example", | ||
| // MCPClientID: "client-id-at-mcp", | ||
| // MCPClientSecret: "secret-at-mcp", | ||
| // MCPScopes: []string{"read", "write"}, | ||
| // } | ||
| // accessToken, err := EnterpriseAuthFlow(ctx, enterpriseConfig, tokens.IDToken) | ||
| // if err != nil { | ||
| // log.Fatal(err) | ||
| // } | ||
| // | ||
| // Option 2: Bring your own ID Token (if you already have one): | ||
| // | ||
| // config := &EnterpriseAuthConfig{ | ||
| // IdPIssuerURL: "https://acme.okta.com", | ||
| // IdPClientID: "client-id-at-idp", | ||
| // IdPClientSecret: "secret-at-idp", | ||
| // MCPAuthServerURL: "https://auth.mcpserver.example", | ||
| // MCPResourceURI: "https://mcp.mcpserver.example", | ||
| // MCPClientID: "client-id-at-mcp", | ||
| // MCPClientSecret: "secret-at-mcp", | ||
| // MCPScopes: []string{"read", "write"}, | ||
| // } | ||
| // | ||
| // // If you already obtained an ID token through your own means | ||
| // accessToken, err := EnterpriseAuthFlow(ctx, config, myIDToken) | ||
| // if err != nil { | ||
| // log.Fatal(err) | ||
| // } | ||
| // | ||
| // // Use accessToken to call MCP Server APIs | ||
| func EnterpriseAuthFlow( | ||
| ctx context.Context, | ||
| config *EnterpriseAuthConfig, | ||
| idToken string, | ||
| ) (*oauth2.Token, error) { | ||
| if config == nil { | ||
| return nil, fmt.Errorf("config is required") | ||
| } | ||
| if idToken == "" { | ||
| return nil, fmt.Errorf("idToken is required") | ||
| } | ||
| // Validate configuration | ||
| if config.IdPIssuerURL == "" { | ||
| return nil, fmt.Errorf("IdPIssuerURL is required") | ||
| } | ||
| if config.MCPAuthServerURL == "" { | ||
| return nil, fmt.Errorf("MCPAuthServerURL is required") | ||
| } | ||
| if config.MCPResourceURI == "" { | ||
| return nil, fmt.Errorf("MCPResourceURI is required") | ||
| } | ||
| httpClient := config.HTTPClient | ||
| if httpClient == nil { | ||
| httpClient = http.DefaultClient | ||
| } | ||
|
|
||
| // Step 1: Discover IdP token endpoint via OIDC discovery | ||
| idpMeta, err := GetAuthServerMetadataForIssuer(ctx, config.IdPIssuerURL, httpClient) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to discover IdP metadata: %w", err) | ||
| } | ||
|
|
||
| // Step 2: Token Exchange (ID Token → ID-JAG) | ||
| tokenExchangeReq := &oauthex.TokenExchangeRequest{ | ||
| RequestedTokenType: oauthex.TokenTypeIDJAG, | ||
| Audience: config.MCPAuthServerURL, | ||
| Resource: config.MCPResourceURI, | ||
| Scope: config.MCPScopes, | ||
| SubjectToken: idToken, | ||
| SubjectTokenType: oauthex.TokenTypeIDToken, | ||
| } | ||
|
|
||
| tokenExchangeResp, err := oauthex.ExchangeToken( | ||
| ctx, | ||
| idpMeta.TokenEndpoint, | ||
| tokenExchangeReq, | ||
| config.IdPClientID, | ||
| config.IdPClientSecret, | ||
| httpClient, | ||
| ) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("token exchange failed: %w", err) | ||
| } | ||
|
|
||
| // Step 3: JWT Bearer Grant (ID-JAG → Access Token) | ||
| mcpMeta, err := GetAuthServerMetadataForIssuer(ctx, config.MCPAuthServerURL, httpClient) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to discover MCP auth server metadata: %w", err) | ||
| } | ||
|
|
||
| accessToken, err := oauthex.ExchangeJWTBearer( | ||
| ctx, | ||
| mcpMeta.TokenEndpoint, | ||
| tokenExchangeResp.AccessToken, | ||
| config.MCPClientID, | ||
| config.MCPClientSecret, | ||
| httpClient, | ||
| ) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("JWT bearer grant failed: %w", err) | ||
| } | ||
| return accessToken, nil | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we still need this if we have the
EnterpriseHandler?