| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package extauth |
| 9 |
|
| 10 |
import ( |
| 11 |
"context" |
| 12 |
"errors" |
| 13 |
"fmt" |
| 14 |
"io" |
| 15 |
"net/http" |
| 16 |
|
| 17 |
"github.com/modelcontextprotocol/go-sdk/auth" |
| 18 |
"github.com/modelcontextprotocol/go-sdk/oauthex" |
| 19 |
"golang.org/x/oauth2" |
| 20 |
) |
| 21 |
|
| 22 |
|
| 23 |
const grantTypeJWTBearer = "urn:ietf:params:oauth:grant-type:jwt-bearer" |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
type IDTokenFetcher func(ctx context.Context) (*oauth2.Token, error) |
| 31 |
|
| 32 |
|
| 33 |
type EnterpriseHandlerConfig struct { |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
IdPIssuerURL string |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
IdPCredentials *oauthex.ClientCredentials |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
MCPAuthServerURL string |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
MCPResourceURI string |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
MCPCredentials *oauthex.ClientCredentials |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
MCPScopes []string |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
IDTokenFetcher IDTokenFetcher |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
HTTPClient *http.Client |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
en |
| 86 |
dP |
| 87 |
er |
| 88 |
type EnterpriseHandler struct { |
| 89 |
config *EnterpriseHandlerConfig |
| 90 |
|
| 91 |
|
| 92 |
tokenSource oauth2.TokenSource |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
var _ auth.OAuthHandler = (*EnterpriseHandler)(nil) |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
func NewEnterpriseHandler(config *EnterpriseHandlerConfig) (*EnterpriseHandler, error) { |
| 101 |
if config == nil { |
| 102 |
return nil, errors.New("config must be provided") |
| 103 |
} |
| 104 |
if config.IdPIssuerURL == "" { |
| 105 |
return nil, errors.New("IdPIssuerURL is required") |
| 106 |
} |
| 107 |
if config.IdPCredentials == nil { |
| 108 |
return nil, errors.New("IdPCredentials is required") |
| 109 |
} |
| 110 |
if err := config.IdPCredentials.Validate(); err != nil { |
| 111 |
return nil, fmt.Errorf("invalid IdPCredentials: %w", err) |
| 112 |
} |
| 113 |
if config.MCPAuthServerURL == "" { |
| 114 |
return nil, errors.New("MCPAuthServerURL is required") |
| 115 |
} |
| 116 |
if config.MCPResourceURI == "" { |
| 117 |
return nil, errors.New("MCPResourceURI is required") |
| 118 |
} |
| 119 |
if config.MCPCredentials == nil { |
| 120 |
return nil, errors.New("MCPCredentials is required") |
| 121 |
} |
| 122 |
if err := config.MCPCredentials.Validate(); err != nil { |
| 123 |
return nil, fmt.Errorf("invalid MCPCredentials: %w", err) |
| 124 |
} |
| 125 |
if config.IDTokenFetcher == nil { |
| 126 |
return nil, errors.New("IDTokenFetcher is required") |
| 127 |
} |
| 128 |
return &EnterpriseHandler{config: config}, nil |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
func (h *EnterpriseHandler) TokenSource(ctx context.Context) (oauth2.TokenSource, error) { |
| 134 |
return h.tokenSource, nil |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
func (h *EnterpriseHandler) Authorize(ctx context.Context, req *http.Request, resp *http.Response) error { |
| 140 |
defer resp.Body.Close() |
| 141 |
defer io.Copy(io.Discard, resp.Body) |
| 142 |
|
| 143 |
httpClient := h.config.HTTPClient |
| 144 |
if httpClient == nil { |
| 145 |
httpClient = http.DefaultClient |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
oidcToken, err := h.config.IDTokenFetcher(ctx) |
| 150 |
if err != nil { |
| 151 |
return fmt.Errorf("failed to obtain ID token: %w", err) |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
idToken, ok := oidcToken.Extra("id_token").(string) |
| 156 |
if !ok || idToken == "" { |
| 157 |
return fmt.Errorf("id_token not found in OIDC token response") |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
idpMeta, err := auth.GetAuthServerMetadata(ctx, h.config.IdPIssuerURL, httpClient) |
| 162 |
if err != nil { |
| 163 |
return fmt.Errorf("failed to discover IdP metadata: %w", err) |
| 164 |
} |
| 165 |
if idpMeta == nil { |
| 166 |
return fmt.Errorf("no authorization server metadata found for IdP: %s", h.config.IdPIssuerURL) |
| 167 |
} |
| 168 |
|
| 169 |
G) |
| 170 |
tokenExchangeReq := &oauthex.TokenExchangeRequest{ |
| 171 |
RequestedTokenType: oauthex.TokenTypeIDJAG, |
| 172 |
Audience: h.config.MCPAuthServerURL, |
| 173 |
Resource: h.config.MCPResourceURI, |
| 174 |
Scope: h.config.MCPScopes, |
| 175 |
SubjectToken: idToken, |
| 176 |
SubjectTokenType: oauthex.TokenTypeIDToken, |
| 177 |
} |
| 178 |
|
| 179 |
idJAGToken, err := oauthex.ExchangeToken( |
| 180 |
ctx, |
| 181 |
idpMeta.TokenEndpoint, |
| 182 |
tokenExchangeReq, |
| 183 |
h.config.IdPCredentials, |
| 184 |
httpClient, |
| 185 |
) |
| 186 |
if err != nil { |
| 187 |
return fmt.Errorf("token exchange failed: %w", err) |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
mcpMeta, err := auth.GetAuthServerMetadata(ctx, h.config.MCPAuthServerURL, httpClient) |
| 192 |
if err != nil { |
| 193 |
return fmt.Errorf("failed to discover MCP auth server metadata: %w", err) |
| 194 |
} |
| 195 |
if mcpMeta == nil { |
| 196 |
return fmt.Errorf("no authorization server metadata found for MCP server: %s", h.config.MCPAuthServerURL) |
| 197 |
} |
| 198 |
|
| 199 |
n) |
| 200 |
|
| 201 |
accessToken, err := exchangeJWTBearer( |
| 202 |
ctx, |
| 203 |
mcpMeta.TokenEndpoint, |
| 204 |
idJAGToken.AccessToken, |
| 205 |
h.config.MCPCredentials, |
| 206 |
httpClient, |
| 207 |
) |
| 208 |
if err != nil { |
| 209 |
return fmt.Errorf("JWT bearer grant failed: %w", err) |
| 210 |
} |
| 211 |
|
| 212 |
|
| 213 |
h.tokenSource = oauth2.StaticTokenSource(accessToken) |
| 214 |
return nil |
| 215 |
} |
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
func exchangeJWTBearer( |
| 220 |
ctx context.Context, |
| 221 |
tokenEndpoint string, |
| 222 |
assertion string, |
| 223 |
clientCreds *oauthex.ClientCredentials, |
| 224 |
httpClient *http.Client, |
| 225 |
) (*oauth2.Token, error) { |
| 226 |
cfg := &oauth2.Config{ |
| 227 |
ClientID: clientCreds.ClientID, |
| 228 |
Endpoint: oauth2.Endpoint{ |
| 229 |
TokenURL: tokenEndpoint, |
| 230 |
AuthStyle: oauth2.AuthStyleInParams, |
| 231 |
}, |
| 232 |
} |
| 233 |
|
| 234 |
if clientCreds.ClientSecretAuth != nil { |
| 235 |
cfg.ClientSecret = clientCreds.ClientSecretAuth.ClientSecret |
| 236 |
} |
| 237 |
|
| 238 |
if httpClient == nil { |
| 239 |
httpClient = http.DefaultClient |
| 240 |
} |
| 241 |
ctxWithClient := context.WithValue(ctx, oauth2.HTTPClient, httpClient) |
| 242 |
|
| 243 |
token, err := cfg.Exchange( |
| 244 |
ctxWithClient, |
| 245 |
"", |
| 246 |
oauth2.SetAuthURLParam("grant_type", grantTypeJWTBearer), |
| 247 |
oauth2.SetAuthURLParam("assertion", assertion), |
| 248 |
) |
| 249 |
if err != nil { |
| 250 |
return nil, fmt.Errorf("JWT bearer grant request failed: %w", err) |
| 251 |
} |
| 252 |
|
| 253 |
return token, nil |
| 254 |
} |