garage44.eu/jeroen/midas

logclone: git clone https://garage44.eu/jeroen/midas.git

.. /web/src/components/admin.tsx

raw

import m, {MithrilComponent} from '@garage44/mithril'
import {Badge, Button, Card, FieldText} from '@garage44/mithril/components'

import {who} from '@/components/who'
import {act} from '@/lib/actions'
import {api} from '@/lib/api'
import {age, when} from '@/lib/format'
import {makeKey, passkeyHere, signGrant} from '@/lib/passkey'
import {bind, local} from '@/lib/reactive'
import {$s} from '@/lib/store'
import type {AdminRequest} from '@/lib/types'

/**
 * Admin windows (src/admin.rs): a session asks for root on the hub's machine,
 * and the person's passkey signs the grant — who, for how long, why — which
 * root checks before sudo works. The hub only carries the signature.
 */

const rpId = () => $s.admin?.setup?.rp_id ?? location.hostname

async function allow(r: AdminRequest) {
    // Hub's time in the grant: root checks it against its own clock.
    const s = await api.adminToSign(r.id)
    const assertion = await signGrant(s.challenge, s.rp_id, s.keys)
    await api.adminGrant(r.id, s.grant.issued_at, assertion)
}

/** One request, under Needs you: what for, how long, and Allow (the passkey) or No. */
export function adminRequestRow(r: AdminRequest): m.Children {
    const blocked = $s.role !== 'admin' ? 'an admin grants root' : passkeyHere(rpId())
    return (
        <div class="decision" id={`admin-${r.id}`}>
            <div>
                <Badge tone="danger">root</Badge> <b>{r.reason}</b>
            </div>
            <div class="hint">
                {who(r.session, {max: 40})} asks for root on the hub's machine for {r.minutes} min · {age(r.at)} ago. Root then can change anything
                there; the machine closes it on time, or when the session ends.
            </div>
            <div class="decision__choices">
                <Button variant="primary" disabled={!!blocked} onclick={() => act(`root for ${r.minutes} min allowed`, () => allow(r))}>
                    Allow with passkey
                </Button>
                <Button variant="secondary" disabled={$s.role !== 'admin'} onclick={() => act(`${r.id} declined`, () => api.adminDecline(r.id))}>
                    No
                </Button>
            </div>
            {blocked ? <p class="hint">{blocked}</p> : null}
        </div>
    )
}

/** The window while it is open: whose, until when, and Close. */
export function adminOpenRow(): m.Children {
    const w = $s.admin?.window
    if (!w) return null
    return (
        <div class="needs__row needs__row--admin">
            <span>
                <Badge tone="danger">root open</Badge> {who(w.session, {max: 40})} until {when(w.until)}: {w.reason}
            </span>
            <Button size="sm" variant="secondary" disabled={$s.role !== 'admin'} onclick={() => act('closing root', () => api.adminClose())}>
                Close now
            </Button>
        </div>
    )
}

/** Settings ▸ Admin: the machine's setup, the passkeys that may grant, making one. */
export class AdminPasskey extends MithrilComponent {
    s = local({label: '', line: '', error: ''})

    async make() {
        const setup = $s.admin?.setup
        if (!setup) return
        try {
            this.s.line = await makeKey(setup.rp_id, this.s.label, setup.keys.map((k) => k.id))
            this.s.error = ''
        } catch (e) {
            this.s.error = (e as Error).message
        }
    }

    view() {
        const setup = $s.admin?.setup
        if (!setup)
            return (
                <Card title="Admin passkey" subtitle="not set up on the hub's machine">
                    <p class="hint">
                        As root there, once: <code>sudo midas hub admin-setup --user agent --rp-id {location.hostname}</code>. The agents then log in as that user, which
                        is root only in a window you allow.
                    </p>
                </Card>
            )
        const blocked = passkeyHere(setup.rp_id)
        const cmd = this.s.line ? `sudo midas hub admin-enroll '${this.s.line}'` : ''
        return (
            <Card
                title="Admin passkey"
                subtitle={`sessions log in as ${setup.user}; root for up to ${setup.max_minutes} min, only when your passkey signs it`}
                footer={[
                    <Button variant="primary" disabled={!!blocked} onclick={() => void this.make()}>
                        Make a passkey on this device
                    </Button>,
                ]}
            >
                {setup.keys.length ? (
                    <ul class="pairs">
                        {setup.keys.map((k) => (
                            <li>
                                <b>{k.label || 'passkey'}</b> <span class="hint">· added {when(k.added)} · {k.id.slice(0, 10)}…</span>
                            </li>
                        ))}
                    </ul>
                ) : (
                    <p class="hint">No passkey yet: until there is one, no session can be given root.</p>
                )}
                <FieldText model={bind(this.s, 'label')} placeholder="what this device is (phone, laptop)" />
                {blocked ? <p class="hint">{blocked}</p> : null}
                {this.s.error ? <p class="hint error">{this.s.error}</p> : null}
                {cmd ? (
                    <div>
                        <p class="hint">
                            Made. The machine trusts it only once you install it yourself, as root, from your own login (not a session's): run this there.
                        </p>
                        <pre class="admin__cmd">{cmd}</pre>
                        <Button size="sm" variant="ghost" onclick={() => void navigator.clipboard.writeText(cmd)}>
                            Copy
                        </Button>
                    </div>
                ) : null}
            </Card>
        )
    }
}