Skip to content

Commit

Permalink
Add fhir_server to sim
Browse files Browse the repository at this point in the history
  • Loading branch information
vlad-ignatov committed May 28, 2024
1 parent 3c79b50 commit 2d4210d
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 8 deletions.
13 changes: 11 additions & 2 deletions backend/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
10 changes: 10 additions & 0 deletions backend/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";


/**
Expand Down Expand Up @@ -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;

Expand Down
5 changes: 3 additions & 2 deletions backend/routes/fhir/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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);
Expand Down
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ declare namespace SMART {
jwks?: string
pkce?: PKCEValidation
client_type?: SMARTClientType
fhir_server?: string
}

interface AuthorizeParams {
Expand Down
15 changes: 11 additions & 4 deletions src/isomorphic/codec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 || ""
]))
}

Expand All @@ -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))
Expand Down Expand Up @@ -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] || "",
}
}

Expand Down Expand Up @@ -190,7 +193,8 @@ function decodeLegacy(object: Record<string, string>): 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
Expand Down Expand Up @@ -223,6 +227,9 @@ function decodeLegacy(object: Record<string, string>): SMART.LaunchParams {
if (object.r) {
out.jwks = object.r
}
if (object.s) {
out.fhir_server = object.s
}
// -------------------------------------------------------------------------

return out as SMART.LaunchParams;
Expand Down

0 comments on commit 2d4210d

Please sign in to comment.