moved the api folder

This commit is contained in:
2021-09-23 17:17:00 +02:00
parent 0edd12ebad
commit d8a836aa90
10 changed files with 383 additions and 55 deletions

View File

@@ -1,45 +1,78 @@
// import crypto from 'crypto';
import fs from 'fs/promises';
export class CertificateDownloader{
static instance: CertificateDownloader;
// private readonly baseUrl = 'https://get.dgc.gov.it';
private readonly keyStorage = 'cerificate_collection';
// 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';
private readonly timeSpan = 86400000;
// private readonly timeSpan = 1000;
private cerficateCollection = {}
// eslint-disable-next-line @typescript-eslint/no-empty-function
private constructor(){}
private cerficateCollection:string[] = [];
private currentValidKids = [];
public getCertificates(): unknown {
const savedData = JSON.parse(localStorage.getItem(this.keyStorage) || '{}');
public constructor(){
this.getCertificates();
}
public async getCertificates(): Promise<string[]> {
const file = await fs.open(this.keyStorage,'r');
const data = await file.readFile();
const savedData = JSON.parse( data.toString() || '{}');
if(savedData.lastupdateDate == null || Date.now() - savedData?.lastupdateDate > this.timeSpan){
this.getAllCertificate()
.then(() => { console.log('could not read the certificates from the local file'); return this.cerficateCollection; })
.catch(console.error);
}
console.log('cerficates collection is valid loading from local source');
// console.log(dataRead.certificates);
this.cerficateCollection = savedData.certificates;
return this.cerficateCollection;
}
public static getCertificateDownloader():CertificateDownloader{
if(CertificateDownloader.instance == undefined){
CertificateDownloader.instance = new CertificateDownloader();
}
return CertificateDownloader.instance;
}
// public static getCertificateDownloader():CertificateDownloader{
// if(CertificateDownloader.instance == undefined){
// CertificateDownloader.instance = new CertificateDownloader();
// }
// return CertificateDownloader.instance;
// }
// async getAllCertificate(): Promise<void> {
// this.cerficateCollection = {};
// const response = (await fetch('https://raw.githubusercontent.com/lovasoa/sanipasse/master/src/assets/Digital_Green_Certificate_Signing_Keys.json'));
// if(response.ok){
// this.cerficateCollection = await response.json();
// console.log(response);
// const lastupdateDate = Date.now();
// const file = await fs.open(this.keyStorage,'rw');
// file.writeFile(JSON.stringify({'certificates':this.cerficateCollection, lastupdateDate}));
// }else{
// throw new Error(response.statusText);
// }
// }
async getAllCertificate(): Promise<void> {
this.cerficateCollection = {};
const response = (await fetch('https://raw.githubusercontent.com/lovasoa/sanipasse/master/src/assets/Digital_Green_Certificate_Signing_Keys.json'));
if(response.ok){
this.cerficateCollection = await response.json();
console.log(response);
const lastupdateDate = Date.now();
localStorage.setItem(this.keyStorage, JSON.stringify({'certificates':this.cerficateCollection, lastupdateDate}));
// fs.writeFile('./cerificate_collection.json', JSON.stringify({'certificates':this.cerficateCollection, lastupdateDate}),'utf8',console.error);
}else{
throw new Error(response.statusText);
let exit = false;
let headers = {};
this.cerficateCollection = [];
while(!exit){
const response = await fetch(this.baseUrl+this.updateApi,{headers});
headers = {'X-RESUME-TOKEN': response.headers.get('X-RESUME-TOKEN')};
const currentKid:string = response.headers.get('X-KID') || '';
if(this.currentValidKids.includes(currentKid as never)){
const cert = await response.text();
this.cerficateCollection.push('-----BEGIN CERTIFICATE-----\n' + cert + '-----END CERTIFICATE-----');
}
exit = (response.status !== 200);
}
const lastupdateDate = Date.now();
const file = await fs.open(this.keyStorage,'rw');
file.writeFile(JSON.stringify({'certificates':this.cerficateCollection, lastupdateDate}));
}
async updateKids(): Promise<void> {
try {
const resp = await fetch(this.baseUrl+this.statusApi);
this.currentValidKids = await resp.json();
} catch (error) {
console.log('could not get keyChild ', error);
}
}
}

View File

@@ -6,7 +6,7 @@ export class RuleDownloader {
// private readonly timeSpan = 1000;
public rules:unknown = {}
// eslint-disable-next-line @typescript-eslint/no-empty-function
private constructor(){
constructor(){
this.getRules();
}
@@ -23,13 +23,6 @@ export class RuleDownloader {
return this.rules;
}
static getRuleDownloader(): RuleDownloader{
if(this.instance == undefined){
RuleDownloader.instance = new RuleDownloader();
}
return RuleDownloader.instance;
}
private async getSettings(): Promise<unknown>{
const response = await fetch(`${this.baseUrl}/v1/dgc/settings`);
const jsonData = await response.json();

View File

@@ -1,21 +1,38 @@
import { CertificateDownloader } from './CertificateDownloader';
import { RuleDownloader } from './RuleDownloader';
import {DCC} from 'dcc-utils';
import jsrsasign from 'jsrsasign';
export default class Verifier {
static certDownloader: CertificateDownloader;
static ruleDownloader: RuleDownloader;
static certificateList: unknown;
static instance: Verifier|undefined = undefined;
private certDownloader: CertificateDownloader;
private ruleDownloader: RuleDownloader;
private certificateList: string[] = [];
static async setup():Promise<void> {
Verifier.certDownloader = CertificateDownloader.getCertificateDownloader();
Verifier.ruleDownloader = RuleDownloader.getRuleDownloader();
Verifier.certificateList = await Verifier.certDownloader.getCertificates();
private constructor(){
this.certDownloader = new CertificateDownloader();
this.ruleDownloader = new RuleDownloader();
}
static async checkCertificate(certificate:string): Promise<unknown>{
public static async instanceVerifier(): Promise<Verifier>{
if (Verifier.instance == undefined){
Verifier.instance = new Verifier();
Verifier.instance.certificateList = await Verifier.instance.certDownloader.getCertificates();
}
return Verifier.instance;
}
async checkCertificate(certificate:string): Promise<unknown>{
const dcc = await DCC.fromRaw(certificate);
const certCheck = await dcc.checkSignatureWithKeysList(Verifier.certificateList);
return certCheck;
let result: unknown;
this.certificateList.forEach(async (cert: string) => {
const verifier = jsrsasign.KEYUTIL.getKey(cert);
if (typeof verifier == typeof jsrsasign.KJUR.crypto.ECDSA ){
const xyCoord = (verifier as jsrsasign.KJUR.crypto.ECDSA).getPublicKeyXYHex();
result = await dcc.checkSignature(xyCoord);
}
});
console.log(result);
return result;
}
}