From 26acc020b919a902268fdf2cb17edd475e467e04 Mon Sep 17 00:00:00 2001 From: gianm Date: Sat, 25 Sep 2021 11:56:18 +0200 Subject: [PATCH] updating certifcates utility --- src/app.ts | 2 +- .../dgcVerifier/CertificateDownloader.ts | 108 +++++++++--------- src/utils/dgcVerifier/Verifier.ts | 5 +- 3 files changed, 57 insertions(+), 58 deletions(-) diff --git a/src/app.ts b/src/app.ts index a806054..f390120 100644 --- a/src/app.ts +++ b/src/app.ts @@ -4,7 +4,7 @@ import cors from 'cors'; const app = express(); -app.set('port', process.env.PORT || 3000); +app.set('port', process.env.PORT || 5050); app.use(cors()); app.use(express.json()); app.use(express.urlencoded()); diff --git a/src/utils/dgcVerifier/CertificateDownloader.ts b/src/utils/dgcVerifier/CertificateDownloader.ts index edb3aff..9d96f8d 100644 --- a/src/utils/dgcVerifier/CertificateDownloader.ts +++ b/src/utils/dgcVerifier/CertificateDownloader.ts @@ -7,9 +7,9 @@ export class CertificateDownloader{ private readonly updateApi = '/v1/dgc/signercertificate/update' private readonly statusApi = '/v1/dgc/signercertificate/status' private readonly keyStorage = './cerificate_collection.json'; - // private readonly timeSpan = 86400000; - private readonly timeSpan = 1; - private cerficateCollection:unknown = {}; + private readonly timeSpan = 86400000; + // private readonly timeSpan = 1; + private certificatesCollection:{kid:string,certificate:string}[] = []; private currentValidKids:string[] = []; public async getCertificates(): Promise { @@ -19,12 +19,12 @@ export class CertificateDownloader{ data = (await file.readFile()).toString('utf-8'); await file.close(); const savedData = JSON.parse( data || '{}'); - // if(savedData.lastupdateDate == null || Date.now() - savedData?.lastupdateDate > this.timeSpan){ - // await this.getAllCertificate(); - // } else { - this.cerficateCollection = savedData.certificates; - // } - return this.cerficateCollection; + if(savedData.lastupdateDate == null || Date.now() - savedData?.lastupdateDate > this.timeSpan){ + await this.getAllCertificate(); + } else { + this.certificatesCollection = savedData.certificates; + } + return this.certificatesCollection; } catch (error) { console.log(error); if(error.errno == -2){ @@ -42,54 +42,52 @@ export class CertificateDownloader{ // return CertificateDownloader.instance; // } + // async getAllCertificate(): Promise { + // this.cerficateCollection = {}; + // const response:AxiosResponse = (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 { - this.cerficateCollection = {}; - const response:AxiosResponse = (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); + let exit = false; + let headers = {}; + this.certificatesCollection = []; + while(!exit){ + const response:AxiosResponse = await axios.get(this.baseUrl+this.updateApi,{headers}); + 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})); + console.log(this.certificatesCollection); + await file.close(); } - // async getAllCertificate(): Promise { - // let exit = false; - // let headers = {}; - // this.cerficateCollection = []; - // while(!exit){ - // // const response = await fetch(this.baseUrl+this.updateApi,{headers}); - // const response:AxiosResponse = await axios.get(this.baseUrl+this.updateApi,{headers}); - // // console.log(response.headers); - // 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 = `-----BEGIN CERTIFICATE-----${response.data}-----END CERTIFICATE-----`; - // // console.log(cert); - // this.cerficateCollection.push(cert); - // } - // exit = (response.status !== 200); - // } - // 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(); - // } - - // async updateKids(): Promise { - // try { - // const resp = await axios.get(this.baseUrl+this.statusApi); - // this.currentValidKids = await resp.data as string[]; - // } catch (error) { - // console.log('could not get keyChild ', error); - // } - // } + async updateKids(): Promise { + try { + const resp = await axios.get(this.baseUrl+this.statusApi); + this.currentValidKids = await resp.data as string[]; + } catch (error) { + console.log('could not get keyChild ', error); + } + } } \ No newline at end of file diff --git a/src/utils/dgcVerifier/Verifier.ts b/src/utils/dgcVerifier/Verifier.ts index 80679c6..8877dbe 100644 --- a/src/utils/dgcVerifier/Verifier.ts +++ b/src/utils/dgcVerifier/Verifier.ts @@ -25,9 +25,10 @@ export default class Verifier { async checkCertificate(certificate:string): Promise{ 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)}; + // const vaccineVerifier = new VaccineVerifier(await this.ruleDownloader.getRules()); + // result = {signature: result, valid:vaccineVerifier.checkCertifcate(dcc)}; console.log(result); return result; }