| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
package extauth |
| 10 |
|
| 11 |
import ( |
| 12 |
"context" |
| 13 |
"crypto/rand" |
| 14 |
"fmt" |
| 15 |
"net/http" |
| 16 |
"net/url" |
| 17 |
"slices" |
| 18 |
"strings" |
| 19 |
|
| 20 |
"github.com/modelcontextprotocol/go-sdk/auth" |
| 21 |
"github.com/modelcontextprotocol/go-sdk/oauthex" |
| 22 |
"golang.org/x/oauth2" |
| 23 |
) |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
type OIDCLoginConfig struct { |
| 29 |
|
| 30 |
|
| 31 |
IssuerURL string |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
Credentials *oauthex.ClientCredentials |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
RedirectURL string |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
Scopes []string |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
LoginHint string |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
HTTPClient *http.Client |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
func PerformOIDCLogin( |
| 67 |
ctx context.Context, |
| 68 |
config *OIDCLoginConfig, |
| 69 |
authCodeFetcher auth.AuthorizationCodeFetcher, |
| 70 |
) (*oauth2.Token, error) { |
| 71 |
if authCodeFetcher == nil { |
| 72 |
return nil, fmt.Errorf("authCodeFetcher is required") |
| 73 |
} |
| 74 |
|
| 75 |
authReq, oauth2Config, err := initiateOIDCLogin(ctx, config) |
| 76 |
if err != nil { |
| 77 |
return nil, fmt.Errorf("failed to initiate OIDC login: %w", err) |
| 78 |
} |
| 79 |
|
| 80 |
authResult, err := authCodeFetcher(ctx, &auth.AuthorizationArgs{URL: authReq.authURL}) |
| 81 |
if err != nil { |
| 82 |
return nil, fmt.Errorf("failed to fetch authorization code: %w", err) |
| 83 |
} |
| 84 |
|
| 85 |
if authResult.State != authReq.state { |
| 86 |
return nil, fmt.Errorf("state mismatch: expected %q, got %q", authReq.state, authResult.State) |
| 87 |
} |
| 88 |
|
| 89 |
tokens, err := completeOIDCLogin(ctx, config, oauth2Config, authResult.Code, authReq.codeVerifier) |
| 90 |
if err != nil { |
| 91 |
return nil, fmt.Errorf("failed to complete OIDC login: %w", err) |
| 92 |
} |
| 93 |
|
| 94 |
return tokens, nil |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
type oidcAuthorizationRequest struct { |
| 99 |
authURL string |
| 100 |
state string |
| 101 |
codeVerifier string |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
func initiateOIDCLogin( |
| 106 |
ctx context.Context, |
| 107 |
config *OIDCLoginConfig, |
| 108 |
) (*oidcAuthorizationRequest, *oauth2.Config, error) { |
| 109 |
if config == nil { |
| 110 |
return nil, nil, fmt.Errorf("config is required") |
| 111 |
} |
| 112 |
if config.IssuerURL == "" { |
| 113 |
return nil, nil, fmt.Errorf("IssuerURL is required") |
| 114 |
} |
| 115 |
if config.Credentials == nil || config.Credentials.ClientID == "" { |
| 116 |
return nil, nil, fmt.Errorf("Credentials.ClientID is required") |
| 117 |
} |
| 118 |
if config.RedirectURL == "" { |
| 119 |
return nil, nil, fmt.Errorf("RedirectURL is required") |
| 120 |
} |
| 121 |
if len(config.Scopes) == 0 { |
| 122 |
return nil, nil, fmt.Errorf("at least one scope is required (must include 'openid')") |
| 123 |
} |
| 124 |
|
| 125 |
if !slices.Contains(config.Scopes, "openid") { |
| 126 |
return nil, nil, fmt.Errorf("the 'openid' scope is required for OIDC") |
| 127 |
} |
| 128 |
|
| 129 |
if err := checkURLScheme(config.IssuerURL); err != nil { |
| 130 |
return nil, nil, fmt.Errorf("invalid IssuerURL: %w", err) |
| 131 |
} |
| 132 |
if err := checkURLScheme(config.RedirectURL); err != nil { |
| 133 |
return nil, nil, fmt.Errorf("invalid RedirectURL: %w", err) |
| 134 |
} |
| 135 |
|
| 136 |
httpClient := config.HTTPClient |
| 137 |
if httpClient == nil { |
| 138 |
httpClient = http.DefaultClient |
| 139 |
} |
| 140 |
meta, err := auth.GetAuthServerMetadata(ctx, config.IssuerURL, httpClient) |
| 141 |
if err != nil { |
| 142 |
return nil, nil, fmt.Errorf("failed to discover OIDC metadata: %w", err) |
| 143 |
} |
| 144 |
if meta == nil { |
| 145 |
return nil, nil, fmt.Errorf("no authorization server metadata found for OIDC issuer: %s", config.IssuerURL) |
| 146 |
} |
| 147 |
if meta.AuthorizationEndpoint == "" { |
| 148 |
return nil, nil, fmt.Errorf("authorization_endpoint not found in OIDC metadata") |
| 149 |
} |
| 150 |
|
| 151 |
codeVerifier := oauth2.GenerateVerifier() |
| 152 |
state := rand.Text() |
| 153 |
|
| 154 |
oauth2Config := &oauth2.Config{ |
| 155 |
ClientID: config.Credentials.ClientID, |
| 156 |
RedirectURL: config.RedirectURL, |
| 157 |
Scopes: config.Scopes, |
| 158 |
Endpoint: oauth2.Endpoint{ |
| 159 |
AuthURL: meta.AuthorizationEndpoint, |
| 160 |
TokenURL: meta.TokenEndpoint, |
| 161 |
}, |
| 162 |
} |
| 163 |
|
| 164 |
if config.Credentials.ClientSecretAuth != nil { |
| 165 |
oauth2Config.ClientSecret = config.Credentials.ClientSecretAuth.ClientSecret |
| 166 |
} |
| 167 |
|
| 168 |
authURLOpts := []oauth2.AuthCodeOption{ |
| 169 |
oauth2.S256ChallengeOption(codeVerifier), |
| 170 |
} |
| 171 |
if config.LoginHint != "" { |
| 172 |
authURLOpts = append(authURLOpts, oauth2.SetAuthURLParam("login_hint", config.LoginHint)) |
| 173 |
} |
| 174 |
authURL := oauth2Config.AuthCodeURL(state, authURLOpts...) |
| 175 |
|
| 176 |
return &oidcAuthorizationRequest{ |
| 177 |
authURL: authURL, |
| 178 |
state: state, |
| 179 |
codeVerifier: codeVerifier, |
| 180 |
}, oauth2Config, nil |
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
func completeOIDCLogin( |
| 186 |
ctx context.Context, |
| 187 |
config *OIDCLoginConfig, |
| 188 |
oauth2Config *oauth2.Config, |
| 189 |
authCode string, |
| 190 |
codeVerifier string, |
| 191 |
) (*oauth2.Token, error) { |
| 192 |
if authCode == "" { |
| 193 |
return nil, fmt.Errorf("authCode is required") |
| 194 |
} |
| 195 |
if codeVerifier == "" { |
| 196 |
return nil, fmt.Errorf("codeVerifier is required") |
| 197 |
} |
| 198 |
|
| 199 |
httpClient := config.HTTPClient |
| 200 |
if httpClient == nil { |
| 201 |
httpClient = http.DefaultClient |
| 202 |
} |
| 203 |
ctxWithClient := context.WithValue(ctx, oauth2.HTTPClient, httpClient) |
| 204 |
|
| 205 |
oauth2Token, err := oauth2Config.Exchange( |
| 206 |
ctxWithClient, |
| 207 |
authCode, |
| 208 |
oauth2.VerifierOption(codeVerifier), |
| 209 |
) |
| 210 |
if err != nil { |
| 211 |
return nil, fmt.Errorf("token exchange failed: %w", err) |
| 212 |
} |
| 213 |
|
| 214 |
|
| 215 |
idToken, ok := oauth2Token.Extra("id_token").(string) |
| 216 |
if !ok || idToken == "" { |
| 217 |
return nil, fmt.Errorf("id_token not found in token response") |
| 218 |
} |
| 219 |
|
| 220 |
return oauth2Token, nil |
| 221 |
} |
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
func checkURLScheme(u string) error { |
| 228 |
if u == "" { |
| 229 |
return nil |
| 230 |
} |
| 231 |
uu, err := url.Parse(u) |
| 232 |
if err != nil { |
| 233 |
return err |
| 234 |
} |
| 235 |
scheme := strings.ToLower(uu.Scheme) |
| 236 |
if scheme == "javascript" || scheme == "data" || scheme == "vbscript" { |
| 237 |
return fmt.Errorf("URL has disallowed scheme %q", scheme) |
| 238 |
} |
| 239 |
return nil |
| 240 |
} |