DGCServerVerifier/src/Services/SettingsDownloader/CertificateDownloader.ts

66 lines
2.5 KiB
TypeScript
Raw Normal View History

2021-09-23 17:17:00 +02:00
import fs from 'fs/promises';
2021-09-24 15:21:24 +02:00
import axios, { AxiosResponse } from 'axios';
2021-09-23 15:38:13 +02:00
export class CertificateDownloader{
2021-09-23 17:17:00 +02:00
// static instance: CertificateDownloader;
private readonly baseUrl = 'https://get.dgc.gov.it';
private readonly updateApi = '/v1/dgc/signercertificate/update'
private readonly statusApi = '/v1/dgc/signercertificate/status'
private readonly keyStorage = './cerificate_collection.json';
2021-09-25 11:56:18 +02:00
private readonly timeSpan = 86400000;
// private readonly timeSpan = 1;
private certificatesCollection:{kid:string,certificate:string}[] = [];
2021-09-24 15:21:24 +02:00
private currentValidKids:string[] = [];
2021-09-23 17:17:00 +02:00
2021-09-25 14:15:49 +02:00
public async getCertificates(): Promise<{kid:string,certificate:string}[]> {
2021-09-24 15:21:24 +02:00
let data = '{}';
try {
const file = await fs.open(this.keyStorage,'r');
2021-09-25 12:06:54 +02:00
data = (await file.readFile()).toString('utf-8') || '{}';
2021-09-24 15:21:24 +02:00
await file.close();
2021-09-25 12:06:54 +02:00
const savedData = JSON.parse(data);
if(savedData.lastupdateDate == null || Date.now() - savedData?.lastupdateDate > this.timeSpan){
2021-09-25 14:15:49 +02:00
await this.updateKids();
2021-09-25 12:06:54 +02:00
await this.getAllCertificate();
} else {
2021-09-25 11:56:18 +02:00
this.certificatesCollection = savedData.certificates;
2021-09-25 12:06:54 +02:00
}
2021-09-25 11:56:18 +02:00
return this.certificatesCollection;
2021-09-24 15:21:24 +02:00
} catch (error) {
2021-09-26 15:42:30 +02:00
if(error.message == 'ENOENT: no such file or directory, open \'rules.json\''){
await fs.writeFile(this.keyStorage,'{}');
return this.getCertificates();
2021-09-24 15:21:24 +02:00
}
2021-09-23 15:38:13 +02:00
}
}
2021-09-25 14:15:49 +02:00
private async getAllCertificate(): Promise<void> {
2021-09-25 11:56:18 +02:00
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)){
const cert = {kid:currentKid, certificate: response.data};
this.certificatesCollection.push(cert);
}
exit = (response.status !== 200);
}
const lastupdateDate = Date.now();
const file = await fs.open(this.keyStorage,'w');
2021-09-25 14:15:49 +02:00
await file.writeFile(JSON.stringify({'certificates':this.certificatesCollection, lastupdateDate}));
2021-09-25 11:56:18 +02:00
console.log(this.certificatesCollection);
await file.close();
}
2021-09-25 14:15:49 +02:00
private async updateKids(): Promise<void> {
2021-09-25 11:56:18 +02:00
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);
}
}
2021-09-23 15:38:13 +02:00
}