garage44.eu/jeroen/midas

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

.. /web/src/components/login.tsx

raw

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

import {api} from '@/lib/api'
import type {SetupInfo} from '@/lib/api'
import {bind, local} from '@/lib/reactive'
import {checkCaller, poll} from '@/lib/store'
import {reconnect} from '@/lib/stream'

/** Name, password and a code from the authenticator app (or a recovery code). */
export class Login extends MithrilComponent {
    s = local({name: '', password: '', code: '', error: '', busy: false})

    async submit(e: Event) {
        e.preventDefault()
        this.s.busy = true
        this.s.error = ''
        try {
            await api.login(this.s.name.trim(), this.s.password, this.s.code.trim())
            this.s.password = ''
            this.s.code = ''
            await checkCaller()
            await poll()
            reconnect()
        } catch (err) {
            this.s.error = (err as Error).message
        } finally {
            this.s.busy = false
        }
    }

    view() {
        return (
            <Card title="Log in" subtitle="your account, its password, and a code from your authenticator app">
                <form class="login" onsubmit={(e: Event) => void this.submit(e)}>
                    <FieldText model={bind(this.s, 'name')} label="name" autocomplete="username" />
                    <FieldText model={bind(this.s, 'password')} type="password" label="password" autocomplete="current-password" />
                    <FieldText model={bind(this.s, 'code')} label="code" placeholder="123456, or a recovery code" autocomplete="one-time-code" />
                    {this.s.error ? <Alert tone="danger">{this.s.error}</Alert> : null}
                    <Button variant="primary" type="submit" disabled={this.s.busy || !this.s.name || !this.s.password || !this.s.code}>
                        Log in
                    </Button>
                    <p class="hint">No account? An admin makes one with <code>midas account add &lt;name&gt;</code> and sends you its setup link.</p>
                </form>
            </Card>
        )
    }
}

/**
 * The setup link's page (`/setup/<token>`, from `midas account add` or
 * `reset`): choose a password, add the secret to an authenticator app, prove
 * it with a first code, and keep the recovery codes, shown this once.
 */
export class Setup extends MithrilComponent<{token: string}> {
    s = local({
        info: null as SetupInfo | null,
        password: '',
        again: '',
        code: '',
        error: '',
        busy: false,
        recovery: [] as string[],
    })

    oninit(vnode: m.Vnode<{token: string}>) {
        api.setupInfo(vnode.attrs.token)
            .then((info) => (this.s.info = info))
            .catch((e: Error) => (this.s.error = e.message))
    }

    async submit(e: Event, token: string) {
        e.preventDefault()
        if (this.s.password !== this.s.again) {
            this.s.error = 'the two passwords differ'
            return
        }
        this.s.busy = true
        this.s.error = ''
        try {
            this.s.recovery = (await api.setupComplete(token, this.s.password, this.s.code.trim())).recovery
            this.s.password = this.s.again = ''
        } catch (err) {
            this.s.error = (err as Error).message
        } finally {
            this.s.busy = false
        }
    }

    view(vnode: m.Vnode<{token: string}>) {
        const info = this.s.info
        if (this.s.recovery.length) {
            return (
                <Card title="Your recovery codes" subtitle="shown this once: each logs you in once, with your password, if your phone is lost">
                    <ul class="recovery">
                        {this.s.recovery.map((c) => (
                            <li>
                                <code>{c}</code>
                            </li>
                        ))}
                    </ul>
                    <Button
                        variant="primary"
                        onclick={() => {
                            // Off the setup link, and loaded again: the login.
                            location.replace('/')
                        }}
                    >
                        I have kept them — log in
                    </Button>
                </Card>
            )
        }
        if (!info) return this.s.error ? <Alert tone="danger">{this.s.error}</Alert> : <p class="hint">Opening the setup link…</p>
        return (
            <Card title={`Set up ${info.name}`} subtitle={`role: ${info.role}`}>
                <form class="login" onsubmit={(e: Event) => void this.submit(e, vnode.attrs.token)}>
                    <p>1. Choose a password, at least twelve characters.</p>
                    <FieldText model={bind(this.s, 'password')} type="password" label="password" autocomplete="new-password" />
                    <FieldText model={bind(this.s, 'again')} type="password" label="again" autocomplete="new-password" />
                    <p>2. Add this account to an authenticator app: scan the code, or type the secret.</p>
                    <div class="qr">{m.trust(info.qr_svg)}</div>
                    <p class="hint">
                        secret <code>{info.secret}</code>
                    </p>
                    <p>3. Type the code the app shows now.</p>
                    <FieldText model={bind(this.s, 'code')} label="code" autocomplete="one-time-code" />
                    {this.s.error ? <Alert tone="danger">{this.s.error}</Alert> : null}
                    <Button variant="primary" type="submit" disabled={this.s.busy || !this.s.password || !this.s.code}>
                        Finish
                    </Button>
                </form>
            </Card>
        )
    }
}