feat(36-01): add TokenScope.Group and AdminAPITokenScope plugin_id/group metadata
This commit is contained in:
@@ -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: <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 {
|
||||
if tr != nil && tr.Has(key) {
|
||||
if s := tr.Get(ctx, key, nil); s != "" && s != key {
|
||||
|
||||
@@ -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() {
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user