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

Cu 86by8rraf investigate encryption key management #224

Open
wants to merge 5 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions JeMPI_Apps/JeMPI_UI/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,10 @@
"axios": "^1.1.3",
"axios-mock-adapter": "^1.21.2",
"cross-env": "^7.0.3",
"crypto-js": "^4.2.0",
"dayjs": "^1.11.8",
"formik": "^2.2.9",
"fs": "^0.0.1-security",
"keycloak-js": "^20.0.2",
"notistack": "^2.0.8",
"react": "^18.2.0",
Expand All @@ -52,6 +54,7 @@
"web-vitals": "^2.1.0"
},
"devDependencies": {
"@types/crypto-js": "^4.2.2",
"@types/dockerode": "^3.3.20",
"@types/jest": "^29.5.5",
"@types/node": "^16.7.13",
Expand Down
50 changes: 40 additions & 10 deletions JeMPI_Apps/JeMPI_UI/src/services/ApiClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@ import {
DemographicData,
PatientRecord
} from 'types/PatientRecord'
import { Notifications, NotificationState} from 'types/Notification'
import { Notifications, NotificationState } from 'types/Notification'
import { Config } from 'config'
import axios from 'axios'
import { getCookie } from '../utils/misc'
import { decryptData, encryptData } from 'utils/encryption'

const apiClientAuth = (() => {
const authKey = 'jempi-auth-key'
Expand Down Expand Up @@ -79,6 +80,31 @@ export class ApiClient {

return request
})

axiosInstance.interceptors.request.use(
config => {
if (config.data) {
config.data = encryptData(config.data)
}
return config
},
error => {
return Promise.reject(error)
}
)

axiosInstance.interceptors.response.use(
response => {
if (response.data) {
response.data = decryptData(response.data)
}
return response
},
error => {
return Promise.reject(error)
}
)

this.client = axiosInstance
}
}
Expand All @@ -95,7 +121,9 @@ export class ApiClient {
endDay: string,
states: string[]
): Promise<Notifications> {
const notificationState = states.includes(NotificationState.ALL.toString()) ? [NotificationState.CLOSED, NotificationState.OPEN] : states;
const notificationState = states.includes(NotificationState.ALL.toString())
? [NotificationState.CLOSED, NotificationState.OPEN]
: states
const url = `${ROUTES.GET_NOTIFICATIONS}?limit=${limit}&startDate=${startDay}&endDate=${endDay}&offset=${offset}&states=${notificationState}`
const { data } = await this.client.get<NotificationResponse>(url)
const { records, skippedRecords, count } = data
Expand Down Expand Up @@ -183,12 +211,18 @@ export class ApiClient {
}

async newGoldenRecord(request: LinkRequest) {
const { data } = await this.client.post<LinkRequest>(ROUTES.POST_IID_NEW_GID_LINK, request)
const { data } = await this.client.post<LinkRequest>(
ROUTES.POST_IID_NEW_GID_LINK,
request
)
return data
}

async linkRecord(linkRequest: LinkRequest) {
const { data } = await this.client.post<LinkRequest>(ROUTES.POST_IID_GID_LINK, linkRequest)
const { data } = await this.client.post<LinkRequest>(
ROUTES.POST_IID_GID_LINK,
linkRequest
)
return data
}

Expand Down Expand Up @@ -345,9 +379,7 @@ export class ApiClient {
}

async getGoldenRecordAuditTrail(gid: string) {
const {
data
} = await this.client.get<Array<AuditTrail>>(
const { data } = await this.client.get<Array<AuditTrail>>(
ROUTES.GET_GOLDEN_RECORD_AUDIT_TRAIL,
{
params: {
Expand All @@ -359,9 +391,7 @@ export class ApiClient {
}

async getInteractionAuditTrail(iid: string) {
const {
data
} = await this.client.get<Array<AuditTrail>>(
const { data } = await this.client.get<Array<AuditTrail>>(
ROUTES.GET_INTERACTION_AUDIT_TRAIL,
{
params: {
Expand Down
17 changes: 17 additions & 0 deletions JeMPI_Apps/JeMPI_UI/src/utils/encryption.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import CryptoJS from 'crypto-js'

const ENCRYPTION_KEY = process.env.ENCRYPTION_KEY as string

export function encryptData(data: object): string {
const ciphertext = CryptoJS.AES.encrypt(
JSON.stringify(data),
ENCRYPTION_KEY
).toString()
return ciphertext
}

export function decryptData(ciphertext: string): any {
const bytes = CryptoJS.AES.decrypt(ciphertext, ENCRYPTION_KEY)
const decryptedData = JSON.parse(bytes.toString(CryptoJS.enc.Utf8))
return decryptedData
}
Loading