-
Notifications
You must be signed in to change notification settings - Fork 1
/
checkdgc.js
116 lines (110 loc) · 4.35 KB
/
checkdgc.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
const { Validator, Service, Certificate } = require('verificac19-sdk');
const crlManager = require('./crlmanager');
const version = '1.1.0';
const verbose = process.argv.includes('--verbose');
const shortMode = process.argv.includes('--short');
const printAll = process.argv.includes('--all');
const waitkey = process.argv.includes('--waitkey');
const noUpdate = process.argv.includes('--no-update');
const mode = process.argv.includes('--super') ? Validator.mode.SUPER_DGP : process.argv.includes('--booster') ? Validator.mode.BOOSTER_DGP : Validator.mode.NORMAL_DGP;
const signature = !process.argv.includes('--no-signature');
/**
* @description Validates a certificate image (Green Pass) and prints the result to the console
*/
const validateGreenPass = async() => {
if (process.argv.length > 2) {
try {
if (process.argv.includes('--help')) {
console.log(printHelp());
return;
}
if (process.argv.includes('--version')) {
console.log(version);
return;
}
if (!noUpdate) {
log('Updating greenpass rules...');
await Service.updateAll(crlManager);
}
let img = ""
if (process.argv.includes('--raw')) {
log('Reading raw qr data...');
img = await Certificate.fromRaw(process.argv[process.argv.length - 1]);
} else {
log('Reading image file...');
img = await Certificate.fromImage(process.argv[process.argv.length - 1]);
}
log('Validating image...');
let validationResult = ""
if (signature) {
validationResult = await Validator.validate(img, mode);
} else {
validationResult = await Validator.checkRules(img, mode);
}
if (typeof validationResult === 'object') {
if (shortMode) {
console.log(validationResult.code);
} else {
printAll ? console.log(JSON.stringify(img, null, 2)) : console.log(JSON.stringify(validationResult, null, 2));
}
}
} catch (error) {
console.error(error);
process.exit(1);
}
} else {
console.error('Please provide a certificate image or raw data');
process.exit(1);
}
}
/**
*
* @param {*} message - The message to log to the console
* @description Logs a message to the console if verbose mode is enabled
*/
function log(message) {
if (verbose) {
console.log(message);
}
}
/**
*
* @returns {string} - The help text for the program in the console
*/
function printHelp() {
return `\n\nCheck Green Pass Validity - v${version}\n\n` +
`This tool is an executable wrapper for the official Verifica C19 SDK.\nIt is used to validate a certificate image.\n` +
`See: https://github.com/italia/verificac19-sdk\n\n` +
`Usage: checkdgc [options] <image>\n\n` +
`Options:\n` +
` --help Print this help message\n` +
` --version Print the version of this tool\n` +
` --verbose Print verbose output\n` +
` --raw Validate a certificate giving the qr extracted string\n` +
` --short Print only the validation code\n` +
` --print-all Print all the certificate in JSON format\n` +
` --super Use the super DGP rules\n` +
` --booster Use the booster DGP rules\n` +
` --no-signature Skip signature validation\n` +
` --no-update Skip update of the DGP rules\n` +
` --waitkey Wait for a keypress before exiting\n` +
`\n\n` +
`Examples:\n` +
` checkdgc --verbose --short --super --no-signature --no-update image.png\n` +
` checkdgc --short --super path/to/image.png\n` +
`\n\n`;
}
(async() => {
await validateGreenPass();
// wait for user input to exit
if (waitkey) {
console.log('\n\nPress any key to exit...');
process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdin.on('data', function(text) {
process.exit(0);
});
} else {
process.exit(0);
}
})()