import m, {MithrilComponent} from '@garage44/mithril' import {Alert, Badge, Button, Card, FieldCheckbox, FieldSelect, FieldText} from '@garage44/mithril/components' import {required, validation, validations} from '@garage44/mithril/validation' import type {AccessToken, AccountSignin} from '@/types' import {$s} from '@/app' import {ProviderLogo} from '@/components/elements/providers/providers' import {PlayerSignIn} from '@/components/elements/signin/signin' import {loadAccountSignin} from '@/lib/auth' import {bind, local} from '@/lib/reactive' import {confirmLabel, reauthValid, reauthWith} from '@/lib/signin' import {playerSignOut} from '@/lib/signup' import {createToken, EXPIRY_CHOICES, expiryAfter, formatUnix, listTokens, revokeToken} from '@/lib/tokens' import {confirmWith, Ways} from './ways' /** * Access tokens: what a bot or a script signs in with instead of a password * (`docs/notes/access-tokens.md`). * * The flow it exists for is a handful of steps a person does by hand — sign in as * the bot's account here, make a token, paste it on the machine the bot runs on * (`midas bot setup`, or `CARTO_TOKEN`) — and revoking is the one click that * stops it, including a bot that is already in the world. So the page is three * things and no more: the list, the form, and the secret shown once. */ export class Tokens extends MithrilComponent { s = local({ busy: false, elevated: false, expiry: 'never', label: '', loaded: false, password: '', /** The secret of the token just made; shown until dismissed, never again. */ shown: '', /** The account's ways in; `null` until asked (and on a server too old to say). */ signin: null as AccountSignin | null, tokens: [] as AccessToken[], }) v = validations({ label: validation(bind(this.s, 'label'), [required]), // Required only of an account that has a password; one without confirms // through its provider instead. password: validation(bind(this.s, 'password')).rule((value) => this.needsPassword() && !value ? 'Enter your password.' : null, ), }) load = async () => { const [tokens, signin] = await Promise.all([listTokens(), loadAccountSignin()]) if (tokens) { this.s.tokens = tokens this.s.loaded = true } if (signin) this.s.signin = signin } loadSignin = async () => { const signin = await loadAccountSignin() if (signin) this.s.signin = signin } /** Whether the form asks for the password: unless the server said there is none. */ needsPassword(): boolean { return this.s.signin?.has_password ?? true } oninit() { if ($s.signup.token) { void this.load() } } create = async (e: Event) => { e.preventDefault() const s = this.s if (s.busy || !this.v.$validate()) { return } s.busy = true const made = await createToken({ elevated: s.elevated, expires_at: expiryAfter(s.expiry), label: s.label.trim(), password: this.needsPassword() ? s.password : undefined, }) s.busy = false // The password goes whatever happened: it was asked for one action. s.password = '' this.v.password.reset() if (made === 'reauth') { // The confirmation lapsed while the form was open: ask again. await this.loadSignin() return } if (made) { s.shown = made.token s.label = '' s.elevated = false s.expiry = 'never' this.v.label.reset() await this.load() } } revoke = async (token: AccessToken) => { this.s.busy = true if (await revokeToken(token.id)) { await this.load() } this.s.busy = false } /** Whether a token may be made now without a password: a recent confirmation. */ confirmed(): boolean { return this.needsPassword() || reauthValid(this.s.signin?.reauth_until) } /** * In place of the password, for an account without one: confirm through * the provider, which comes back here with ten minutes to make the token. */ confirmView() { const signin = this.s.signin const first = signin ? reauthWith(signin.identities) : null if (signin && reauthValid(signin.reauth_until)) { const until = new Date(signin.reauth_until! * 1000).toLocaleTimeString(undefined, { hour: '2-digit', minute: '2-digit', }) return Confirmed. You can make a token until {until}. } if (!first) return null return (

Confirm it's you before a new token is made.

) } view() { const s = this.s if (!$s.signup.token) { return ( ) } // Signed in on this page after it mounted: `oninit` had no session to load with. if (!s.loaded && !s.busy) { s.busy = true void this.load().finally(() => (s.busy = false)) } return (
{s.shown && ( (s.shown = '')} size="sm" variant="primary"> I have copied it } title="Your new token" >

Copy it now — it is not stored anywhere it can be shown again. Give it to the bot as CARTO_TOKEN, or paste it into midas bot setup.

{s.shown}
)} {s.signin && } {s.tokens.length === 0 ? (

{s.loaded ? 'No tokens yet.' : 'Loading…'}

) : (
    {s.tokens.map((t) => (
  • {t.label} {t.elevated && elevated}
    made {formatUnix(t.created_at, '—')} · last used{' '} {formatUnix(t.last_used, 'never')} · expires {formatUnix(t.expires_at, 'never')}
  • ))}
)}

Without it the token acts as a player would, whatever this account's roles say — what a bot needs, and all a stolen token could do.

{this.needsPassword() ? ( ) : ( this.confirmView() )}
void playerSignOut()} size="sm" variant="ghost"> Sign out } >

Revoking a token disconnects anything signed in with it within seconds.

) } }