@openade/fe
Version:
Fatturazione Elettronica - Electronic Invoicing for Sistema di Interscambio (SDI)
91 lines • 3.73 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.TSAService = void 0;
const asn1_service_1 = require("./asn1.service");
const http_service_1 = require("./http.service");
class TSAService {
constructor(config) {
this.config = config;
this.httpService = new http_service_1.HttpService();
this.asn1Service = new asn1_service_1.Asn1Service();
}
async requestTimestamp(contentHash, _contentType) {
try {
const timestampRequest = await this.asn1Service.createTimestampRequest(contentHash);
const headers = {
'Content-Type': 'application/timestamp-query',
'Content-Length': timestampRequest.length.toString(),
};
if (this.config.username && this.config.password) {
const auth = Buffer.from(`${this.config.username}:${this.config.password}`).toString('base64');
headers['Authorization'] = `Basic ${auth}`;
}
const response = await this.httpService.request({
url: this.config.endpoint,
method: 'POST',
headers,
body: timestampRequest,
timeout: this.config.timeout || 30000,
});
if (response.status !== 200) {
throw new Error(`TSA request failed with status ${response.status}`);
}
const timestampData = await this.asn1Service.parseTimestampResponse(response.data);
return {
timestampToken: Buffer.from(response.data).toString('base64'),
timestamp: timestampData.timestamp,
tsa: this.config.endpoint,
accuracy: timestampData.accuracy,
};
}
catch (error) {
console.warn('TSA service failed, using mock timestamp:', error instanceof Error ? error.message : 'Unknown error');
return this.createMockTimestamp(contentHash);
}
}
async verifyTimestamp(timestampToken, _expectedContent) {
try {
const tokenBuffer = Buffer.from(timestampToken, 'base64');
const timestampData = await this.asn1Service.parseTimestampResponse(tokenBuffer);
const timestampDate = new Date(timestampData.timestamp);
const now = new Date();
const oneYearAgo = new Date(now.getTime() - 365 * 24 * 60 * 60 * 1000);
return timestampDate >= oneYearAgo;
}
catch (error) {
console.error('Timestamp verification failed:', error);
return false;
}
}
createMockTimestamp(contentHash) {
const now = new Date();
const mockToken = Buffer.from(JSON.stringify({
hash: contentHash,
timestamp: now.getTime(),
tsa: 'mock-tsa',
accuracy: 1,
})).toString('base64');
return {
timestampToken: mockToken,
timestamp: now.toISOString(),
tsa: 'mock-tsa',
accuracy: 1,
};
}
async getTimestampInfo(timestampToken) {
try {
const tokenBuffer = Buffer.from(timestampToken, 'base64');
const result = await this.asn1Service.parseTimestampResponse(tokenBuffer);
return {
timestamp: result.timestamp,
tsa: result.tsa || 'unknown-tsa',
accuracy: result.accuracy,
};
}
catch (error) {
throw new Error(`Failed to parse timestamp token: ${error instanceof Error ? error.message : 'Unknown error'}`);
}
}
}
exports.TSAService = TSAService;
//# sourceMappingURL=timestamping.service.js.map