Skip to content

Auth Module Architecture

The auth code lives in two crates. octofhir-auth holds the logic and the storage traits. octofhir-auth-postgres implements those traits on PostgreSQL. octofhir-server mounts the HTTP routes and the middleware.

For how to use it, see Authentication.

┌──────────────────────────────────────────────────────────────┐
│ octofhir-server │
│ Axum router → auth middleware → FHIR handlers │
└──────────────┬───────────────────────────────────────────────┘
▼
┌──────────────────────────────────────────────────────────────┐
│ octofhir-auth │
│ OAuth 2.0 server SMART on FHIR UDAP │
│ Token service AccessPolicy engine (matcher, QuickJS) │
└──────────────┬───────────────────────────────────────────────┘
▼
┌──────────────────────────────────────────────────────────────┐
│ octofhir-auth-postgres │
│ Clients, users, roles, sessions, tokens, policies │
└──────────────────────────────────────────────────────────────┘
Module (octofhir-auth) Purpose
config The [auth] configuration
oauth Authorization, client authentication, PKCE, sessions
http Handlers for the /auth/* endpoints
token JWT signing and validation, introspection, revocation
smart SMART scopes, launch context, discovery
policy AccessPolicy engine, policy cache and reload
federation External identity provider types, client JWKS cache
udap UDAP registration, see UDAP
middleware, extractors Axum extractors such as BearerAuth and AdminAuth
audit Security audit events
storage Storage traits
  • Authorization code flow with PKCE
  • Client credentials flow for backend services
  • Refresh token flow
  • Password grant, for clients that list it, such as the built-in octofhir-ui client
  • Token introspection (RFC 7662) and revocation (RFC 7009)
  • SMART v2 scopes (patient/*.rs, user/*.cruds, system/*.rs), and v1 suffixes
  • EHR launch and standalone launch
  • Patient and encounter context
  • OpenID Connect
  • JWT with RS256, RS384 and ES384
  • Lifetimes from [auth.oauth]
  • See JWT Key Persistence for signing keys

A request first passes the SMART scope check. Matching AccessPolicy resources are then evaluated in priority order. A policy uses the allow or deny engine, or a QuickJS script. Policies are kept in a cache that reloads when an AccessPolicy changes. See Access Policies.

IdentityProvider resources name OpenID Connect, OAuth 2.0 and SAML 2.0 providers. http/federated.rs runs the sign-in: PKCE and a nonce for OAuth, the ID token checked against the provider’s keys, SAML responses checked in federation/saml.rs; then it finds or creates the user and applies the role rules. The IdentityProviderHook keeps the registered providers in step with the resources, so a change needs no restart. See Identity Providers.

Security-relevant events are written as FHIR AuditEvent resources.

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.signing]
algorithm = "RS384"
[auth.smart]
launch_ehr_enabled = true
launch_standalone_enabled = true
public_clients_allowed = true
openid_enabled = true
[auth.policy]
quickjs_enabled = true

See Configuration for the rest.