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:
1 parent
da128c5081
commit
b52698ffbc
9 files changed
+674
-296
No files matched your search
+36
-4
@@ -2,6 +2,7 @@ package api
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"slices"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -268,7 +269,11 @@ func (s *Server) ssoSettingsView(c *gin.Context, p *principal, page web.Page, me
|
||||
}
|
||||
|
||||
func (s *Server) orgSettingsView(_ *gin.Context, p *principal, page web.Page, me web.Me) (templ.Component, error) {
|
||||
return web.OrgSettingsPage(page, me, web.OrgSettings{RequireTOTP: p.Org.RequireTotp, RequireSSO: p.Org.RequireSso}), nil
|
||||
v := web.OrgSettings{RequireTOTP: p.Org.RequireTotp, RequireSSO: p.Org.RequireSso}
|
||||
if p.Org.ApiTokenMaxDays != nil {
|
||||
v.TokenMaxDays = int(*p.Org.ApiTokenMaxDays)
|
||||
}
|
||||
return web.OrgSettingsPage(page, me, v), nil
|
||||
}
|
||||
|
||||
func (s *Server) membersView(c *gin.Context, p *principal, page web.Page, me web.Me) (templ.Component, error) {
|
||||
@@ -337,7 +342,7 @@ func (s *Server) rolesView(c *gin.Context, p *principal, page web.Page, me web.M
|
||||
|
||||
func (s *Server) tokensView(c *gin.Context, p *principal, page web.Page, me web.Me) (templ.Component, error) {
|
||||
ctx := c.Request.Context()
|
||||
showAll := p.Grants.Can(authz.MembersManage)
|
||||
showAll := p.Grants.Can(authz.TokensManage)
|
||||
var filter *uuid.UUID
|
||||
if !showAll {
|
||||
filter = &p.UserID
|
||||
@@ -351,10 +356,17 @@ func (s *Server) tokensView(c *gin.Context, p *principal, page web.Page, me web.
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
v := web.TokensView{Grantable: grantable(p), ShowOwner: showAll}
|
||||
v := web.TokensView{Grantable: grantable(p), ShowOwner: showAll, ExpiryDays: expiryChoices(p.Org.ApiTokenMaxDays)}
|
||||
if p.Org.ApiTokenMaxDays != nil {
|
||||
v.MaxDays = int(*p.Org.ApiTokenMaxDays)
|
||||
}
|
||||
never := i18n.T(ctx, "tokens.never")
|
||||
for _, r := range rows {
|
||||
t := web.Token{ID: r.ID.String(), Name: r.Name, Prefix: r.Prefix, UserEmail: r.UserEmail, Permissions: r.Permissions, ExpiresAt: never, LastUsedAt: "–"}
|
||||
t := web.Token{
|
||||
ID: r.ID.String(), Name: r.Name, Prefix: r.Prefix, UserEmail: r.UserEmail,
|
||||
Permissions: authz.NewSet(r.Permissions...).Strings(), CreatedAt: r.CreatedAt.Format(time.DateOnly),
|
||||
ExpiresAt: never, LastUsedAt: "–",
|
||||
}
|
||||
if r.ExpiresAt != nil {
|
||||
t.ExpiresAt = r.ExpiresAt.Format(time.DateOnly)
|
||||
}
|
||||
@@ -366,6 +378,26 @@ func (s *Server) tokensView(c *gin.Context, p *principal, page web.Page, me web.
|
||||
return web.TokensPage(page, me, v), nil
|
||||
}
|
||||
|
||||
// expiryChoices returns the token lifetimes to offer, in days, honoring the
|
||||
// org's limit. 0 stands for no expiry and is only offered without a limit.
|
||||
func expiryChoices(limit *int32) []int {
|
||||
defaults := []int{90, 30, 365, 0}
|
||||
if limit == nil {
|
||||
return defaults
|
||||
}
|
||||
max := int(*limit)
|
||||
var out []int
|
||||
for _, d := range defaults {
|
||||
if d > 0 && d <= max {
|
||||
out = append(out, d)
|
||||
}
|
||||
}
|
||||
if !slices.Contains(out, max) {
|
||||
out = append(out, max)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func toWebMe(m meView) web.Me {
|
||||
out := web.Me{
|
||||
Email: m.Email,
|
||||
|
||||
Reference in new issue
Block a user