mirror of
https://github.com/hanzoai/iam.git
synced 2026-11-19 18:37:16 +00:00
Signed-in clients POST their refresh to /v1/iam/oauth/refresh_token. Only the canonical /v1/iam/oauth/token was registered, so every refresh answered 404 and each one logged a session out — 11,758 of them in three hours across hanzo.ai, hanzo.chat and hanzo.app. Both addresses are now ONE handler value via zip.Alias: the handler dispatches on grant_type, which travels in the body every caller already sends, so the second address is a spelling of the same endpoint rather than a separate grant. Discovery still names only the canonical path, so nothing new is invited onto the half we intend to delete. Only the refresh spelling is registered. The Casdoor-era access_token path and the /v1/iam/userinfo spelling drew no requests at all over the same window, so neither is surface worth carrying. Tests drive a real rotation — fresh verifiable access token, new refresh token, presented token retired — at BOTH addresses, and assert discovery advertises only the canonical one. Removing the alias fails them with the same 404 seen in production, so the behaviour cannot be dropped again without a red suite. Co-authored-by: Hanzo Dev <[email protected]>
84 lines
2.8 KiB
Go
84 lines
2.8 KiB
Go
// Copyright 2026 Hanzo AI, Inc. All rights reserved.
|
|
|
|
package oidc
|
|
|
|
import (
|
|
"context"
|
|
"net/url"
|
|
"testing"
|
|
)
|
|
|
|
// The token endpoint answers at two addresses: the canonical PathToken and
|
|
// PathRefreshToken, where signed-in clients send their refresh. Serving the
|
|
// canonical one alone logs every signed-in session out, so this drives a REAL
|
|
// rotation at each address rather than probing for a status code — routing and
|
|
// behaviour together, which is what a caller actually depends on.
|
|
func TestToken_rotatesAtBothAddresses(t *testing.T) {
|
|
for _, path := range []string{PathToken, PathRefreshToken} {
|
|
t.Run(path, func(t *testing.T) {
|
|
app, db := newServer(t)
|
|
seedApp(t, db, appOpts{clientID: "pub", redirectURIs: []string{testRedirect}, refreshHours: 24})
|
|
seedUser(t, db, "alice", "[email protected]", "pw")
|
|
|
|
tok := grantViaPKCE(t, app, "pub", "openid offline_access")
|
|
presented, _ := tok["refresh_token"].(string)
|
|
if presented == "" {
|
|
t.Fatalf("grant did not mint a refresh token: %v", tok)
|
|
}
|
|
|
|
resp, body := do(t, app, formReq("POST", path, url.Values{
|
|
"grant_type": {"refresh_token"},
|
|
"refresh_token": {presented},
|
|
"client_id": {"pub"},
|
|
}))
|
|
out := decode(t, body)
|
|
if resp.StatusCode != 200 {
|
|
t.Fatalf("refresh at %s: status = %d, body = %v", path, resp.StatusCode, out)
|
|
}
|
|
access, _ := out["access_token"].(string)
|
|
rotated, _ := out["refresh_token"].(string)
|
|
if access == "" || rotated == "" {
|
|
t.Fatalf("refresh at %s did not rotate: %v", path, out)
|
|
}
|
|
if rotated == presented {
|
|
t.Errorf("refresh at %s reused the presented token; rotation is required", path)
|
|
}
|
|
if _, err := verifyToken(context.Background(), db, access); err != nil {
|
|
t.Fatalf("refresh at %s: rotated access token does not verify: %v", path, err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// Discovery names ONE token endpoint. The second address is a spelling a caller
|
|
// holds, not a thing we publish, so advertising it would invite new callers onto
|
|
// the half we intend to delete.
|
|
func TestDiscovery_advertisesOnlyTheCanonicalTokenEndpoint(t *testing.T) {
|
|
app, db := newServer(t)
|
|
_ = db
|
|
|
|
resp, body := do(t, app, formReqNoBody("GET", PathDiscovery))
|
|
if resp.StatusCode != 200 {
|
|
t.Fatalf("discovery: status = %d", resp.StatusCode)
|
|
}
|
|
doc := decode(t, body)
|
|
endpoint, _ := doc["token_endpoint"].(string)
|
|
if endpoint == "" {
|
|
t.Fatalf("discovery published no token_endpoint: %v", doc)
|
|
}
|
|
if got := pathOf(t, endpoint); got != PathToken {
|
|
t.Errorf("discovery token_endpoint = %q, want the canonical %q", got, PathToken)
|
|
}
|
|
}
|
|
|
|
// pathOf reduces an absolute issuer URL to its path so the assertion does not
|
|
// depend on whatever host the test server was given.
|
|
func pathOf(t *testing.T, raw string) string {
|
|
t.Helper()
|
|
u, err := url.Parse(raw)
|
|
if err != nil {
|
|
t.Fatalf("token_endpoint %q is not a URL: %v", raw, err)
|
|
}
|
|
return u.Path
|
|
}
|