apitokensv1
import apitokensv1 "pocketone.dev/ext/contracts/api-tokens/v1"
Package apitokensv1 is the api-tokens/v1 contract: an app's API tokens. ext/api-tokens provides it. In Go, Issuer checks a token; over HTTP, its API at /api/_c/api-tokens/v1 lists, creates and revokes them (a routed contract):
GET /api/_c/api-tokens/v1/scopes the scopes a token may hold, and who may own tokens
GET /api/_c/api-tokens/v1 your tokens (superusers: ?owner=, ?ownerCollection=)
POST /api/_c/api-tokens/v1 create one ({name, scopes, expires|ttl}); the secret is shown once
DELETE /api/_c/api-tokens/v1/{id} revoke one (`pocketone logout` revokes its own)
POST /api/_c/api-tokens/v1/revoke-all revoke all of yours
Extensions that accept API tokens (deploy, mcp, remotes) work with it through this package alone: they register their scopes with the Issuer, read the token a request came with (FromRequest) and limit in-process requests (WithScopes). The OAuth server requires it.
Source: ext/contracts/api-tokens/v1.
Constants
const OfflineAccess = "offline_access"
OfflineAccess is the OAuth scope asking for a refresh token; it limits nothing.
const RequestKey = "api-tokens.grant"
RequestKey is the request store key holding the *Grant of a request authenticated by an API token.
const ScopeValueSep = "="
ScopeValueSep joins a scope name and a value in a token's scope list: "instances=abc123".
Variables
var (
ErrInvalid = errors.New("invalid API token") // unknown, malformed or revoked
ErrExpired = errors.New("expired API token")
)
Errors Issuer.Verify returns for a token that isn't valid; any other error is the provider failing.
var Contract = extkit.NewContract[Issuer]("api-tokens/v1", extkit.One)
Contract is api-tokens/v1.
var Prefix = extkit.Routed(Contract)
Prefix is where the token API is served: /api/_c/api-tokens/v1.
Functions
CoversScope
func CoversScope(scopes []string, scope string) bool
CoversScope reports whether a token with these scopes may hand out scope to a token it creates: "instances" covers "instances" and "instances=abc"; "instances=abc" covers only "instances=abc". No scopes: everything.
HoldsScope
func HoldsScope(scopes []string, scope string) bool
HoldsScope reports whether a token with these scopes may act with scope (see Grant.HasScope). No scopes: every scope.
IntersectScopes
func IntersectScopes(a, b []string) []string
IntersectScopes is what both lists allow (both non-empty: an empty list here means nothing, not full rights): "instances" ∩ "instances=abc" is "instances=abc". The result is sorted.
JoinScope
func JoinScope(name, value string) string
JoinScope is "name=value", or name when value is "".
ScopesFromContext
func ScopesFromContext(ctx context.Context) ([]string, bool)
ScopesFromContext returns the scopes set with WithScopes, and whether there are any limits.
SplitScope
func SplitScope(scope string) (name, value string)
SplitScope splits a token scope into its name and value ("" when it has none).
ValuesOf
func ValuesOf(scopes []string, scope string) (values []string, all bool)
ValuesOf is Grant.ScopeValues for a scope list. No scopes: every value.
WithScopes
func WithScopes(ctx context.Context, scopes []string) context.Context
WithScopes limits in-process requests made with ctx to these scopes, the way a token limited to them would be (ext/mcp's tool calls on a scoped connection). A nested WithScopes can only narrow it. An empty (or nil) list allows nothing but the token-wide paths, so call it only for a scoped connection.
Types
Grant
type Grant struct {
TokenID string `json:"tokenId"`
Name string `json:"name,omitempty"` // the token's label
OwnerCollection string `json:"ownerCollection"`
OwnerID string `json:"ownerId"`
Scopes []string `json:"scopes"` // empty: the owner's full rights
Audience string `json:"audience,omitempty"` // the resource it's bound to (RFC 8707), or ""
ClientID string `json:"clientId,omitempty"` // the OAuth client it was issued to, or ""
Owner *core.Record `json:"-"` // the owner's record (inside the app)
}
Grant is what a verified token allows.
FromRequest
func FromRequest(e *core.RequestEvent) *Grant
FromRequest returns the API token a request was authenticated with, or nil (no auth, or a JWT).
Grant.HasScope
func (g *Grant) HasScope(scope string) bool
HasScope reports whether the token may act with scope: unscoped tokens have every scope. A bare name ("instances") is held by "instances" and by any "instances=<value>"; "instances=abc" is held by "instances" or "instances=abc".
Grant.HasScopeFor
func (g *Grant) HasScopeFor(scope, value string) bool
HasScopeFor reports whether the token may act with scope on value: it holds the scope with no value (every value), or holds "scope=value". Unscoped tokens may.
Grant.ScopeValues
func (g *Grant) ScopeValues(scope string) (values []string, all bool)
ScopeValues lists the values the token holds scope for. all is true when it holds the scope for every value (an unscoped token, or the bare name); otherwise values may be empty (it doesn't hold the scope).
Issuer
type Issuer interface {
// Verify resolves a token secret to who it acts as and what it may do.
Verify(ctx context.Context, secret string) (Grant, error)
// RegisterScope adds a scope a token can be limited to (an extension's own, e.g. deploy's).
RegisterScope(s Scope) error
// Scopes lists the registered scopes.
Scopes() []Scope
// Reaches reports whether a token limited to scopes may call method on path.
Reaches(scopes []string, method, path string) bool
}
Issuer is an app's API tokens. Its provider also serves the token API (extkit.RouteBinder) and authenticates requests that carry a token (setting RequestKey).
Scope
type Scope struct {
// Name, e.g. "deploy" or "notes:write": lower-case letters, digits, ":" and "_", starting with a letter or digit.
Name string `json:"name"`
// Description is shown on the consent page and in the token screens.
Description string `json:"description"`
// Paths are URL path prefixes where a token with this scope authenticates as its owner.
Paths []string `json:"paths,omitempty"`
// Methods, when not empty, limits Paths to these HTTP methods (GET also covers HEAD). Empty: every method.
Methods []string `json:"methods,omitempty"`
// Match is an optional guard: a token with this scope also authenticates on any request it returns true for.
// It runs before the owner is loaded, so it may look only at the request, not at e.Auth.
Match func(e *core.RequestEvent) bool `json:"-"`
// Collections, when not empty, limits which auth collections may hold a token with this scope.
Collections []string `json:"collections,omitempty"`
// Values, when set, lets a token hold the scope for some values only ("instances=abc123"), e.g. record ids. The
// bare name still means every value. Paths and Match don't see values: whoever serves the scope's requests
// checks them ([Grant.HasScopeFor], [Grant.ScopeValues]).
Values *ScopeValues `json:"values,omitempty"`
}
Scope is a named slice of an owner's rights that a token can be limited to. A token whose scopes are set authenticates only on requests its scopes match (Paths + Methods, or Match).
Scope.Infrastructure
func (s Scope) Infrastructure() bool
Infrastructure reports whether the scope only reaches PocketOne's own routes (contract APIs under /api/_c/): a client that needs one asks for it; consent pages and MCP don't offer it otherwise.
ScopeValues
type ScopeValues struct {
// Description says what a value is (e.g. "an instance id"); token screens show it.
Description string `json:"description"`
// Check, if set, vets a value when a token is created for owner (e.g. that owner owns that record); an error
// refuses the token.
Check func(owner *core.Record, value string) error `json:"-"`
}
ScopeValues describes what a scope's values are.
Questions and comments
Comments are a GitHub Discussions thread; sign in with GitHub to post.