# Apps acting for a person

> A registered app signs a WhatsBuilt person in with the authorization code flow and PKCE, and calls the REST API with that person's rights. Consent, tokens, refresh, revocation, CORS and the organisation's limits.

An **app** acts for one signed-in person. The person approves it once on a consent page; the app then calls `/v1` with that person's rights. It never does more than the person could do in WhatsBuilt, and what it writes shows as *{person} · via {app}*.

An app is not an [integration](/en/integrations/create-an-integration/). An integration is a system of your organisation that runs without a person; an app is something a person uses, such as a site diary, a mobile tool or a partner's web service.

## Register the app

WhatsBuilt staff register apps. Send support the app's name, who provides it, its type, its exact redirect URIs and the permissions it needs. You get a `client_id`.

| Type | For | Client authentication | Connection lasts |
|:-----|:----|:----------------------|:-----------------|
| Public | A mobile app, an app on the person's computer or an app in a web browser, which cannot keep a secret; see [Calls from a browser](#calls-from-a-browser) | `client_id` only, with PKCE | 30 days |
| Confidential | A web service with a server | A client assertion signed with its private key (`private_key_jwt`), as in [Get an access token](/en/integrations/get-an-access-token/), plus PKCE | 90 days |

Redirect URIs match **exactly**, character for character. Allowed are `https://` addresses, `http://127.0.0.1`, `http://[::1]` or `http://localhost` with any port for apps on the person's computer, and a private-use scheme named after a domain you own, such as `fi.example.diary:/oauth`, for mobile apps (RFC 8252).

| Environment | Issuer |
|:------------|:-------|
| Production | `https://api.whatsbuilt.fi` |
| Test | `https://api.test.whatsbuilt.fi` |
| Development | `https://api.dev.whatsbuilt.fi` |

The endpoints are in the issuer's metadata at `/.well-known/oauth-authorization-server`: `/oauth/authorize`, `/oauth/token` and `/oauth/revoke`.

## 1. Send the person to sign in

Make a random `code_verifier` of 43 to 128 characters (letters, digits, `-`, `.`, `_`, `~`) and its `code_challenge`, the base64url SHA-256 of the verifier without padding. Then open in the browser:

```text
https://api.dev.whatsbuilt.fi/oauth/authorize
  ?response_type=code
  &client_id=<client_id>
  &redirect_uri=https%3A%2F%2Fdiary.example%2Fcallback
  &scope=read%20findings%3Awrite%20offline_access
  &state=<random value you check later>
  &code_challenge=<code_challenge>
  &code_challenge_method=S256
  &resource=https%3A%2F%2Fapi.dev.whatsbuilt.fi%2Fv1
```

| Parameter | Rule |
|:----------|:-----|
| `response_type` | `code` |
| `code_challenge_method` | `S256`. PKCE is required for every app |
| `scope` | Space separated. A subset of the app's registered permissions; all of them when left out. Add `offline_access` to get a refresh token |
| `state` | Recommended. Up to 1,024 characters; returned unchanged |
| `resource` | Optional. `<issuer>/v1` or the issuer |

The person signs in to WhatsBuilt as usual, with a password or SSO, and sees a consent page with the app's registered name, who provides it, the address the sign-in returns to, its permissions in plain words and the projects it reaches. A permission that records verdicts, deletes data or grants access is its own line with a warning.

When the person allows, the browser returns to your redirect URI with `code`, `state` and `iss`. Check that `state` is yours and that `iss` is the issuer you started with (RFC 9207). When the person cancels, you get `error=access_denied`. Errors in the request also come back as `error` with `state` and `iss`; an unknown `client_id` or a redirect URI that is not registered gets an error page instead, never a redirect. Send each parameter once: a repeated `client_id` or `redirect_uri` also gets the error page, and any other repeated parameter comes back as `invalid_request` (`invalid_target` for `resource`).

## 2. Exchange the code

The code lasts two minutes and works once. `POST` a form to `/oauth/token`:

| Field | Value |
|:------|:------|
| `grant_type` | `authorization_code` |
| `code` | The code |
| `redirect_uri` | The same redirect URI as in step 1 |
| `code_verifier` | The verifier from step 1 |
| `client_id` | Public app |
| `client_assertion_type`, `client_assertion` | Confidential app, instead of `client_id` |

```json
{ "access_token": "wba_at_…", "token_type": "Bearer", "expires_in": 3600, "refresh_token": "wba_rt_…", "scope": "read findings:write offline_access" }
```

Send the access token on every call as `Authorization: Bearer wba_at_…`. It lasts an hour. `GET /v1/me` names the person, the app and the permissions.

If the same code is sent twice, the second exchange fails and the connection the code made ends at once.

## 3. Refresh

Only with `offline_access`. `POST` to `/oauth/token` with `grant_type=refresh_token`, the `refresh_token` and the same client authentication. The answer has a new access token and a **new** refresh token: keep the new one. A refresh token may be used again for 10 minutes after its first use, at most three times in all, so a retry after a lost answer works. Used after that, it ends the connection: the app must send the person through step 1 again.

The connection lasts 30 days for a public app and 90 days for a confidential one, counted from the person's approval. Refreshing does not lengthen it.

## 4. Sign out

`POST` to `/oauth/revoke` with `token` (an access or refresh token) and the client authentication. The connection ends at once. The answer is `200` also for a token that is unknown or already ended (RFC 7009). A request without `token` gets `400 invalid_request`.

## Calls from a browser

`/v1`, `/oauth/token` and `/oauth/revoke` answer CORS requests from the origins of the app's registered `https` and loopback redirect URIs, never with credentials (cookies), so the token goes in the `Authorization` header. A page on any other address is refused.

## What limits the app

- **The person's own rights.** The app acts as the person: their projects, project roles and the organisation's rules apply. A viewer's app cannot write, whatever its permissions say.
- **The organisation's admin** sees the apps of the organisation's people on *Integrations* › *Apps*, can remove any of them at once, and can limit the permissions apps get. A permission the admin does not allow is not offered at consent, and stops working for a connected app at its next call, which answers `403 INSUFFICIENT_SCOPE`.
- **The organisation's API switch.** When the admin turns the API off on *Settings* › *Connections*, consent is refused and every call answers `403 PUBLIC_API_DISABLED`.
- **The person** sees their apps under *Your access* and can remove them.

A removed, expired or replaced connection answers `401` with `WWW-Authenticate`; send the person through step 1 again. Limits and the other errors are on [Errors and limits](/en/integrations/errors-and-limits/).
