Security
OpenEMR and SMART on FHIR
The lab's "hospital CIS": where FHIR and SMART fit in a real hospital, how OpenEMR is wired in, and how to run EHR and standalone launches with the SMART Inspector.
Where FHIR and SMART fit in practice#
A hospital isn't "a FHIR system". It's a clinical information system (CIS) plus departmental systems, tied together by an integration engine:
PAS / EMR (the CIS) Departmental systems (lab, radiology, pharmacy)
│ HL7 v2 ADT │ HL7 v2 ORM / ORU
▼ ▼
┌────────────── Integration engine: routes, transforms, v2 ⇄ FHIR ──────────────┐
└──────┬────────────────────────────┬──────────────────────────────┬─────────────┘
▼ ▼ ▼
other internal systems FHIR server / CDR (apps read) external partners
▲ (My Health Record, eRequesting)
│ FHIR REST + SMART tokens
apps: portals, clinician apps, analyticsFHIR shows up in three places:
- As an API facade over the CIS. The EHR keeps its own database and exposes FHIR at the edge. Epic, Cerner and OpenEMR all work this way.
- As a clinical data repository. The engine turns v2 feeds into FHIR resources stored in a FHIR server, as with the lab's HAPI.
- For external exchange, using AU Core, eRequesting and the Patient Summary.
SMART on FHIR is how an app gets permission to use the EHR's FHIR API. The EHR is the authorisation server: it issues a token scoped to a user, a patient and a set of resource types. The app never touches the database.
How the lab maps to that#
| Real world | In the lab |
|---|---|
| The EMR / CIS | OpenEMR 8.5 at https://emr.fhirdrill.dev (LAN http://192.168.1.181:8300) |
| The EMR's FHIR facade | https://emr.fhirdrill.dev/apis/default/fhir (FHIR R4, US Core profiles) |
| The EMR's SMART authorisation server | https://emr.fhirdrill.dev/oauth2/default/… |
| A SMART app | SMART Inspector at https://emr.fhirdrill.dev/smart/ |
| Integration engine | Open Integration Engine |
| CDR / FHIR server | HAPI (/fhir) |
| External partner | AU Core mock (/au-core/fhir) |
US Core, not AU Core
OpenEMR's FHIR uses US Core profiles. Turning its data into AU Core (in the engine, then posting it to the AU Core mock) is a realistic integration exercise.
The data in OpenEMR#
15 living Synthea patients, seeded through OpenEMR's Standard REST API by ~/fhir-lab/openemr/seed/seed.py: demographics, active problems (SNOMED codes), allergies, active medications (RxNorm) and one encounter with vital signs each. Log in as admin with the password from ~/fhir-lab/openemr/.env (OE_PASS). Move it to your password manager, and change it in OpenEMR if you like.
Run an EHR launch#
Open a patient's chart#
Log in to OpenEMR, then go to Patient → Finder and open a patient, for example Chester Aufderhar, who has rich data.
Launch the app from the chart#
On the dashboard, expand SMART Enabled Apps and click Launch next to FHIR Drill SMART Inspector.
Approve the scopes#
OpenEMR shows which resource types the app wants for this patient. Click Authorize. The first time, it may ask you to sign in to its OAuth screen.
Read what happened#
The Inspector shows each step: the iss and launch token it received, discovery, the authorize request (including PKCE), the callback, the token response (with patient and fhirUser), and every FHIR call. Then it shows the chart patient's problems, allergies, medications and vitals.
Run a standalone launch#
Open https://emr.fhirdrill.dev/smart/ and click Start standalone launch. With no launch token, the app asks for launch/patient, and OpenEMR shows a patient picker before consent. That's how patient-facing and independent apps work.
What the Inspector is registered as#
| Setting | Value | Why |
|---|---|---|
| Client type | Public (no secret), PKCE S256 | A browser app can't keep a secret |
| Scopes | openid fhirUser launch patient/Patient.rs patient/Condition.rs patient/AllergyIntolerance.rs patient/MedicationRequest.rs patient/Observation.rs patient/Encounter.rs |
patient/*: only the patient in context |
| Standalone extra | launch/patient |
Asks OpenEMR to show the patient picker |
| Launch and redirect address | https://emr.fhirdrill.dev/smart/ |
Same origin as OpenEMR, so no cross-site complications |
Lessons from setting it up:
- OpenEMR refuses
user/*scopes for public clients. Clinician-wide access needs an app that can authenticate itself (a confidential client with a secret or JWKS). A browser app getspatient/*. - Don't ask for
launch/patientin an EHR launch. It means "let the user pick a patient", so OpenEMR shows a picker even though the chart already supplied one.launchalone carries the chart's patient. - The password grant is admin-only. Self-registration strips it. The seeder's client had it switched on in the database, and the global OAuth2 password grant setting is off except while seeding.
Register an app#
docker run --rm --network openemr_default curlimages/curl:8.11.1 -sk -X POST https://openemr/oauth2/default/registration \
-H "Content-Type: application/json" --data '{
"application_type": "public",
"client_name": "My SMART app",
"redirect_uris": ["https://emr.fhirdrill.dev/myapp/"],
"initiate_login_uri": "https://emr.fhirdrill.dev/myapp/",
"scope": "openid fhirUser launch patient/Patient.rs patient/Observation.rs",
"contacts": ["[email protected]"]
}'New clients start disabled. Enable one in OpenEMR under Administration → System → API Clients (or in the oauth_clients table). Serve the app from the gateway (~/fhir-lab/private/html/<app>/) so it shares the emr.fhirdrill.dev origin. The Inspector's client id is in smart/config.js.
The OpenEMR patch#
OpenEMR 8.5.0 has two bugs in the code that binds a clinician-launched token to its patient. Without fixing them, every provider-launched app with patient/* scopes gets 401:
populateTokenContextForRequest()passes the user's numeric id where a user UUID is required.findPatientByUuid()looks the patient up by the text UUID, but the column is binary.
The lab mounts a patched copy of BearerTokenAuthorizationStrategy.php with both one-line fixes (~/fhir-lab/openemr/patches/, explained in its README; the original is kept as .orig). When upgrading OpenEMR, remove the mount first and test whether upstream has fixed them.
Reseed or add patients#
cd ~/fhir-lab/openemr && set -a && . ./.env && set +a
q(){ docker exec -e MYSQL_PWD="$MYSQL_ROOT_PASS" openemr-db-1 mariadb -uroot openemr -e "$1"; }
q "UPDATE globals SET gl_value='1' WHERE gl_name='oauth_password_grant'" # on, only while seeding
docker run --rm --network openemr_default -e OE_PASS -v $PWD/seed:/seed:ro \
-v $HOME/fhir-lab/synthea/output/fhir:/synthea:ro python:3.13-slim python -I /seed/seed.py 25
q "UPDATE globals SET gl_value='0' WHERE gl_name='oauth_password_grant'" # off againThe seeder is safe to re-run. It skips patients that already exist, but still adds medications to any patient that has none.
Troubleshooting#
| Symptom | Cause |
|---|---|
| Launch button missing on the chart | The app isn't enabled, or has no initiate_login_uri |
invalid_client_metadata … only allowed for confidential clients |
A public client asked for user/* or system/* scopes |
| Patient picker during an EHR launch | The app asked for launch/patient. Use launch |
| 401 on every FHIR read after a good token | The patch mount is missing (see above). Check /var/log/apache2/error.log in the container |
| "State mismatch" in the Inspector | The flow was started in a different browser. Click Start over |