diff --git a/internal/mcpauth/mcpauth.go b/internal/mcpauth/mcpauth.go index 349a25f17..7365134fe 100644 --- a/internal/mcpauth/mcpauth.go +++ b/internal/mcpauth/mcpauth.go @@ -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{ diff --git a/internal/mcpauth/mcpauth_test.go b/internal/mcpauth/mcpauth_test.go index f5bb9ee72..ec8878635 100644 --- a/internal/mcpauth/mcpauth_test.go +++ b/internal/mcpauth/mcpauth_test.go @@ -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 diff --git a/internal/mcpauth/mcpauthtest/fake.go b/internal/mcpauth/mcpauthtest/fake.go index b53bdb28e..a2eafb661 100644 --- a/internal/mcpauth/mcpauthtest/fake.go +++ b/internal/mcpauth/mcpauthtest/fake.go @@ -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(®) + 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