# Personas

A persona is a named login identity declared in `cofactor.sandbox.yaml`:

```yaml
personas:
  owner:  { email: owner@example.test }
  member: { email: member@example.test }
  viewer: { email: viewer@example.test }
```

Declare one per role your tests need (see "What to build first" in
`seeds-and-actions`). Only the email is fixed in the definition.
Everything else about a persona is assigned per sandbox or per seed:

## Passwords — set when the snapshot is built

Each ready snapshot gets a randomly generated password per persona,
created by the snapshot's own `prepare` (see the `overview` topic); every
copy claimed from that snapshot has the same passwords. Those passwords
never appear in the definition — they're generated fresh for the snapshot
and handed to
`lifecycle.prepare` as `COFACTOR_PERSONAS`, a JSON map of persona name to
`{email, password}`. Your prepare script reads it and makes each login
work in your app: the example app's `sandbox/personas.sh` creates or
updates one user per persona (email + password hash), with no workspace
and no role — just the login itself. The `upload` topic covers the
details (prepare runs while the app is up; logins must be read per
sign-in, not once at startup).

A copy's `lifecycle.prepare` still runs, with the snapshot's unchanged
personas, so make the persona step cheap when nothing changed (skip
hashing a password the user already has): the example app's personas.sh
skips when a digest of COFACTOR_PERSONAS and of the users' stored hashes
both match what its last run left, so a reset or replaced user still
takes the full path.

If your app gates sign-in behind a first-run step (accepting terms,
initializing the instance, verifying the email address, finishing
onboarding), clear it in prepare for each persona, the way you set the
password: a seed or test signing in through the real login API should
land signed in, not on the gate.

## Roles and memberships — set by seeds

A persona existing (having a login) is separate from what it can do.
Workspace membership and role are set by seed steps, not by the
definition: `create-workspace` makes its `as` persona the owner of the
new workspace, and `add-member` gives a persona a role in it. This split
matters: the same persona can be "just a login" in one environment and
a workspace owner in another, because that comes from which seeds
actually ran. If a persona's email is an account your app already has
with the right role, prepare setting its password is enough.

## How a seed or action signs in as a persona

Actions receive `COFACTOR_PERSONAS` too. An action that performs a real
operation takes the acting persona as an input (`as`, of type
`persona`, which arrives as `INPUT_AS` holding the persona's name),
reads that persona's email and password from `COFACTOR_PERSONAS`, and
signs in through the app's real login endpoint — the same way a user
would — keeping the session cookie for its next calls:

```bash
login=$(jq -c --arg p "$INPUT_AS" '.[$p] | {email, password}' <<<"$COFACTOR_PERSONAS")
jar=$(mktemp)
curl -fsS -c "$jar" -H 'content-type: application/json' \
  -d "$login" "$API_URL/api/auth/login" >&2
```

See the `seeds-and-actions` topic on why `as` is never assumed.

An action runs inside the sandbox, so it can reach the app on
localhost. If your app's session cookie is `Secure` (or `__Host-`
prefixed) and your HTTP client won't send it back over plain http, call
the app through its public port URL (here `API_URL`, from a
`{port: api}` param) instead.

Keep whatever the login returns that later calls need: the session
cookie, a bearer token from the response body, or both (some apps bind
a bearer token to an httpOnly cookie and refuse one without the other).

Every action signs in from the same address, so a seed that runs several
actions can trip the app's login rate limit (often around 10 attempts a
minute). If your app has one, raise it for the sandbox through the
definition's top-level `env:` (it reaches every service) rather than
adding retries to your actions.

## Where logins are shown

Because copies of a snapshot share persona passwords, anyone who has
claimed one can sign in to another copy of the same snapshot whose URL
they learn; keep sensitive data out of sandboxes.

Persona logins (email + generated password) are surfaced by
`cofactor sandbox show <id>` and, for PR previews, by `cofactor sandbox
preview` and the preview's Cofactor page (`/sandboxes/previews/<application
id>/<pr number>`, org members signed in only). They are never sent to
GitHub — the PR's deployment only links that page, keeping generated
passwords out of anything that ends up in GitHub's own history.
Cofactor's test runs sign in with them automatically, as the
application's persona of the same name.

A person who claims a sandbox signs in the ordinary way: open its URL and
use your app's own login with the email and password the claim prints.
There is no CLI command that hands you a signed-in browser session;
`authentication.run` (below) is only for Cofactor's automated test runs.

## Google accounts

When the definition declares the `google-oauth` emulator, a persona's
email is also its Google identity: the Google account chooser lists each
persona, and `authentication.emulated` signs a persona in through Google
as that email. `google-oauth` needs at least one persona. See
`cofactor sandbox guide v2 authentication`.

## Native workflow login nodes

Sandboxes v2 uses `authentication` (`run`, or `emulated` for Google
sign-in) to mint a fresh browser session for the persona named by each
native workflow login node. Read
`cofactor sandbox guide v2 authentication` for the complete script contract,
cookie and localStorage formats, and how to keep provider-login journeys
clean and unauthenticated at the start. A Workflow `sandbox.reset` keeps the
same mapped persona while minting fresh authentication for its replacement
copy; it never copies the previous browser's cookies or storage state. An
anonymous session stays anonymous.
