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

+15 -2
View File
@@ -429,6 +429,9 @@ func (s *Server) roleResult(c *gin.Context, err error, code string, ok func()) {
type policyRequest struct {
RequireTOTP bool `json:"require_totp"`
RequireSSO bool `json:"require_sso"`
// APITokenMaxDays limits how long API tokens stay valid; 0 or null
// means no limit.
APITokenMaxDays *int `json:"api_token_max_days" binding:"omitempty,min=0,max=3650"`
}
func (s *Server) updatePolicy(c *gin.Context) {
@@ -457,8 +460,18 @@ func (s *Server) updatePolicy(c *gin.Context) {
return errNoSSOProvider
}
}
var maxDays *int32
if req.APITokenMaxDays != nil && *req.APITokenMaxDays > 0 {
maxDays = ptr(int32(*req.APITokenMaxDays))
}
var err error
org, err = q.UpdateOrgPolicy(ctx, store.UpdateOrgPolicyParams{ID: p.Org.ID, RequireTotp: req.RequireTOTP, RequireSso: req.RequireSSO})
org, err = q.UpdateOrgPolicy(ctx, store.UpdateOrgPolicyParams{
ID: p.Org.ID, RequireTotp: req.RequireTOTP, RequireSso: req.RequireSSO, ApiTokenMaxDays: maxDays,
})
if err != nil || maxDays == nil {
return err
}
_, err = q.ClampAPITokenExpiry(ctx, store.ClampAPITokenExpiryParams{OrgID: p.Org.ID, MaxDays: *maxDays})
return err
})
if errors.Is(err, errNoSSOProvider) {
@@ -469,7 +482,7 @@ func (s *Server) updatePolicy(c *gin.Context) {
internalError(c, err)
return
}
c.JSON(http.StatusOK, gin.H{"require_totp": org.RequireTotp, "require_sso": org.RequireSso})
c.JSON(http.StatusOK, gin.H{"require_totp": org.RequireTotp, "require_sso": org.RequireSso, "api_token_max_days": org.ApiTokenMaxDays})
}
var errNoSSOProvider = errors.New("api: no enabled sso provider")