116 lines
4.9 KiB
TypeScript
116 lines
4.9 KiB
TypeScript
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 <name>.ovpn or <name>.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/<name>/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<string | null>(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 (
|
|
<Dialog open onClose={(_event, reason) => reason !== 'backdropClick' && props.onClose()} maxWidth="xs" fullWidth slotProps={{ paper: { component: 'form', onSubmit: submit } }}>
|
|
<DialogTitle>{`${kind.title} of ${props.name}`}</DialogTitle>
|
|
<DialogContent>
|
|
<Stack sx={{ gap: 2, pt: 1 }}>
|
|
{error && <Alert severity="error" sx={{ whiteSpace: 'pre-line' }}>{error}</Alert>}
|
|
{props.own && (
|
|
<>
|
|
<TextField label="Current password" type="password" value={currentPassword} autoFocus autoComplete="current-password" onChange={(event) => setCurrentPassword(event.target.value)} />
|
|
{props.otp && (
|
|
<TextField
|
|
label="Code"
|
|
value={currentCode}
|
|
autoComplete="one-time-code"
|
|
slotProps={{ htmlInput: { inputMode: 'numeric', maxLength: 6 } }}
|
|
onChange={(event) => setCurrentCode(event.target.value.trim())}
|
|
/>
|
|
)}
|
|
</>
|
|
)}
|
|
{apple && (
|
|
<>
|
|
<TextField
|
|
label="Profile password"
|
|
type="password"
|
|
value={password}
|
|
autoFocus={!props.own}
|
|
autoComplete="new-password"
|
|
error={passwordProblem !== ''}
|
|
helperText={passwordProblem || 'The password asked when the profile is installed.'}
|
|
onChange={(event) => setPassword(event.target.value)}
|
|
/>
|
|
<TextField
|
|
label="Profile password again"
|
|
type="password"
|
|
value={again}
|
|
autoComplete="new-password"
|
|
error={againProblem !== ''}
|
|
helperText={againProblem}
|
|
onChange={(event) => setAgain(event.target.value)}
|
|
/>
|
|
</>
|
|
)}
|
|
</Stack>
|
|
</DialogContent>
|
|
<DialogActions>
|
|
<Button onClick={props.onClose} disabled={saving}>Cancel</Button>
|
|
<Button type="submit" variant="contained" disabled={saving || blocked}>Download</Button>
|
|
</DialogActions>
|
|
</Dialog>
|
|
)
|
|
}
|