fixed check on type of vaccine

This commit is contained in:
Gianmarco Pettinato 2021-09-25 14:15:49 +02:00
parent b2630bab9b
commit 5cdb1e978d
4 changed files with 35 additions and 36 deletions

File diff suppressed because one or more lines are too long

View File

@ -12,7 +12,7 @@ export class CertificateDownloader{
private certificatesCollection:{kid:string,certificate:string}[] = [];
private currentValidKids:string[] = [];
public async getCertificates(): Promise<unknown> {
public async getCertificates(): Promise<{kid:string,certificate:string}[]> {
let data = '{}';
try {
const file = await fs.open(this.keyStorage,'r');
@ -20,6 +20,7 @@ export class CertificateDownloader{
await file.close();
const savedData = JSON.parse(data);
if(savedData.lastupdateDate == null || Date.now() - savedData?.lastupdateDate > this.timeSpan){
await this.updateKids();
await this.getAllCertificate();
} else {
this.certificatesCollection = savedData.certificates;
@ -35,31 +36,7 @@ export class CertificateDownloader{
}
}
// public static getCertificateDownloader():CertificateDownloader{
// if(CertificateDownloader.instance == undefined){
// CertificateDownloader.instance = new CertificateDownloader();
// }
// return CertificateDownloader.instance;
// }
// async getAllCertificate(): Promise<void> {
// this.cerficateCollection = {};
// const response:AxiosResponse<JSON> = (await axios.get('https://raw.githubusercontent.com/lovasoa/sanipasse/master/src/assets/Digital_Green_Certificate_Signing_Keys.json'));
// if(response.status == 200){
// console.log(response.data);
// this.cerficateCollection = response.data;
// console.log(response);
// const lastupdateDate = Date.now();
// const file = await fs.open(this.keyStorage,'w');
// file.writeFile(JSON.stringify({'certificates':this.cerficateCollection, lastupdateDate}));
// console.log(this.cerficateCollection);
// await file.close();
// }else{
// throw new Error(response.statusText);
// }
// }
async getAllCertificate(): Promise<void> {
private async getAllCertificate(): Promise<void> {
let exit = false;
let headers = {};
this.certificatesCollection = [];
@ -68,21 +45,19 @@ export class CertificateDownloader{
headers = {'X-RESUME-TOKEN': response.headers['x-resume-token']};
const currentKid:string = response.headers['x-kid'];
if(this.currentValidKids.includes(currentKid)){
// console.log('=========AGGIUNG===========');
const cert = {kid:currentKid, certificate: response.data};
// console.log(cert);
this.certificatesCollection.push(cert);
}
exit = (response.status !== 200);
}
const lastupdateDate = Date.now();
const file = await fs.open(this.keyStorage,'w');
file.writeFile(JSON.stringify({'certificates':this.certificatesCollection, lastupdateDate}));
await file.writeFile(JSON.stringify({'certificates':this.certificatesCollection, lastupdateDate}));
console.log(this.certificatesCollection);
await file.close();
}
async updateKids(): Promise<void> {
private async updateKids(): Promise<void> {
try {
const resp = await axios.get(this.baseUrl+this.statusApi);
this.currentValidKids = await resp.data as string[];

View File

@ -38,6 +38,7 @@ export class VaccineVerifier {
if(vaccineDiff <= 0){
return this.getLogicValidityDays(validRulesSet, this.vaccineStartDayComplete, this.vaccineEndDayComplete,inoculationDate);
} else {
console.log('single dose');
return this.getLogicValidityDays(validRulesSet, this.vaccineStartDayNotComplete, this.vaccineEndDayNotComplete,inoculationDate);
}
}
@ -101,12 +102,13 @@ export class VaccineVerifier {
private getRulesSet(type:string): unknown[]{
return this.settings.filter((rule:unknown)=>{
return rule['type'] = type;
return rule['type'] == type;
});
}
private getLogicValidityDays(validRulesSet:unknown[],startKey:string, endKey:string, inoculationDate: dayjs.Dayjs): checkResult {
const now = dayjs();
console.log(validRulesSet);
const ruleStart = validRulesSet.find((elem:any)=>{return elem.name == startKey;});
const ruleEnd = validRulesSet.find((elem:any)=>{return elem.name == endKey;});
const startValidity = inoculationDate.add(parseInt(ruleStart['value']),'days');

View File

@ -2,7 +2,7 @@ import { CertificateDownloader } from './CertificateDownloader';
import { RuleDownloader } from './RuleDownloader';
import { VaccineVerifier } from './VaccineVerifier';
import {DCC} from 'dcc-utils';
import jsrsasign from 'jsrsasign';
export default class Verifier {
static instance: Verifier|undefined = undefined;
private certDownloader: CertificateDownloader;
@ -26,10 +26,32 @@ export default class Verifier {
console.log(certificate);
const dcc = await DCC.fromRaw(certificate);
console.log(dcc.payload);
let result = await dcc.checkSignatureWithKeysList(await this.certDownloader.getCertificates());
// const vaccineVerifier = new VaccineVerifier(await this.ruleDownloader.getRules());
// result = {signature: result, valid:vaccineVerifier.checkCertifcate(dcc)};
let result:unknown = {};
result = await this.checkKey(dcc);
const vaccineVerifier = new VaccineVerifier(await this.ruleDownloader.getRules());
result = {signature: result, valid: vaccineVerifier.checkCertifcate(dcc)};
console.log(result);
return result;
}
async checkKey(dcc:DCC):Promise<{valid:boolean, key?:string}>{
const publicCertificateCollection = await this.certDownloader.getCertificates();
const result = {valid:false, key: ''};
for(const tupla of publicCertificateCollection){
try {
const cECDSA = (jsrsasign.KEYUTIL
.getKey('-----BEGIN CERTIFICATE-----\n' + tupla.certificate+ '-----END CERTIFICATE-----') as jsrsasign.KJUR.crypto.ECDSA).getPublicKeyXYHex();
const signCheckResult = await dcc.checkSignature(cECDSA);
if(signCheckResult){
result.valid = true;
result.key = tupla.kid;
break;
}
} catch (error) {
if(error.message != 'Signature missmatch')
console.log(error); //to silence the errors
}
}
return result;
}
}