From cb259ba910d6004f2b436c9091ca82c8c165c313 Mon Sep 17 00:00:00 2001 From: Jakub Zych Date: Tue, 24 Nov 2026 19:28:59 +0100 Subject: [PATCH] feat(36-01): add TokenScope.Group and AdminAPITokenScope plugin_id/group metadata --- modules/cabana/account_tokens.go | 88 ++++++++++++++++++++++++++++++-- modules/cabana/mcp_consent.go | 2 +- modules/cabana/token_scopes.go | 4 +- modules/pact/capabilities.go | 4 +- 4 files changed, 92 insertions(+), 6 deletions(-) diff --git a/modules/cabana/account_tokens.go b/modules/cabana/account_tokens.go index d686f851..4d113530 100644 --- a/modules/cabana/account_tokens.go +++ b/modules/cabana/account_tokens.go @@ -3,6 +3,7 @@ package cabana import ( "context" "net/http" + "slices" "strconv" "strings" "time" @@ -17,6 +18,8 @@ type AdminAPITokenScope struct { Description string `json:"description"` ReadOnly bool `json:"read_only"` Known bool `json:"known"` + PluginID string `json:"plugin_id"` + Group string `json:"group"` } // 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) { 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) @@ -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 { if reg != nil && reg.tokenScopes != nil { 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} } -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 desc := scope.Description if strings.Contains(label, "::") { @@ -295,9 +298,88 @@ func mapCompiledScope(ctx context.Context, tr *phrasebook.Translator, scope *com Description: desc, ReadOnly: readOnly, 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: ::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 { if tr != nil && tr.Has(key) { if s := tr.Get(ctx, key, nil); s != "" && s != key { diff --git a/modules/cabana/mcp_consent.go b/modules/cabana/mcp_consent.go index a65ad5cc..aaa6b500 100644 --- a/modules/cabana/mcp_consent.go +++ b/modules/cabana/mcp_consent.go @@ -144,7 +144,7 @@ func (s *service) mcpConsentShow(w http.ResponseWriter, r *http.Request) { tr := s.translator() scopes := []AdminAPITokenScope{} 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 := "" if !req.ExpiresAt.IsZero() { diff --git a/modules/cabana/token_scopes.go b/modules/cabana/token_scopes.go index 0f1f7554..7dda7565 100644 --- a/modules/cabana/token_scopes.go +++ b/modules/cabana/token_scopes.go @@ -23,6 +23,7 @@ type compiledTokenScope struct { Name string Label string Description string + Group string Permissions []string Methods map[string]bool Full bool // framework scopes * and admin:read @@ -90,6 +91,7 @@ func compileTokenScopes(reg *Registry, plugins []party.Plugin) error { Name: scope.Name, Label: scope.Label, Description: scope.Description, + Group: strings.TrimSpace(scope.Group), Permissions: perms, Methods: methods, PluginID: plugin.ID(), @@ -166,7 +168,7 @@ func validateTokenScopeMessageKeys(reg *Registry, tr *phrasebook.Translator) err if who == "" { 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) { return fmt.Errorf("cabana: token scope %s (%s) names missing phrase key %s", scope.Name, who, text) } diff --git a/modules/pact/capabilities.go b/modules/pact/capabilities.go index 0f8792ce..655be1bd 100644 --- a/modules/pact/capabilities.go +++ b/modules/pact/capabilities.go @@ -862,11 +862,13 @@ type HasPermissions interface { } // 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 { Name string Label string Description string + Group string Permissions []string Methods []string }