-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
178 lines (137 loc) · 5.39 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
const express = require('express')
const path = require('path')
const fetch = require('node-fetch');
const https = require('https');
const { DCC } = require('dcc-utils');
const rs = require('jsrsasign');
const vaccination = require("./vaccination.js")
const test = require("./test.js")
const recovery = require("./recovery.js")
const app = express();
const PORT = process.env.PORT || 5000
const urlUpdate = "https://get.dgc.gov.it/v1/dgc/signercertificate/update";
const urlStatus = "https://get.dgc.gov.it/v1/dgc/signercertificate/status";
const urlSettings = "https://get.dgc.gov.it/v1/dgc/settings";
const ADD_HOLDER_DETAILS = true;
let validKids;
let signerCertificates;
let settings;
const updateCertificates = (async () => {
console.log("Updating signer certificates...");
// get the list of valid KIDs
response = await fetch(urlStatus);
validKids = await response.json();
console.log(validKids.length + " valid KIDs downloaded" );
// get the list of certificates
signerCertificates = [];
certificateDownloadedCount = 0;
certificateAddedCount = 0;
let headers = {};
const httpsAgent = new https.Agent({ keepAlive: true });
do {
response = await fetch(urlUpdate, {
headers,
httpsAgent
})
headers = {'X-RESUME-TOKEN' : response.headers.get('X-RESUME-TOKEN')};
const certificateKid = response.headers.get('X-KID');
const certificate = await response.text();
// a certificate has been downloaded
if(certificate) {
certificateDownloadedCount++;
// the certificate is valid, add it to the list
if(validKids.includes(certificateKid)) {
certificateAddedCount++;
signerCertificates.push("-----BEGIN CERTIFICATE-----\n" + certificate + "-----END CERTIFICATE-----");
}
}
} while (response.status === 200);
console.log(certificateDownloadedCount + " certificates downloaded, " + certificateAddedCount + " added");
});
const updateSettings = (async () => {
console.log("Updating settings...");
response = await fetch(urlSettings);
settings = await response.json();
console.log("Done");
});
const main = (async () => {
await updateCertificates();
await updateSettings();
app.use(function(req, res, next) { //allow cross origin requests
res.setHeader("Access-Control-Allow-Methods", "POST, PUT, OPTIONS, DELETE, GET");
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
app.use(express.urlencoded());
app.use(express.json());
app.post('/', async(req, res) => {
let dcc;
try {
dcc = await DCC.fromRaw(req.body.params.updates[0].value);
} catch (e) {
res.statusCode = 400;
res.setHeader('Content-Type', 'application/json');
res.send({result: false, message: e.message});
return;
}
// check DGC signature
let signatureVerified = false;
for(let certificate of signerCertificates) {
try {
// get key and jwk from certificate
key = rs.KEYUTIL.getKey(certificate);
jwk = rs.KEYUTIL.getJWKFromKey(key);
// EC key, the library expects x and y coordinates as hex strings
if(jwk.kty == 'EC') {
verifier = {
x: Buffer.from(jwk.x, 'base64').toString('hex'),
y: Buffer.from(jwk.y, 'base64').toString('hex')
};
}
// RSA key, the library expects modulus and exponent as Buffers
else if(jwk.kty == 'RSA') {
verifier = {
n: Buffer.from(jwk.n, 'base64'),
e: Buffer.from(jwk.e, 'base64')
};
}
signatureVerified = await dcc.checkSignature(verifier);
} catch {}
if(signatureVerified) break;
}
// no signer certificate found
if(!signatureVerified) {
res.statusCode = 200;
res.setHeader('Content-Type', 'application/json');
res.send(Object.assign({result: false, message: "Firma non valida"}, dcc));
return;
}
// check DGC content
let validate;
// 1. vaccination
if(dcc.payload.v) validate = vaccination.validateVaccination(settings, dcc);
// 2. test
if(dcc.payload.t) validate = test.validateTest(settings, dcc);
// 3. recovery
if(dcc.payload.r) validate = recovery.validateRecovery(settings, dcc);
// Add holder details if required
let response;
if(ADD_HOLDER_DETAILS) {
/*let surname = dcc.payload.nam.fn;
let forename = dcc.payload.nam.gn;
let dob = dcc.payload.dob;
response = validate + " - " + surname + " " + forename + " (" + dob + ")";*/
response = Object.assign(validate, dcc);
} else {
response = Object.assign(validate, dcc);
}
res.statusCode = 200;
res.setHeader('Content-Type', 'application/json');
res.send(response);
});
app.listen(PORT, () => {
console.log("GP Node è in attesa...");
});
});
main();