mcpauth: a server whose token endpoint takes a secret only by client_secret_basic is signed in to by it, whatever its metadata lists first (#1490, ggbdpq)

register took token_endpoint_auth_methods_supported[0] whenever the list
had no "none", and moved it to client_secret_post only when that was
listed. A server listing ["private_key_jwt", "client_secret_basic"] passed
the "magpie can do one of these" check and then had magpie registered for
private_key_jwt, which it can't do: the registration is refused
(invalid_client_metadata) or, if taken, the code exchange is. RFC 8414's
list has no order of preference.

What changed:
- register picks client_secret_post where listed, else
  client_secret_basic, else says the server takes only what magpie can't
  do, as before. "none" is still taken first.
- mcpauthtest.Fake gets AuthMethods: its metadata lists them, a
  registration it can't honour without jwks or a certificate is refused,
  a client registered for a secret is given one, and the token endpoint
  checks it as registered.

Run:
- TestSignInBySecretBasic fails with the old register ("registering
  magpie with the server's sign-in failed (400): invalid_client_metadata
  ... private_key_jwt") and passes now.
- go test -tags nogui ./internal/mcpauth/... ./internal/library/...
  ./internal/gateway/... under a temp HOME; gofmt -l; GOOS=linux and
  GOOS=windows go vet -tags nogui.
- Not tried against a real authorization server that lists methods this
  way; the reporter found none in the wild either.

Patch by ggbdpq in #1490, taken as is.

Co-authored-by: ggbdpq <ggbdpq@gmail.com>
This commit is contained in:
yetone
2026-10-10 17:54:45 +08:00
co-authored by ggbdpq
parent 7f66a0cce2
commit c606da5f8b
3 changed files with 91 additions and 10 deletions
+8 -5
View File
@@ -616,12 +616,15 @@ func register(ctx context.Context, m meta, redirect string) (id, secret, method
}
method = "none"
if len(m.AuthMethods) > 0 && !contains(m.AuthMethods, "none") {
method = m.AuthMethods[0]
if !contains(m.AuthMethods, "client_secret_post") && !contains(m.AuthMethods, "client_secret_basic") {
return "", "", "", fmt.Errorf("the server's token endpoint takes only %s, which magpie can't do", strings.Join(m.AuthMethods, ", "))
}
if contains(m.AuthMethods, "client_secret_post") {
// the metadata's order is no preference: one listing private_key_jwt
// first, which magpie can't do, may take a secret too
switch {
case contains(m.AuthMethods, "client_secret_post"):
method = "client_secret_post"
case contains(m.AuthMethods, "client_secret_basic"):
method = "client_secret_basic"
default:
return "", "", "", fmt.Errorf("the server's token endpoint takes only %s, which magpie can't do", strings.Join(m.AuthMethods, ", "))
}
}
reg := map[string]any{
+31
View File
@@ -210,6 +210,37 @@ func TestRenewRefusedForGood(t *testing.T) {
}
}
// A token endpoint that takes no public client and no client_secret_post
// is registered with and signed in to by client_secret_basic, whatever it
// lists first (RFC 8414's list has no order of preference): one listing
// private_key_jwt or tls_client_auth before it was asked to register
// magpie for that, which it can't do, and turned the sign-in away.
func TestSignInBySecretBasic(t *testing.T) {
home(t)
for _, methods := range [][]string{
{"private_key_jwt", "client_secret_basic"},
{"tls_client_auth", "client_secret_jwt", "client_secret_basic"},
{"client_secret_basic"},
} {
f := mcpauthtest.New(t)
f.AuthMethods = methods
f.SignIn(t, "neon")
if got := f.Registered[len(f.Registered)-1]["token_endpoint_auth_method"]; got != "client_secret_basic" {
t.Fatalf("%v: registered for %v", methods, got)
}
if r, _ := mcpauth.Get("neon"); r.AuthMethod != "client_secret_basic" || r.Secret == "" || r.Access == "" || f.Exchanged != 1 {
t.Fatalf("%v: record %+v, exchanged %d", methods, r, f.Exchanged)
}
}
// client_secret_post is still taken over basic where both are
f := mcpauthtest.New(t)
f.AuthMethods = []string{"private_key_jwt", "client_secret_basic", "client_secret_post"}
f.SignIn(t, "kc")
if r, _ := mcpauth.Get("kc"); r.AuthMethod != "client_secret_post" || f.Exchanged != 1 {
t.Fatalf("both: record %+v", r)
}
}
// An authorization server that is an OpenID provider, as Vercel's is, gives
// a refresh token only to a sign-in that asks for offline_access, while the
// server names only "openid": magpie asks for it too when the authorization
+52 -5
View File
@@ -48,9 +48,16 @@ type Fake struct {
// and a refresh token is given only to a sign-in that asked for
// offline_access, which the authorization server's metadata offers
OpenID bool
// AuthMethods are the token endpoint's client authentications, as its
// metadata lists them (none when unset): a registration asking for
// another is refused (RFC 7591's invalid_client_metadata), and a client
// registered for a secret is given one and has to send it as it said
AuthMethods []string
codes map[string]codeGrant
valid map[string]bool // access tokens
secrets map[string]string // client id → secret, of those that have one
methods map[string]string // client id → its token_endpoint_auth_method
valid map[string]bool // access tokens
refresh map[string]bool
n int
}
@@ -59,7 +66,14 @@ type codeGrant struct{ challenge, client, redirect, resource, scope string }
// New starts the server.
func New(t *testing.T) *Fake {
f := &Fake{codes: map[string]codeGrant{}, valid: map[string]bool{}, refresh: map[string]bool{}, ExpiresIn: 3600}
f := &Fake{codes: map[string]codeGrant{}, valid: map[string]bool{}, refresh: map[string]bool{}, ExpiresIn: 3600,
secrets: map[string]string{}, methods: map[string]string{}}
methods := func() []string {
if len(f.AuthMethods) == 0 {
return []string{"none"}
}
return f.AuthMethods
}
mux := http.NewServeMux()
f.Server = httptest.NewServer(mux)
t.Cleanup(f.Server.Close)
@@ -80,7 +94,7 @@ func New(t *testing.T) *Fake {
m := map[string]any{
"issuer": issuer, "authorization_endpoint": issuer + "/authorize", "token_endpoint": issuer + "/token",
"registration_endpoint": issuer + "/register", "code_challenge_methods_supported": []string{"S256"},
"token_endpoint_auth_methods_supported": []string{"none"},
"token_endpoint_auth_methods_supported": methods(),
}
if f.OpenID {
m["scopes_supported"] = []string{"openid", "email", "offline_access", "profile"}
@@ -91,12 +105,45 @@ func New(t *testing.T) *Fake {
register := func(w http.ResponseWriter, r *http.Request) {
var reg map[string]any
json.NewDecoder(r.Body).Decode(&reg)
method, _ := reg["token_endpoint_auth_method"].(string)
f.mu.Lock()
f.Registered = append(f.Registered, reg)
id := fmt.Sprintf("client-%d", len(f.Registered))
// private_key_jwt and tls_client_auth need a key set or a
// certificate the registration didn't give
ok := slices.Contains(methods(), method) && method != "private_key_jwt" && method != "tls_client_auth"
out := map[string]any{"client_id": id, "redirect_uris": reg["redirect_uris"], "token_endpoint_auth_method": method}
if ok && method != "none" {
f.secrets[id] = "secret-" + id
out["client_secret"] = f.secrets[id]
}
f.methods[id] = method
f.mu.Unlock()
if !ok {
w.WriteHeader(http.StatusBadRequest)
json.NewEncoder(w).Encode(map[string]string{"error": "invalid_client_metadata", "error_description": "token_endpoint_auth_method " + method + " can't be registered without jwks or a certificate, or isn't offered"})
return
}
w.WriteHeader(http.StatusCreated)
json.NewEncoder(w).Encode(map[string]any{"client_id": id, "redirect_uris": reg["redirect_uris"]})
json.NewEncoder(w).Encode(out)
}
// client is the client a token request authenticates as, "" when it
// doesn't as it registered to
client := func(r *http.Request) string {
id, secret, basic := r.BasicAuth()
if basic {
id, _ = url.QueryUnescape(id)
secret, _ = url.QueryUnescape(secret)
} else {
id, secret = r.PostForm.Get("client_id"), r.PostForm.Get("client_secret")
}
switch m := f.methods[id]; {
case m == "client_secret_basic" && (!basic || secret != f.secrets[id]),
m == "client_secret_post" && (basic || secret != f.secrets[id]),
m == "client_secret_jwt" && r.PostForm.Get("client_assertion") == "":
return ""
}
return id
}
authorize := func(w http.ResponseWriter, r *http.Request) {
q := r.URL.Query()
@@ -130,7 +177,7 @@ func New(t *testing.T) *Fake {
g, ok := f.codes[r.PostForm.Get("code")]
delete(f.codes, r.PostForm.Get("code"))
sum := sha256.Sum256([]byte(r.PostForm.Get("code_verifier")))
if !ok || base64.RawURLEncoding.EncodeToString(sum[:]) != g.challenge || r.PostForm.Get("client_id") != g.client ||
if !ok || base64.RawURLEncoding.EncodeToString(sum[:]) != g.challenge || client(r) != g.client ||
r.PostForm.Get("redirect_uri") != g.redirect || r.PostForm.Get("resource") != g.resource {
fail("invalid_grant")
return