89 lines
4.5 KiB
TypeScript
89 lines
4.5 KiB
TypeScript
import { Alert, Button, Checkbox, Dialog, DialogActions, DialogContent, DialogTitle, FormControlLabel, MenuItem, Stack, TextField, Typography } from '@mui/material'
|
|
import { useState, type FormEvent } from 'react'
|
|
import { HTTPS_CERTIFICATES, type HttpsCertificateSource, type HttpsChange, type HttpsSettings } from './api.ts'
|
|
import { CERTIFICATE_LABELS } from './https.ts'
|
|
|
|
/**
|
|
* A dialog that edits where the certificate of the server's HTTPS names comes from, https.certificate: the ACME CA of
|
|
* AcmeSettingsDialog, a self-signed certificate valid for https.self_signed_days, the Servers Issuing CA, or another
|
|
* CA's certificate, imported on the Server tab of the Certificates page; self-signed is selected while the first setup
|
|
* has not chosen one. Without ACME, https.ip_sans adds the server's addresses to the self-signed certificate and the
|
|
* Servers Issuing CA's. onSave sends the source, the validity and ip_sans only; the dialog closes when it succeeds and
|
|
* shows the server's message when it fails.
|
|
*/
|
|
export default function HttpsSettingsDialog(props: {
|
|
current: HttpsSettings
|
|
onSave: (settings: HttpsChange) => Promise<void>
|
|
onCancel: () => void
|
|
}) {
|
|
const [certificate, setCertificate] = useState<HttpsCertificateSource>(props.current.certificate || 'self-signed')
|
|
const [days, setDays] = useState(String(props.current.self_signed_days))
|
|
const [ipSans, setIpSans] = useState(props.current.ip_sans)
|
|
const [saving, setSaving] = useState(false)
|
|
const [error, setError] = useState<string | null>(null)
|
|
|
|
const daysProblem = /^\d{1,4}$/.test(days) && Number(days) >= 1 && Number(days) <= 3650 ? '' : '1 to 3650 days.'
|
|
const blocked = certificate === 'self-signed' && daysProblem !== ''
|
|
|
|
async function submit(event: FormEvent) {
|
|
event.preventDefault()
|
|
if (blocked) {
|
|
return
|
|
}
|
|
setSaving(true)
|
|
setError(null)
|
|
try {
|
|
await props.onSave(certificate === 'self-signed' ? { certificate, self_signed_days: Number(days), ip_sans: ipSans }
|
|
: certificate === 'acme' ? { certificate } : { certificate, ip_sans: ipSans })
|
|
} 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>HTTPS certificate</DialogTitle>
|
|
<DialogContent>
|
|
<Stack sx={{ gap: 2, pt: 1 }}>
|
|
{error && <Alert severity="error" sx={{ whiteSpace: 'pre-line' }}>{error}</Alert>}
|
|
<TextField select label="Certificate of the server" value={certificate} onChange={(event) => setCertificate(event.target.value as HttpsCertificateSource)}>
|
|
{HTTPS_CERTIFICATES.map((item) => <MenuItem key={item} value={item}>{CERTIFICATE_LABELS[item]}</MenuItem>)}
|
|
</TextField>
|
|
{certificate === 'acme' && (
|
|
<Typography variant="body2" color="text.secondary">
|
|
Caddy takes the certificate from the ACME CA, which its own settings choose.
|
|
</Typography>
|
|
)}
|
|
{certificate === 'self-signed' && (
|
|
<TextField
|
|
label="Validity (days)"
|
|
value={days}
|
|
error={daysProblem !== ''}
|
|
helperText={daysProblem || 'The server makes a new one when a third of it is left.'}
|
|
slotProps={{ htmlInput: { inputMode: 'numeric' } }}
|
|
onChange={(event) => setDays(event.target.value.trim())}
|
|
/>
|
|
)}
|
|
{(certificate === 'self-signed' || certificate === 'ca') && (
|
|
<FormControlLabel
|
|
control={<Checkbox checked={ipSans} onChange={(event) => setIpSans(event.target.checked)} />}
|
|
label="Also name the server's IP addresses, to reach it over HTTPS by address"
|
|
/>
|
|
)}
|
|
{certificate === 'external' && (
|
|
<Typography variant="body2" color="text.secondary">
|
|
The certificate is imported on this tab, from a CSR the server makes or as a PFX file. The server serves a self-signed certificate until then.
|
|
</Typography>
|
|
)}
|
|
<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>
|
|
)
|
|
}
|