# Tokens

Named deploy tokens — one per person or pipeline, scoped to apps, revocable one at a time.

> [!NOTE]
> **Preview** — This works today, but the surface may still change.

Every call to the control plane carries a bearer token. A **named token**
belongs to one person or one pipeline, covers the apps it was minted for,
and can be revoked without touching anyone else's.

The commands on this page need `pilvia` 0.2.5 or later (`pilvia --version`).

## What a token looks like

```
pilvia_3f2a91c07d4e_9b1c…(64 hex)
```

`pilvia_`, a 12-character public id, and a 256-bit secret. The whole token is
shown **once**, when it is minted. The platform keeps only a hash of the
secret, so a lost token cannot be shown again. Mint a new one and revoke the
old.

## Using one

A person gets one by signing in (`pilvia app login`, see below) or, on the
hosted platform during early access, from us: see
[Sign in](/start/installation.md#sign-in).
Anywhere a token goes:

```sh
pilvia app login --token pilvia_…
# or, in CI
export PILVIA_DEPLOY_TOKEN=pilvia_…
pilvia app deploy
```

`pilvia app whoami` says which token the CLI is using and what it reaches.

## Scope

A token covers:

- **Its apps**: one or more by name, or every app.
- **New apps, if it may create them.** A token minted with `--create` can
  deploy to a name nobody holds, including a name the platform generates for a
  first deploy with no name. The new app then belongs to that token's scope.
- **A target.** Most tokens deploy anywhere. A token pinned to `production` or
  to one `branch:<name>` covers exactly one app, only deploys to that target,
  and cannot roll back or change secrets. That is the shape a build pipeline
  should hold.

To a token, an app outside its scope does not exist: every request about it
answers `404`, and `pilvia app list` shows only the token's own apps.

## Expiry

A token expires after **90 days without use**. Every use pushes the deadline
forward, so a token in daily use never expires. A token can instead be minted
with a fixed lifetime (`--fixed --days 7`), which ends on schedule however
much it is used.

## Signing in mints one

`pilvia app login` with no token opens a browser. You sign in at the
platform's identity provider, and the deploy plane mints you a token over
every app, with `create`, on the default sliding expiry. It is named
`login <your machine>`, and its record says which person it was minted for.
Only people in the organisation the plane is configured with can sign in;
on the hosted platform that is our own organisation for now.
Anyone else is refused by name, and the refusal is logged. Logging in again
revokes the token the new one replaces. A plane that takes no browser
logins answers `login_unavailable`.

## Minting, listing, revoking

Minting and listing take an operator token. On the hosted platform, write
to [support@pilvia.com](mailto:support@pilvia.com) for another token, such
as one pinned to a pipeline. On a self-hosted installation, ask whoever
runs it.

```sh
pilvia app token create ci-docs --app docs              # one app
pilvia app token create agency-ci --app a --app b       # several
pilvia app token create onboarding --create             # may create apps
pilvia app token create build --app docs \
  --target branch:feature/nav --fixed --days 1          # one preview, deploy-only
pilvia app token list
pilvia app token revoke 3f2a91c07d4e
```

`token create` prints the token alone on stdout and its description on
stderr, so `TOKEN=$(pilvia app token create …)` captures exactly the token.

Any token can end itself:

```sh
pilvia app token revoke self
```

Revocation takes effect at once. A revoked or expired token is refused with a
message saying which, so "ask for a new token" is never confused with "check
what you pasted".

## The REST surface

| Request | Who | Does |
| --- | --- | --- |
| `POST /v1/tokens` | operator | mint: `{name, apps, create, target, days, sliding}` → the record plus `token` |
| `GET /v1/tokens` | operator | every token, never a secret |
| `DELETE /v1/tokens/{id}` | operator | revoke one |
| `GET /v1/tokens/self` | any token | who am I |
| `DELETE /v1/tokens/self` | a named token | revoke itself |
| `GET /v1/login` | nobody | where a browser signs in: `authorization_endpoint`, `registration_endpoint` |
| `POST /v1/login` | nobody | `{code, code_verifier, client_id, redirect_uri, name?}` from a loopback sign-in → the record plus `token` |

Every request made with a token is recorded with the token's id and name, so
"who deployed this" always has an answer.
