From 2d4210d826d43cf268081646c0822a9785d325d3 Mon Sep 17 00:00:00 2001 From: Vladimir Ignatov Date: Tue, 28 May 2024 16:00:13 -0400 Subject: [PATCH] Add fhir_server to sim --- backend/index.ts | 13 +++++++++++-- backend/lib.ts | 10 ++++++++++ backend/routes/fhir/proxy.ts | 5 +++-- index.d.ts | 1 + src/isomorphic/codec.ts | 15 +++++++++++---- 5 files changed, 36 insertions(+), 8 deletions(-) diff --git a/backend/index.ts b/backend/index.ts index e3dc012..930e966 100644 --- a/backend/index.ts +++ b/backend/index.ts @@ -40,10 +40,19 @@ app.get("/smart-style.json", (_, res) => { }) // Auth server -app.use(["/v/:fhir_release/sim/:sim/auth", "/v/:fhir_release/auth"], authServer) +app.use([ + "/v/:fhir_release/sim/:sim/s/:server/auth", + "/v/:fhir_release/sim/:sim/auth", + "/v/:fhir_release/auth" +], authServer) // FHIR servers -app.use(["/v/:fhir_release/sim/:sim/fhir", "/v/:fhir_release/fhir"], fhirServer) +app.use([ + "/v/:fhir_release/sim/:sim/s/:server/fhir", + "/v/:fhir_release/sim/:sim/fhir", + "/v/:fhir_release/s/:server/fhir", + "/v/:fhir_release/fhir" +], fhirServer) // The launcher endpoint app.get("/launcher", launcher); diff --git a/backend/lib.ts b/backend/lib.ts index b28f40f..0e24483 100644 --- a/backend/lib.ts +++ b/backend/lib.ts @@ -2,6 +2,7 @@ import jwt from "jsonwebtoken" import { NextFunction, Request, Response, RequestHandler } from "express" import config from "./config"; import { HttpError, InvalidRequestError } from "./errors"; +import { decode } from "../src/isomorphic/codec"; /** @@ -45,6 +46,15 @@ export function notSupported(message: string = "", code = 400) { } export function getFhirServerBaseUrl(req: Request) { + try { + var sim = decode(req.params.sim) + if (sim.fhir_server) { + return sim.fhir_server + } + } catch (ex) { + console.error("Invalid sim: " + ex) + } + const fhirVersion = req.params.fhir_release.toUpperCase(); let fhirServer = config[`fhirServer${fhirVersion}` as keyof typeof config] as string; diff --git a/backend/routes/fhir/proxy.ts b/backend/routes/fhir/proxy.ts index 8441f71..513fc6d 100644 --- a/backend/routes/fhir/proxy.ts +++ b/backend/routes/fhir/proxy.ts @@ -49,7 +49,8 @@ export default async function proxy(req: Request, res: Response) { } // Proxy --------------------------------------------------------------- - const response = await fetch(new URL(fhirServer + req.url).href, fhirRequestOptions); + const _url = new URL(fhirServer + req.url).href.replace(/\/\s*\//g, "/") + const response = await fetch(_url, fhirRequestOptions); res.status(response.status); @@ -62,7 +63,7 @@ export default async function proxy(req: Request, res: Response) { let body = await response.text() if (!isBinary) { - body = body.replaceAll(fhirServer + "", `${getRequestBaseURL(req)}/v/${fhirVersionLower}/fhir`); + body = body.replaceAll(fhirServer + "", `${getRequestBaseURL(req)}${req.baseUrl}`); } res.end(body); diff --git a/index.d.ts b/index.d.ts index 72e4fc0..ce9bc95 100644 --- a/index.d.ts +++ b/index.d.ts @@ -69,6 +69,7 @@ declare namespace SMART { jwks?: string pkce?: PKCEValidation client_type?: SMARTClientType + fhir_server?: string } interface AuthorizeParams { diff --git a/src/isomorphic/codec.ts b/src/isomorphic/codec.ts index 9e67a4d..d725700 100644 --- a/src/isomorphic/codec.ts +++ b/src/isomorphic/codec.ts @@ -64,7 +64,8 @@ export function encode(params: SMART.LaunchParams, ignoreErrors = false): string params.jwks_url || "", params.jwks || "", clientTypes.indexOf(params.client_type || "public"), - PKCEValidationTypes.indexOf(params.pkce || "auto") + PKCEValidationTypes.indexOf(params.pkce || "auto"), + params.fhir_server || "" ])) } @@ -84,7 +85,8 @@ export function encode(params: SMART.LaunchParams, ignoreErrors = false): string params.jwks_url || "", params.jwks || "", clientTypes.indexOf(params.client_type || "public"), - PKCEValidationTypes.indexOf(params.pkce || "auto") + PKCEValidationTypes.indexOf(params.pkce || "auto"), + params.fhir_server || "" ]; return base64UrlEncode(JSON.stringify(arr)) @@ -124,7 +126,8 @@ export function decode(launch: string): SMART.LaunchParams { jwks_url : arr[12] || "", jwks : arr[13] || "", client_type : clientTypes[arr[14]], - pkce : PKCEValidationTypes[arr[15]] + pkce : PKCEValidationTypes[arr[15]], + fhir_server : arr[16] || "", } } @@ -190,7 +193,8 @@ function decodeLegacy(object: Record): SMART.LaunchParams { jwks_url : "", jwks : "", client_type : "public", // "backend-service" - pkce : "auto" + pkce : "auto", + fhir_server : "" } if (object.d && Number.isInteger(+object.d)) { // auth_error @@ -223,6 +227,9 @@ function decodeLegacy(object: Record): SMART.LaunchParams { if (object.r) { out.jwks = object.r } + if (object.s) { + out.fhir_server = object.s + } // ------------------------------------------------------------------------- return out as SMART.LaunchParams;