| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package extauth |
| 6 |
|
| 7 |
import ( |
| 8 |
"context" |
| 9 |
"fmt" |
| 10 |
"net/http" |
| 11 |
"net/http/httptest" |
| 12 |
"strings" |
| 13 |
"testing" |
| 14 |
|
| 15 |
"github.com/modelcontextprotocol/go-sdk/internal/oauthtest" |
| 16 |
"github.com/modelcontextprotocol/go-sdk/oauthex" |
| 17 |
"golang.org/x/oauth2" |
| 18 |
) |
| 19 |
|
| 20 |
func validEnterpriseHandlerConfig() *EnterpriseHandlerConfig { |
| 21 |
return &EnterpriseHandlerConfig{ |
| 22 |
IdPIssuerURL: "https://idp.example.com", |
| 23 |
IdPCredentials: &oauthex.ClientCredentials{ClientID: "idp_client_id"}, |
| 24 |
MCPAuthServerURL: "https://mcp-auth.example.com", |
| 25 |
MCPResourceURI: "https://mcp.example.com", |
| 26 |
MCPCredentials: &oauthex.ClientCredentials{ClientID: "mcp_client_id"}, |
| 27 |
IDTokenFetcher: func(ctx context.Context) (*oauth2.Token, error) { |
| 28 |
token := &oauth2.Token{AccessToken: "mock", TokenType: "Bearer"} |
| 29 |
return token.WithExtra(map[string]any{"id_token": "mock_id_token"}), nil |
| 30 |
}, |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
func TestNewEnterpriseHandler_Validation(t *testing.T) { |
| 35 |
tests := []struct { |
| 36 |
name string |
| 37 |
config *EnterpriseHandlerConfig |
| 38 |
wantError string |
| 39 |
}{ |
| 40 |
{ |
| 41 |
name: "nil config", |
| 42 |
config: nil, |
| 43 |
wantError: "config must be provided", |
| 44 |
}, |
| 45 |
{ |
| 46 |
name: "missing IdPIssuerURL", |
| 47 |
config: func() *EnterpriseHandlerConfig { |
| 48 |
c := validEnterpriseHandlerConfig() |
| 49 |
c.IdPIssuerURL = "" |
| 50 |
return c |
| 51 |
}(), |
| 52 |
wantError: "IdPIssuerURL is required", |
| 53 |
}, |
| 54 |
{ |
| 55 |
name: "nil IdPCredentials", |
| 56 |
config: func() *EnterpriseHandlerConfig { |
| 57 |
c := validEnterpriseHandlerConfig() |
| 58 |
c.IdPCredentials = nil |
| 59 |
return c |
| 60 |
}(), |
| 61 |
wantError: "IdPCredentials is required", |
| 62 |
}, |
| 63 |
{ |
| 64 |
name: "invalid IdPCredentials - empty ClientID", |
| 65 |
config: func() *EnterpriseHandlerConfig { |
| 66 |
c := validEnterpriseHandlerConfig() |
| 67 |
c.IdPCredentials = &oauthex.ClientCredentials{ClientID: ""} |
| 68 |
return c |
| 69 |
}(), |
| 70 |
wantError: "invalid IdPCredentials", |
| 71 |
}, |
| 72 |
{ |
| 73 |
name: "invalid IdPCredentials - empty ClientSecret in ClientSecretAuth", |
| 74 |
config: func() *EnterpriseHandlerConfig { |
| 75 |
c := validEnterpriseHandlerConfig() |
| 76 |
c.IdPCredentials = &oauthex.ClientCredentials{ |
| 77 |
ClientID: "idp_client_id", |
| 78 |
ClientSecretAuth: &oauthex.ClientSecretAuth{ClientSecret: ""}, |
| 79 |
} |
| 80 |
return c |
| 81 |
}(), |
| 82 |
wantError: "invalid IdPCredentials", |
| 83 |
}, |
| 84 |
{ |
| 85 |
name: "missing MCPAuthServerURL", |
| 86 |
config: func() *EnterpriseHandlerConfig { |
| 87 |
c := validEnterpriseHandlerConfig() |
| 88 |
c.MCPAuthServerURL = "" |
| 89 |
return c |
| 90 |
}(), |
| 91 |
wantError: "MCPAuthServerURL is required", |
| 92 |
}, |
| 93 |
{ |
| 94 |
name: "missing MCPResourceURI", |
| 95 |
config: func() *EnterpriseHandlerConfig { |
| 96 |
c := validEnterpriseHandlerConfig() |
| 97 |
c.MCPResourceURI = "" |
| 98 |
return c |
| 99 |
}(), |
| 100 |
wantError: "MCPResourceURI is required", |
| 101 |
}, |
| 102 |
{ |
| 103 |
name: "nil MCPCredentials", |
| 104 |
config: func() *EnterpriseHandlerConfig { |
| 105 |
c := validEnterpriseHandlerConfig() |
| 106 |
c.MCPCredentials = nil |
| 107 |
return c |
| 108 |
}(), |
| 109 |
wantError: "MCPCredentials is required", |
| 110 |
}, |
| 111 |
{ |
| 112 |
name: "invalid MCPCredentials - empty ClientID", |
| 113 |
config: func() *EnterpriseHandlerConfig { |
| 114 |
c := validEnterpriseHandlerConfig() |
| 115 |
c.MCPCredentials = &oauthex.ClientCredentials{ClientID: ""} |
| 116 |
return c |
| 117 |
}(), |
| 118 |
wantError: "invalid MCPCredentials", |
| 119 |
}, |
| 120 |
{ |
| 121 |
name: "missing IDTokenFetcher", |
| 122 |
config: func() *EnterpriseHandlerConfig { |
| 123 |
c := validEnterpriseHandlerConfig() |
| 124 |
c.IDTokenFetcher = nil |
| 125 |
return c |
| 126 |
}(), |
| 127 |
wantError: "IDTokenFetcher is required", |
| 128 |
}, |
| 129 |
{ |
| 130 |
name: "valid public clients", |
| 131 |
config: validEnterpriseHandlerConfig(), |
| 132 |
wantError: "", |
| 133 |
}, |
| 134 |
{ |
| 135 |
name: "valid confidential clients", |
| 136 |
config: func() *EnterpriseHandlerConfig { |
| 137 |
c := validEnterpriseHandlerConfig() |
| 138 |
c.IdPCredentials.ClientSecretAuth = &oauthex.ClientSecretAuth{ClientSecret: "idp_secret"} |
| 139 |
c.MCPCredentials.ClientSecretAuth = &oauthex.ClientSecretAuth{ClientSecret: "mcp_secret"} |
| 140 |
return c |
| 141 |
}(), |
| 142 |
wantError: "", |
| 143 |
}, |
| 144 |
} |
| 145 |
|
| 146 |
for _, tt := range tests { |
| 147 |
t.Run(tt.name, func(t *testing.T) { |
| 148 |
handler, err := NewEnterpriseHandler(tt.config) |
| 149 |
if tt.wantError != "" { |
| 150 |
if err == nil { |
| 151 |
t.Fatalf("expected error containing %q, got nil", tt.wantError) |
| 152 |
} |
| 153 |
if !strings.Contains(err.Error(), tt.wantError) { |
| 154 |
t.Fatalf("expected error containing %q, got %v", tt.wantError, err) |
| 155 |
} |
| 156 |
} else { |
| 157 |
if err != nil { |
| 158 |
t.Fatalf("expected no error, got %v", err) |
| 159 |
} |
| 160 |
if handler == nil { |
| 161 |
t.Fatal("expected handler to be non-nil") |
| 162 |
} |
| 163 |
} |
| 164 |
}) |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
func TestEnterpriseHandler_Authorize_E2E(t *testing.T) { |
| 169 |
const idJAGToken = "id-jag-token-from-idp" |
| 170 |
|
| 171 |
idpServer := oauthtest.NewFakeIdPServer(oauthtest.IdPConfig{ |
| 172 |
PreregisteredClients: map[string]oauthtest.ClientInfo{ |
| 173 |
"idp_client_id": {Secret: "idp_secret"}, |
| 174 |
}, |
| 175 |
TokenExchangeConfig: &oauthtest.TokenExchangeConfig{ |
| 176 |
IDJAGToken: idJAGToken, |
| 177 |
}, |
| 178 |
}) |
| 179 |
idpServer.Start(t) |
| 180 |
|
| 181 |
mcpAuthServer := oauthtest.NewFakeAuthorizationServer(oauthtest.Config{ |
| 182 |
RegistrationConfig: &oauthtest.RegistrationConfig{ |
| 183 |
PreregisteredClients: map[string]oauthtest.ClientInfo{ |
| 184 |
"mcp_client_id": {Secret: "mcp_secret"}, |
| 185 |
}, |
| 186 |
}, |
| 187 |
JWTBearerConfig: &oauthtest.JWTBearerConfig{ |
| 188 |
ValidAssertions: []string{idJAGToken}, |
| 189 |
}, |
| 190 |
}) |
| 191 |
mcpAuthServer.Start(t) |
| 192 |
|
| 193 |
config := validEnterpriseHandlerConfig() |
| 194 |
config.IdPIssuerURL = idpServer.URL() |
| 195 |
config.IdPCredentials.ClientSecretAuth = &oauthex.ClientSecretAuth{ClientSecret: "idp_secret"} |
| 196 |
config.MCPAuthServerURL = mcpAuthServer.URL() |
| 197 |
config.MCPCredentials.ClientSecretAuth = &oauthex.ClientSecretAuth{ClientSecret: "mcp_secret"} |
| 198 |
config.MCPScopes = []string{"read", "write"} |
| 199 |
config.IDTokenFetcher = func(ctx context.Context) (*oauth2.Token, error) { |
| 200 |
token := &oauth2.Token{AccessToken: "mock_access_token", TokenType: "Bearer"} |
| 201 |
return token.WithExtra(map[string]any{"id_token": "mock_id_token_from_user_login"}), nil |
| 202 |
} |
| 203 |
|
| 204 |
handler, err := NewEnterpriseHandler(config) |
| 205 |
if err != nil { |
| 206 |
t.Fatalf("NewEnterpriseHandler failed: %v", err) |
| 207 |
} |
| 208 |
|
| 209 |
req := httptest.NewRequest(http.MethodGet, "https://mcp.example.com/api", nil) |
| 210 |
resp := &http.Response{ |
| 211 |
StatusCode: http.StatusUnauthorized, |
| 212 |
Header: make(http.Header), |
| 213 |
Body: http.NoBody, |
| 214 |
Request: req, |
| 215 |
} |
| 216 |
|
| 217 |
if err := handler.Authorize(context.Background(), req, resp); err != nil { |
| 218 |
t.Fatalf("Authorize failed: %v", err) |
| 219 |
} |
| 220 |
|
| 221 |
tokenSource, err := handler.TokenSource(context.Background()) |
| 222 |
if err != nil { |
| 223 |
t.Fatalf("TokenSource failed: %v", err) |
| 224 |
} |
| 225 |
if tokenSource == nil { |
| 226 |
t.Fatal("expected token source to be set after authorization") |
| 227 |
} |
| 228 |
|
| 229 |
token, err := tokenSource.Token() |
| 230 |
if err != nil { |
| 231 |
t.Fatalf("Token() failed: %v", err) |
| 232 |
} |
| 233 |
if token.AccessToken != "test_access_token" { |
| 234 |
t.Errorf("AccessToken = %q, want %q", token.AccessToken, "test_access_token") |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
func TestEnterpriseHandler_Authorize_IDTokenFetcherError(t *testing.T) { |
| 239 |
config := validEnterpriseHandlerConfig() |
| 240 |
config.IDTokenFetcher = func(ctx context.Context) (*oauth2.Token, error) { |
| 241 |
return nil, fmt.Errorf("user cancelled login") |
| 242 |
} |
| 243 |
|
| 244 |
handler, err := NewEnterpriseHandler(config) |
| 245 |
if err != nil { |
| 246 |
t.Fatalf("NewEnterpriseHandler failed: %v", err) |
| 247 |
} |
| 248 |
|
| 249 |
req := httptest.NewRequest(http.MethodGet, "https://mcp.example.com/api", nil) |
| 250 |
resp := &http.Response{ |
| 251 |
StatusCode: http.StatusUnauthorized, |
| 252 |
Header: make(http.Header), |
| 253 |
Body: http.NoBody, |
| 254 |
Request: req, |
| 255 |
} |
| 256 |
|
| 257 |
err = handler.Authorize(context.Background(), req, resp) |
| 258 |
if err == nil { |
| 259 |
t.Fatal("expected error from Authorize, got nil") |
| 260 |
} |
| 261 |
if !strings.Contains(err.Error(), "failed to obtain ID token") { |
| 262 |
t.Errorf("error = %v, want error containing %q", err, "failed to obtain ID token") |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
func TestEnterpriseHandler_TokenSource_BeforeAuthorization(t *testing.T) { |
| 267 |
handler, err := NewEnterpriseHandler(validEnterpriseHandlerConfig()) |
| 268 |
if err != nil { |
| 269 |
t.Fatalf("NewEnterpriseHandler failed: %v", err) |
| 270 |
} |
| 271 |
|
| 272 |
tokenSource, err := handler.TokenSource(context.Background()) |
| 273 |
if err != nil { |
| 274 |
t.Fatalf("TokenSource failed: %v", err) |
| 275 |
} |
| 276 |
if tokenSource != nil { |
| 277 |
t.Errorf("expected nil token source before authorization, got %v", tokenSource) |
| 278 |
} |
| 279 |
} |