annals.tokens#

Read tokens: a credential for an agent that cannot reach the store (a cloud session).

A token is scoped to ONE group, can only read (GET), is stored hashed, and expires (14 days unless told, 90 at most; there is no “never”). It never reaches a document tagged private or noindex (annals.records.is_withheld()), however the group is edited later. The server accepts it as Authorization: Bearer <token> on the API (annals.api.mk_app()), where it is answered by a read-only room over the group (annals.store.GroupScopedStore), so a route added later is out of reach until it is named in annals.api.TOKEN_ROUTES.

The records are tokens/<id>.json under the data root (through the store’s target, so annals token new works over ssh like annals publish):

{"id": "...", "name": "cloud-agent", "group": "<gid>", "created": "<iso>",
 "expires": "<iso>", "hash": "<sha256 of the secret>", "revoked": "<iso>" | null}

The token itself, annt_<id>.<secret>, is shown once at creation and never stored. The secret is 256 random bits, so a plain SHA-256 is enough (there is nothing to guess and nothing to stretch); it is compared in constant time.

>>> import tempfile
>>> from annals.store import DocStore
>>> store = DocStore(tempfile.mkdtemp())
>>> d = store.publish(text="# a", title="A")["id"]
>>> g = store.make_group("G", [d])["id"]
>>> tokens = ReadTokens(store)
>>> token, rec = tokens.create(g, name="agent")
>>> tokens.verify(token)["group"] == g, tokens.verify(token + "x")
(True, None)
>>> _ = tokens.revoke(rec["id"]); tokens.verify(token)

Module Attributes

MAX_DAYS

a long-lived credential is what the scope exists to avoid

Functions

bearer_of(header)

The token in an Authorization header, or None when it carries none of ours.

Classes

ReadTokens(store, *[, clock, max_days])

Make, check, list and revoke the read tokens of a DocStore's groups.

annals.tokens.MAX_DAYS = 90#

a long-lived credential is what the scope exists to avoid

Type:

the longest a token may live

class annals.tokens.ReadTokens(store, *, clock=<function now_utc>, max_days=90)[source]#

Bases: object

Make, check, list and revoke the read tokens of a DocStore’s groups.

create(group, *, name, expires='14d')[source]#

(token, record): the token is shown once, only its hash is kept.

Return type:

tuple[str, dict]

list(*, all=False)[source]#

The records without their hash, newest first (all: revoked and expired too).

Return type:

list[dict]

revoke(ident)[source]#

Stop a token working at once (its id, or the token itself); the record stays.

Return type:

dict

verify(token)[source]#

The record of a good token, else None: unknown, wrong secret, revoked, expired.

Return type:

dict | None

annals.tokens.bearer_of(header)[source]#

The token in an Authorization header, or None when it carries none of ours.

>>> bearer_of("Bearer annt_x.y"), bearer_of("Basic abc"), bearer_of(None)
('annt_x.y', None, None)
Return type:

str | None