Something went wrong. Try again.
fossabot clone for streamplace superbot.mmatt.net
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091import { randomBytes } from 'node:crypto'import type { Context } from 'hono'import { deleteCookie, getSignedCookie, setSignedCookie } from 'hono/cookie'import type { NodeOAuthClient } from '@atproto/oauth-client-node'import { DASHBOARD_PERMISSION_IDS } from '../../shared/permissions.ts'import type { BotAccount } from '../atproto/bot.ts'import { getProfile } from '../atproto/identity.ts'import type { StreamerActions } from '../atproto/streamer.ts'import type { RecordSync } from '../records/sync.ts'import type { BotManager } from '../bot/manager.ts'import type { ChannelRow, Store } from '../db/store.ts'import { env } from '../env.ts'
export interface Deps { store: Store manager: BotManager oauth: NodeOAuthClient streamer: StreamerActions records: RecordSync bot: BotAccount}
export interface SessionUser { did: string handle: string displayName: string avatar: string}
export type AppEnv = { Variables: { user: SessionUser | null } }
const COOKIE = 'sb_session'const SESSION_DAYS = 30
export async function createSession(c: Context, deps: Deps, did: string) { const id = randomBytes(24).toString('base64url') const now = Date.now() deps.store.db.run('INSERT INTO sessions (id, did, created_at, expires_at) VALUES (?, ?, ?, ?)', id, did, now, now + SESSION_DAYS * 86400_000) await setSignedCookie(c, COOKIE, id, env.sessionSecret, { httpOnly: true, sameSite: 'Lax', secure: env.publicUrl.startsWith('https://'), path: '/', maxAge: SESSION_DAYS * 86400, })}
export async function readSession(c: Context, deps: Deps): Promise<SessionUser | null> { const id = await getSignedCookie(c, env.sessionSecret, COOKIE) if (!id) return null const row = deps.store.db.get<{ did: string; expires_at: number }>('SELECT did, expires_at FROM sessions WHERE id = ?', id) if (!row || row.expires_at < Date.now()) return null const p = await getProfile(row.did).catch(() => null) const ch = p ? undefined : deps.store.getChannel(row.did) return { did: row.did, handle: p?.handle ?? ch?.handle ?? row.did, displayName: p?.displayName ?? ch?.display_name ?? '', avatar: p?.avatar ?? ch?.avatar ?? '' }}
export async function destroySession(c: Context, deps: Deps) { const id = await getSignedCookie(c, env.sessionSecret, COOKIE) if (id) deps.store.db.run('DELETE FROM sessions WHERE id = ?', id) deleteCookie(c, COOKIE, { path: '/' })}
export interface Access { channel: ChannelRow owner: boolean permissions: Set<string>}
export function resolveChannel(deps: Deps, ref: string): ChannelRow | undefined { const decoded = decodeURIComponent(ref) if (decoded.startsWith('did:') || decoded.startsWith('preset:')) return deps.store.getChannel(decoded) return deps.store.getChannelByHandle(decoded)}
/** What a signed-in user may do on a channel's dashboard, or null for no access. */export async function accessFor(deps: Deps, user: SessionUser, channel: ChannelRow): Promise<Access | null> { if (channel.owner_did === user.did) return { channel, owner: true, permissions: new Set(['*']) } if (channel.kind === 'preset') return null const rt = deps.manager.runtimeFor(channel.id) if (!rt) return null const roles = await rt.rolesFor({ did: user.did, handle: user.handle, displayName: user.displayName, badges: [] }) if (roles.administrator) return { channel, owner: false, permissions: new Set(['*']) } if (!DASHBOARD_PERMISSION_IDS.some((p) => roles.permissions.has(p))) return null return { channel, owner: false, permissions: roles.permissions }}
export function can(access: Access, permission: string): boolean { return access.permissions.has('*') || access.permissions.has(permission)}