127 lines
5.8 KiB
TypeScript
127 lines
5.8 KiB
TypeScript
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<void>
|
|
onCancel: () => void
|
|
}) {
|
|
const current = props.current
|
|
const [provider, setProvider] = useState<AcmeProvider>(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<string | null>(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 (
|
|
<Dialog open onClose={(_event, reason) => reason !== 'backdropClick' && props.onCancel()} maxWidth="sm" fullWidth slotProps={{ paper: { component: 'form', onSubmit: submit } }}>
|
|
<DialogTitle>ACME CA</DialogTitle>
|
|
<DialogContent>
|
|
<Stack sx={{ gap: 2, pt: 1 }}>
|
|
{error && <Alert severity="error" sx={{ whiteSpace: 'pre-line' }}>{error}</Alert>}
|
|
<Typography variant="body2" color="text.secondary">
|
|
Caddy takes certificates from this CA for the server, when its certificate is ACME, and for the web sites with TLS ACME.
|
|
</Typography>
|
|
<TextField select label="Provider" value={provider} onChange={(event) => setProvider(event.target.value as AcmeProvider)}>
|
|
{ACME_PROVIDERS.map((item) => <MenuItem key={item} value={item}>{PROVIDER_LABELS[item]}</MenuItem>)}
|
|
</TextField>
|
|
{provider === 'letsencrypt' && (
|
|
<FormControlLabel control={<Switch checked={staging} onChange={(event) => setStaging(event.target.checked)} />} label="Staging environment, for tests: no browser trusts its certificates" />
|
|
)}
|
|
{provider === 'zerossl' && (
|
|
<TextField
|
|
label="API key"
|
|
type="password"
|
|
value={apiKey}
|
|
autoComplete="off"
|
|
helperText={current.zerossl_api_key_set ? 'Set; empty keeps it.' : 'Optional; without it the EAB credentials come from the e-mail address of https.email.'}
|
|
onChange={(event) => setApiKey(event.target.value.trim())}
|
|
/>
|
|
)}
|
|
{custom && (
|
|
<>
|
|
<TextField
|
|
label="ACME directory URL"
|
|
value={url}
|
|
autoComplete="off"
|
|
error={url !== '' && urlProblem(url) !== ''}
|
|
helperText={(url !== '' && urlProblem(url)) || 'Such as a HashiCorp Vault, Caddy or Simple CA ACME directory; the CA of its HTTPS certificate is in the server\'s trust store.'}
|
|
onChange={(event) => setUrl(event.target.value.trim())}
|
|
/>
|
|
<TextField label="EAB key ID" value={keyId} autoComplete="off" helperText="Optional." onChange={(event) => setKeyId(event.target.value.trim())} />
|
|
<TextField
|
|
label="EAB MAC key"
|
|
type="password"
|
|
value={macKey}
|
|
autoComplete="off"
|
|
error={eabProblem !== ''}
|
|
helperText={eabProblem || (current.acme_eab_mac_key_set ? 'Set; empty keeps it.' : 'With the key ID.')}
|
|
onChange={(event) => setMacKey(event.target.value.trim())}
|
|
/>
|
|
</>
|
|
)}
|
|
<Alert severity="info">The server runs its setup again, which applies the change.</Alert>
|
|
</Stack>
|
|
</DialogContent>
|
|
<DialogActions>
|
|
<Button onClick={props.onCancel} disabled={saving}>Cancel</Button>
|
|
<Button type="submit" variant="contained" disabled={saving || blocked}>Save</Button>
|
|
</DialogActions>
|
|
</Dialog>
|
|
)
|
|
}
|