Initial Commit

This commit is contained in:
2025-02-09 14:53:16 -06:00
parent a1e6d06669
commit 2743a1c042
5 changed files with 206 additions and 1 deletions

View File

@@ -0,0 +1,88 @@
return {
getToken() {
return new Promise(async (resolve, reject) => {
try {
getRedisKey = function () {
return `fhir-${fhirSettings.jwt.clientId}`;
};
const tokenEntry = await redis.hGetAll(getRedisKey());
const currentTimeInSeconds = Math.floor(new Date().getTime() / 1000);
if (tokenEntry.exp >= currentTimeInSeconds) {
resolve(JSON.parse(tokenEntry.token));
return;
}
// start the expiry base before we create a new token
let expiryInSeconds = Math.floor(new Date().getTime() / 1000);
// Define headers
const headers = {
alg: "RS256",
typ: "JWT",
};
// Define your payload
const payload = {
iss: fhirSettings.jwt.client_id,
sub: fhirSettings.jwt.client_id,
aud: fhirSettings.token_url,
};
// Define token options (optional)
const options = {
expiresIn: "4m", // Token expires in 1 hour
notBefore: "0",
algorithm: "RS384",
keyid: "iva_id_1234",
jwtid: uuidv5("jwtid", fhirSettings.jwt.uuid_namespace),
};
let data = qs.stringify({
grant_type: "client_credentials",
client_assertion_type:
"urn:ietf:params:oauth:client-assertion-type:jwt-bearer",
client_assertion: jwt.sign(
payload,
fhirSettings.jwt.privateKey,
options
),
});
let config = {
method: "post",
maxBodyLength: Infinity,
url: fhirSettings.token_url,
headers: {
"Content-Type": "application/x-www-form-urlencoded",
},
data: data,
};
const result = await axios(config);
if (result.status == 200) {
expiryInSeconds += result.data.expires_in;
console.log(
`fhirInterface.getToken: Adding ${getRedisKey()} to redis with expiry=${new Date(
expiryInSeconds * 1000
)}`
);
await redis.hSet(getRedisKey(), "exp", expiryInSeconds);
await redis.hSet(getRedisKey(), "token", JSON.stringify(result.data));
resolve(result.data);
} else {
reject(result.status);
}
} catch (err) {
console.log("fhirInterface.getToken: Error getting Token");
console.log(err);
if (err.response) {
console.log(err.response.data); // => the response payload
}
reject(err);
}
});
},
};

View File

@@ -0,0 +1,10 @@
{
"base_url": "https://fhir.epic.com/interconnect-fhir-oauth/api/FHIR",
"token_url": "https://fhir.epic.com/interconnect-fhir-oauth/oauth2/token",
"jwt": {
"uuid_namespace": "30e3078d-59fe-4151-8571-304c87580209",
"client_id": "66eaeb38-c5f7-41cb-8cd3-791efacf25e5",
"privateKey": "*****"
},
"patient_id": "T81lum-5p6QvDR7l6hv7lfE52bAbA2ylWBnv9CZEzNb0B"
}