import { Alert, Button, Dialog, DialogActions, DialogContent, DialogTitle, FormControlLabel, MenuItem, Stack, Switch, TextField, Typography } from '@mui/material' import { useState, type FormEvent } from 'react' import { ACME_PROVIDERS, type AcmeProvider, type HttpsChange, type HttpsSettings } from './api.ts' import { PROVIDER_LABELS } from './https.ts' /** The problem of a custom ACME directory URL, or ''. */ function urlProblem(value: string) { try { const url = new URL(value.trim()) return url.protocol === 'https:' && url.hostname !== '' && url.hash === '' ? '' : 'An https:// URL.' } catch { return 'An https:// URL.' } } /** * A dialog that edits the ACME CA Caddy takes certificates from, for the server's FQDN with https.certificate acme and * for the web sites with TLS acme: Let's Encrypt, with its staging environment, ZeroSSL, with an optional API key, or a * custom directory URL with optional EAB credentials. A secret left empty keeps its value. onSave sends the ACME * settings only; the dialog closes when it succeeds and shows the server's message when it fails. */ export default function AcmeSettingsDialog(props: { current: HttpsSettings onSave: (settings: HttpsChange) => Promise onCancel: () => void }) { const current = props.current const [provider, setProvider] = useState(current.acme_provider) const [staging, setStaging] = useState(current.acme_staging) const [url, setUrl] = useState(current.acme_url) const [keyId, setKeyId] = useState(current.acme_eab_key_id) const [macKey, setMacKey] = useState('') const [apiKey, setApiKey] = useState('') const [saving, setSaving] = useState(false) const [error, setError] = useState(null) const custom = provider === 'custom' const eabProblem = custom && ((keyId !== '' && macKey === '' && !current.acme_eab_mac_key_set) || (keyId === '' && macKey !== '')) ? 'The key ID and the MAC key are given together.' : '' const blocked = (custom && urlProblem(url) !== '') || eabProblem !== '' async function submit(event: FormEvent) { event.preventDefault() if (blocked) { return } setSaving(true) setError(null) const change: HttpsChange = { acme_provider: provider, acme_staging: staging, acme_url: custom ? url.trim() : current.acme_url, acme_eab_key_id: custom ? keyId.trim() : current.acme_eab_key_id, } if (custom && macKey !== '') { change.acme_eab_mac_key = macKey } if (custom && keyId.trim() === '') { change.acme_eab_mac_key = '' } if (provider === 'zerossl' && apiKey !== '') { change.zerossl_api_key = apiKey } try { await props.onSave(change) } catch (caught) { setError((caught as Error).message) setSaving(false) } } return ( reason !== 'backdropClick' && props.onCancel()} maxWidth="sm" fullWidth slotProps={{ paper: { component: 'form', onSubmit: submit } }}> ACME CA {error && {error}} Caddy takes certificates from this CA for the server, when its certificate is ACME, and for the web sites with TLS ACME. setProvider(event.target.value as AcmeProvider)}> {ACME_PROVIDERS.map((item) => {PROVIDER_LABELS[item]})} {provider === 'letsencrypt' && ( setStaging(event.target.checked)} />} label="Staging environment, for tests: no browser trusts its certificates" /> )} {provider === 'zerossl' && ( setApiKey(event.target.value.trim())} /> )} {custom && ( <> setUrl(event.target.value.trim())} /> setKeyId(event.target.value.trim())} /> setMacKey(event.target.value.trim())} /> )} The server runs its setup again, which applies the change. ) }