# Authentication

Auth is a platform feature, not app code — declare what you need, read env.user.

Apps on Pilvia App contain **zero lines of login code**. No session
handling, no token validation, no CSRF dance, no password storage. The
manifest declares who may call the app; the host enforces it before your
code runs; your code reads `env.user`.

> [!NOTE]
> **Early access**
>
> Sign-in works against an organisation in Pilvia Mieli, the platform's
> identity and data plane. An early-access account does not include one,
> and an app that requires sign-in admits nobody without it. Deploy with
> `"auth": "public"`, and write to [support@pilvia.com](mailto:support@pilvia.com)
> if your app needs sign-in.

## Three modes

```json
{ "auth": "required" }
```

| Mode | Who gets in |
| --- | --- |
| `required` *(default)* | Signed-in users of the organization. Everyone else gets a login redirect (browsers) or a 401 (API calls). |
| `public` | Everyone. An explicit, auditable declaration — never a default. |
| `token` | Machines holding the app's trigger token — for webhooks and system-to-system calls. |

The default **is** the security posture: an app with no `auth` key is
locked. A missing key can only ever make an app more private, not more
public — a typo cannot open anything, and an unknown manifest field is a
deploy-time error rather than a silent ignore.

## `env.user` — identity as data

On an authenticated request, the host validates the session and constructs
the user object **before the isolate starts**:

```ts
const { principal_id, org_id, role, name, email } = env.user;
```

If there is no valid session on an `auth: required` app, your code never
executes — there is no code path in which a handler "forgets" to check.
The dependency is structural: `env.user` cannot be constructed without a
validated session, and an app that requires auth cannot even be served if
the auth layer is down. Failing closed is not a convention here; it is how
the pieces fit.

## Two principals, and which one acts

Data operations carry a principal, and authorization follows it
automatically — see [Data & AI](/data/overview.md):

- **The user principal** is the default on authenticated routes: data reads
  and writes run *on behalf of* the signed-in person, row-level security
  scopes what they see, and the audit trail names the human — with no
  authorization code in your app.
- **The app principal** is the app's own service identity. It acts on
  public routes and [background jobs](/data/jobs.md) — and only if the
  manifest granted `"app_data": true`. The grant is declared privilege,
  never ambient: a public app without it is compute-only.

## Token mode for machines

`auth: token` is for callers that are not people: the caller sends the
app's `TRIGGER_TOKEN` secret in the `X-Pilvia-Trigger` header, the host
checks it and, on a match, runs the request as the
app's own principal — no session, no user. This is the shape a webhook
receiver or a triggered sync wants; `required` would make machine calls
impersonate a person, and `public` would leave the endpoint open. Token
mode with no `TRIGGER_TOKEN` secret set serves nothing.

## Where identities live

Your organization's data plane is also its identity provider — one login,
one set of accounts, OAuth 2.1 underneath. Apps never keep user tables of
their own, which is precisely why they contain nothing that can leak
passwords.
