Skip to content

Commit

Permalink
AC - cleaned up code
Browse files Browse the repository at this point in the history
  • Loading branch information
andrew-cleveland committed Nov 14, 2023
1 parent 0568c19 commit 7d9dd4c
Show file tree
Hide file tree
Showing 10 changed files with 107 additions and 71 deletions.
19 changes: 10 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import markAsRead from "./src/put/mark_as_read.js";
import sendMessage from "./src/post/send_message.js";
import sendMessageChunks from "./src/post/send_message_chunks.js";

let logLevel = process.env.LOG_LEVEL || "DEBUG";
log.setLevel(log.levels[logLevel]);

// defaults to the local url for the sandbox environment, defaults for sandbox
let url = process.env.MESH_URL || "https://localhost:8700";

Expand All @@ -19,9 +22,8 @@ let sharedKey = process.env.MESH_SHARED_KEY || "TestKey";
let sandbox = process.env.MESH_SANDBOX || "true";
let senderAgent;
let receiverAgent;

if (sandbox === "true") {
log.info("Running in sandbox mode");
console.log("Running in sandbox mode");
// just setup to ignore self-signed certs
senderAgent = new Agent({
rejectUnauthorized: false,
Expand All @@ -31,7 +33,7 @@ if (sandbox === "true") {
rejectUnauthorized: false,
});
} else {
log.info("Running in integration mode");
console.log("Running in integration mode");
// Setup the https agents for integration, you can ignore this for sandbox
senderAgent = new Agent({
cert: readFileSync(process.env.MESH_SENDER_CERT_LOCATION),
Expand All @@ -45,9 +47,6 @@ if (sandbox === "true") {
});
}

let logLevel = process.env.LOG_LEVEL || "INFO";
log.setLevel(log.levels[logLevel]);

// The 'sender' is the mailbox we will be sending the message from, defaults to sandbox
let senderMailboxID = process.env.MESH_SENDER_MAILBOX_ID || "X26ABC1";
let senderMailboxPassword =
Expand Down Expand Up @@ -89,6 +88,7 @@ async function createMessages() {
mailboxPassword: senderMailboxPassword,
message: messageContent,
mailboxTarget: receiverMailboxID,
sharedKey: sharedKey,
agent: senderAgent,
});
log.debug("New message created with an ID: " + newMessage.data["message_id"]);
Expand All @@ -112,6 +112,7 @@ async function createMessageChunks() {
mailboxPassword: senderMailboxPassword,
mailboxTarget: receiverMailboxID,
messageFile: messageFile,
sharedKey: sharedKey,
agent: senderAgent,
});
}
Expand Down Expand Up @@ -196,9 +197,9 @@ async function receiveMessage() {
log.debug("clearing the message from the mailbox");
await markAsRead({
url: url,
mailbox_id: receiverMailboxID,
mailbox_password: receiverMailboxPassword,
shared_key: sharedKey,
mailboxID: receiverMailboxID,
mailboxPassword: receiverMailboxPassword,
sharedKey: sharedKey,
message: message,
agent: receiverAgent,
});
Expand Down
26 changes: 15 additions & 11 deletions src/get/generate_headers.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,36 @@ import crypto from "crypto";
import { v4 as uuid } from "uuid";

// Generates the token, this will be generated fresh for each call, as required by MESH
async function generateToken(
async function generateToken({
mailboxID,
password,
mailboxPassword,
sharedKey,
nonce = uuid(),
nonceCount = 0
) {
nonceCount = 0,
}) {
// Make sure to leave a space at the end of the schema.
const auth_schema_name = "NHSMESH ";
const timestamp = new Date()
let auth_schema_name = "NHSMESH ";
let timestamp = new Date()
.toISOString()
.replace(/[-:.TZ]/g, "")
.slice(0, 12);
const hmac_msg = `${mailboxID}:${nonce}:${nonceCount}:${password}:${timestamp}`;
let hmac_msg = `${mailboxID}:${nonce}:${nonceCount}:${mailboxPassword}:${timestamp}`;

const hmac = crypto
let hmac = crypto
.createHmac("sha256", sharedKey)
.update(hmac_msg)
.digest("hex");

return `${auth_schema_name}${mailboxID}:${nonce}:${nonceCount}:${timestamp}:${hmac}`;
}

async function generateHeaders(mailboxID, mailboxPassword, sharedKey) {
const token = await generateToken(mailboxID, mailboxPassword, sharedKey);
const header = {
async function generateHeaders({ mailboxID, mailboxPassword, sharedKey }) {
let token = await generateToken({
mailboxID: mailboxID,
mailboxPassword: mailboxPassword,
sharedKey: sharedKey,
});
let header = {
accept: "application/vnd.mesh.v2+json",
authorization: token,
"mex-clientversion": "ApiDocs==0.0.1",
Expand Down
12 changes: 8 additions & 4 deletions src/get/handshake.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,19 @@ async function handShake({
sharedKey,
agent,
}) {
const full_url = `${url}/messageexchange/${mailboxID}`;
const headers = await generateHeaders(mailboxID, mailboxPassword, sharedKey);
let full_url = `${url}/messageexchange/${mailboxID}`;
let headers = await generateHeaders({
mailboxID: mailboxID,
mailboxPassword: mailboxPassword,
sharedKey: sharedKey,
});

let config = { headers: headers };
// attach agent to headers
config.httpsAgent = agent;
// const response = await axios.get(full_url, config);
// let response = await axios.get(full_url, config);
try {
const response = await axios.get(full_url, config);
let response = await axios.get(full_url, config);
if (response.status === 200) {
log.info(`Handshake successful, status ${response.status}\n`);
return response;
Expand Down
6 changes: 5 additions & 1 deletion src/get/message_count.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ async function getMessageCount({
agent,
}) {
let fullUrl = `${url}/messageexchange/${mailboxID}/inbox`;
let headers = await generateHeaders(mailboxID, mailboxPassword, sharedKey);
let headers = await generateHeaders({
mailboxID: mailboxID,
mailboxPassword: mailboxPassword,
sharedKey: sharedKey,
});

let config = { headers: headers };
// attach agent to headers
Expand Down
16 changes: 10 additions & 6 deletions src/get/read_message.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ async function readMessage({
}
let chunkedMessage = "";
let fullUrl = `${url}/messageexchange/${mailboxID}/inbox/${messageID}`;
let headers = await generateHeaders(mailboxID, mailboxPassword, sharedKey);
let headers = await generateHeaders({
mailboxID: mailboxID,
mailboxPassword: mailboxPassword,
sharedKey: sharedKey,
});

let config = { headers: headers };
// attach agent to headers
Expand All @@ -41,11 +45,11 @@ async function readMessage({
let [currentChunk, totalChunks] = chunkRange.split(":").map(Number);
log.debug(`chunk ${currentChunk} of ${totalChunks} downloaded`);
if (currentChunk < totalChunks) {
let headers = await generateHeaders(
mailboxID,
mailboxPassword,
sharedKey
);
let headers = await generateHeaders({
mailboxID: mailboxID,
mailboxPassword: mailboxPassword,
sharedKey: sharedKey,
});

let config = { headers: headers };
// attach agent to headers
Expand Down
35 changes: 21 additions & 14 deletions src/post/generate_headers.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,41 @@ import crypto from "crypto";
import { v4 as uuid } from "uuid";

// Generates the token, this will be generated fresh for each call, as required by MESH
async function generateToken(
async function generateToken({
mailboxID,
mailboxPassword,
sharedKey,
nonce = uuid(),
nonce_count = 0
) {
// Make sure the mesh shared key is set as an environmental var
const mesh_shared_key = process.env.MESH_SHARED_KEY;

nonce_count = 0,
}) {
// Make sure to leave a space at the end of the schema.
const auth_schema_name = "NHSMESH ";
const timestamp = new Date()
let auth_schema_name = "NHSMESH ";
let timestamp = new Date()
.toISOString()
.replace(/[-:.TZ]/g, "")
.slice(0, 12);
const hmac_msg = `${mailboxID}:${nonce}:${nonce_count}:${mailboxPassword}:${timestamp}`;
let hmac_msg = `${mailboxID}:${nonce}:${nonce_count}:${mailboxPassword}:${timestamp}`;

const hmac = crypto
.createHmac("sha256", mesh_shared_key)
let hmac = crypto
.createHmac("sha256", sharedKey)
.update(hmac_msg)
.digest("hex");

return `${auth_schema_name}${mailboxID}:${nonce}:${nonce_count}:${timestamp}:${hmac}`;
}

async function generateHeaders(mailboxID, mailboxPassword, mailboxTarget) {
const token = await generateToken(mailboxID, mailboxPassword);
const header = {
async function generateHeaders({
mailboxID,
mailboxPassword,
mailboxTarget,
sharedKey,
}) {
let token = await generateToken({
mailboxID: mailboxID,
mailboxPassword: mailboxPassword,
sharedKey: sharedKey,
});
let header = {
accept: "application/vnd.mesh.v2+json",
authorization: token,
"content-type": "application/octet-stream",
Expand Down
12 changes: 7 additions & 5 deletions src/post/send_message.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,18 @@ async function sendMessage({
url,
mailboxID,
mailboxPassword,
sharedKey,
message,
mailboxTarget,
agent,
}) {
const fullUrl = `${url}/messageexchange/${mailboxID}/outbox`;
const headers = await generateHeaders(
mailboxID,
mailboxPassword,
mailboxTarget
);
const headers = await generateHeaders({
mailboxID: mailboxID,
mailboxPassword: mailboxPassword,
mailboxTarget: mailboxTarget,
sharedKey: sharedKey,
});

let config = { headers: headers };
// attach agent to headers
Expand Down
12 changes: 7 additions & 5 deletions src/post/send_message_chunks.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ async function sendMessageChunks({
mailboxID,
mailboxPassword,
mailboxTarget,
sharedKey,
messageFile,
agent,
}) {
Expand All @@ -105,11 +106,12 @@ async function sendMessageChunks({
fullUrl = `${url}/messageexchange/${mailboxID}/outbox`;
}

let headers = await generateHeaders(
mailboxID,
mailboxPassword,
mailboxTarget
);
let headers = await generateHeaders({
mailboxID: mailboxID,
mailboxPassword: mailboxPassword,
mailboxTarget: mailboxTarget,
sharedKey: sharedKey,
});
headers["mex-chunk-range"] = `${chunk}:${fileCount}`;
headers["content-encoding"] = "gzip";

Expand Down
26 changes: 15 additions & 11 deletions src/put/generate_headers.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,35 @@ import crypto from "crypto";
import { v4 as uuid } from "uuid";

// Generates the token, this will be generated fresh for each call, as required by MESH
async function generateToken(
mailbox_id,
password,
shared_key,
async function generateToken({
mailboxID,
mailboxPassword,
sharedKey,
nonce = uuid(),
nonce_count = 0
) {
nonce_count = 0,
}) {
// Make sure to leave a space at the end of the schema.
const auth_schema_name = "NHSMESH ";
const timestamp = new Date()
.toISOString()
.replace(/[-:.TZ]/g, "")
.slice(0, 12);
const hmac_msg = `${mailbox_id}:${nonce}:${nonce_count}:${password}:${timestamp}`;
const hmac_msg = `${mailboxID}:${nonce}:${nonce_count}:${mailboxPassword}:${timestamp}`;

const hmac = crypto
.createHmac("sha256", shared_key)
.createHmac("sha256", sharedKey)
.update(hmac_msg)
.digest("hex");

return `${auth_schema_name}${mailbox_id}:${nonce}:${nonce_count}:${timestamp}:${hmac}`;
return `${auth_schema_name}${mailboxID}:${nonce}:${nonce_count}:${timestamp}:${hmac}`;
}

async function generateHeaders(mailbox_id, mailbox_password, shared_key) {
const token = await generateToken(mailbox_id, mailbox_password, shared_key);
async function generateHeaders({ mailboxID, mailboxPassword, sharedKey }) {
const token = await generateToken({
mailboxID: mailboxID,
mailboxPassword: mailboxPassword,
sharedKey: sharedKey,
});
const header = {
accept: "application/vnd.mesh.v2+json",
authorization: token,
Expand Down
14 changes: 9 additions & 5 deletions src/put/mark_as_read.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,18 @@ import generateHeaders from "./generate_headers.js";

async function markAsRead({
url,
mailbox_id,
mailbox_password,
shared_key,
mailboxID,
mailboxPassword,
sharedKey,
message,
agent,
}) {
let full_url = `${url}/messageexchange/${mailbox_id}/inbox/${message}/status/acknowledged`;
let headers = await generateHeaders(mailbox_id, mailbox_password, shared_key);
let full_url = `${url}/messageexchange/${mailboxID}/inbox/${message}/status/acknowledged`;
let headers = await generateHeaders({
mailboxID: mailboxID,
mailboxPassword: mailboxPassword,
sharedKey: sharedKey,
});

let config = { headers: headers };
// attach agent to headers
Expand Down

0 comments on commit 7d9dd4c

Please sign in to comment.