mcp: add optional issuer validator for pre-registered client validation (SEP-2352) (#946)
Fixes #978
This commit is contained in:
parent
189a85ad92
commit
dfb45f1e11
4 changed files with 190 additions and 5 deletions
|
|
@ -507,6 +507,9 @@ func (h *AuthorizationCodeHandler) handleRegistration(ctx context.Context, asm *
|
|||
// 2. Attempt to use pre-registered client configuration.
|
||||
preCfg := h.config.PreregisteredClient
|
||||
if preCfg != nil {
|
||||
if preCfg.Issuer != "" && !authutil.IssuersEqual(preCfg.Issuer, asm.Issuer) {
|
||||
return nil, fmt.Errorf("authorization server issuer %q does not match pre-registered credentials issuer %q", asm.Issuer, preCfg.Issuer)
|
||||
}
|
||||
authStyle := selectTokenAuthMethod(asm.TokenEndpointAuthMethodsSupported)
|
||||
clientSecret := ""
|
||||
if preCfg.ClientSecretAuth != nil {
|
||||
|
|
|
|||
|
|
@ -609,6 +609,8 @@ func TestHandleRegistration(t *testing.T) {
|
|||
asm *oauthex.AuthServerMeta
|
||||
want *resolvedClientConfig
|
||||
wantError bool
|
||||
issuerMatch bool
|
||||
issuerSuffix string
|
||||
}{
|
||||
{
|
||||
name: "ClientIDMetadataDocument",
|
||||
|
|
@ -647,6 +649,79 @@ func TestHandleRegistration(t *testing.T) {
|
|||
authStyle: oauth2.AuthStyleInParams,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "Preregistered_IssuerMatch",
|
||||
serverConfig: &oauthtest.RegistrationConfig{
|
||||
PreregisteredClients: map[string]oauthtest.ClientInfo{
|
||||
"pre_client_id": {
|
||||
Secret: "pre_client_secret",
|
||||
},
|
||||
},
|
||||
},
|
||||
handlerConfig: &AuthorizationCodeHandlerConfig{
|
||||
PreregisteredClient: &oauthex.ClientCredentials{
|
||||
ClientID: "pre_client_id",
|
||||
ClientSecretAuth: &oauthex.ClientSecretAuth{
|
||||
ClientSecret: "pre_client_secret",
|
||||
},
|
||||
Issuer: "", // set dynamically in the test
|
||||
},
|
||||
},
|
||||
want: &resolvedClientConfig{
|
||||
registrationType: registrationTypePreregistered,
|
||||
clientID: "pre_client_id",
|
||||
clientSecret: "pre_client_secret",
|
||||
authStyle: oauth2.AuthStyleInParams,
|
||||
},
|
||||
issuerMatch: true,
|
||||
},
|
||||
{
|
||||
name: "Preregistered_IssuerMismatch",
|
||||
serverConfig: &oauthtest.RegistrationConfig{
|
||||
PreregisteredClients: map[string]oauthtest.ClientInfo{
|
||||
"pre_client_id": {
|
||||
Secret: "pre_client_secret",
|
||||
},
|
||||
},
|
||||
},
|
||||
handlerConfig: &AuthorizationCodeHandlerConfig{
|
||||
PreregisteredClient: &oauthex.ClientCredentials{
|
||||
ClientID: "pre_client_id",
|
||||
ClientSecretAuth: &oauthex.ClientSecretAuth{
|
||||
ClientSecret: "pre_client_secret",
|
||||
},
|
||||
Issuer: "https://other-issuer.example.com",
|
||||
},
|
||||
},
|
||||
wantError: true,
|
||||
},
|
||||
{
|
||||
name: "Preregistered_IssuerMatchTrailingSlash",
|
||||
serverConfig: &oauthtest.RegistrationConfig{
|
||||
PreregisteredClients: map[string]oauthtest.ClientInfo{
|
||||
"pre_client_id": {
|
||||
Secret: "pre_client_secret",
|
||||
},
|
||||
},
|
||||
},
|
||||
handlerConfig: &AuthorizationCodeHandlerConfig{
|
||||
PreregisteredClient: &oauthex.ClientCredentials{
|
||||
ClientID: "pre_client_id",
|
||||
ClientSecretAuth: &oauthex.ClientSecretAuth{
|
||||
ClientSecret: "pre_client_secret",
|
||||
},
|
||||
Issuer: "", // set dynamically in the test (with trailing slash)
|
||||
},
|
||||
},
|
||||
want: &resolvedClientConfig{
|
||||
registrationType: registrationTypePreregistered,
|
||||
clientID: "pre_client_id",
|
||||
clientSecret: "pre_client_secret",
|
||||
authStyle: oauth2.AuthStyleInParams,
|
||||
},
|
||||
issuerMatch: true,
|
||||
issuerSuffix: "/",
|
||||
},
|
||||
{
|
||||
name: "NoneSupported",
|
||||
handlerConfig: &AuthorizationCodeHandlerConfig{
|
||||
|
|
@ -660,6 +735,10 @@ func TestHandleRegistration(t *testing.T) {
|
|||
t.Run(tt.name, func(t *testing.T) {
|
||||
s := oauthtest.NewFakeAuthorizationServer(oauthtest.Config{RegistrationConfig: tt.serverConfig})
|
||||
s.Start(t)
|
||||
// Set the Issuer dynamically if requested by the test case.
|
||||
if tt.issuerMatch {
|
||||
tt.handlerConfig.PreregisteredClient.Issuer = s.URL() + tt.issuerSuffix
|
||||
}
|
||||
tt.handlerConfig.AuthorizationCodeFetcher = func(ctx context.Context, args *AuthorizationArgs) (*AuthorizationResult, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
|
@ -679,6 +758,9 @@ func TestHandleRegistration(t *testing.T) {
|
|||
}
|
||||
return
|
||||
}
|
||||
if tt.wantError {
|
||||
t.Fatal("handleRegistration() expected error, got nil")
|
||||
}
|
||||
if got.registrationType != tt.want.registrationType {
|
||||
t.Errorf("handleRegistration() registrationType = %v, want %v", got.registrationType, tt.want.registrationType)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -128,6 +128,11 @@ func (h *ClientCredentialsHandler) Authorize(ctx context.Context, req *http.Requ
|
|||
}
|
||||
}
|
||||
|
||||
creds := h.config.Credentials
|
||||
if creds.Issuer != "" && !authutil.IssuersEqual(creds.Issuer, asm.Issuer) {
|
||||
return fmt.Errorf("authorization server issuer %q does not match pre-registered credentials issuer %q", asm.Issuer, creds.Issuer)
|
||||
}
|
||||
|
||||
// Determine requestedScopes: use PRM's scopes_supported if available.
|
||||
requestedScopes := scopesFromChallenges(wwwChallenges)
|
||||
if len(requestedScopes) == 0 && len(prm.ScopesSupported) > 0 {
|
||||
|
|
@ -140,7 +145,6 @@ func (h *ClientCredentialsHandler) Authorize(ctx context.Context, req *http.Requ
|
|||
requestedScopes = authutil.UnionScopes(h.grantedScopes[asm.Issuer], requestedScopes)
|
||||
|
||||
// Step 3: Exchange client credentials for an access token.
|
||||
creds := h.config.Credentials
|
||||
cfg := &clientcredentials.Config{
|
||||
ClientID: creds.ClientID,
|
||||
ClientSecret: creds.ClientSecretAuth.ClientSecret,
|
||||
|
|
|
|||
|
|
@ -221,6 +221,51 @@ func TestClientCredentialsHandler_Authorize(t *testing.T) {
|
|||
}
|
||||
})
|
||||
|
||||
t.Run("issuer mismatch", func(t *testing.T) {
|
||||
config := validClientCredentialsConfig()
|
||||
config.Credentials.Issuer = "https://other-issuer.example.com"
|
||||
handler, err := NewClientCredentialsHandler(config)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
resp := &http.Response{
|
||||
StatusCode: http.StatusUnauthorized,
|
||||
Header: http.Header{},
|
||||
Body: http.NoBody,
|
||||
}
|
||||
req := httptest.NewRequest("GET", resourceURL, nil)
|
||||
err = handler.Authorize(t.Context(), req, resp)
|
||||
if err == nil {
|
||||
t.Fatal("expected Authorize to fail with issuer mismatch")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "does not match") {
|
||||
t.Errorf("error %q does not mention issuer mismatch", err.Error())
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("issuer match ignoring trailing slash", func(t *testing.T) {
|
||||
config := validClientCredentialsConfig()
|
||||
// authServer.URL() has no trailing slash; configure with one to
|
||||
// verify the comparison tolerates the difference (per RFC 8414 §3.3
|
||||
// normalization applied in oauthex.IssuersEqual).
|
||||
config.Credentials.Issuer = authServer.URL() + "/"
|
||||
handler, err := NewClientCredentialsHandler(config)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
resp := &http.Response{
|
||||
StatusCode: http.StatusUnauthorized,
|
||||
Header: http.Header{},
|
||||
Body: http.NoBody,
|
||||
}
|
||||
req := httptest.NewRequest("GET", resourceURL, nil)
|
||||
if err := handler.Authorize(t.Context(), req, resp); err != nil {
|
||||
t.Fatalf("Authorize() unexpected error = %v", err)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("PRM via resource_metadata in challenge", func(t *testing.T) {
|
||||
prmMux := http.NewServeMux()
|
||||
prmMux.Handle("/custom-prm", auth.ProtectedResourceMetadataHandler(&oauthex.ProtectedResourceMetadata{
|
||||
|
|
|
|||
Loading…
Reference in a new issue