Files
summercms/modules/cabana/token_scopes_export.go

115 lines
3.5 KiB
Go

package cabana
import (
"context"
"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"
)
// TokenScopeNames answers the names of the personal API token scope catalog
// in catalog order: *, admin:read, then every plugin's TokenScopes in
// activation order. It compiles the catalog from the activated plugins
// (party.Activated), so it works at plugin Boot, before cabana.Activate. A
// plugin that advertises the same scopes elsewhere (an OAuth server whose
// grants act as administrators, for example) reads them here. The error is
// the catalog's compile error (an invalid or duplicate plugin scope).
func TokenScopeNames(app *backpack.App) ([]string, error) {
reg, err := tokenCatalogForApp(app)
if err != nil {
return nil, err
}
if reg == nil || reg.tokenScopes == nil {
return []string{}, nil
}
out := make([]string, 0, len(reg.tokenScopes.ordered))
for _, scope := range reg.tokenScopes.ordered {
out = append(out, scope.Name)
}
return out, nil
}
// TokenScopeLabels answers the translated label of each name, in input
// order: the label the token screens show for a catalog scope, and the name
// itself for a name the catalog does not know (or when the catalog fails to
// compile). The locale is the one ctx carries.
func TokenScopeLabels(ctx context.Context, app *backpack.App, names []string) []string {
if ctx == nil {
ctx = context.Background()
}
var tr *phrasebook.Translator
if app != nil {
tr, _ = app.Lookup[*phrasebook.Translator]()
}
reg, err := tokenCatalogForApp(app)
out := make([]string, 0, len(names))
for _, name := range names {
label := name
if err == nil && reg != nil && reg.tokenScopes != nil {
if scope, ok := reg.tokenScopes.byName[name]; ok {
label = scope.Label
if strings.Contains(label, "::") {
label = resolveScopePhrase(ctx, tr, label, label)
}
if strings.TrimSpace(label) == "" {
label = name
}
}
}
out = append(out, label)
}
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)
}