Something went wrong. Try again.
fossabot clone for streamplace superbot.mmatt.net
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182import { Hono } from 'hono'import { getProfile, isDid } from '../atproto/identity.ts'import { SIGN_IN_SCOPE, STREAMER_SCOPE } from '../atproto/oauth.ts'import { env } from '../env.ts'import { createSession, destroySession, type AppEnv, type Deps } from './context.ts'
const safeNext = (next: string | undefined) => (next && next.startsWith('/') && !next.startsWith('//') ? next : '')
export function authRoutes(deps: Deps) { const app = new Hono<AppEnv>()
/** * Start an OAuth sign-in. Streamers grant the moderation scopes; people * signing in only to help moderate someone else's channel can skip them. */ app.get('/auth/login', async (c) => { const input = (c.req.query('handle') ?? '').trim().replace(/^@/, '') const next = safeNext(c.req.query('next')) if (!input) return c.redirect('/login?error=missing') let scope = c.req.query('as') === 'helper' ? SIGN_IN_SCOPE : STREAMER_SCOPE // Never downgrade a streamer's stored session: the bot needs its scopes to moderate. const did = isDid(input) ? input : (await getProfile(input).catch(() => null))?.did if (did && deps.store.getChannel(did)) scope = STREAMER_SCOPE try { const url = await deps.oauth.authorize(input, { scope, state: JSON.stringify({ next, scope }) }) return c.redirect(url.toString()) } catch (err) { console.warn('[auth] authorize failed:', err) return c.redirect(`/login?error=${encodeURIComponent('We couldn’t find that account. Check the handle and try again.')}`) } })
app.get('/oauth/callback', async (c) => { const params = new URLSearchParams(c.req.url.split('?')[1] ?? '') try { const { session, state } = await deps.oauth.callback(params) const { next, scope } = JSON.parse(state ?? '{}') as { next?: string; scope?: string } const profile = await getProfile(session.did) if (!profile) throw new Error('Profile not found') await createSession(c, deps, session.did) deps.streamer.forget(session.did) // Signing in with the streamer scope sets up (or refreshes) the person's own channel. if (scope === STREAMER_SCOPE) { const existed = !!deps.store.getChannel(session.did) deps.store.upsertChannel({ did: profile.did, handle: profile.handle, displayName: profile.displayName, avatar: profile.avatar }) if (!existed) deps.store.audit(session.did, { did: profile.did, handle: profile.handle }, 'bot', `Joined Superbot to stream.place/${profile.handle}`) void deps.records.reconnected(session.did).catch((err) => console.warn('[records] catch-up failed:', err)) return c.redirect(safeNext(next) || `/c/${profile.handle}/dashboard`) } void deps.records.reconnected(session.did).catch((err) => console.warn('[records] catch-up failed:', err)) return c.redirect(safeNext(next) || '/channels') } catch (err) { console.warn('[auth] callback failed:', err) return c.redirect(`/login?error=${encodeURIComponent('Sign-in didn’t complete. Please try again.')}`) } })
app.post('/auth/logout', async (c) => { await destroySession(c, deps) return c.json({ ok: true }) })
app.get('/oauth-client-metadata.json', (c) => c.json(deps.oauth.clientMetadata)) app.get('/oauth/jwks.json', (c) => c.json(deps.oauth.jwks))
app.get('/auth/config', (c) => c.json({ loopback: env.isLoopback }))
// Development only: sign in as a local test channel without OAuth, to work on the dashboard. if (!env.production && env.isLoopback && process.env.DEV_LOGIN === '1') { app.get('/auth/dev-login', async (c) => { const handle = (c.req.query('handle') ?? 'demo.test').replace(/[^a-z0-9.-]/gi, '') const did = `did:plc:dev${handle.replace(/[^a-z0-9]/gi, '').slice(0, 20)}` deps.store.upsertChannel({ did, handle, displayName: handle.split('.')[0], avatar: '' }) await createSession(c, deps, did) return c.redirect(`/c/${handle}/dashboard`) }) console.warn('[auth] DEV_LOGIN is on: /auth/dev-login signs anyone in as a test channel.') }
return app}