feat(36-01): add TokenScope.Group and AdminAPITokenScope plugin_id/group metadata

This commit is contained in:
Jakub Zych
2026-11-24 19:28:59 +01:00
parent 7644786d62
commit cb259ba910
4 changed files with 92 additions and 6 deletions

View File

@@ -3,6 +3,7 @@ package cabana
import ( import (
"context" "context"
"net/http" "net/http"
"slices"
"strconv" "strconv"
"strings" "strings"
"time" "time"
@@ -17,6 +18,8 @@ type AdminAPITokenScope struct {
Description string `json:"description"` Description string `json:"description"`
ReadOnly bool `json:"read_only"` ReadOnly bool `json:"read_only"`
Known bool `json:"known"` Known bool `json:"known"`
PluginID string `json:"plugin_id"`
Group string `json:"group"`
} }
// AdminAPIToken is one personal API token row in list/revoke responses. // AdminAPIToken is one personal API token row in list/revoke responses.
@@ -180,7 +183,7 @@ func (s *service) accountTokenScopes(w http.ResponseWriter, r *http.Request) {
if !scopeOverlaps(s.reg, principal, scope.Name) { if !scopeOverlaps(s.reg, principal, scope.Name) {
continue continue
} }
out = append(out, mapCompiledScope(r.Context(), tr, scope)) out = append(out, mapCompiledScope(r.Context(), tr, s.reg, scope))
} }
} }
WriteData(w, http.StatusOK, out, nil) WriteData(w, http.StatusOK, out, nil)
@@ -264,13 +267,13 @@ func mapAdminAPIToken(ctx context.Context, tr *phrasebook.Translator, reg *Regis
func mapStoredScope(ctx context.Context, tr *phrasebook.Translator, reg *Registry, name string) AdminAPITokenScope { func mapStoredScope(ctx context.Context, tr *phrasebook.Translator, reg *Registry, name string) AdminAPITokenScope {
if reg != nil && reg.tokenScopes != nil { if reg != nil && reg.tokenScopes != nil {
if scope, ok := reg.tokenScopes.byName[name]; ok { if scope, ok := reg.tokenScopes.byName[name]; ok {
return mapCompiledScope(ctx, tr, scope) return mapCompiledScope(ctx, tr, reg, scope)
} }
} }
return AdminAPITokenScope{Name: name, Label: name, Description: "", ReadOnly: false, Known: false} return AdminAPITokenScope{Name: name, Label: name, Description: "", ReadOnly: false, Known: false}
} }
func mapCompiledScope(ctx context.Context, tr *phrasebook.Translator, scope *compiledTokenScope) AdminAPITokenScope { func mapCompiledScope(ctx context.Context, tr *phrasebook.Translator, reg *Registry, scope *compiledTokenScope) AdminAPITokenScope {
label := scope.Label label := scope.Label
desc := scope.Description desc := scope.Description
if strings.Contains(label, "::") { if strings.Contains(label, "::") {
@@ -295,9 +298,88 @@ func mapCompiledScope(ctx context.Context, tr *phrasebook.Translator, scope *com
Description: desc, Description: desc,
ReadOnly: readOnly, ReadOnly: readOnly,
Known: true, Known: true,
PluginID: scope.PluginID,
Group: resolveScopeGroup(ctx, tr, reg, scope),
} }
} }
func resolveScopeGroup(ctx context.Context, tr *phrasebook.Translator, reg *Registry, scope *compiledTokenScope) string {
if scope == nil || scope.PluginID == "" {
return ""
}
if scope.Group != "" {
if strings.Contains(scope.Group, "::") {
if s := strings.TrimSpace(resolveScopePhrase(ctx, tr, scope.Group, scope.Group)); s != "" {
return s
}
}
return scope.Group
}
// Fallback 1: <pluginID>::lang.plugin.name
pluginNameKey := scope.PluginID + "::lang.plugin.name"
if tr != nil && tr.Has(pluginNameKey) {
if s := strings.TrimSpace(tr.Get(ctx, pluginNameKey, nil)); s != "" && s != pluginNameKey {
return s
}
}
// Fallback 2: unanimous non-empty Tab across matched reg.permissions
if reg != nil && len(reg.permissions) > 0 {
codes := reg.permissionOrder
if len(codes) == 0 {
codes = make([]string, 0, len(reg.permissions))
for code := range reg.permissions {
codes = append(codes, code)
}
slices.Sort(codes)
}
var firstTab string
matched := false
unanimous := true
for _, code := range codes {
if !scopeMatchesPermission(scope.Permissions, code) {
continue
}
p, ok := reg.permissions[code]
if !ok {
continue
}
tab := strings.TrimSpace(p.Tab)
if tab == "" {
unanimous = false
break
}
if strings.Contains(tab, "::") {
tab = strings.TrimSpace(resolveScopePhrase(ctx, tr, tab, tab))
}
if tab == "" {
unanimous = false
break
}
if !matched {
firstTab = tab
matched = true
} else if tab != firstTab {
unanimous = false
break
}
}
if matched && unanimous && firstTab != "" {
return firstTab
}
}
// Fallback 3: raw PluginID
return scope.PluginID
}
func scopeMatchesPermission(patterns []string, code string) bool {
for _, pattern := range patterns {
if scopeMatch(pattern, code) != matchNone {
return true
}
}
return false
}
func resolveScopePhrase(ctx context.Context, tr *phrasebook.Translator, key, fallback string) string { func resolveScopePhrase(ctx context.Context, tr *phrasebook.Translator, key, fallback string) string {
if tr != nil && tr.Has(key) { if tr != nil && tr.Has(key) {
if s := tr.Get(ctx, key, nil); s != "" && s != key { if s := tr.Get(ctx, key, nil); s != "" && s != key {

View File

@@ -144,7 +144,7 @@ func (s *service) mcpConsentShow(w http.ResponseWriter, r *http.Request) {
tr := s.translator() tr := s.translator()
scopes := []AdminAPITokenScope{} scopes := []AdminAPITokenScope{}
for _, scope := range s.grantableConsentScopes(principal, req.Scopes) { for _, scope := range s.grantableConsentScopes(principal, req.Scopes) {
scopes = append(scopes, mapCompiledScope(ctx, tr, scope)) scopes = append(scopes, mapCompiledScope(ctx, tr, s.reg, scope))
} }
expires := "" expires := ""
if !req.ExpiresAt.IsZero() { if !req.ExpiresAt.IsZero() {

View File

@@ -23,6 +23,7 @@ type compiledTokenScope struct {
Name string Name string
Label string Label string
Description string Description string
Group string
Permissions []string Permissions []string
Methods map[string]bool Methods map[string]bool
Full bool // framework scopes * and admin:read Full bool // framework scopes * and admin:read
@@ -90,6 +91,7 @@ func compileTokenScopes(reg *Registry, plugins []party.Plugin) error {
Name: scope.Name, Name: scope.Name,
Label: scope.Label, Label: scope.Label,
Description: scope.Description, Description: scope.Description,
Group: strings.TrimSpace(scope.Group),
Permissions: perms, Permissions: perms,
Methods: methods, Methods: methods,
PluginID: plugin.ID(), PluginID: plugin.ID(),
@@ -166,7 +168,7 @@ func validateTokenScopeMessageKeys(reg *Registry, tr *phrasebook.Translator) err
if who == "" { if who == "" {
who = "framework" who = "framework"
} }
for _, text := range []string{scope.Label, scope.Description} { for _, text := range []string{scope.Label, scope.Description, scope.Group} {
if strings.Contains(text, "::") && !tr.Has(text) { if strings.Contains(text, "::") && !tr.Has(text) {
return fmt.Errorf("cabana: token scope %s (%s) names missing phrase key %s", scope.Name, who, text) return fmt.Errorf("cabana: token scope %s (%s) names missing phrase key %s", scope.Name, who, text)
} }

View File

@@ -862,11 +862,13 @@ type HasPermissions interface {
} }
// TokenScope is one personal API token scope a plugin registers at boot. // TokenScope is one personal API token scope a plugin registers at boot.
// Label and Description are phrase keys (containing "::") or literal text. // Label, Description and the optional Group are phrase keys (containing "::")
// or literal text; an empty Group falls back to plugin metadata in cabana.
type TokenScope struct { type TokenScope struct {
Name string Name string
Label string Label string
Description string Description string
Group string
Permissions []string Permissions []string
Methods []string Methods []string
} }