137 lines
6.3 KiB
TypeScript
137 lines
6.3 KiB
TypeScript
import {
|
|
Alert, Autocomplete, Button, Dialog, DialogActions, DialogContent, DialogTitle, MenuItem, Stack, TextField, Typography,
|
|
} from '@mui/material'
|
|
import { useState, type FormEvent } from 'react'
|
|
import { daysProblem, NAME_PATTERN, NEW_TEMPLATE } from './pki.ts'
|
|
import {
|
|
BUILTIN_TEMPLATES, KEY_USAGES, OID_PATTERN, PKI_KEYS, SAN_TYPES, TEMPLATE_SUBJECTS, type Pki, type PkiTemplate,
|
|
} from './api.ts'
|
|
|
|
/**
|
|
* A dialog that adds a certificate template, or with name changes one, a built-in template included: its name, fixed
|
|
* once it exists, the key the server makes, the lifetime, the key usages, the extended key usages, built in or custom,
|
|
* the kind of common name, the subject alternative names allowed and the certificate policy OIDs. onSave gets the name and the options; the dialog closes when it
|
|
* succeeds and shows the server's message when it fails.
|
|
*/
|
|
export default function TemplateDialog(props: {
|
|
pki: Pki
|
|
name?: string
|
|
onSave: (name: string, values: PkiTemplate) => Promise<void>
|
|
onCancel: () => void
|
|
}) {
|
|
const [name, setName] = useState(props.name ?? '')
|
|
const [values, setValues] = useState<PkiTemplate>(props.name !== undefined ? props.pki.templates[props.name] : NEW_TEMPLATE)
|
|
const [saving, setSaving] = useState(false)
|
|
const [error, setError] = useState<string | null>(null)
|
|
|
|
const change = (next: Partial<PkiTemplate>) => setValues({ ...values, ...next })
|
|
const nameError = props.name !== undefined ? ''
|
|
: !NAME_PATTERN.test(name.trim()) ? 'Up to 31 lower case letters, digits and dashes, starting with a letter.'
|
|
: props.pki.templates[name.trim()] !== undefined ? 'A template has this name.' : ''
|
|
const daysError = daysProblem(values.days)
|
|
const usageError = values.key_usage.length === 0 ? 'At least one key usage.' : ''
|
|
const wrongPolicies = values.policies.filter((item) => !OID_PATTERN.test(item))
|
|
const policiesError = wrongPolicies.length > 0 ? `Not OIDs in dotted form: ${wrongPolicies.join(', ')}.` : ''
|
|
const blocked = nameError !== '' || daysError !== '' || usageError !== '' || policiesError !== ''
|
|
const builtin = props.name !== undefined && (BUILTIN_TEMPLATES as readonly string[]).includes(props.name)
|
|
|
|
async function submit(event: FormEvent) {
|
|
event.preventDefault()
|
|
if (blocked) {
|
|
return
|
|
}
|
|
setSaving(true)
|
|
setError(null)
|
|
try {
|
|
await props.onSave(name.trim(), values)
|
|
} 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>{props.name === undefined ? 'Add template' : `Edit template ${props.name}`}</DialogTitle>
|
|
<DialogContent>
|
|
<Stack sx={{ gap: 2, pt: 1 }}>
|
|
{error && <Alert severity="error" sx={{ whiteSpace: 'pre-line' }}>{error}</Alert>}
|
|
<Typography variant="body2" color="text.secondary">
|
|
{builtin
|
|
? 'A built-in template: its options can be changed, and Reset brings back its defaults.'
|
|
: 'Applies to the certificates issued afterwards; the issuing CAs name the templates they issue.'}
|
|
</Typography>
|
|
<TextField
|
|
label="Name"
|
|
value={name}
|
|
disabled={props.name !== undefined}
|
|
autoComplete="off"
|
|
error={nameError !== ''}
|
|
helperText={nameError}
|
|
onChange={(event) => setName(event.target.value)}
|
|
/>
|
|
<Stack direction="row" sx={{ gap: 2 }}>
|
|
<TextField select fullWidth label="Key" value={values.key} onChange={(event) => change({ key: event.target.value })}>
|
|
{PKI_KEYS.map((item) => <MenuItem key={item} value={item}>{item}</MenuItem>)}
|
|
</TextField>
|
|
<TextField
|
|
fullWidth
|
|
label="Days"
|
|
type="number"
|
|
value={values.days}
|
|
error={daysError !== ''}
|
|
helperText={daysError}
|
|
onChange={(event) => change({ days: Math.trunc(Number(event.target.value) || 0) })}
|
|
/>
|
|
<TextField select fullWidth label="Subject" value={values.subject} onChange={(event) => change({ subject: event.target.value })}>
|
|
{TEMPLATE_SUBJECTS.map((item) => <MenuItem key={item} value={item}>{item}</MenuItem>)}
|
|
</TextField>
|
|
</Stack>
|
|
<Autocomplete
|
|
multiple
|
|
options={[...KEY_USAGES]}
|
|
value={values.key_usage}
|
|
onChange={(_event, next) => change({ key_usage: next })}
|
|
renderInput={(params) => <TextField {...params} label="Key usage" error={usageError !== ''} helperText={usageError} />}
|
|
/>
|
|
<Autocomplete
|
|
multiple
|
|
options={Object.keys(props.pki.extended_key_usages)}
|
|
value={values.extended_key_usage}
|
|
onChange={(_event, next) => change({ extended_key_usage: next })}
|
|
renderInput={(params) => (
|
|
<TextField {...params} label="Extended key usage" helperText="Custom usages are added on the Extended key usages tab." />
|
|
)}
|
|
/>
|
|
<Autocomplete
|
|
multiple
|
|
freeSolo
|
|
options={[]}
|
|
value={values.policies}
|
|
onChange={(_event, next) => change({ policies: next.map((item) => item.trim()).filter((item) => item !== '') })}
|
|
renderInput={(params) => (
|
|
<TextField
|
|
{...params}
|
|
label="Certificate policies"
|
|
error={policiesError !== ''}
|
|
helperText={policiesError || 'Policy OIDs in dotted form; type one and press Enter.'}
|
|
/>
|
|
)}
|
|
/>
|
|
<Autocomplete
|
|
multiple
|
|
options={[...SAN_TYPES]}
|
|
value={values.sans}
|
|
onChange={(_event, next) => change({ sans: next })}
|
|
renderInput={(params) => <TextField {...params} label="Alternative names" helperText="The kinds of subject alternative names allowed." />}
|
|
/>
|
|
</Stack>
|
|
</DialogContent>
|
|
<DialogActions>
|
|
<Button onClick={props.onCancel}>Cancel</Button>
|
|
<Button type="submit" variant="contained" disabled={saving || blocked}>Save</Button>
|
|
</DialogActions>
|
|
</Dialog>
|
|
)
|
|
}
|