import Apple from '@mui/icons-material/Apple' import Delete from '@mui/icons-material/Delete' import Download from '@mui/icons-material/Download' import Key from '@mui/icons-material/Key' import Password from '@mui/icons-material/Password' import { Alert, Box, Button, CircularProgress, Dialog, DialogActions, DialogContent, DialogTitle, IconButton, Stack, Table, TableBody, TableCell, TableHead, TableRow, TextField, Tooltip, Typography, } from '@mui/material' import { useEffect, useState, type FormEvent } from 'react' import OtpDialog from './OtpDialog.tsx' import PasskeyDialog from './PasskeyDialog.tsx' import ProfileDialog from './ProfileDialog.tsx' import { Board, BoardButtons, type BoardPanel } from './PanelBoard.tsx' import { useBoard } from './useBoard.ts' import Toolbar from './Toolbar.tsx' import { ACCOUNT_SHAPE, CODE_PATTERN, USER_SHAPE, request, type Account, type User } from './api.ts' /** The localStorage key of the page's layout. */ const LAYOUT_KEY = 'linux-manager-ui.account-layout' /** * The account of the signed-in user, from the server's /account, in two columns: the details with Change password, * which opens the password dialog, and the passkeys in the first row, the enrolment for one-time passwords and the * OpenVPN and the Apple VPN profile of a user with a certificate in the second. Each change but the removal of a * passkey takes the current password and, for an enrolled user, a current code. A changed password ends the session, * through onPasswordChanged. The panels are on a PanelBoard whose layout is kept in the browser. */ export default function AccountPanel(props: { onPasswordChanged: () => void }) { const [account, setAccount] = useState(null) const [error, setError] = useState(null) const [enrolling, setEnrolling] = useState(false) const [currentPassword, setCurrentPassword] = useState('') const [currentCode, setCurrentCode] = useState('') const [password, setPassword] = useState('') const [again, setAgain] = useState('') const [saving, setSaving] = useState(false) const [passwordError, setPasswordError] = useState(null) const [changingPassword, setChangingPassword] = useState(false) const [addingPasskey, setAddingPasskey] = useState(false) const [profile, setProfile] = useState<'openvpn' | 'apple' | null>(null) const [passkeyError, setPasskeyError] = useState(null) const board = useBoard(LAYOUT_KEY) useEffect(() => { request('GET', '/account', ACCOUNT_SHAPE) .then(setAccount) .catch((caught: Error) => setError(caught.message)) }, []) if (account === null) { return error ? {error} : } const againProblem = again !== '' && again !== password ? 'The passwords differ.' : '' const blocked = currentPassword === '' || (account.otp && !CODE_PATTERN.test(currentCode)) || password === '' || again !== password async function changePassword(event: FormEvent) { event.preventDefault() if (account === null || blocked) { return } setSaving(true) setPasswordError(null) try { const proof = account.otp ? { current_password: currentPassword, current_code: currentCode } : { current_password: currentPassword } await request('PUT', '/account/password', USER_SHAPE, { ...proof, password }) props.onPasswordChanged() } catch (caught) { setPasswordError((caught as Error).message) setSaving(false) } } /** Closes the password dialog, unless a change is being saved, and empties its fields. */ function closePasswordDialog() { if (saving) { return } setChangingPassword(false) setCurrentPassword('') setCurrentCode('') setPassword('') setAgain('') setPasswordError(null) } async function removePasskey(id: string) { if (account === null) { return } setPasskeyError(null) try { setAccount({ ...(await request('DELETE', `/account/passkeys/${encodeURIComponent(id)}`, USER_SHAPE)), admin: account.admin }) } catch (caught) { setPasskeyError((caught as Error).message) } } const rows: [string, string][] = [ ['User name', account.name], ['Kind', account.manager_only ? 'Manager-only account' : 'VPN user'], ...(account.manager_only ? [] : [['First name', account.first_name ?? '-'], ['Last name', account.last_name ?? '-']] as [string, string][]), ['E-mail address', account.email ?? '-'], ['Router administrator', account.admin ? 'yes' : 'no'], ['One-time passwords', account.otp ? 'enrolled' : 'not enrolled'], ] const panels: BoardPanel[] = [ { id: 'account', title: 'Account', content: ( <> {rows.map(([label, value]) => ( {label} {value} ))} ), }, { id: 'passkeys', title: 'Passkeys', content: ( {passkeyError && {passkeyError}} {account.passkeys.length === 0 ? No passkeys. : ( Label Added Actions {account.passkeys.map((passkey) => ( {passkey.label} {new Date(passkey.created).toLocaleString()} void removePasskey(passkey.id)}> ))}
)}
), }, { id: 'otp', title: 'One-time passwords', content: ( {account.otp ? 'Signing in takes a code of the authenticator app. Enrolling again replaces the authenticator; an administrator removes the enrolment.' : 'Enrol an authenticator app; signing in then takes a code of it.'} ), }, ...(account.certificate ? [{ id: 'profiles', title: 'VPN profiles', content: ( ), }] : []), ] return (
Change password {passwordError && {passwordError}} Changing the password signs you out. setCurrentPassword(event.target.value)} /> {account.otp && ( setCurrentCode(event.target.value.trim())} /> )} setPassword(event.target.value)} /> setAgain(event.target.value)} />
{profile && ( setProfile(null)} /> )} {addingPasskey && ( setAddingPasskey(false)} onSaved={(user) => { setAccount({ ...user, admin: account.admin }) setAddingPasskey(false) }} /> )} {enrolling && ( setEnrolling(false)} onSave={async (secret, code, proof) => { setAccount({ ...(await request('PUT', '/account/otp', USER_SHAPE, { ...proof, secret, code })), admin: account.admin }) setEnrolling(false) }} /> )}
) }