feat(36-01): export cabana.TokenScopeCovers and warn on uncovered backend write MCP tools in pier.Mount
This commit is contained in:
@@ -5,6 +5,8 @@ import (
|
||||
"strings"
|
||||
|
||||
"git.golem15.com/golem15/summercms/modules/backpack"
|
||||
"git.golem15.com/golem15/summercms/modules/bouncer"
|
||||
"git.golem15.com/golem15/summercms/modules/party"
|
||||
"git.golem15.com/golem15/summercms/modules/phrasebook"
|
||||
)
|
||||
|
||||
@@ -61,3 +63,52 @@ func TokenScopeLabels(ctx context.Context, app *backpack.App, names []string) []
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func tokenScopeCatalogFor(app *backpack.App, plugins []party.Plugin) *tokenScopeCatalog {
|
||||
if guard := publishedResolverGuard(app); guard != nil && guard.reg != nil && guard.reg.tokenScopes != nil {
|
||||
return guard.reg.tokenScopes
|
||||
}
|
||||
if len(plugins) > 0 {
|
||||
reg, err := compileTokenCatalog(plugins)
|
||||
if err == nil && reg != nil {
|
||||
return reg.tokenScopes
|
||||
}
|
||||
return nil
|
||||
}
|
||||
reg, err := tokenCatalogForApp(app)
|
||||
if err == nil && reg != nil {
|
||||
return reg.tokenScopes
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// TokenScopeCovers reports whether at least one non-"*" personal API token
|
||||
// scope allows method and covers every permission code in permissions. It uses
|
||||
// the activated cabana catalog on app when available, else compiles from
|
||||
// plugins (falling back to party.Activated). Returns false when permissions is
|
||||
// empty on a non-read-only method, when method is unsupported, or when no
|
||||
// non-"*" scope covers every code.
|
||||
func TokenScopeCovers(app *backpack.App, plugins []party.Plugin, method string, permissions []string) bool {
|
||||
cat := tokenScopeCatalogFor(app, plugins)
|
||||
if cat == nil {
|
||||
return false
|
||||
}
|
||||
m := strings.ToUpper(strings.TrimSpace(method))
|
||||
if m == "" {
|
||||
return false
|
||||
}
|
||||
var scopes [][]string
|
||||
for _, scope := range cat.ordered {
|
||||
if scope.Name == scopeAll {
|
||||
continue
|
||||
}
|
||||
if !scope.Methods[m] {
|
||||
continue
|
||||
}
|
||||
if scope.Full {
|
||||
return true
|
||||
}
|
||||
scopes = append(scopes, scope.Permissions)
|
||||
}
|
||||
return scopeCoversAll(&bouncer.ScopeLimit{Scopes: scopes}, permissions)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user