This page is part of the Smart App Launch Implementation Guide (v1.0.0: STU 1) based on FHIR R3. The current version which supercedes this version is 2.0.0. For a full list of available versions, see the Directory of published versions
# !pip3 install python-jose
from Crypto.PublicKey import RSA
import json
import jose.jwk
import jose.jwt
import jose.constants
To create self-contained example, we'll generate a new RSA Key for a fake organization called "my-ehr.org", and we'll use that for the operations below.
key = RSA.generate(2048)
private = key.exportKey('PEM').decode()
public = key.publickey().exportKey().decode()
print(public, "\n\n", private)
Servers will create a signed JWT by following a process like this.
These should include:
sub
: the useraud
: the app for whom this ID Token is being producediss
: an identifier for this EHR system)profile
: the absolute URL of the FHIR resource representing the current userSigning with the server's private key
claims = {
"sub": "alice",
"aud": "growth-chart-app-123",
"iss": "https://my-ehr.org/fhir",
"fhirUser": "https://my-ehr.org/fhir/Practitioner/123"
}
id_token = jose.jwt.encode(
claims,
key,
algorithm='RS384')
print(id_token)
A client obtains the ID Token as the result of an authorization operation. To validate the token, the client fetches the servers's public key, and then decodes the token. While decoding the token, the client must verify that the audience ('aud') matches its own client_id.
jose.jwt.decode(id_token, public, audience='growth-chart-app-123')