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 onCancel: () => void }) { const [name, setName] = useState(props.name ?? '') const [values, setValues] = useState(props.name !== undefined ? props.pki.templates[props.name] : NEW_TEMPLATE) const [saving, setSaving] = useState(false) const [error, setError] = useState(null) const change = (next: Partial) => 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 ( reason !== 'backdropClick' && props.onCancel()} maxWidth="sm" fullWidth slotProps={{ paper: { component: 'form', onSubmit: submit } }}> {props.name === undefined ? 'Add template' : `Edit template ${props.name}`} {error && {error}} {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.'} setName(event.target.value)} /> change({ key: event.target.value })}> {PKI_KEYS.map((item) => {item})} change({ days: Math.trunc(Number(event.target.value) || 0) })} /> change({ subject: event.target.value })}> {TEMPLATE_SUBJECTS.map((item) => {item})} change({ key_usage: next })} renderInput={(params) => } /> change({ extended_key_usage: next })} renderInput={(params) => ( )} /> change({ policies: next.map((item) => item.trim()).filter((item) => item !== '') })} renderInput={(params) => ( )} /> change({ sans: next })} renderInput={(params) => } /> ) }