# OAuth 2.0 apps

An OAuth app is a product that people connect to their projqo account, such as a time tracker or a reporting tool. The person signs in to projqo, sees what your app asks for, and allows it. Your app never sees their password.

projqo uses OAuth 2.0's **authorization code** flow with **PKCE** for every app, with a server or without one. There is no implicit flow and no device flow.

## Register your app

An organisation owner or admin registers the app in **Organisation settings › Develop apps**:

- its name, description, logo, homepage, and privacy and terms addresses;
- its **redirect addresses**: `https` addresses, matched exactly, and `http://127.0.0.1` with any port for apps that run on a computer;
- the **scopes** it may ask for;
- **who may use it**: this organisation only, or other organisations too, once their admin allows it.

Registering gives a **client id**, and for an app with a server, a **client secret**, shown once.

- A secret can be rotated: the old one keeps working for 24 hours, so you can deploy the new one without stopping.
- Deleting the app revokes every grant and token it holds.

## The endpoints

| What         | Address                                                                    |
| ------------ | -------------------------------------------------------------------------- |
| Consent page | `GET https://<your projqo address>/oauth/authorize`                        |
| Tokens       | `POST https://<your projqo address>/api/oauth/token`                       |
| Revoking     | `POST https://<your projqo address>/api/oauth/revoke`                      |
| Metadata     | `GET https://<your projqo address>/.well-known/oauth-authorization-server` |

The metadata document lists these addresses and what the server supports, in the standard form many OAuth libraries read by themselves.

## The flow, step by step

### 1. Make a code verifier and its challenge

For every sign-in, make a random **code verifier** and keep it. Send its SHA-256, base64url-encoded, as the **code challenge**.

:::tabs

```js title="JavaScript"
import { createHash, randomBytes } from "node:crypto";

const verifier = randomBytes(32).toString("base64url");
const challenge = createHash("sha256").update(verifier).digest("base64url");
const state = randomBytes(16).toString("base64url");
```

```py title="Python"
import base64, hashlib, secrets

verifier = secrets.token_urlsafe(32)
challenge = base64.urlsafe_b64encode(hashlib.sha256(verifier.encode()).digest()).rstrip(b"=").decode()
state = secrets.token_urlsafe(16)
```

:::

### 2. Send the person to the consent page

```text
https://<your projqo address>/oauth/authorize
  ?response_type=code
  &client_id=<your client id>
  &redirect_uri=https://app.example.com/callback
  &scope=org:read tasks:read
  &state=<state>
  &code_challenge=<challenge>
  &code_challenge_method=S256
```

(On one line, with the values URL-encoded.) `state` and the PKCE challenge with `S256` are required for every app.

The consent page shows your app's name and logo, the organisation that registered it (and "not from your organisation" when it isn't theirs), each scope in a plain sentence, and **Allow** and **Deny**. The person picks the organisation to connect: one where they are a member or admin and your app is allowed. Guests can't connect apps.

Consent is remembered: a person who allowed your app isn't asked again, unless you ask for new scopes or send `prompt=consent`.

### 3. Take the code from your redirect address

projqo sends the person back to your redirect address with `code` and your `state`. Check that `state` is the one you sent. The code lasts **2 minutes** and works **once**; using it a second time revokes what the first use gave.

### 4. Swap the code for tokens

```sh
curl -X POST "https://<your projqo address>/api/oauth/token" \
  -d grant_type=authorization_code \
  -d code="<code>" \
  -d redirect_uri="https://app.example.com/callback" \
  -d client_id="<your client id>" \
  -d code_verifier="<verifier>"
```

An app with a server also sends its client secret, as `client_secret` in the body or in a Basic `Authorization` header.

The answer holds an **access token** (`pqa_…`, valid for 1 hour) and a **refresh token** (`pqr_…`). A refresh token is always given; you don't need to ask for offline access.

```json
{
  "access_token": "pqa_…",
  "token_type": "Bearer",
  "expires_in": 3600,
  "refresh_token": "pqr_…",
  "scope": "org:read tasks:read"
}
```

The answer doesn't name the organisation: call `GET /api/v1/me` (or `GET /api/v1/orgs`) with the access token to see which one the person connected. Send the parameters as `application/x-www-form-urlencoded`; the client secret and tokens are never accepted in the address.

### 5. Call the API

```sh
curl "https://<your projqo address>/api/v1/orgs" \
  -H "Authorization: Bearer pqa_…"
```

`GET /api/v1/orgs` lists the organisation the person connected.

### 6. Refresh before the hour is up

```sh
curl -X POST "https://<your projqo address>/api/oauth/token" \
  -d grant_type=refresh_token \
  -d refresh_token="pqr_…" \
  -d client_id="<your client id>"
```

Each refresh gives a new access token **and a new refresh token**: keep the new one and throw the old one away.

- A refresh token ends after **90 days without use**.
- Using an old refresh token again revokes the whole grant: the person has to connect your app again. If two parts of your app refresh at once, let only one of them do it.

### 7. Revoking

`POST /api/oauth/revoke` with a token revokes it, for example when the person disconnects your app on your side.

## Errors

The token endpoint answers with OAuth 2.0's standard error codes (`invalid_request`, `invalid_client`, `invalid_grant`, `unsupported_grant_type`, `invalid_scope`), as `{ "error": …, "error_description": … }`. The token endpoint has limits of its own: at most 20 tokens a connection and 60 a person in 10 minutes, and 10 live access tokens a connection (the oldest end). Over them it answers 429 with `slow_down` and `Retry-After`.

Errors on the consent page go back to your redirect address as `error` (`invalid_request`, `unsupported_response_type`, `invalid_scope`, `access_denied`, `consent_required` for `prompt=none`), with your `state` and `iss`. An unknown client id or a redirect address you didn't register is never sent back: the person sees the problem on projqo's page.

## What people and admins control

- **Connected apps**, in the person's Personal settings, lists each app, its organisation, its scopes, when it was connected and last used, with **Revoke**. Changing their password offers to revoke them all. They are emailed when a new app is connected.
- **Organisation admins** choose in **Organisation settings › App access** which apps from other organisations may be connected: "Only apps an admin approved" (the default), "Any app" or "No apps". They see every app connected in the organisation and can revoke one for everyone.

Tokens are opaque strings looked up at every call, not self-contained tokens, so a revoke works at once.
