feat: plugin-declared member token scopes (pact.HasFrontendTokenScopes)

- CompileAPITokenScopes builds the catalog from the built-ins and the
  activated plugins' declarations; a bad name, a redeclared built-in, a
  duplicate, an empty label or a missing phrase key fails boot
- POST /tokens, user:token:create and MintAPITokenFor check the
  application's own catalog (422 text unchanged); MintAPIToken keeps its
  signature with a process-wide fallback for declared scopes
- GET /_user/api/v1/tokens/scopes lists the built-ins and the declared
  scopes with translated labels
- built-in scope labels in en and pl; TestMemberTokenScopesSmoke
This commit is contained in:
Jakub Zych
2026-10-23 17:00:12 +02:00
parent 970719ce75
commit ee5c096d0d
11 changed files with 470 additions and 16 deletions

View File

@@ -18,6 +18,9 @@ import (
const DefaultAPITokenPrefix = "usr_"
// DefaultAPITokenScopes is what a token minted without scopes carries, and
// AllowedAPITokenScopes are the built-in member token scopes. Plugins add
// their own through pact.HasFrontendTokenScopes (APITokenScopeCatalog).
var (
DefaultAPITokenScopes = []string{"read"}
AllowedAPITokenScopes = []string{"read", "write", "ai"}
@@ -44,8 +47,27 @@ func APITokenPrefix(app *backpack.App) (string, error) {
}
// MintAPIToken builds a token record and returns its one-time clear-text
// secret. A nil scopes slice receives DefaultAPITokenScopes.
// secret. A nil scopes slice receives DefaultAPITokenScopes. It accepts the
// built-in scopes (AllowedAPITokenScopes) and, as a fallback for existing
// callers, every member token scope a catalog compiled in this process
// declared (CompileAPITokenScopes). New code calls MintAPITokenFor, which
// checks the scopes against the application's own catalog.
func MintAPIToken(prefix string, userID uint, name string, scopes []string, expiresAt *time.Time, collectionIDs []uint) (string, *models.APIToken, error) {
return mintAPIToken(prefix, userID, name, scopes, expiresAt, collectionIDs, func(scope string) bool {
return slices.Contains(AllowedAPITokenScopes, scope) || declaredScopes.has(scope)
})
}
// MintAPITokenFor is MintAPIToken with the scopes checked against app's
// member token scope catalog (APITokenScopesFor): the built-ins and the
// scopes app's activated plugins declared through
// pact.HasFrontendTokenScopes. A scope another application in the same
// process declared is refused.
func MintAPITokenFor(app *backpack.App, prefix string, userID uint, name string, scopes []string, expiresAt *time.Time, collectionIDs []uint) (string, *models.APIToken, error) {
return mintAPIToken(prefix, userID, name, scopes, expiresAt, collectionIDs, APITokenScopesFor(app).Allowed)
}
func mintAPIToken(prefix string, userID uint, name string, scopes []string, expiresAt *time.Time, collectionIDs []uint, allowed func(string) bool) (string, *models.APIToken, error) {
name = strings.TrimSpace(name)
if userID == 0 || name == "" || len(name) > 255 {
return "", nil, fmt.Errorf("a user and token name of 1 to 255 characters are required")
@@ -59,7 +81,7 @@ func MintAPIToken(prefix string, userID uint, name string, scopes []string, expi
scopes = slices.Clone(scopes)
}
for _, scope := range scopes {
if !slices.Contains(AllowedAPITokenScopes, scope) {
if !allowed(scope) {
return "", nil, fmt.Errorf("scope %q is not allowed", scope)
}
}