# OAuth integrations

OAuth sign-in for a seeded integration: the authorization code grant with PKCE, refresh token rotation, 60-minute access tokens on the simulator clock and refresh replay for about five minutes.

An OAuth integration signs in to the simulator as it does to Airtable, through two routes under `airtable.com/oauth2/v1/`:

- `GET /oauth2/v1/authorize` gives consent.
- `POST /oauth2/v1/token` handles the authorization code and refresh token grants.

The simulator creates no integrations, so seed yours in `state_oauth_integration`:

| Field | Value |
|---|---|
| `client_id`, `client_secret` | the integration's credentials |
| `redirect_uris` | the URIs `GET /oauth2/v1/authorize` accepts |
| `consent_actor` | the `id` of the user who approves consent |

```bash
curl -s -o /dev/null -w '%{http_code}\n' -X POST "$RYSTIC_URL/_rystic/seed" -d '{"state_oauth_integration": {"int_sync": {"id": "int_sync", "client_id": "cid_sync", "client_secret": "cs_sync", "redirect_uris": ["http://localhost:3000/callback"], "consent_actor": "usrLnch0000000002"}}}'
# -> 204
```

### Sign in with the code grant

`GET /oauth2/v1/authorize` with a registered `redirect_uri` approves at once and redirects there with a `code` and your `state`.

**Differs from Airtable:** no consent screen shows; the request is approved at once.

```bash
curl -s -o /dev/null -w '%{http_code} %{redirect_url}\n' "$RYSTIC_URL/oauth2/v1/authorize?client_id=cid_sync&redirect_uri=http%3A%2F%2Flocalhost%3A3000%2Fcallback&response_type=code&scope=data.records:read&state=xyz&code_challenge=E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM&code_challenge_method=S256" | sed 's/code=[^&]*&//'
# -> 302 http://localhost:3000/callback?state=xyz
```

Exchange the code once, as a form, with the `code_verifier` of its PKCE `S256` challenge. The client goes under HTTP Basic or as `client_id`. The access token acts as the user who approved it:

```bash
CODE=$(curl -s -o /dev/null -w '%{redirect_url}' "$RYSTIC_URL/oauth2/v1/authorize?client_id=cid_sync&redirect_uri=http%3A%2F%2Flocalhost%3A3000%2Fcallback&response_type=code&scope=data.records:read&state=xyz&code_challenge=E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM&code_challenge_method=S256" | sed 's/.*code=\([^&]*\).*/\1/')
curl -s "$RYSTIC_URL/oauth2/v1/token" -d grant_type=authorization_code -d client_id=cid_sync -d code="$CODE" \
  -d redirect_uri=http://localhost:3000/callback -d code_verifier=dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk > grant.json
jq -c '{token_type, expires_in, refresh_expires_in, scope}' grant.json
# -> {"token_type":"Bearer","expires_in":3600,"refresh_expires_in":5184000,"scope":"data.records:read"}
curl -s "$RYSTIC_URL/v0/meta/whoami" -H "Authorization: Bearer $(jq -r .access_token grant.json)"
# -> {"id":"usrLnch0000000002","scopes":["data.records:read"]}
```

With an OAuth token, `GET /v0/meta/whoami` returns the token's `scopes`, and the `email` only when they include `user.email:read`.

Consent requires PKCE with the `S256` method, as Airtable does. Without a `code_challenge` or a `code_challenge_method`, consent redirects to your `redirect_uri` with `error=invalid_request` and no `code`:

```bash
curl -s -o /dev/null -w '%{redirect_url}\n' "$RYSTIC_URL/oauth2/v1/authorize?client_id=cid_sync&redirect_uri=http%3A%2F%2Flocalhost%3A3000%2Fcallback&response_type=code&scope=data.records:read&state=xyz"
# -> http://localhost:3000/callback?error=invalid_request&error_description=Must+include+both+%22code_challenge%22+and+%22code_challenge_method%22.&state=xyz
```

Any method other than `S256`, `plain` among them, redirects with another `error_description`:

```bash
curl -s -o /dev/null -w '%{redirect_url}\n' "$RYSTIC_URL/oauth2/v1/authorize?client_id=cid_sync&redirect_uri=http%3A%2F%2Flocalhost%3A3000%2Fcallback&response_type=code&scope=data.records:read&state=xyz&code_challenge=dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk&code_challenge_method=plain"
# -> http://localhost:3000/callback?error=invalid_request&error_description=%22code_challenge_method%22+must+be+%22S256%22.+Note+that+the+plain+method+is+not+supported.&state=xyz
```

An exchange with a wrong `code_verifier` spends the code, so a retry returns 400 `invalid_grant`:

```bash
CODE=$(curl -s -o /dev/null -w '%{redirect_url}' "$RYSTIC_URL/oauth2/v1/authorize?client_id=cid_sync&redirect_uri=http%3A%2F%2Flocalhost%3A3000%2Fcallback&response_type=code&scope=data.records:read&state=xyz&code_challenge=E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM&code_challenge_method=S256" | sed 's/.*code=\([^&]*\).*/\1/')
curl -s -w ' %{http_code}\n' "$RYSTIC_URL/oauth2/v1/token" -d grant_type=authorization_code -d client_id=cid_sync -d code="$CODE" \
  -d redirect_uri=http://localhost:3000/callback -d code_verifier=wrong-verifier-wrong-verifier-wrong-verifier-0
# -> {"error":"invalid_grant","error_description":"Unrecognized grant code"} 400
curl -s -w ' %{http_code}\n' "$RYSTIC_URL/oauth2/v1/token" -d grant_type=authorization_code -d client_id=cid_sync -d code="$CODE" \
  -d redirect_uri=http://localhost:3000/callback -d code_verifier=dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk
# -> {"error":"invalid_grant","error_description":"Unrecognized grant code"} 400
```

### Refresh a token

The refresh token grant returns a new access token and a new refresh token, and ends the old access token at once. Replaying a spent refresh token within 303 seconds returns the same new pair again, so a client that lost the response can recover it. After that the replay returns 400 `invalid_grant`, and the newest refresh token still works. Airtable's window is at least five minutes and shorter than 15 minutes:

```bash
R=$(jq -r .refresh_token grant.json)
curl -s "$RYSTIC_URL/oauth2/v1/token" -d grant_type=refresh_token -d client_id=cid_sync -d refresh_token="$R" > next.json
curl -s "$RYSTIC_URL/oauth2/v1/token" -d grant_type=refresh_token -d client_id=cid_sync -d refresh_token="$R" > again.json
[ "$(jq -c '[.access_token, .refresh_token]' again.json)" = "$(jq -c '[.access_token, .refresh_token]' next.json)" ] && echo same pair
# -> same pair
curl -s "$RYSTIC_URL/v0/meta/whoami" -H "Authorization: Bearer $(jq -r .access_token grant.json)"
# -> {"error":{"message":"Invalid authentication token","type":"UNAUTHORIZED"}}
curl -s -o /dev/null -w '%{http_code}\n' -X POST "$RYSTIC_URL/_rystic/clock" -d '{"advance_seconds": 304}'
# -> 204
curl -s -w ' %{http_code}\n' "$RYSTIC_URL/oauth2/v1/token" -d grant_type=refresh_token -d client_id=cid_sync -d refresh_token="$R"
# -> {"error":"invalid_grant","error_description":"Invalid token."} 400
curl -s -o newest.json -w '%{http_code}\n' "$RYSTIC_URL/oauth2/v1/token" -d grant_type=refresh_token -d client_id=cid_sync -d refresh_token="$(jq -r .refresh_token next.json)"
# -> 200
```

### Token expiry

Access tokens last 60 minutes and refresh tokens 60 days on the simulator clock, as the response's `expires_in` and `refresh_expires_in` show. An expired access token returns 401 `UNAUTHORIZED` on every API route:

```bash
curl -s -o /dev/null -w '%{http_code}\n' -X POST "$RYSTIC_URL/_rystic/clock" -d '{"advance_seconds": 3600}'
# -> 204
curl -s -o /dev/null -w '%{http_code}\n' "$RYSTIC_URL/v0/meta/whoami" -H "Authorization: Bearer $(jq -r .access_token newest.json)"
# -> 401
```

**Differs from Airtable:** an expired or replaced token's 401 response has no `WWW-Authenticate` header.

---

Source: https://www.rystic.ai/docs/simulators/airtable/features/oauth · Markdown: https://www.rystic.ai/docs/simulators/airtable/features/oauth.md
