import m, {MithrilComponent} from '@garage44/mithril' import {Alert, Badge, Button, FieldSegmented, FieldSwitch, FieldText} from '@garage44/mithril/components' import type {AdminProvider, AdminSignin, NewAccounts} from '@/types' import {ProviderLogo} from '@/components/elements/providers/providers' import {begin, deleteProvider, loadAdminSignin, orderProviders, type ProviderChange, saveProvider} from '@/lib/auth' import {ADMIN} from '@/lib/base' import {Icon} from '@/lib/icons' import {bind, local} from '@/lib/reactive' import { customId, hasKeys, mayEnable, moved, newAccountChoices, providerLine, providerStatus, setupSteps, } from '@/lib/signin' /** The open row's form, filled from the provider when it opens. */ interface Draft { client_id: string /** Empty means "keep the stored one": a secret is never sent back to be shown. */ client_secret: string issuer: string name: string new_accounts: NewAccounts } /** * Admin › People › Sign-in (`docs/mockups/external-sign-in/v3-communities.html`, * sections 2 and 3): which providers people may sign in with, in what order, * and each one's keys. * * The rule the page is built around: a provider says "working" only after a * test came back with the operator's own account, and its switch stays grey * until then. A wrong redirect address looks exactly like a right one until a * member hits the provider's error page, and the test is the only thing that * catches it first. The server enforces it too (a 409 on enabling untested). */ export class SigninAdmin extends MithrilComponent { s = local({ adding: false, busy: false, data: null as AdminSignin | null, draft: {client_id: '', client_secret: '', issuer: '', name: '', new_accounts: 'anyone'} as Draft, newIssuer: '', newName: '', open: '', }) load = async () => { const data = await loadAdminSignin() if (data) this.s.data = {...data, providers: [...data.providers].sort((a, b) => a.position - b.position)} } oninit() { void this.load() } toggleOpen(p: AdminProvider) { const s = this.s if (s.open === p.id) { s.open = '' return } s.open = p.id s.draft = { client_id: p.client_id, client_secret: '', issuer: p.issuer ?? '', name: p.name, new_accounts: p.new_accounts, } } /** Only what changed: new keys or a new issuer switch a provider off and untest it. */ change(p: AdminProvider): ProviderChange { const d = this.s.draft const out: ProviderChange = {} if (d.new_accounts !== p.new_accounts) out.new_accounts = d.new_accounts if (!p.needs_keys) return out if (d.client_id.trim() !== p.client_id) out.client_id = d.client_id.trim() if (d.client_secret) out.client_secret = d.client_secret if (p.custom && d.issuer.trim() !== (p.issuer ?? '')) out.issuer = d.issuer.trim() if (p.custom && d.name.trim() && d.name.trim() !== p.name) out.name = d.name.trim() return out } save = async (p: AdminProvider, thenTest: boolean) => { const s = this.s s.busy = true const change = this.change(p) const saved = Object.keys(change).length === 0 || (await saveProvider(p.id, change, thenTest)) if (saved && thenTest) { // A real round trip with the operator's own account; `/welcome` // says how it went and links back here. if (await begin(p.id, 'test', {return: ADMIN})) return } s.busy = false if (saved) { s.draft.client_secret = '' await this.load() } } setEnabled = async (p: AdminProvider, enabled: boolean) => { this.s.busy = true await saveProvider(p.id, {enabled}, true) await this.load() this.s.busy = false } move = async (p: AdminProvider, by: -1 | 1) => { const s = this.s if (!s.data) return const ids = moved(s.data.providers.map((x) => x.id), p.id, by) s.busy = true if (await orderProviders(ids)) await this.load() s.busy = false } remove = async (p: AdminProvider) => { const what = p.custom ? `Remove ${p.name}?` : `Clear the keys for ${p.name} and switch it off?` if (!confirm(`${what} Accounts that sign in only with it will need another way in.`)) return this.s.busy = true if (await deleteProvider(p)) { this.s.open = '' await this.load() } this.s.busy = false } addCustom = async (e: Event) => { e.preventDefault() const s = this.s const id = customId(s.newName) if (!id || !s.newIssuer.trim() || s.busy) return s.busy = true if (await saveProvider(id, {issuer: s.newIssuer.trim(), name: s.newName.trim()}, true)) { s.adding = false s.newName = '' s.newIssuer = '' await this.load() const added = s.data?.providers.find((p) => p.id === id) if (added) this.toggleOpen(added) } s.busy = false } view() { const s = this.s const data = s.data if (!data) return
Loading…
return (Nothing to set up: the server registers itself with each fediverse server the first time somebody from it signs in. Servers on the federation blocklist are refused.
)}Only accounts that already linked {p.name} may sign in with it.
)} {keysChanging && p.tested_at && (New keys switch {p.name} off until it is tested again.
)}