# 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()`](annals.records.html.md#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`](annals.store.html.md#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`):

```default
{"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.

```pycon
>>> 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`](#annals.tokens.MAX_DAYS)   | a long-lived credential is what the scope exists to avoid   |
|-------------------------------------------------------------|-------------------------------------------------------------|

### Functions

| [`bearer_of`](#annals.tokens.bearer_of)(header)   | The token in an `Authorization` header, or `None` when it carries none of ours.   |
|----------------------------------------------------------------------|-----------------------------------------------------------------------------------|

### Classes

| [`ReadTokens`](#annals.tokens.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)

Bases: [`object`](https://docs.python.org/3/builtins/functions.html#object)

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

#### create(group, , name, expires='14d')

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

* **Return type:**
  [`tuple`](https://docs.python.org/3/builtins/stdtypes.html#tuple)[[`str`](https://docs.python.org/3/builtins/stdtypes.html#str), [`dict`](https://docs.python.org/3/builtins/stdtypes.html#dict)]

#### list(, all=False)

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

* **Return type:**
  [`list`](#annals.tokens.ReadTokens.list)[[`dict`](https://docs.python.org/3/builtins/stdtypes.html#dict)]

#### revoke(ident)

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

* **Return type:**
  [`dict`](https://docs.python.org/3/builtins/stdtypes.html#dict)

#### verify(token)

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

* **Return type:**
  [`dict`](https://docs.python.org/3/builtins/stdtypes.html#dict) | [`None`](https://docs.python.org/3/builtins/constants.html#None)

### annals.tokens.bearer_of(header)

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

```pycon
>>> bearer_of("Bearer annt_x.y"), bearer_of("Basic abc"), bearer_of(None)
('annt_x.y', None, None)
```

* **Return type:**
  [`str`](https://docs.python.org/3/builtins/stdtypes.html#str) | [`None`](https://docs.python.org/3/builtins/constants.html#None)
