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

+10 -5
View File
@@ -98,10 +98,10 @@ type tokenView struct {
func (s *Server) listTokens(c *gin.Context) {
p := current(c)
ctx := c.Request.Context()
// Members see their own tokens; members.manage reveals everyone's so
// tokens of departing staff can be revoked.
// Members see their own tokens; tokens.manage reveals everyone's so the
// owner can audit and revoke them.
var filter *uuid.UUID
if !p.Grants.Can(authz.MembersManage) {
if !p.Grants.Can(authz.TokensManage) {
filter = &p.UserID
}
var rows []store.ListAPITokensRow
@@ -127,7 +127,8 @@ func (s *Server) listTokens(c *gin.Context) {
type createTokenRequest struct {
Name string `json:"name" binding:"required,max=100"`
Permissions []string `json:"permissions" binding:"required,min=1,max=50,dive,max=50"`
// ExpiresInDays of 0 creates a token that does not expire.
// ExpiresInDays of 0 creates a token that does not expire, unless the
// org limits token lifetime.
ExpiresInDays int `json:"expires_in_days" binding:"min=0,max=3650"`
}
@@ -146,6 +147,10 @@ func (s *Server) createToken(c *gin.Context) {
fail(c, http.StatusForbidden, "permission_escalation")
return
}
if limit := p.Org.ApiTokenMaxDays; limit != nil && (req.ExpiresInDays == 0 || req.ExpiresInDays > int(*limit)) {
fail(c, http.StatusUnprocessableEntity, "token_lifetime_exceeds_policy", "Count", int(*limit))
return
}
var expires *time.Time
if req.ExpiresInDays > 0 {
t := time.Now().AddDate(0, 0, req.ExpiresInDays)
@@ -187,7 +192,7 @@ func (s *Server) deleteToken(c *gin.Context) {
if err != nil {
return err
}
if row.UserID != p.UserID && !p.Grants.Can(authz.MembersManage) {
if row.UserID != p.UserID && !p.Grants.Can(authz.TokensManage) {
return nil
}
found = true