import { Alert, Button, Dialog, DialogActions, DialogContent, DialogTitle, Stack, TextField } from '@mui/material' import { useState, type FormEvent } from 'react' import { CODE_PATTERN, PROFILE_SHAPE, request } from './api.ts' import { saveFile } from './saveFile.ts' /** The length of a password the server accepts for the key in an Apple profile. */ const PASSWORD_MIN = 8 const PASSWORD_MAX = 128 /** The profiles: their title, file extension and content type. */ const KINDS = { openvpn: { title: 'OpenVPN profile', extension: 'ovpn', type: 'application/x-openvpn-profile' }, apple: { title: 'Apple VPN profile', extension: 'mobileconfig', type: 'application/x-apple-aspen-config' }, } /** * A dialog that downloads a VPN profile of a user, saved as .ovpn or .mobileconfig: for the signed-in * user's own profile (own) the current password and, when enrolled (otp), a code, and for an Apple profile the * password that protects the user's key in it, typed twice. path is the server's call, such as * /users//apple-profile or /account/profile. The dialog closes when the file is saved and shows the server's * message when it fails. */ export default function ProfileDialog(props: { kind: keyof typeof KINDS name: string path: string own: boolean otp: boolean onClose: () => void }) { const kind = KINDS[props.kind] const apple = props.kind === 'apple' const [currentPassword, setCurrentPassword] = useState('') const [currentCode, setCurrentCode] = useState('') const [password, setPassword] = useState('') const [again, setAgain] = useState('') const [saving, setSaving] = useState(false) const [error, setError] = useState(null) const passwordProblem = password !== '' && (password.length < PASSWORD_MIN || password.length > PASSWORD_MAX) ? `${PASSWORD_MIN} to ${PASSWORD_MAX} characters.` : '' const againProblem = again !== '' && again !== password ? 'The passwords differ.' : '' const blocked = (apple && (password === '' || passwordProblem !== '' || again !== password)) || (props.own && (currentPassword === '' || (props.otp && !CODE_PATTERN.test(currentCode)))) async function submit(event: FormEvent) { event.preventDefault() if (blocked) { return } setSaving(true) setError(null) try { const proof = props.own ? { current_password: currentPassword, ...(props.otp ? { current_code: currentCode } : {}) } : {} const { profile } = await request<{ profile: string }>('POST', props.path, PROFILE_SHAPE, { ...proof, ...(apple ? { password } : {}) }) saveFile(`${props.name}.${kind.extension}`, profile, kind.type) props.onClose() } catch (caught) { setError((caught as Error).message) setSaving(false) } } return ( reason !== 'backdropClick' && props.onClose()} maxWidth="xs" fullWidth slotProps={{ paper: { component: 'form', onSubmit: submit } }}> {`${kind.title} of ${props.name}`} {error && {error}} {props.own && ( <> setCurrentPassword(event.target.value)} /> {props.otp && ( setCurrentCode(event.target.value.trim())} /> )} )} {apple && ( <> setPassword(event.target.value)} /> setAgain(event.target.value)} /> )} ) }