Files
p2ns/includes/admin/ui/certs.js
T
2025-12-17 20:05:50 -05:00

170 lines
5.3 KiB
JavaScript

// Certificates UI functions
function regenerateCA() {
if (window.showConfirm) {
window.showConfirm('Regenerate Root CA?', async () => {
try {
const response = await fetch('/api/regenerate-ca', { method: 'POST' });
if (!response.ok) {
throw new Error(await response.text());
}
if (window.showNotification) window.showNotification('Root CA regenerated successfully');
if (window.genericFetch) window.genericFetch('certs', true);
} catch (err) {
console.error('Failed to regenerate CA:', err);
if (window.showNotification) window.showNotification('Failed to regenerate CA: ' + err.message, 'error');
}
});
}
}
function installCA() {
if (window.showConfirm) {
window.showConfirm('Install Root CA?', async () => {
try {
const response = await fetch('/api/install-ca', { method: 'POST' });
if (!response.ok) {
throw new Error(await response.text());
}
if (window.showNotification) window.showNotification('Root CA installed successfully');
} catch (err) {
console.error('Failed to install CA:', err);
if (window.showNotification) window.showNotification('Failed to install CA: ' + err.message, 'error');
}
});
}
}
async function generateCert() {
const domainEl = document.getElementById('cert-domain');
if (!domainEl) return;
const domain = domainEl.value;
try {
const response = await fetch('/api/generate-cert', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ domain })
});
if (!response.ok) {
throw new Error(await response.text());
}
if (window.showNotification) window.showNotification('Certificate generated successfully');
if (window.genericFetch) window.genericFetch('certs', true);
} catch (err) {
console.error('Failed to generate cert:', err);
if (window.showNotification) window.showNotification('Failed to generate cert: ' + err.message, 'error');
}
}
function deleteCert(domain) {
if (window.showConfirm) {
window.showConfirm(`Delete certificate for ${domain}?`, async () => {
try {
const response = await fetch('/api/delete-cert', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ domain })
});
if (!response.ok) {
throw new Error(await response.text());
}
if (window.showNotification) window.showNotification('Certificate deleted successfully');
if (window.genericFetch) window.genericFetch('certs', true);
} catch (err) {
console.error('Failed to delete cert:', err);
if (window.showNotification) window.showNotification('Failed to delete cert: ' + err.message, 'error');
}
});
}
}
function regenerateCert(domain) {
if (window.showConfirm) {
window.showConfirm(`Regenerate certificate for ${domain}?`, async () => {
try {
const response = await fetch('/api/regenerate-cert', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ domain })
});
if (!response.ok) {
throw new Error(await response.text());
}
if (window.showNotification) window.showNotification('Certificate regenerated successfully');
if (window.genericFetch) window.genericFetch('certs', true);
} catch (err) {
console.error('Failed to regenerate cert:', err);
if (window.showNotification) window.showNotification('Failed to regenerate cert: ' + err.message, 'error');
}
});
}
}
async function showCertDetails(domain) {
try {
const res = await fetch(`/api/cert-details?domain=${encodeURIComponent(domain)}`);
if (!res.ok) {
throw new Error(await res.text());
}
const data = await res.text();
const formatted = formatCertificate(data);
const contentEl = document.getElementById('cert-details-content');
const modal = document.getElementById('certDetailsModal');
if (contentEl) contentEl.textContent = formatted;
if (modal) modal.showModal();
} catch (err) {
console.error('Failed to fetch cert details:', err);
if (window.showNotification) window.showNotification('Failed to load certificate details: ' + err.message, 'error');
}
}
function formatCertificate(certPem) {
return certPem.replace(/(-----BEGIN CERTIFICATE-----)/g, '\n$1\n')
.replace(/(-----END CERTIFICATE-----)/g, '\n$1\n')
.trim();
}
function copyCertDetails() {
const contentEl = document.getElementById('cert-details-content');
if (!contentEl) return;
const content = contentEl.textContent;
navigator.clipboard.writeText(content).then(() => {
if (window.showNotification) window.showNotification('Certificate details copied to clipboard', 'success');
}).catch(err => {
console.error('Failed to copy:', err);
if (window.showNotification) window.showNotification('Failed to copy certificate details', 'error');
});
}
window.regenerateCA = regenerateCA;
window.installCA = installCA;
window.generateCert = generateCert;
window.deleteCert = deleteCert;
window.regenerateCert = regenerateCert;
window.showCertDetails = showCertDetails;
window.formatCertificate = formatCertificate;
window.copyCertDetails = copyCertDetails;