|
1 |
| -function _backend_services_create_jwt(config::BackendServicesConfig) |
| 1 | +function _backend_services_create_jwt(config::BackendServicesConfig, token_endpoint::AbstractString) |
| 2 | + # Random string that uniquely identifies the JWT |
2 | 3 | jti = Random.randstring(150)
|
3 | 4 |
|
4 |
| - now = TimeZones.now(TimeZones.localzone()) |
5 |
| - expiration_time = now + Dates.Minute(4) |
6 |
| - expiration_time_seconds_since_epoch_utc = integer_seconds_since_the_epoch_utc(expiration_time) |
7 |
| - |
| 5 | + # Expiration time (integer) in seconds since "epoch" |
| 6 | + # SHALL be no more than 5 minutes in the future |
| 7 | + expiration_time = Dates.now(Dates.UTC) + Dates.Minute(4) |
| 8 | + expiration_time_seconds_since_epoch_utc = round(Int, Dates.datetime2unix(expiration_time)) |
| 9 | + |
8 | 10 | jwt_payload_claims_dict = Dict(
|
9 |
| - "iss" => config.iss, |
10 |
| - "sub" => config.sub, |
11 |
| - "aud" => config.token_endpoint, |
| 11 | + "iss" => config.client_id, |
| 12 | + "sub" => config.client_id, |
| 13 | + "aud" => token_endpoint, |
12 | 14 | "jti" => jti,
|
13 | 15 | "exp" => expiration_time_seconds_since_epoch_utc,
|
14 | 16 | )
|
15 |
| - jwt = JSONWebTokens.encode(config.private_key, jwt_payload_claims_dict) |
| 17 | + jwt = JWTs.JWT(; payload = jwt_payload_claims_dict) |
| 18 | + |
| 19 | + # Sign |
| 20 | + JWTs.sign!(jwt, config.key, config.keyid) |
| 21 | + @assert JWTs.issigned(jwt) |
| 22 | + @assert JWTs.kid(jwt) == config.keyid |
16 | 23 |
|
17 |
| - return jwt |
| 24 | + return string(jwt) |
18 | 25 | end
|
19 | 26 |
|
| 27 | +# Obtain the token endpoint from the well-known URIs |
| 28 | +# Ref: https://www.hl7.org/fhir/smart-app-launch/backend-services.html#retrieve-well-knownsmart-configuration |
| 29 | +function _token_endpoint_wellknown(config::BackendServicesConfig) |
| 30 | + # Request the SMART configuration file |
| 31 | + _config_response = HTTP.request( |
| 32 | + "GET", |
| 33 | + joinpath(config.base_url, ".well-known/smart-configuration"); |
| 34 | + # In principle, it should be possible to omit the header |
| 35 | + # (and servers may ignore it anyway) |
| 36 | + # Ref: https://www.hl7.org/fhir/smart-app-launch/conformance.html#using-well-known |
| 37 | + headers = ("Accept" => "application/json",), |
| 38 | + # Old servers might still only support the /metadata endpoint (even though its use for SMART capabilities is deprecated) |
| 39 | + # Hence we do not throw an exception if the request fails but try the /metadata endpoint first |
| 40 | + # Ref: https://hl7.org/fhir/smart-app-launch/1.0.0/conformance/index.html#declaring-support-for-oauth2-endpoints |
| 41 | + # Ref: https://www.hl7.org/fhir/smart-app-launch/conformance.html#smart-on-fhir-oauth-authorization-endpoints-and-capabilities |
| 42 | + status_exception = false, |
| 43 | + ) |
| 44 | + |
| 45 | + # Exit gracefully (return `nothing`) if the server does not convey its SMART capabilities using well-known URIs |
| 46 | + if _config_response.status != 200 |
| 47 | + return nothing |
| 48 | + end |
| 49 | + |
| 50 | + # Extract the token endpoint from the JSON response |
| 51 | + config_response = JSON3.read(_config_response.body) |
| 52 | + get(config_response, :token_endpoint) do |
| 53 | + error( |
| 54 | + "SMART configuration: Violation of the FHIR specification. The mandatory `token_endpoint` is missing from the Well-Known Uniform Resource Identifiers (URIs) JSON document.", |
| 55 | + ) |
| 56 | + end::String |
| 57 | +end |
| 58 | + |
| 59 | +# Obtain the token endpoint from the CapabilityStatement at the /metadata endpoint |
| 60 | +# Note: Declaring SMART capabilities using the /metadata endpoint is deprecated but old servers might still not support the well-known URIs |
| 61 | +# Ref: https://hl7.org/fhir/smart-app-launch/1.0.0/conformance/index.html#declaring-support-for-oauth2-endpoints |
| 62 | +function _token_endpoint_metadata(config::BackendServicesConfig) |
| 63 | + # Request the CapabilityStatement |
| 64 | + _metadata_response = HTTP.request( |
| 65 | + "GET", |
| 66 | + joinpath(config.base_url, "metadata"); |
| 67 | + # We only support FHIR version R4 |
| 68 | + # Ref: https://hl7.org/fhir/R4/versioning.html#mt-version |
| 69 | + headers = ("Accept" => "application/fhir+json; fhirVersion=4.0"), |
| 70 | + # We throw our own, hopefully more descriptive, exception if necessary |
| 71 | + status_exception = false, |
| 72 | + ) |
| 73 | + |
| 74 | + # Exit gracefully (return `nothing`) if the server does not convey its SMART capabilities at the /metadata endpoint |
| 75 | + if _metadata_response.status != 200 |
| 76 | + return nothing |
| 77 | + end |
| 78 | + |
| 79 | + # Extract the token endpoint from the JSON response |
| 80 | + # Ref: https://hl7.org/fhir/smart-app-launch/1.0.0/conformance/index.html#declaring-support-for-oauth2-endpoints |
| 81 | + # Ref: https://hl7.org/fhir/R4/capabilitystatement.html |
| 82 | + compat_statement = JSON3.read(_metadata_response.body) |
| 83 | + rest = get(compat_statement, :rest, nothing) |
| 84 | + if rest !== nothing |
| 85 | + for rest in compat_statement.rest |
| 86 | + security = get(rest, :security, nothing) |
| 87 | + if security !== nothing |
| 88 | + extensions = get(security, :extension, nothing) |
| 89 | + if extensions !== nothing |
| 90 | + for extension in extensions |
| 91 | + if get(extension, :url, nothing) === |
| 92 | + "http://fhir-registry.smarthealthit.org/StructureDefinition/oauth-uris" |
| 93 | + for url_value in extension.extension |
| 94 | + if url_value.url === "token" |
| 95 | + return url_value.valueUri::String |
| 96 | + end |
| 97 | + end |
| 98 | + end |
| 99 | + end |
| 100 | + end |
| 101 | + end |
| 102 | + end |
| 103 | + end |
| 104 | + |
| 105 | + error( |
| 106 | + "SMART configuration: Violation of the FHIR specification. The mandatory `token` url of the OAuth2 token endpoint is missing from the FHIR CompatibilityStatement.", |
| 107 | + ) |
| 108 | +end |
| 109 | + |
| 110 | +# Ref: https://www.hl7.org/fhir/smart-app-launch/backend-services.html |
20 | 111 | """
|
21 | 112 | backend_services(config::BackendServicesConfig)
|
22 | 113 | """
|
23 | 114 | function backend_services(config::BackendServicesConfig)
|
24 |
| - jwt = _backend_services_create_jwt(config) |
| 115 | + # Obtain the token endpoint: Try first the well-known URI and then the /metadata endpoint (deprecated) |
| 116 | + # On Julia >= 1.7 this can be simplified to |
| 117 | + # token_endpoint = @something _token_endpoint_wellknown(config) _token_endpoint_metadata(config) error("...") |
| 118 | + token_endpoint = _token_endpoint_wellknown(config) |
| 119 | + if token_endpoint === nothing |
| 120 | + token_endpoint = _token_endpoint_metadata(config) |
| 121 | + if token_endpoint === nothing |
| 122 | + # Ref: https://www.hl7.org/fhir/smart-app-launch/conformance.html#smart-on-fhir-oauth-authorization-endpoints-and-capabilities |
| 123 | + # Ref: https://hl7.org/fhir/smart-app-launch/1.0.0/conformance/index.html#smart-on-fhir-oauth-authorization-endpoints |
| 124 | + error( |
| 125 | + "SMART configuration: Violation of the FHIR specification. The FHIR server does neither convey its SMART capabilities using a Well-Known Uniform Resource Identifiers (URIs) JSON file nor its CapabilityStatement.", |
| 126 | + ) |
| 127 | + end |
| 128 | + end |
| 129 | + |
| 130 | + # Obtain the access token |
| 131 | + # Ref: https://www.hl7.org/fhir/smart-app-launch/backend-services.html#obtain-access-token |
| 132 | + # Create JWT |
| 133 | + jwt = _backend_services_create_jwt(config, token_endpoint) |
25 | 134 |
|
26 | 135 | body_params = Dict{String, String}()
|
27 | 136 | body_params["grant_type"] = "client_credentials"
|
28 | 137 | body_params["client_assertion_type"] = "urn:ietf:params:oauth:client-assertion-type:jwt-bearer"
|
29 | 138 | body_params["client_assertion"] = jwt
|
30 |
| - |
31 |
| - if config.scope !== nothing |
32 |
| - body_params["scope"] = config.scope |
33 |
| - end |
| 139 | + body_params["scope"] = config.scope |
34 | 140 |
|
35 | 141 | _response = HTTP.request(
|
36 | 142 | "POST",
|
37 |
| - config.token_endpoint; |
38 |
| - headers = Dict("Content-Type" => "application/x-www-form-urlencoded"), |
| 143 | + token_endpoint; |
| 144 | + headers = ("Content-Type" => "application/x-www-form-urlencoded",), |
39 | 145 | body = URIs.escapeuri(body_params),
|
40 | 146 | )
|
41 | 147 |
|
42 |
| - access_token_response = JSON3.read(String(_response.body)) |
| 148 | + access_token_response = JSON3.read(_response.body) |
43 | 149 | access_token = access_token_response.access_token
|
44 | 150 |
|
45 | 151 | access_token_is_jwt, access_token_jwt_decoded = try_decode_jwt(access_token)
|
|
0 commit comments