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
Confirm it's you before a new token is made.
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.loaded ? 'No tokens yet.' : 'Loading…'}
) : (Revoking a token disconnects anything signed in with it within seconds.