fix(cert): fix tsclass ICert usage

This commit is contained in:
Juergen Kunz
2025-07-13 00:41:44 +00:00
parent 2d2e9e9475
commit eac6075a12
2 changed files with 30 additions and 20 deletions

View File

@ -15,12 +15,15 @@ const testCert = fs.readFileSync(path.join(__dirname, 'helpers/test-cert.pem'),
const testKey = fs.readFileSync(path.join(__dirname, 'helpers/test-key.pem'), 'utf8'); const testKey = fs.readFileSync(path.join(__dirname, 'helpers/test-key.pem'), 'utf8');
tap.test('SmartProxy should support custom certificate provision function', async () => { tap.test('SmartProxy should support custom certificate provision function', async () => {
// Create test certificate object - the type is from plugins.tsclass.network.ICert // Create test certificate object matching ICert interface
// but we return a simple object with the required properties
const testCertObject = { const testCertObject = {
cert: testCert, id: 'test-cert-1',
key: testKey, domainName: 'test.example.com',
ca: '' created: Date.now(),
validUntil: Date.now() + 90 * 24 * 60 * 60 * 1000, // 90 days
privateKey: testKey,
publicKey: testCert,
csr: ''
}; };
// Custom certificate store for testing // Custom certificate store for testing
@ -35,7 +38,7 @@ tap.test('SmartProxy should support custom certificate provision function', asyn
// Return custom cert for known domains // Return custom cert for known domains
if (customCerts.has(domain)) { if (customCerts.has(domain)) {
console.log(`Returning custom certificate for ${domain}`); console.log(`Returning custom certificate for ${domain}`);
return customCerts.get(domain)! as unknown as TSmartProxyCertProvisionObject; return customCerts.get(domain)!;
} }
// Fallback to Let's Encrypt for other domains // Fallback to Let's Encrypt for other domains
@ -81,13 +84,16 @@ tap.test('Custom certificate provision function should be called', async () => {
provisionCalled = true; provisionCalled = true;
provisionedDomains.push(domain); provisionedDomains.push(domain);
// Return a test certificate using the loaded files // Return a test certificate matching ICert interface
// We need to return a proper cert object that satisfies the type
return { return {
cert: testCert, id: `test-cert-${domain}`,
key: testKey, domainName: domain,
ca: '' created: Date.now(),
} as unknown as TSmartProxyCertProvisionObject; validUntil: Date.now() + 90 * 24 * 60 * 60 * 1000,
privateKey: testKey,
publicKey: testCert,
csr: ''
};
}, },
acme: { acme: {
email: 'test@example.com', email: 'test@example.com',
@ -278,10 +284,14 @@ tap.test('Should return http01 for unknown domains', async () => {
certProvisionFunction: async (domain: string): Promise<TSmartProxyCertProvisionObject> => { certProvisionFunction: async (domain: string): Promise<TSmartProxyCertProvisionObject> => {
if (domain === 'known.example.com') { if (domain === 'known.example.com') {
return { return {
cert: testCert, id: `test-cert-${domain}`,
key: testKey, domainName: domain,
ca: '' created: Date.now(),
} as unknown as TSmartProxyCertProvisionObject; validUntil: Date.now() + 90 * 24 * 60 * 60 * 1000,
privateKey: testKey,
publicKey: testCert,
csr: ''
};
} }
returnedHttp01 = true; returnedHttp01 = true;
return 'http01'; return 'http01';

View File

@ -260,11 +260,11 @@ export class SmartCertManager {
// Convert to internal certificate format // Convert to internal certificate format
const certData: ICertificateData = { const certData: ICertificateData = {
cert: customCert.cert, cert: customCert.publicKey,
key: customCert.key, key: customCert.privateKey,
ca: customCert.ca || '', ca: '',
issueDate: new Date(), issueDate: new Date(),
expiryDate: this.extractExpiryDate(customCert.cert), expiryDate: this.extractExpiryDate(customCert.publicKey),
source: 'custom' source: 'custom'
}; };