# auth.md

You are an agent. Flaghand supports **agentic registration** by the open [auth.md](https://github.com/workos/auth.md)
protocol (v0.6.0): register your person, they complete the sign-in handoff, you collect an API token and call the API.
Follow the steps in order. The same sign-up without the protocol is in [set up and connect](https://app.flaghand.com/docs/setup.md).

Flaghand accepts one method, `service_auth`: you send your person's email. There is no `anonymous` registration
(every workspace has an owner) and no `identity_assertion` (ID-JAG) sign-in: no ID-JAG issuer is trusted. This is separate
from the person's configured sign-in methods below.

## Step 1: Discover

You may have done this already. Any call to `https://app.flaghand.com/api/v1/…` or `https://app.flaghand.com/mcp` without a token answers `401`
with `WWW-Authenticate: Bearer resource_metadata="…"`.

1. `GET https://app.flaghand.com/.well-known/oauth-protected-resource`: `resource` is `https://app.flaghand.com`, `resource_name` is
   `Flaghand`, `authorization_servers` is `["https://app.flaghand.com"]`.
2. `GET https://app.flaghand.com/.well-known/oauth-authorization-server`: `token_endpoint` is `https://app.flaghand.com/oauth/token`,
   `revocation_endpoint` is `https://app.flaghand.com/oauth/revoke`, `grant_types_supported` includes
   `urn:ietf:params:oauth:grant-type:jwt-bearer` and `urn:workos:agent-auth:grant-type:claim`, and:

```json
"agent_auth": {
  "skill": "https://app.flaghand.com/auth.md",
  "identity_endpoint": "https://app.flaghand.com/agent/identity",
  "claim_endpoint": "https://app.flaghand.com/agent/identity/claim",
  "identity_types_supported": ["service_auth"]
}
```

## Step 2: Register

Tell your person first: you are signing them up for Flaghand with their email, and the token you get can draft,
upload media, check and schedule posts (each publishes at its time), but never approve or post immediately. Read the price and the free start
from `GET https://app.flaghand.com/api/v1/plans` (no sign-in) and tell them those too.

```http
POST https://app.flaghand.com/agent/identity
Content-Type: application/json

{ "type": "service_auth", "login_hint": "person@example.com", "agent_name": "Claude Code" }
```

`agent_name` is optional but recommended: their sign-up page shows it as the name you gave yourself, and it names the
token. `time_zone` (an IANA name like `Europe/London`), `found_via` (where you found Flaghand) and
`plan` (`monthly`, `yearly` or `founding`) are optional too. No plan means no preference, not a free subscription;
Checkout defaults to monthly. To name a new workspace with `workspace_name`, use the ordinary
[HTTP signup quickstart](https://app.flaghand.com/llms.txt#quickstart-sign-up-claim-getsetup) instead of this protocol endpoint.

Response (`200`):

```json
{
  "registration_id": "sgn_…",
  "registration_type": "service_auth",
  "claim_url": "https://app.flaghand.com/agent/identity/claim",
  "claim_token": "clm_…",
  "claim_token_expires": "2026-10-28T09:00:00.000Z",
  "claim": {
    "user_code": "K7QM-2XRT",
    "verification_uri": "https://app.flaghand.com/start#signup=sup_…",
    "verification_uri_complete": "https://app.flaghand.com/start#signup=sup_…",
    "expires_in": 86400,
    "interval": 5
  }
}
```

Treat `claim_token` as a secret like the token: save this response in a new private file outside the repository
(`umask 077`) before printing anything. Never print `claim_token` or put it in command arguments, shell history or shared
logs; show only the verification link, `claim.user_code` and other safe handoff fields. Step 4 can use it again if its answer is lost. The
registration lasts 24 hours. This response does
not predict `post_claim_scopes`: the granted set depends on account creation or the owner's consent, and only the
token response's `scope` tells you what was granted.

`POST /agent/identity` shares one approximate 30-request-per-minute allowance per IP at each edge location with
`POST /api/v1/signup` and `POST /api/v1/auth/enroll`. An exhausted allowance answers `429` with
`error.code: rate_limited` and `Retry-After: 60`: wait before retrying and keep any existing sign-up link.
Neither claim path, `/oauth/token` nor OAuth client registration is part of this allowance.

## Step 3: Hand off to your person

Send them `claim.verification_uri` and `claim.user_code` together, in one message. `user_code` is an 8-character code
shown as `XXXX-XXXX`: an existing owner types it on the page before Allow, so a link someone else started can't be
allowed with one click. It isn't a secret like `claim_token`, but only you, the agent that started the link, show it.
New people create their account with Google or an email link or code and type no code. Existing owners sign in with a
configured method, choose a workspace they own, type the code and explicitly Allow the agent, with their passkey or, if
they have none, a code emailed to them. A wrong or missing code allows nothing. Flaghand has no passwords. If they choose email,
they use the link or six-digit code from their own inbox; it is not the registration's `user_code`. Never collect or
enter their inbox code, consume their sign-in link, answer provider consent or perform their passkey tap.
Suggested copy:

> Open this link to create your account with Google or an email link or code, or sign in and allow this agent: <verification_uri>
> If you already have an account, type this code from your agent before you allow it: <user_code>. Never type a code from anyone else.
> Tell me when you're done.

The page shows them your `agent_name`, and existing-owner consent shows the four OAuth scopes. New-account setup
shows the plans, prices and free days before the first charge. Card details go only to Stripe; you never see them.
New, pending and existing emails receive the same registration response; the link alone never grants access.
Google and email create the account without a passkey or card. The first approval handles the card step and creates
a passkey if needed, before a separate approval tap.

## Step 4: Collect the token

```http
POST https://app.flaghand.com/oauth/token
Content-Type: application/x-www-form-urlencoded

grant_type=urn:workos:agent-auth:grant-type:claim&claim_token=clm_…
```

- `400 {"error": "authorization_pending"}`: they haven't created the account yet, or haven't signed in, typed the code and allowed the agent.
  Wait at least `interval` seconds. It is usually cheaper to ask them to tell you when they're done and poll once then;
  nothing here refuses faster polling.
- `200`: the token:

```json
{
  "access_token": "tok_…",
  "token_type": "Bearer",
  "scope": "read drafts media request-approval cancel connections billing webhooks calendar",
  "identity_assertion": "eyJ…",
  "assertion_expires": "2026-11-03T09:00:00.000Z",
  "workspace_id": "ws_…",
  "token_id": "tkn_…"
}
```

Write this answer to a private file before printing anything, then save `access_token` in secret storage
(`FLAGHAND_TOKEN`, or `flaghand login --token-stdin`). Read the returned
`scope`: new-account claims get every agent scope as illustrated; existing-owner grants get only `read`, `drafts`,
`media` and `request-approval`, like OAuth. It is an ordinary Flaghand API token: it doesn't expire (there is no
`expires_in`) and stops only when it's revoked. The `drafts` scope can schedule future posts without a per-post passkey.
No token can approve or post immediately. Keep `identity_assertion` for Step 5.

- `400 {"error": "expired_token"}`: the 24 hours ran out before registration or consent, or the token was waiting longer
  than 7 days. Go back to Step 2.
- After this `200`, `claim_token` alone never issues the token again. Within 7 days of account creation or Allow:
  - To rotate it, send the same request with the current `access_token` as `Authorization: Bearer tok_…`. The same
    `token_id` comes back with a new `access_token` and `identity_assertion`, and the earlier ones stop working.
  - Lost it (never saved, a dropped reply, a crashed shell)? Send the same request without it. The answer is
    `400 {"error": "authorization_pending", "approve_url": "…", "reissue_code": "rsq_…"}`. Give your person
    `approve_url`: a workspace owner signs in and taps Reissue. Then send the request with `reissue_code=rsq_…` added to
    collect the new token, as above. Treat `reissue_code` as a secret: the link doesn't carry it, so whoever sees the
    link can't collect. Until then the current token keeps working, and a request without `reissue_code` replaces the
    earlier one and its link.
- `400 {"error": "invalid_grant"}`: a token from this registration, or its assertion, was revoked. Go back to Step 2 if
  you need another.

## Step 5: Another token from the assertion

```http
POST https://app.flaghand.com/oauth/token
Content-Type: application/x-www-form-urlencoded

grant_type=urn:ietf:params:oauth:grant-type:jwt-bearer&assertion=eyJ…&resource=https://app.flaghand.com
```

The answer is another token with the same scopes (`access_token`, `token_type`, `scope`, `workspace_id`,
`token_id`). The assertion works until `assertion_expires`, 7 days after account creation or Allow, and never again once any token
from this registration has been revoked, by you or by the owner. Then `invalid_grant`: go back to Step 2. There are no
refresh tokens; nothing needs refreshing.

## Step 6: Use the token

Send `Authorization: Bearer tok_…` to `https://app.flaghand.com/api/v1` and `https://app.flaghand.com/mcp`. Start with `GET https://app.flaghand.com/api/v1/me`
and `GET https://app.flaghand.com/api/v1/workspaces/{workspace_id}/setup`. Follow `next.operation_id`, `next.arguments` and
`next.tell_person`; reuse the returned connections and billing rather than downloading a full setup manual.
[Continue to the first draft](https://app.flaghand.com/docs/first-post.md#2-check-the-workspace).

## Starting the ceremony again

If you lost the link, or the person needs a new one: `POST https://app.flaghand.com/agent/identity/claim` with
`{"claim_token": "clm_…", "email": "person@example.com"}` (the same email). The answer has a fresh `claim_attempt`
block, the same shape as `claim`; the link given before stops working, and the new link has a new `user_code`. Send
the person both again. It works until the person confirms and while
the registration's 24 hours last.

## Revocation

- `POST https://app.flaghand.com/oauth/revoke` with `token=tok_…` (RFC 7009) ends that token, and the registration makes no more.
- `token=<identity_assertion>` ends the assertion; the tokens it made keep working until they're revoked themselves.
- The owner sees and revokes every token in Settings › Agents (`https://app.flaghand.com/agents`). A `401` on a token that worked:
  try Step 5 once; `invalid_grant` there means go back to Step 2.

There is no auth.md events endpoint: Flaghand trusts no ID-JAG issuer whose events it would take.

## Errors

| Code | Where | What to do |
| --- | --- | --- |
| `anonymous_not_enabled` | `/agent/identity` | Use `service_auth`. |
| `issuer_not_enabled` | `/agent/identity` | No ID-JAG provider is trusted. Use `service_auth`. |
| `invalid_login_hint` | `/agent/identity` | Send your person's email address. |
| `signup_closed` | `/agent/identity` | Sign-up isn't open; the description says how to join the waitlist. |
| `invalid_request` | any | Fix the body; the description says what's wrong. |
| `invalid_claim_token` | `/agent/identity/claim` | Go back to Step 2. |
| `claimed_or_in_flight` | `/agent/identity/claim` | The person confirmed: collect the token (Step 4). |
| `claim_expired` | `/agent/identity/claim` | Go back to Step 2. |
| `authorization_pending` | `/oauth/token` | Not yet: wait for the person. With `approve_url`, give it to them first (Step 4). |
| `expired_token` | `/oauth/token` | Go back to Step 2. |
| `invalid_grant` | `/oauth/token` | Revoked or expired: go back to Step 2. |
| `unsupported_grant_type` | `/oauth/token` | Use one of the grants in Step 1. |

Every other error code Flaghand answers is in [errors](https://app.flaghand.com/docs/errors.md).
