feat(api): let owners audit all api tokens and cap their lifetime

Holders of tokens.manage list every token of the org with owner,
permissions, creation, expiry and last use, and can revoke any of them.
A maximum lifetime rejects tokens that would outlive it and shortens
existing ones when it is set.

Closes #48

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
This commit is contained in:
Sebastian UnterschützandClaude Opus 5.5 committed 2026-11-22 00:55:14 +01:00
1 parent da128c5081
commit b52698ffbc
9 files changed
+674 -296

No files matched your search

+65
View File
@@ -209,3 +209,68 @@ func TestPermissions(t *testing.T) {
}
})
}
func TestTokenOversightAndLifetimePolicy(t *testing.T) {
e := newEnv(t)
domain := uniq("t") + ".test"
owner := e.browser(t)
owner.signup("owner@"+domain, uniq("tok-"))
editor := e.joinVia(t, owner.invite("editor@"+domain, owner.roleID("editor")))
st, body := editor.do("POST", "/api/v1/org/tokens", map[string]any{"name": "forever", "permissions": []string{"monitors.read"}})
expect(t, "editor creates non-expiring token", st, body, http.StatusCreated, "")
editorTokenID := body["id"].(string)
api := &browser{t: t, base: editor.base, hc: e.srv.Client(), bearer: body["token"].(string)}
owner.do("POST", "/api/v1/org/tokens", map[string]any{"name": "own", "permissions": []string{"monitors.read"}})
tokenNames := func(b *browser) []string {
_, body := b.do("GET", "/api/v1/org/tokens", nil)
var names []string
for _, tk := range body["tokens"].([]any) {
names = append(names, tk.(map[string]any)["name"].(string))
}
return names
}
if got := tokenNames(owner); len(got) != 2 {
t.Errorf("owner sees tokens %v, want both", got)
}
if got := tokenNames(editor); len(got) != 1 || got[0] != "forever" {
t.Errorf("editor sees tokens %v, want only own", got)
}
st, body = owner.do("PUT", "/api/v1/org/policy", map[string]any{"api_token_max_days": 30})
expect(t, "set lifetime policy", st, body, http.StatusOK, "")
_, list := owner.do("GET", "/api/v1/org/tokens", nil)
for _, tk := range list["tokens"].([]any) {
tk := tk.(map[string]any)
exp, _ := time.Parse(time.RFC3339Nano, fmtString(tk["expires_at"]))
if exp.IsZero() || exp.After(time.Now().Add(31*24*time.Hour)) {
t.Errorf("token %v expires_at = %v, want within 30 days", tk["name"], tk["expires_at"])
}
}
for _, days := range []int{0, 31} {
st, body = editor.do("POST", "/api/v1/org/tokens", map[string]any{"name": "x", "permissions": []string{"monitors.read"}, "expires_in_days": days})
expect(t, "token beyond policy", st, body, http.StatusUnprocessableEntity, "token_lifetime_exceeds_policy")
}
st, body = editor.do("POST", "/api/v1/org/tokens", map[string]any{"name": "ok", "permissions": []string{"monitors.read"}, "expires_in_days": 30})
expect(t, "token within policy", st, body, http.StatusCreated, "")
st, body = owner.do("DELETE", "/api/v1/org/tokens/"+editorTokenID, nil)
expect(t, "owner revokes editor token", st, body, http.StatusNoContent, "")
st, body = api.do("GET", "/api/v1/org/tokens", nil)
expect(t, "revoked token", st, body, http.StatusUnauthorized, "unauthenticated")
st, body = owner.do("PUT", "/api/v1/org/policy", map[string]any{"api_token_max_days": 0})
if st != http.StatusOK || body["api_token_max_days"] != nil {
t.Errorf("clear lifetime policy = %d %v, want 200 with null", st, body)
}
if got := owner.visit("/settings/tokens"); got.Path != "/settings/tokens" {
t.Errorf("tokens page ended at %s", got)
}
}
func fmtString(v any) string {
s, _ := v.(string)
return s
}