git clone https://garage44.eu/jeroen/midas.git.. /web/src/components/accounts.tsx
import m, {MithrilComponent} from '@garage44/mithril'
import {Button, Card, FieldSelect, FieldText} from '@garage44/mithril/components'
import {askConfirm} from '@/components/confirm'
import {act} from '@/lib/actions'
import {api} from '@/lib/api'
import type {Account} from '@/lib/api'
import {age} from '@/lib/format'
import {bind, local} from '@/lib/reactive'
import {$s} from '@/lib/store'
const ROLES = [
{value: 'viewer', label: 'viewer: sees the page'},
{value: 'operator', label: 'operator: drains, releases, deploys, decides'},
{value: 'steer', label: 'steer: also types into agent sessions'},
{value: 'admin', label: 'admin: also manages accounts and settings'},
]
/** The page a setup token opens, as `midas account add` prints it. */
const setupLink = (token: string) => `${location.origin}/setup/${token}`
/** How an account stands, in a few words. */
function state(a: Account): string {
if (a.locked) return 'locked after failed logins'
if (!a.ready) return a.setup_pending ? 'setup link sent, not set up yet' : 'not set up'
const seen = a.last_seen ? `seen ${age(a.last_seen)} ago` : 'not logged in'
return `${seen} · ${a.recovery_left} recovery codes left`
}
/**
* The people who log in to this page (accounts.rs), managed as `midas account`
* does: add one, change a role, start one over, remove one. A new or reset
* account gets a setup link, shown here once, to send its holder; they choose
* the password and the authenticator themselves. Your own account is not
* changed from here: that is how the last admin locks everyone out.
*/
export class Accounts extends MithrilComponent {
s = local({
list: null as Account[] | null,
name: '',
role: 'operator',
/** The setup link just made, and whose: shown until dismissed. */
link: null as {name: string; url: string} | null,
error: '',
})
oninit() {
void this.load()
}
async load() {
try {
this.s.list = (await api.accounts()).sort((a, b) => a.name.localeCompare(b.name))
} catch (e) {
this.s.error = (e as Error).message
}
}
/** A role chosen is a role set: no Save for one field. */
roleOf(a: Account) {
const reload = () => this.load()
return {
get value() {
return a.role
},
set value(role: string) {
if (role !== a.role) void act(`${a.name} is ${role}`, async () => {
await api.setAccountRole(a.name, role)
await reload()
})
},
}
}
account(a: Account) {
const mine = a.name === $s.account
return (
<li key={a.name} class="accounts__row">
<div class="accounts__who">
<strong>{a.name}</strong>
{mine ? <span class="hint"> (you)</span> : null}
<div class={a.locked ? 'hint error' : 'hint'}>{state(a)}</div>
</div>
<FieldSelect
label="role"
disabled={mine}
model={this.roleOf(a)}
options={ROLES.map((r) => ({value: r.value, label: r.value}))}
/>
<div class="accounts__actions">
<Button
size="sm"
variant="ghost"
disabled={mine}
onclick={async () => {
const yes = await askConfirm({
title: `Start ${a.name} over?`,
text: 'Their password, authenticator secret, recovery codes and logins are gone, and they get a new setup link. For a lost phone or a leaked password.',
yes: 'Start over',
danger: true,
})
if (yes) await act(`${a.name} starts over`, async () => {
this.s.link = {name: a.name, url: setupLink((await api.resetAccount(a.name)).token)}
await this.load()
})
}}
>
{a.ready ? 'Reset' : 'New link'}
</Button>
<Button
size="sm"
variant="ghost"
disabled={mine}
onclick={async () => {
const yes = await askConfirm({
title: `Remove ${a.name}?`,
text: 'The account is gone and its logins end now.',
yes: 'Remove',
danger: true,
})
if (yes) await act(`${a.name} removed`, async () => {
await api.removeAccount(a.name)
await this.load()
})
}}
>
Remove
</Button>
</div>
</li>
)
}
view() {
const {list, link} = this.s
if (!list) return this.s.error ? <p class="hint">{this.s.error}</p> : null
const name = this.s.name.trim()
const valid = /^[a-z0-9_-]{1,40}$/.test(name) && !list.some((a) => a.name === name)
return [
<Card title="Accounts" subtitle={`${list.length} ${list.length === 1 ? 'account' : 'accounts'} that log in to this page`}>
{link ? (
<div class="accounts__link">
<p class="hint">
{link.name}'s setup link, good for a day. Send it to them: they choose a password, add the code to an authenticator app and
get their recovery codes. It is not shown again.
</p>
<code>{link.url}</code>
<div class="accounts__actions">
<Button size="sm" variant="ghost" onclick={() => act('link copied', () => navigator.clipboard.writeText(link.url))}>
Copy
</Button>
<Button size="sm" variant="ghost" onclick={() => (this.s.link = null)}>
Done
</Button>
</div>
</div>
) : null}
<ul class="accounts">{list.map((a) => this.account(a))}</ul>
</Card>,
<Card
title="Add a person"
subtitle="an account, and a setup link for them"
footer={[
<Button
variant="primary"
disabled={!valid}
onclick={() =>
act(`${name} added`, async () => {
this.s.link = {name, url: setupLink((await api.addAccount(name, this.s.role)).token)}
this.s.name = ''
await this.load()
})
}
>
Add
</Button>,
]}
>
<FieldText model={bind(this.s, 'name')} label="Name" help="lowercase letters, digits, - and _" placeholder="jo" autocomplete="off" />
<FieldSelect label="Role" help="each role includes the ones before it" model={bind(this.s, 'role')} options={ROLES} />
</Card>,
]
}
}