Blueprint for standing up login and reauthentication for apps embedded in or launched from Epic Hyperdrive/Hyperspace and MyChart, implementing SMART on FHIR EHR-launch and standalone-launch over OAuth 2.0 with PKCE. Covers the launch handshake, token exchange against Epic's authorize/token endpoints, and short-lived session management with silent reauth.
Tags: healthcare, epic, smart-on-fhir, fhir, us-core, interoperability, api-led, patient-access
End-User Authentication
Objective: Apps launched from Epic Hyperdrive, Hyperspace, or MyChart must not present their own username/password prompt; they must trust Epic's OAuth 2.0 authorization server and derive the user, patient, and encounter context from the SMART launch. Done looks like a clinician double-clicking an embedded activity and landing inside the app already authenticated as themselves, scoped to the open chart, with the app holding a short-lived access token it can refresh silently until the Epic session ends. The problem is that home-grown login screens inside an Epic workflow break single sign-on, leak credentials, lose patient context, and get rejected in App Orchard / Epic security review. This accelerator packages the End-User Authentication Epic Data Sharing Playbook as a MuleSoft-anchored solution plan.
What's Getting Built
- Experience API Login (Experience API) — receives the EHR and standalone SMART launch and materializes the app session behind an opaque cookie ·
GET /launch, GET /callback
- Process API Auth (Process API) — orchestrates discovery, PKCE authorize, token exchange, and id_token validation against Epic OAuth ·
POST /authorize, POST /exchange, POST /session/refresh
- state-verifier store (Object Store) — short-TTL state and PKCE code_verifier per launch keyed by correlation ID ·
PUT /{correlationId}, GET /{correlationId}, DELETE /{correlationId}
- Epic OAuth Server (target) — Epic authorization server issuing the auth code and access/id tokens over PKCE ·
GET /oauth2/authorize, POST /oauth2/token
- Epic FHIR R4 (target) — serves SMART discovery and JWKS and dereferences the fhirUser claim to hydrate Practitioner or Patient identity ·
GET /.well-known/smart-configuration, GET /Practitioner/{id}, GET /Patient/{id}
Prerequisites
- App registered on open.epic.com / fhir.epic.com with a client id, redirect URI, and requested SMART scopes, promoted from the sandbox to the customer's production Epic via App Orchard
- Epic OAuth 2.0 endpoints known via the FHIR server SMART configuration document (.well-known/smart-configuration): authorize, token, and issuer URLs
- Public client using PKCE (S256) for browser-embedded apps, or a confidential client with a JWT client assertion (RS384, JWKS URL registered) for backend token exchange
- Registered launch and redirect URIs exactly matching what Epic sends, including the EHR launch URL for Hyperdrive embedding
- Anypoint Platform environment with a TLS-terminated Experience API and secure properties for client secret / signing key
- Agreement with the Epic team on requested scopes (launch, launch/patient, openid, fhirUser, offline_access) and whether refresh is permitted
1. Epic interfaces and standards
This accelerator implements the End-User Authentication Epic Data Sharing Playbook. Register the connecting app on Epic on FHIR and confirm the customer has the relevant Epic modules licensed before building.
- SMART on FHIR EHR launch and standalone launch
- OAuth 2.0 authorization code flow with PKCE (S256)
- Hyperdrive / Hyperspace embedded activity and MyChart web/mobile launch
- FHIR R4 Practitioner and Patient for the id_token fhirUser reference
- OpenID Connect id_token, JWKS, and .well-known/smart-configuration discovery
Architecture
2. Business requirements
- Never render an app-owned login form inside an Epic workflow; delegate every authentication to Epic's OAuth 2.0 authorization server.
- Preserve single sign-on so a clinician already logged into Hyperdrive is not challenged again when opening the embedded app.
- Carry launch context forward: the resolved user, patient, and encounter must scope everything the app does.
- Keep access tokens short-lived and bound to the Epic session; when Epic logs the user out, the app session must end too.
- Support both EHR launch from Hyperdrive/Hyperspace and standalone launch from MyChart with one codebase and distinct scope sets.
3. Launch handshake and discovery
- On EHR launch, capture the iss (FHIR base URL) and opaque launch parameter Epic passes; treat launch as context, never as a credential.
- Fetch iss plus /.well-known/smart-configuration to resolve the authorization and token endpoints at runtime rather than hardcoding per-customer URLs.
- Generate a cryptographically random state and PKCE code_verifier per launch and store them keyed by correlation ID in a short-TTL Object Store.
- Redirect the browser to authorize with response_type=code, client_id, redirect_uri, scope, state, aud=iss, launch, and code_challenge.
- Reject any callback whose state does not match the stored value to block CSRF and cross-launch mixups.
4. Token exchange and identity resolution
- Exchange the authorization code at the token endpoint with grant_type=authorization_code and the original code_verifier; confidential clients send a signed JWT assertion instead of a shared secret.
- Read patient, encounter, and other SMART launch context returned alongside the token and DataWeave-map them into the app session object.
- Validate the id_token signature against the issuer JWKS and check iss, aud, exp, and nonce before trusting any identity claim.
- Dereference the fhirUser claim to the Practitioner or Patient resource to hydrate a display name and role for the app.
- Store the access and refresh tokens encrypted, keyed to the app session, and expose only an opaque session cookie to the browser.
5. Session lifecycle and silent reauth
- Refresh proactively with offline_access refresh tokens a few seconds before exp, not on the first 401.
- For public browser apps without refresh, perform a silent re-authorize using the still-live Epic session to obtain a fresh code without user interaction.
- Alert on the absence of an expected token refresh, which is the failure mode that silently logs clinicians out mid-charting.
- Tie app session TTL to the Epic session; on refresh failure, tear down the app session rather than falling back to a local login.
- Bind each session to the launch patient and reject any request whose token context does not match the session's patient.
6. Error handling and operations
- Propagate a correlation ID from launch through authorize, callback, and token exchange so one clinician's failed login is traceable end to end.
- Route failed token exchanges and JWKS lookups to a dead-letter queue with the sanitized OAuth error, never the token or code itself.
- Distinguish user-recoverable errors (consent denied, session expired) from misconfiguration (invalid_client, redirect_uri mismatch) in logs and dashboards.
- Cache the JWKS and smart-configuration with a bounded TTL so an Epic key rotation is picked up without a redeploy.
- Surface a plain re-launch prompt to the clinician on unrecoverable failure instead of an OAuth stack trace.
7. Security and compliance
- Encrypt PHI in transit and at rest.
- Audit-log every access to and modification of patient data, and carry a correlation ID end to end.
- Enforce minimum-necessary access for integration operators and support users.
- Use PKCE (S256) for every browser-embedded public client and prefer confidential-client JWT assertions signed with a rotated RS384 key over shared secrets.
- Validate the id_token fully (signature via JWKS, iss, aud, exp, nonce) and never trust the launch or state parameters as proof of identity.
- Store client secrets and signing keys in Anypoint secure properties or a vault, never in the app bundle or the browser.
- Set the app session cookie HttpOnly, Secure, and SameSite, and expose only an opaque session handle so raw Epic tokens never reach the browser.
Sample payload — SMART EHR launch redirect to Epic authorize endpoint
GET https://fhir.epic.com/interconnect-fhir-oauth/oauth2/authorize?
response_type=code&
client_id=8a7f6c21-3b2e-4d19-9f0a-1c2d3e4f5a6b&
redirect_uri=https%3A%2F%2Fapp.example.org%2Fcallback&
scope=launch%20openid%20fhirUser%20launch%2Fpatient%20offline_access&
state=Zx9k2Qs7mVn4Lp3R&
aud=https%3A%2F%2Ffhir.epic.com%2Finterconnect-fhir-oauth%2Fapi%2FFHIR%2FR4&
launch=eyJhbGciOiJ...opaqueLaunchToken&
code_challenge=E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM&
code_challenge_method=S256
Testing and validation
- Test EHR launch from the Epic sandbox against fhir.epic.com and confirm patient and encounter context arrive in the token response.
- Test standalone MyChart launch with patient scopes and verify no clinician-only scopes are granted.
- Force a state mismatch and a replayed code and confirm both are rejected without issuing a session.
- Let an access token expire and assert silent refresh or silent re-authorize restores the session without a visible prompt.
- Rotate the Epic JWKS in the sandbox and confirm id_token validation recovers after TTL without redeploy.
- Verify redirect_uri and scope mismatches surface as invalid_request/invalid_client with the correlation ID logged, not a 500.
Phased rollout
Phase 1 — Connect
- Register the app on fhir.epic.com, obtain the client id, and record the sandbox authorize and token endpoints via smart-configuration.
- Stand up the Experience and Process APIs on Anypoint with PKCE and complete one end-to-end EHR launch in the sandbox.
Phase 2 — Map and test
- Map SMART launch context and id_token claims into the app session with DataWeave and validate against sandbox Practitioner/Patient resources.
- Run the negative-path suite (state mismatch, code replay, expiry, JWKS rotation) and confirm dead-letter routing and correlation IDs.
Phase 3 — Production
- Promote the app through App Orchard to the customer's production Epic and swap in production endpoints and keys via secure properties.
- Pilot with a small clinician cohort in Hyperdrive, watch refresh telemetry, then widen the embedded activity to all users.
Success metrics
- Login success rate and median time from Epic launch to authenticated app session
- Silent reauth / token refresh success rate and count of user-visible re-login prompts
- id_token validation failure rate broken down by cause (signature, aud, exp)
- Dead-letter queue depth for failed token exchanges
- Orphaned app sessions still live after Epic logout
Troubleshooting
- Epic returns invalid_request or redirect_uri mismatch at the authorize step. — Likely cause: The redirect_uri sent does not byte-for-byte match a URI registered for the client on fhir.epic.com, or the aud does not equal the launch iss. Fix: Register the exact redirect URI (scheme, host, path, no trailing-slash drift) and set aud to the iss received in the launch.
- Clinician is logged out mid-charting and forced to re-launch. — Likely cause: The access token expired and no proactive refresh or silent re-authorize ran before exp. Fix: Refresh a few seconds before exp using offline_access, or silently re-authorize against the live Epic session, and alert on missing refresh events.
- id_token rejected with a signature validation error after working for weeks. — Likely cause: Epic rotated its signing keys and the cached JWKS is stale. Fix: Cache JWKS with a bounded TTL and refetch on an unknown kid so key rotation is absorbed without a redeploy.
- App loads but has no patient context and FHIR reads are unscoped. — Likely cause: The launch scope was omitted or launch/patient was not requested, so Epic did not return patient in the token response. Fix: Request launch plus launch/patient for EHR launch and read the patient parameter from the token response into the session.
Deliverable: A deployable MuleSoft Experience API acting as a SMART on FHIR / OAuth 2.0 relying party for Epic-launched apps: EHR-launch and standalone-launch flows, PKCE, token exchange, session materialization from launch context, silent reauth, and token-lifecycle telemetry, packaged with Anypoint policies and DataWeave transforms.