Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HCX-806 : Javascript HCX Integrator SDK - Enhance SDK to integrate the user's API access token generation #75

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions javascript/__tests__/HCXIncomingRequest.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ jest.mock("../src/impl/HCXIncomingRequest.js");

describe("HCXIntegrator Integration Tests", () => {
const mockConfig = {
participantCode: "testprovider1.swasthmock@swasth-hcx-staging",
participant_code: "testprovider1.swasthmock@swasth-hcx-staging",
KrutikaPhirangi marked this conversation as resolved.
Show resolved Hide resolved
authBasePath:
"http://staging-hcx.swasth.app/api/v0.8/participant/auth/token/generate",
protocolBasePath: "https://staging-hcx.swasth.app/api/v0.8",
Expand All @@ -19,7 +19,7 @@ describe("HCXIntegrator Integration Tests", () => {
};

const mockConfig2 = {
participantCode: "testpayor1.swasthmock@swasth-hcx-staging",
participant_code: "testpayor1.swasthmock@swasth-hcx-staging",
authBasePath:
"http://staging-hcx.swasth.app/api/v0.8/participant/auth/token/generate",
protocolBasePath: "https://staging-hcx.swasth.app/api/v0.8",
Expand Down
15 changes: 10 additions & 5 deletions javascript/src/hcx_integrator.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,23 @@ export class HCXIntegrator {
constructor(config) {
this.config = config;
this.protocolBasePath = null;
this.participantCode = null;
this.participant_code = null;
this.authBasePath = null;
this.username = null;
this.password = null;
this.secret = null;
this.encryptionPrivateKeyURL = null;
this.igURL = null;
this.validateConfig();
}

validateConfig() {
this.protocolBasePath = this.config.protocolBasePath;
this.participantCode = this.config.participantCode;
this.participant_code = this.config.participant_code;
this.authBasePath = this.config.authBasePath;
this.username = this.config.username;
this.password = this.config.password;
this.secret = this.config.secret;
this.encryptionPrivateKeyURL = this.config.encryptionPrivateKeyURL;
this.igURL = this.config.igURL;
}
Expand All @@ -29,10 +31,11 @@ export class HCXIntegrator {
try {
const outgoing = new HCXOutgoingRequest(
this.protocolBasePath,
this.participantCode,
this.participant_code,
this.authBasePath,
this.username,
this.password,
this.secret,
this.encryptionPrivateKeyURL,
this.igURL
);
Expand All @@ -54,10 +57,11 @@ export class HCXIntegrator {
try {
const outgoing = new HCXOutgoingRequest(
this.protocolBasePath,
this.participantCode,
this.participant_code,
this.authBasePath,
this.username,
this.password,
this.secret,
this.encryptionPrivateKeyURL,
this.igURL
);
Expand All @@ -80,10 +84,11 @@ export class HCXIntegrator {
try {
let incoming = new HCXIncomingRequest(
this.protocolBasePath,
this.participantCode,
this.participant_code,
this.authBasePath,
this.username,
this.password,
this.secret,
this.encryptionPrivateKeyURL,
this.igURL
);
Expand Down
6 changes: 4 additions & 2 deletions javascript/src/impl/HCXIncomingRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,20 @@ import { ErrorCodes, ResponseMessage } from "../utils/Errors.js"
export class HCXIncomingRequest {
constructor(
protocolBasePath,
participantCode,
participant_code,
authBasePath,
username,
password,
secret,
encryptionPrivateKeyURL,
igURL
) {
this.protocolBasePath = protocolBasePath;
this.participantCode = participantCode;
this.participant_code = participant_code;
this.authBasePath = authBasePath;
this.username = username;
this.password = password;
this.secret = secret;
this.encryptionPrivateKeyURL = encryptionPrivateKeyURL;
this.igURL = igURL;
this.headers = null;
Expand Down
29 changes: 20 additions & 9 deletions javascript/src/impl/HCXOutgoingRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,20 @@ import { ErrorCodes, ResponseMessage } from "../utils/Errors.js";
export class HCXOutgoingRequest {
constructor(
protocolBasePath,
participantCode,
participant_code,
authBasePath,
username,
password,
secret,
encryptionPrivateKeyURL,
igURL
) {
this.protocolBasePath = protocolBasePath;
this.participantCode = participantCode;
this.participant_code = participant_code;
this.authBasePath = authBasePath;
this.username = username;
this.password = password;
this.secret = secret;
this.encryptionPrivateKeyURL = encryptionPrivateKeyURL; // not needed in outgoing
this.igURL = igURL;
this.hcxToken = null;
Expand All @@ -43,7 +45,7 @@ export class HCXOutgoingRequest {
[this.Constants.HCX_TIMESTAMP]: new Date().toISOString(),
}
if (recipientCode.length != 0) {
headers[this.Constants.HCX_SENDER_CODE] = this.participantCode;
headers[this.Constants.HCX_SENDER_CODE] = this.participant_code;
headers[this.Constants.HCX_RECIPIENT_CODE] = recipientCode;
headers[this.Constants.HCX_CORRELATION_ID] = correlationId || uuidv4();
headers[this.Constants.WORKFLOW_ID] = workflowId || uuidv4();
Expand All @@ -69,10 +71,14 @@ export class HCXOutgoingRequest {
throw new Error("Fhir payload must be an object");
}
if (!this.hcxToken) {
const payload = {}
if(this.username) payload[this.Constants.USERNAME] = this.username;
if(this.password) payload[this.Constants.PASSWORD] = this.password;
if(this.secret) payload[this.Constants.SECRET] = this.secret;
if(this.participant_code) payload[this.Constants.PARTICIPANT_CODE] = this.participant_code;
this.hcxToken = await generateToken(
this.authBasePath,
this.username,
this.password
payload
);
}
const registryData = await searchRegistry(
Expand All @@ -89,6 +95,7 @@ export class HCXOutgoingRequest {
});
return encrypted;
} catch (error) {
console.log(error.response.data);
console.error(`Error in encryptPayload: ${error.message}\n${error.stack}`);
this.error = {
[ErrorCodes.ERR_INVALID_ENCRYPTION]: ResponseMessage.INVALID_PAYLOAD_VALUES_ERR_MSG
Expand All @@ -101,10 +108,14 @@ export class HCXOutgoingRequest {
try {
const url = `${this.protocolBasePath}${operation}`;
if (!this.hcxToken) {
this.hcxToken = await generateHcxToken(
const payload = {}
if(this.username) payload[this.Constants.USERNAME] = this.username;
if(this.password) payload[this.Constants.PASSWORD] = this.password;
if(this.secret) payload[this.Constants.SECRET] = this.secret;
if(this.participant_code) payload[this.Constants.PARTICIPANT_CODE] = this.participant_code;
this.hcxToken = await generateToken(
this.authBasePath,
this.username,
this.password
payload
);
}
const payload = JSON.stringify({ payload: jwePayload, });
Expand All @@ -116,7 +127,7 @@ export class HCXOutgoingRequest {
const response = await axios.post(url, payload, { headers });
return response.data;
} catch (e) {
console.error(`Initialize HCX: ${e}`);
console.error(`Initialize HCX: ${e.response.data}`);
}
} catch (error) {
console.error(`Initialize HCX: ${error}`);
Expand Down
Loading
Loading