Skip to content

Authentication

OctoFHIR implements SMART on FHIR authorization based on OAuth 2.0 and OpenID Connect.

Endpoint Description
GET /.well-known/smart-configuration SMART configuration metadata
GET /.well-known/jwks.json, GET /auth/jwks Server’s public keys for JWT verification
Endpoint Description
GET /auth/authorize Authorization endpoint (interactive user login & consent)
POST /auth/authorize Authorization form submission (login/consent actions)
POST /auth/token Token endpoint (exchange codes, refresh tokens)
POST /auth/revoke Token revocation (RFC 7009)
POST /auth/introspect Token introspection (RFC 7662)
GET /auth/userinfo OpenID Connect UserInfo endpoint
POST /auth/logout Browser-based logout (revokes token, clears cookie)
  • authorization_code - Standard OAuth flow with PKCE
  • client_credentials - Machine-to-machine (system scope)
  • refresh_token - Refresh access tokens
Type Authentication PKCE Requirement Use Case
Public PKCE only Required (RFC 8252) Browser apps, mobile apps
Confidential (symmetric) client_secret Recommended (RFC 9207) Server-side apps
Confidential (asymmetric) private_key_jwt Recommended (RFC 9207) Bulk Data, secure backends

OctoFHIR enforces PKCE requirements according to OAuth 2.0 security best practices:

  • Public clients (confidential: false): PKCE is REQUIRED. Authorization requests without code_challenge and code_challenge_method will be rejected.
  • Confidential clients (confidential: true): PKCE is RECOMMENDED but optional. A warning is logged if PKCE is not used.
  • Only S256 challenge method is supported (SHA-256). The plain method is forbidden per SMART on FHIR security requirements.
  • launch - EHR launch (receive context from EHR)
  • launch/patient - Standalone launch with patient picker
  • launch/encounter - Standalone launch with encounter picker

Format: {context}/{resourceType}.{permissions}

  • Context: patient, user, system
  • Permissions: c (create), r (read), u (update), d (delete), s (search)

Examples:

patient/Patient.rs # Read/search Patient in patient context
user/Observation.cruds # Full access to Observations for logged-in user
system/*.rs # Read/search all resources (backend service)

SMART v1 suffixes are accepted and mean the same as their v2 counterparts. Clients of the Bulk Data IG STU 1 and 2 still ask for them.

v1 suffix v2 permissions Example
.read .rs system/*.read
.write .cud system/Patient.write
.* .cruds patient/*.*
  • openid - Enable OpenID Connect (required for /auth/userinfo)
  • fhirUser - Include FHIR resource reference in token/userinfo

All auth settings are in octofhir.toml under [auth]:

[auth]
issuer = "http://localhost:8888"
[auth.oauth]
access_token_lifetime = "1h"
refresh_token_lifetime = "90d"
grant_types = ["authorization_code", "client_credentials", "refresh_token"]
[auth.smart]
launch_ehr_enabled = true
launch_standalone_enabled = true
public_clients_allowed = true
openid_enabled = true
supported_scopes = ["openid", "fhirUser", "patient/*.cruds", "..."]
[auth.signing]
algorithm = "RS384" # RS256, RS384, or ES384

See octofhir.toml for the full configuration reference including:

  • [auth.policy] - Access policy engine settings
  • [auth.audit] - Audit logging options
Terminal window
# 1. Redirect user to authorize
open "http://localhost:8888/auth/authorize?\
client_id=my-app&\
redirect_uri=http://localhost:3000/callback&\
response_type=code&\
scope=openid%20fhirUser%20patient/Patient.rs&\
state=random-state&\
code_challenge=BASE64URL_CHALLENGE&\
code_challenge_method=S256"
# 2. Exchange code for tokens
curl -X POST http://localhost:8888/auth/token \
-d "grant_type=authorization_code" \
-d "code=AUTHORIZATION_CODE" \
-d "redirect_uri=http://localhost:3000/callback" \
-d "client_id=my-app" \
-d "code_verifier=ORIGINAL_VERIFIER"
# 3. Use access token
curl -H "Authorization: Bearer ACCESS_TOKEN" \
http://localhost:8888/fhir/Patient
Terminal window
curl -X POST http://localhost:8888/auth/token \
-u "client_id:client_secret" \
-d "grant_type=client_credentials" \
-d "scope=system/Patient.rs"

A backend service authenticates with a signed JWT (private_key_jwt) instead of a secret. The server holds only the client’s public keys.

Register a confidential Client with the client_credentials grant and its public keys. Clients are internal resources, served at the root rather than under /fhir:

PUT /Client/reporting-backend
Authorization: Bearer {admin token}
Content-Type: application/json
{
"resourceType": "Client",
"id": "reporting-backend",
"clientId": "reporting-backend",
"name": "Reporting backend",
"confidential": true,
"active": true,
"grantTypes": ["client_credentials"],
"scopes": ["system/*.read"],
"jwks": "{\"keys\":[{\"kty\":\"EC\",\"crv\":\"P-384\",\"alg\":\"ES384\",\"use\":\"sig\",\"kid\":\"key-1\",\"x\":\"...\",\"y\":\"...\"}]}"
}

Give the keys in one of two ways:

Field Value
jwks The JWK Set as a JSON string. The resource has no JSON object type, so the set is text inside the resource.
jwksUri Where the server fetches the JWK Set. HTTPS only; other schemes are refused at the token endpoint. Fetched sets are cached.

With both, jwks wins.

Scopes alone do not grant access. The policy engine denies by default, so the client also needs an access policy that matches it:

PUT /AccessPolicy/reporting-backend-access
Authorization: Bearer {admin token}
Content-Type: application/json
{
"resourceType": "AccessPolicy",
"id": "reporting-backend-access",
"name": "Reporting backend",
"active": true,
"engine": {"type": "allow"},
"matcher": {"clients": ["reporting-backend"]},
"priority": 1
}

Sign an assertion with the private key. Use RS384 or ES384, the algorithms SMART Backend Services requires, and name the key in the kid header.

Claim Value
iss, sub The client id
aud The token endpoint, exactly: {base_url}/auth/token
exp At most 5 minutes from now
jti A unique value per assertion

Send it with no client_id: the client is the assertion’s iss, verified against the client’s keys.

Terminal window
curl -X POST https://fhir.example.org/auth/token \
-d "grant_type=client_credentials" \
-d "scope=system/*.read" \
-d "client_assertion_type=urn:ietf:params:oauth:client-assertion-type:jwt-bearer" \
-d "client_assertion=SIGNED_JWT"

The server records each jti until its assertion expires. A second use of the same jti is refused with invalid_client, and the record is in the database, so a restart does not reopen the window.

The requested scopes must be covered by the client’s scopes. A client that runs $bulk-submit needs system/bulk-submit there. To advertise a scope in the SMART configuration, add it to auth.smart.supported_scopes.

The Inferno Bulk Data test kit authenticates this way: scripts/inferno/run-bulk.ts registers such a client with its AccessPolicy.

People can sign in through OpenID Connect, OAuth 2.0 and SAML 2.0 providers, with roles from their groups. See Identity Providers.

The /auth/authorize endpoint provides a server-rendered user interface for authentication and consent. This enables standard OAuth 2.0 authorization code flow with a user-friendly login experience.

  1. Initial Request - Client redirects user to /auth/authorize with OAuth parameters
  2. Authentication - User sees a login form and enters credentials
  3. Consent - After successful login, user sees requested scopes and can approve/deny
  4. Authorization Code - User is redirected back to client with authorization code
  5. Token Exchange - Client exchanges code for access token at /auth/token
  • Server-rendered UI with modern glassmorphism design matching OctoFHIR’s aesthetics
  • Session management via secure HTTP-only cookies
  • Consent tracking - remembers user’s previous consent decisions
  • PKCE validation - enforced based on client type (public vs confidential)
  • Error handling - user-friendly error pages with detailed diagnostics
Parameter Required Description
response_type Yes Must be code
client_id Yes Client identifier
redirect_uri Yes Must match registered redirect URI
scope Yes Requested scopes (space-separated)
state Yes CSRF protection (min 122 bits entropy)
code_challenge Conditional Required for public clients
code_challenge_method Conditional Must be S256 if provided
aud Yes (SMART) FHIR server base URL
launch Optional EHR launch context identifier
nonce Optional OpenID Connect replay protection

The authorization flow uses a session cookie (oauth_session) to track the user’s authentication state:

  • Name: oauth_session
  • Path: /auth
  • Lifetime: 10 minutes
  • Attributes: HttpOnly, SameSite=Lax, Secure (in production)

User consent decisions are stored in the database (octofhir_auth.user_consents table). When a user approves access for a client with specific scopes, subsequent authorization requests with the same scopes skip the consent screen for better UX.