Help us shape the future of Go! Take the 2026 Go Developer Survey

tokenperm

package
v0.54.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 8, 2026 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package tokenperm is the catalogue of an API key's permissions that come in LEVELS: what a key may do with one kind of thing beside its files - read it, or read and write it. The first one is `comments` (task #157).

A key's list (model.APIToken.Scopes) holds three kinds of entries:

  • the verbs `read`, `write`, `delete`, `mcp`, `admin` (auth/drivers/apitoken);
  • at most one `root:<storage>://<path>` confinement (package confine);
  • a permission at a level, `<key>:<level>` - `comments:rw`.

Every permission states its default (the maintainer, 2026-10-06)

A key whose list does not name a permission holds that permission's DefaultLevel. That is how every key minted before a permission existed gets it, and how a new key gets it when whoever minted it chose nothing - with no migration and no rewrite of stored rows. The rule for the default:

  • every permission defaults to Read;
  • a super-administrator kind of permission (administration, tenants, server settings) defaults to None: it is never handed to a key that did not ask for it. Superadmin marks one.

"bundan sonra eklenecek tüm permissionlar default read olarak konulur, superadmin tarzı yetkiler sistemde yoksa" - the maintainer's words. Each catalogue entry says its default explicitly, and tokenperm_test.go fails for an entry that does not, for a default that breaks the rule, and for a shipped default that changed (a changed default silently changes every key that never named the permission). docs/RBAC.md → "Permissions with a level" and docs/CONTRIBUTING.md → "Adding a permission" say it for people.

⚠ This package imports nothing of filex's own: the model, the auth chain and the token issuer all read it, and it must not pull any of them in.

Index

Constants

View Source
const Comments = "comments"

Comments is reading a file's or a folder's comments (Read) and adding and deleting them (ReadWrite) - on /api/files/comments, /api/ai/comments and the MCP file_comment_* tools alike (auth.CommentsRead, auth.CommentsWrite).

Variables

This section is empty.

Functions

func Canonical

func Canonical(levels map[string]Level) []string

Canonical is the entries a list stores for levels: one `<key>:<level>` per permission whose level is not its default, in catalogue order. A permission at its default is not written - it reads the same, and a key minted before the permission existed then looks exactly like one minted after it.

func LevelsIn

func LevelsIn(scopes string) map[string]Level

LevelsIn is LevelIn for every permission of the catalogue: what the token lists answer beside `scopes` (`"permissions": {"comments": "read"}`), so a screen draws a key's levels without a copy of the default rule.

func Replace

func Replace(scopes string, changes map[string]Level) string

Replace returns scopes with the levels of changes set: every other entry - the verbs, the `root:` - stays where it was, the permissions changes does not name keep their level, and the permission entries are written again in their canonical form at the end. An entry this version cannot read is dropped with them (Replace writes what it can read back).

Types

type Def

type Def struct {
	// Key names the permission in a key's list (`comments` → `comments:rw`)
	// and in the answers that list a key's levels.
	Key string
	// Levels are the levels a key may be given, lowest first.
	Levels []Level
	// DefaultLevel is the level of every key whose list does not name the
	// permission - the keys that existed before it was added among them.
	// ⚠ Required, and fixed once shipped: Read, or None for a Superadmin
	// permission (the package comment; tokenperm_test.go).
	DefaultLevel Level
	// Superadmin marks a permission that amounts to administering the server
	// or a tenant. It defaults to None.
	Superadmin bool
}

Def is one permission of the catalogue.

func All

func All() []Def

All returns the catalogue. The slice is a copy.

func Lookup

func Lookup(key string) (Def, bool)

Lookup returns the permission named key.

type Level

type Level string

Level is how far a key may go with one permission.

const (
	// None: not at all. Only a super-administrator kind of permission
	// defaults to it.
	None Level = "none"
	// Read: see it.
	Read Level = "read"
	// ReadWrite: see it, add to it and remove what one may remove. Written
	// `rw` in a key's list; `write` is read as the same level.
	ReadWrite Level = "rw"
)

func LevelIn

func LevelIn(scopes, key string) Level

LevelIn is the level a stored list gives the permission key: the level it names, else the permission's default.

⚠ An EMPTY list grants nothing (model.APIToken.HasScope), so it holds every permission at None, and so does a key the catalogue does not know. An entry this version cannot read - a level the permission does not have, say one a later version added - is passed over and the default holds.

func ParseEntry

func ParseEntry(entry string) (key string, level Level, isPerm bool, err error)

ParseEntry reads one entry of a key's list. isPerm is false for an entry that is not a permission of this catalogue (a verb, a `root:`, anything else - the caller judges those); err is a *LevelError for an entry that names a permission with a level it does not have.

func ParseLevel

func ParseLevel(s string) (Level, bool)

ParseLevel reads a level as a request or a stored list spells it. `write` is the same level as `rw`: a refusal says `comments:write`, and a client that adds exactly what the refusal named gets what it asked for.

func (Level) Covers

func (l Level) Covers(need Level) bool

Covers reports whether a key at level l may do what need asks.

type LevelError

type LevelError struct {
	Key   string
	Level string
}

LevelError is a permission entry with a level its permission does not have.

func (*LevelError) Error

func (e *LevelError) Error() string

type Need

type Need struct {
	Key   string
	Level Level
}

Need is what one action asks of a key: a permission at a level.

func (Need) String

func (n Need) String() string

String is how a refusal names the need: `comments:read`, `comments:write`.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL