This repository has been archived by the owner on Mar 27, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: provide basic implementation for data requests and submissions
- Loading branch information
1 parent
be63d3a
commit b330d53
Showing
16 changed files
with
233 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,7 @@ | ||
import { greet } from '.'; | ||
import Iris from '.'; | ||
|
||
describe('index', () => { | ||
describe('greet', () => { | ||
it('should return greeting with name specified', () => { | ||
const testName = 'testName'; | ||
const greetingPhrase = greet(testName); | ||
|
||
expect(greetingPhrase).toEqual(`Hello ${testName}!`); | ||
}); | ||
it('provides the Iris class', () => { | ||
expect(new Iris({})).toBeDefined(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,2 @@ | ||
export const greet = (name: string): string => `Hello ${name}!`; | ||
|
||
console.log(greet('World')); | ||
import Iris from './lib/'; | ||
export default Iris; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import axios, { AxiosInstance } from 'axios'; | ||
|
||
import { encryptData } from './crypto'; | ||
import { getNameCheckHash, getBirthDateCheckHash } from './util'; | ||
|
||
import IrisOptions from '../types/IrisOptions'; | ||
import IrisCode from '../types/IrisCode'; | ||
import IrisDataRequest from '../types/IrisDataRequest'; | ||
import IrisCodeKeyMap from '../types/IrisCodeKeyMap'; | ||
import IrisDataRequestDTO from '../types/dto/IrisDataRequestDTO'; | ||
import IrisContactsEvents from '../types/IrisContactsEvents'; | ||
import IrisContactsEventsSubmissionDTO from '../types/dto/IrisContactsEventsSubmissionDTO'; | ||
import IrisUserInfo from '../types/IrisUserInfo'; | ||
|
||
const defaultOptions: IrisOptions = { | ||
baseUrl: '', | ||
}; | ||
|
||
export default class Iris { | ||
axiosInstance: AxiosInstance; | ||
codeKeyMap: IrisCodeKeyMap; | ||
|
||
constructor(options: Partial<IrisOptions>) { | ||
this.codeKeyMap = new Map(); | ||
const opts: IrisOptions = Object.assign(defaultOptions, options); | ||
this.axiosInstance = axios.create({ | ||
baseURL: opts.baseUrl, | ||
}); | ||
} | ||
|
||
async getDataRequest(code: IrisCode): Promise<IrisDataRequest> { | ||
const response = await this.axiosInstance.get(`/data-requests/${code}`); | ||
if (response.status !== 200) { | ||
console.error('IRIS Gateway responded the following data', response.data); | ||
throw new Error(`Request failed with status Code ${response.status}`); | ||
} | ||
const dataRequest = response.data as IrisDataRequestDTO; | ||
this.codeKeyMap.set(code, { | ||
keyOfHealthDepartment: dataRequest.keyOfHealthDepartment, | ||
keyReferenz: dataRequest.keyReferenz, | ||
}); | ||
return { | ||
healthDepartment: dataRequest.healthDepartment, | ||
start: dataRequest.start, | ||
end: dataRequest.end, | ||
requestDetails: dataRequest.requestDetails, | ||
}; | ||
} | ||
|
||
async sendContactsEvents(code: IrisCode, data: IrisContactsEvents, user: IrisUserInfo): Promise<void> { | ||
if (!this.codeKeyMap.has(code)) { | ||
throw new Error("Code could not be found in key map. Did you perform 'getDataRequest' before?"); | ||
} | ||
const keys = this.codeKeyMap.get(code); | ||
const { dataToTransport, keyToTransport } = encryptData(keys.keyOfHealthDepartment, data); | ||
const response = await this.axiosInstance.post(`/data-submissions/${code}/contacts_events`, { | ||
checkCode: [ getNameCheckHash(user.firstName, user.lastName), getBirthDateCheckHash(user.birthDate) ].filter(c => !!c), | ||
secret: keyToTransport, | ||
keyReferenz: keys.keyReferenz, | ||
encryptedData: dataToTransport, | ||
} as IrisContactsEventsSubmissionDTO); | ||
if (response.status !== 200) { | ||
console.error('IRIS Gateway responded the following data', response.data); | ||
throw new Error(`Request failed with status Code ${response.status}`); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import * as crypto from 'crypto'; | ||
|
||
export function encryptData(keyOfHealthDepartment: string, data): { dataToTransport: string; keyToTransport: string } { | ||
const publicKey = crypto.createPublicKey(keyOfHealthDepartment); | ||
const iv = crypto.randomBytes(16); | ||
const key = crypto.randomBytes(32); | ||
const cipher = crypto.createCipheriv('aes-256', key, iv); | ||
const encryptedData = Buffer.concat([cipher.update(JSON.stringify(data), 'utf8'), cipher.final()]); | ||
const encryptedKey = crypto.publicEncrypt( | ||
{ key: publicKey, padding: crypto.constants.RSA_PKCS1_OAEP_PADDING, oaepHash: 'sha3' }, | ||
key, | ||
); | ||
return { | ||
dataToTransport: encryptedData.toString('base64'), | ||
keyToTransport: encryptedKey.toString('base64'), | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
import Iris from './Iris'; | ||
export default Iris; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import * as crypto from 'crypto'; | ||
|
||
export function getNameCheckHash(firstName: string, lastName: string): string { | ||
const str = `${firstName.trim()}${lastName.trim()}`.toLowerCase().replace(/\W/g, ''); | ||
return crypto.createHash('md5').update(str).digest('base64'); | ||
} | ||
export function getBirthDateCheckHash(birthDate?: string): string | undefined { | ||
if (!birthDate) { | ||
return undefined; | ||
} | ||
const date = new Date(birthDate); | ||
const str = `${date.getFullYear()}${(date.getMonth() + 1).toString().padStart(2, '0')}${date.getDate().toString().padStart(2, '0')}` | ||
return crypto.createHash('md5').update(str).digest('base64'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
type IrisCode = string; | ||
export default IrisCode; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import IrisDataRequestDTO from './dto/IrisDataRequestDTO'; | ||
import IrisCode from './IrisCode'; | ||
|
||
type IrisCodeKeyMap = Map<IrisCode, Pick<IrisDataRequestDTO, 'keyOfHealthDepartment' | 'keyReferenz'>>; | ||
export default IrisCodeKeyMap; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
interface DataProvider { | ||
firstName?: string; | ||
lastName?: string; | ||
dateOfBirth?: string; | ||
} | ||
enum Sex { | ||
MALE = 'MALE', | ||
FEMALE = 'FEMALE', | ||
OTHER = 'OTHER', | ||
UNKNOWN = 'UNKNOWN', | ||
} | ||
interface Address { | ||
street: string; | ||
houseNumber: string; | ||
zipCode: string; | ||
city: string; | ||
} | ||
|
||
enum ContactCategory { | ||
HIGH_RISK = 'HIGH_RISK', | ||
HIGH_RISK_MED = 'HIGH_RISK_MED', | ||
MEDIUM_RISK_MED = 'MEDIUM_RISK_MED', | ||
LOW_RISK = 'LOW_RISK', | ||
NO_RISK = 'NO_RISK', | ||
} | ||
interface ContactPerson { | ||
firstName: string; | ||
lastName: string; | ||
dateOfBirth?: string; | ||
sex?: Sex; | ||
email?: string; | ||
phone?: string; | ||
mobilPhone?: string; | ||
address?: Address; | ||
workPlace?: { | ||
name?: string; | ||
pointOfContact?: string; | ||
phone?: string; | ||
address?: Address; | ||
}; | ||
contactInformation?: { | ||
date?: string; | ||
contactCategory?: ContactCategory; | ||
basicConditions?: string; | ||
}; | ||
} | ||
|
||
interface Event { | ||
name?: string; | ||
phone?: string; | ||
address?: Address; | ||
additionalInformation?: string; | ||
} | ||
|
||
interface ContactPersonList { | ||
contactPersons: Array<ContactPerson>; | ||
dataProvider?: DataProvider; | ||
startDate?: string; | ||
endDate?: string; | ||
} | ||
|
||
interface EventList { | ||
events: Array<Event>; | ||
dataProvider?: DataProvider; | ||
startDate?: string; | ||
endDate?: string; | ||
} | ||
|
||
export default interface IrisContactsEvents { | ||
contacts: ContactPersonList; | ||
events: EventList; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import IrisDataRequestDTO from './dto/IrisDataRequestDTO'; | ||
|
||
type IrisDataRequest = Pick<IrisDataRequestDTO, 'healthDepartment' | 'start' | 'end' | 'requestDetails'> | ||
export default IrisDataRequest |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export default interface IrisOptions { | ||
baseUrl: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export default interface IrisUserInfo { | ||
firstName: string; | ||
lastName: string; | ||
birthDate?: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export default interface IrisContactsEventsSubmissionDTO { | ||
checkCode: Array<string>; | ||
secret: string; | ||
keyReferenz: string; | ||
encryptedData: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
export default interface IrisDataRequestDTO { | ||
healthDepartment: string; | ||
keyOfHealthDepartment: string; | ||
keyReferenz: string; | ||
start?: string; | ||
end?: string; | ||
requestDetails?: string; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters