# Scopes

Scopes are granted to your client at registration. Requesting a scope your client was not
granted causes the token mint to fail with `invalid_scope`. SMART v2 syntax (`.rs`, `.cru`)
is used: `r`=read, `s`=search, `c`=create, `u`=update.

## Patient / user context (App Launch)

Used with the Authorization Code + PKCE flow when a user is present.

| Scope | Grants |
|---|---|
| `patient/Patient.rs` | Read and search the in-context patient |
| `user/Practitioner.rs` | Read and search practitioners visible to the user |
| `launch` | EHR launch context |
| `launch/patient` | Patient launch context |

## System context (Backend Services)

Used with the `client_credentials` grant. Every `system/*` scope requires a `tenant_id`.

| Scope | Grants |
|---|---|
| `system/Patient.rs` | Read and search Patients in the token's tenant |
| `system/Practitioner.rs` | Read and search Practitioners |
| `system/Encounter.rs` | Read and search Encounters |
| `system/DocumentReference.cru` | Create, read, and update DocumentReferences |

## Identity and session

| Scope | Grants |
|---|---|
| `openid` | OpenID Connect sign-in (`sub`, `tenant_id` claims) |
| `profile` | `name` claim |
| `email` | `email` claim |
| `fhirUser` | `fhirUser` claim: an absolute URL to the user's Practitioner or Patient resource |
| `offline_access` | Issues a `refresh_token` |

## What lands in the token response

| You requested | You get back |
|---|---|
| `openid` | `id_token`, `sub`, `tenant_id` |
| `fhirUser` | `fhirUser` claim in the access token |
| `launch/patient` (with launch context) | `patient` (FHIR patient id) in the token response body |
| `offline_access` | `refresh_token` |

## Multi-tenant narrowing

All access is scoped to one tenant. A `system/Patient.rs` token **cannot** read another
tenant's patients regardless of scope: searches only return the token's tenant, and reading
another tenant's resource by id returns `404`.
