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

+60 -9
View File
@@ -2,6 +2,7 @@ package web
import (
"slices"
"strconv"
"git.zb-server.de/ZB-Server/uptimesebi/internal/auth"
"git.zb-server.de/ZB-Server/uptimesebi/internal/i18n"
@@ -257,6 +258,7 @@ type Token struct {
Prefix string
UserEmail string
Permissions []string
CreatedAt string
ExpiresAt string
LastUsedAt string
}
@@ -265,12 +267,25 @@ type Token struct {
type TokensView struct {
Tokens []Token
Grantable []string
// ShowOwner is set when the viewer oversees all tokens of the org.
ShowOwner bool
// MaxDays is the org's token lifetime limit, 0 if none.
MaxDays int
// ExpiryDays are the lifetimes offered when creating a token; 0 means
// no expiry.
ExpiryDays []int
}
templ TokensPage(p Page, me Me, v TokensView) {
@consoleShell(p, me) {
@settingsHeading("tokens.title", "tokens.intro")
if v.ShowOwner {
@settingsHeading("tokens.title", "tokens.intro_all")
} else {
@settingsHeading("tokens.title", "tokens.intro")
}
if v.MaxDays > 0 {
<p class="-mt-4 mb-8 text-sm">{ i18n.T(ctx, "tokens.policy", "Count", v.MaxDays) }</p>
}
if len(v.Tokens) > 0 {
<table class="mb-12 w-full text-left text-sm">
<thead class="border-b border-rule text-xs uppercase tracking-wider text-muted">
@@ -279,6 +294,8 @@ templ TokensPage(p Page, me Me, v TokensView) {
if v.ShowOwner {
<th class="py-2 pr-4 font-medium">{ i18n.T(ctx, "tokens.owner") }</th>
}
<th class="py-2 pr-4 font-medium">{ i18n.T(ctx, "tokens.permissions") }</th>
<th class="py-2 pr-4 font-medium">{ i18n.T(ctx, "tokens.created") }</th>
<th class="py-2 pr-4 font-medium">{ i18n.T(ctx, "tokens.expires") }</th>
<th class="py-2 pr-4 font-medium">{ i18n.T(ctx, "tokens.last_used") }</th>
<th class="py-2"></th>
@@ -286,13 +303,21 @@ templ TokensPage(p Page, me Me, v TokensView) {
</thead>
<tbody class="divide-y divide-rule">
for _, t := range v.Tokens {
<tr>
<tr class="align-top">
<td class="py-2.5 pr-4">{ t.Name } <span class="block font-mono text-xs text-muted">{ t.Prefix }…</span></td>
if v.ShowOwner {
<td class="py-2.5 pr-4">{ t.UserEmail }</td>
}
<td class="py-2.5 pr-4 font-mono text-xs">{ t.ExpiresAt }</td>
<td class="py-2.5 pr-4 font-mono text-xs">{ t.LastUsedAt }</td>
<td class="py-2.5 pr-4">
<ul class="flex max-w-xs flex-wrap gap-1 text-xs">
for _, perm := range t.Permissions {
<li class="border border-rule px-1 py-px">{ i18n.T(ctx, "perm."+perm) }</li>
}
</ul>
</td>
<td class="py-2.5 pr-4 whitespace-nowrap font-mono text-xs">{ t.CreatedAt }</td>
<td class="py-2.5 pr-4 whitespace-nowrap font-mono text-xs">{ t.ExpiresAt }</td>
<td class="py-2.5 pr-4 whitespace-nowrap font-mono text-xs">{ t.LastUsedAt }</td>
<td class="py-2.5 text-right">
<button
type="button"
@@ -315,10 +340,13 @@ templ TokensPage(p Page, me Me, v TokensView) {
<label class="mb-4 block">
<span class="mb-1 block text-xs font-medium uppercase tracking-wider text-muted">{ i18n.T(ctx, "tokens.expires") }</span>
<select name="expires_in_days" data-number class="w-full border border-rule bg-transparent px-2 py-1.5 text-sm">
<option value="30">{ i18n.T(ctx, "tokens.days", "Count", 30) }</option>
<option value="90" selected>{ i18n.T(ctx, "tokens.days", "Count", 90) }</option>
<option value="365">{ i18n.T(ctx, "tokens.days", "Count", 365) }</option>
<option value="0">{ i18n.T(ctx, "tokens.never") }</option>
for i, d := range v.ExpiryDays {
if d == 0 {
<option value="0">{ i18n.T(ctx, "tokens.never") }</option>
} else {
<option value={ strconv.Itoa(d) } selected?={ i == 0 }>{ i18n.T(ctx, "tokens.days", "Count", d) }</option>
}
}
</select>
</label>
@submit(i18n.T(ctx, "tokens.create"))
@@ -335,6 +363,8 @@ templ TokensPage(p Page, me Me, v TokensView) {
type OrgSettings struct {
RequireTOTP bool
RequireSSO bool
// TokenMaxDays is 0 when API token lifetime is unlimited.
TokenMaxDays int
}
templ OrgSettingsPage(p Page, me Me, v OrgSettings) {
@@ -347,7 +377,21 @@ templ OrgSettingsPage(p Page, me Me, v OrgSettings) {
@checkbox(i18n.T(ctx, "org.require_totp"), "require_totp", v.RequireTOTP)
<p class="mb-4 ml-6 text-xs text-muted">{ i18n.T(ctx, "org.require_totp_hint") }</p>
@checkbox(i18n.T(ctx, "org.require_sso"), "require_sso", v.RequireSSO)
<p class="mb-4 ml-6 text-xs text-muted">{ i18n.T(ctx, "org.require_sso_hint") }</p>
<p class="mb-6 ml-6 text-xs text-muted">{ i18n.T(ctx, "org.require_sso_hint") }</p>
<label class="mb-1 block">
<span class="mb-1 block text-xs font-medium uppercase tracking-wider text-muted">{ i18n.T(ctx, "org.token_max_days") }</span>
<input
type="number"
name="api_token_max_days"
data-number
min="0"
max="3650"
value={ maxDaysValue(v.TokenMaxDays) }
placeholder={ i18n.T(ctx, "org.token_max_days_none") }
class="w-40 border border-rule bg-transparent px-2.5 py-1.5 font-mono text-sm outline-none focus:border-ink"
/>
</label>
<p class="mb-6 text-xs text-muted">{ i18n.T(ctx, "org.token_max_days_hint") }</p>
@submit(i18n.T(ctx, "common.save"))
</form>
</section>
@@ -394,3 +438,10 @@ templ InvitationPage(p Page, v Invitation) {
}
}
}
func maxDaysValue(d int) string {
if d == 0 {
return ""
}
return strconv.Itoa(d)
}