2026-03-06 22:13:19 +00:00
|
|
|
import { Hono } from 'hono'
|
|
|
|
|
import { nanoid } from 'nanoid'
|
|
|
|
|
import { createHash, randomBytes } from 'crypto'
|
|
|
|
|
import { db, schema } from '../db/index.js'
|
2026-03-07 00:14:46 +00:00
|
|
|
import { eq, sql } from 'drizzle-orm'
|
|
|
|
|
import { isAllowedWebhookUrl } from '../engine/orchestrator.js'
|
2026-03-07 08:52:24 +00:00
|
|
|
import { validateCustomization } from '../engine/customization.js'
|
2026-03-07 00:14:46 +00:00
|
|
|
import { testWebhook } from '../engine/webhook-test.js'
|
|
|
|
|
import { rateLimit } from '../middleware/rate-limit.js'
|
2026-03-06 22:13:19 +00:00
|
|
|
|
|
|
|
|
export const authRouter = new Hono()
|
|
|
|
|
|
2026-03-07 00:14:46 +00:00
|
|
|
// Login with Nostr pubkey
|
2026-03-06 22:13:19 +00:00
|
|
|
authRouter.post('/login', async (c) => {
|
|
|
|
|
const body = await c.req.json()
|
|
|
|
|
const { pubkey } = body
|
|
|
|
|
|
|
|
|
|
if (!pubkey || typeof pubkey !== 'string' || pubkey.length !== 64) {
|
|
|
|
|
return c.json({ error: 'Invalid pubkey.' }, 400)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const rows = await db.select({
|
|
|
|
|
id: schema.bots.id,
|
|
|
|
|
name: schema.bots.name,
|
|
|
|
|
avatarSeed: schema.bots.avatarSeed,
|
|
|
|
|
archetype: schema.bots.archetype,
|
|
|
|
|
profilePicUrl: schema.bots.profilePicUrl,
|
|
|
|
|
eloRating: schema.bots.eloRating,
|
|
|
|
|
wins: schema.bots.wins,
|
|
|
|
|
losses: schema.bots.losses,
|
|
|
|
|
winStreak: schema.bots.winStreak,
|
|
|
|
|
bestStreak: schema.bots.bestStreak,
|
|
|
|
|
tier: schema.bots.tier,
|
2026-03-07 00:14:46 +00:00
|
|
|
isActive: schema.bots.isActive,
|
2026-03-07 08:52:24 +00:00
|
|
|
customization: schema.bots.customization,
|
2026-03-06 22:13:19 +00:00
|
|
|
}).from(schema.bots).where(eq(schema.bots.publicKey, pubkey)).limit(1)
|
|
|
|
|
|
|
|
|
|
if (rows.length === 0) {
|
|
|
|
|
return c.json({ exists: false, pubkey })
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-07 08:52:24 +00:00
|
|
|
const bot = rows[0]
|
|
|
|
|
return c.json({
|
|
|
|
|
exists: true,
|
|
|
|
|
bot: {
|
|
|
|
|
...bot,
|
|
|
|
|
customization: bot.customization ? JSON.parse(bot.customization) : null,
|
|
|
|
|
},
|
|
|
|
|
})
|
2026-03-06 22:13:19 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// Register a new bot with Nostr pubkey
|
2026-03-07 00:14:46 +00:00
|
|
|
authRouter.post('/register', rateLimit(3600_000, 5), async (c) => {
|
2026-03-06 22:13:19 +00:00
|
|
|
const body = await c.req.json()
|
2026-03-07 08:52:24 +00:00
|
|
|
const { pubkey, name, webhookUrl, archetype, profilePicUrl, customization: rawCustomization } = body
|
2026-03-06 22:13:19 +00:00
|
|
|
|
|
|
|
|
if (!pubkey || typeof pubkey !== 'string' || pubkey.length !== 64) {
|
|
|
|
|
return c.json({ error: 'Invalid pubkey.' }, 400)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!name || typeof name !== 'string' || name.length < 2 || name.length > 32) {
|
|
|
|
|
return c.json({ error: 'Name must be 2-32 characters.' }, 400)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!/^[a-zA-Z0-9_-]+$/.test(name)) {
|
|
|
|
|
return c.json({ error: 'Name must be alphanumeric, hyphens, or underscores.' }, 400)
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-07 08:52:24 +00:00
|
|
|
// Validate customization
|
|
|
|
|
const custResult = validateCustomization(rawCustomization)
|
|
|
|
|
if (!custResult.valid) {
|
|
|
|
|
return c.json({ error: custResult.error }, 400)
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-07 00:14:46 +00:00
|
|
|
const normalizedName = name.toLowerCase()
|
|
|
|
|
|
2026-03-06 22:13:19 +00:00
|
|
|
if (!webhookUrl || typeof webhookUrl !== 'string') {
|
|
|
|
|
return c.json({ error: 'webhookUrl is required.' }, 400)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
new URL(webhookUrl)
|
|
|
|
|
} catch {
|
|
|
|
|
return c.json({ error: 'webhookUrl must be a valid URL.' }, 400)
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-07 00:14:46 +00:00
|
|
|
if (!isAllowedWebhookUrl(webhookUrl)) {
|
|
|
|
|
return c.json({ error: 'webhookUrl must not point to private/internal addresses.' }, 400)
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-06 22:13:19 +00:00
|
|
|
// Check pubkey not already used
|
|
|
|
|
const existingPk = await db.select({ id: schema.bots.id })
|
|
|
|
|
.from(schema.bots)
|
|
|
|
|
.where(eq(schema.bots.publicKey, pubkey))
|
|
|
|
|
.limit(1)
|
|
|
|
|
|
|
|
|
|
if (existingPk.length > 0) {
|
|
|
|
|
return c.json({ error: 'This Nostr key already has a bot.' }, 409)
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-07 00:14:46 +00:00
|
|
|
// Check name not taken (case-insensitive)
|
2026-03-06 22:13:19 +00:00
|
|
|
const existingName = await db.select({ id: schema.bots.id })
|
|
|
|
|
.from(schema.bots)
|
2026-03-07 00:14:46 +00:00
|
|
|
.where(eq(sql`LOWER(${schema.bots.name})`, normalizedName))
|
2026-03-06 22:13:19 +00:00
|
|
|
.limit(1)
|
|
|
|
|
|
|
|
|
|
if (existingName.length > 0) {
|
|
|
|
|
return c.json({ error: 'A bot with that name already exists.' }, 409)
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-07 00:14:46 +00:00
|
|
|
// Test the webhook
|
|
|
|
|
const testResult = await testWebhook(webhookUrl)
|
|
|
|
|
if (!testResult.reachable || !testResult.validResponse) {
|
|
|
|
|
return c.json({
|
|
|
|
|
error: 'Webhook verification failed.',
|
|
|
|
|
details: testResult.error || 'Webhook must return {"answer": "..."} as JSON.',
|
|
|
|
|
latencyMs: testResult.latencyMs,
|
|
|
|
|
}, 422)
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-06 22:13:19 +00:00
|
|
|
const id = nanoid(12)
|
|
|
|
|
const secret = randomBytes(32).toString('hex')
|
|
|
|
|
|
2026-03-07 08:52:24 +00:00
|
|
|
const effectiveArchetype = custResult.data.archetype || archetype || 'standard'
|
|
|
|
|
const custJson = Object.keys(custResult.data).length > 0 ? JSON.stringify(custResult.data) : null
|
|
|
|
|
|
2026-03-06 22:13:19 +00:00
|
|
|
await db.insert(schema.bots).values({
|
|
|
|
|
id,
|
2026-03-07 00:14:46 +00:00
|
|
|
name: normalizedName,
|
|
|
|
|
webhookUrl,
|
|
|
|
|
avatarSeed: normalizedName,
|
2026-03-07 08:52:24 +00:00
|
|
|
archetype: effectiveArchetype,
|
2026-03-06 22:13:19 +00:00
|
|
|
secretHash: createHash('sha256').update(secret).digest('hex'),
|
|
|
|
|
publicKey: pubkey,
|
|
|
|
|
profilePicUrl: profilePicUrl || null,
|
2026-03-07 08:52:24 +00:00
|
|
|
customization: custJson,
|
2026-03-06 22:13:19 +00:00
|
|
|
createdAt: new Date().toISOString(),
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
return c.json({
|
|
|
|
|
id,
|
2026-03-07 00:14:46 +00:00
|
|
|
name: normalizedName,
|
2026-03-07 08:52:24 +00:00
|
|
|
archetype: effectiveArchetype,
|
|
|
|
|
customization: custResult.data,
|
2026-03-07 00:14:46 +00:00
|
|
|
webhookLatencyMs: testResult.latencyMs,
|
|
|
|
|
message: 'Bot registered. Webhook verified.',
|
2026-03-06 22:13:19 +00:00
|
|
|
}, 201)
|
|
|
|
|
})
|
|
|
|
|
|
2026-03-07 08:52:24 +00:00
|
|
|
// Update bot webhook and/or customization (requires pubkey match)
|
2026-03-06 22:13:19 +00:00
|
|
|
authRouter.post('/update', async (c) => {
|
|
|
|
|
const body = await c.req.json()
|
2026-03-07 08:52:24 +00:00
|
|
|
const { pubkey, webhookUrl, profilePicUrl, customization: rawCustomization } = body
|
2026-03-06 22:13:19 +00:00
|
|
|
|
|
|
|
|
if (!pubkey || typeof pubkey !== 'string') {
|
|
|
|
|
return c.json({ error: 'Invalid pubkey.' }, 400)
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-07 08:52:24 +00:00
|
|
|
const rows = await db.select({ id: schema.bots.id, customization: schema.bots.customization })
|
2026-03-06 22:13:19 +00:00
|
|
|
.from(schema.bots)
|
|
|
|
|
.where(eq(schema.bots.publicKey, pubkey))
|
|
|
|
|
.limit(1)
|
|
|
|
|
|
|
|
|
|
if (rows.length === 0) {
|
|
|
|
|
return c.json({ error: 'No bot found for this key.' }, 404)
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-07 00:14:46 +00:00
|
|
|
const updates: Record<string, unknown> = {}
|
|
|
|
|
|
|
|
|
|
if (webhookUrl) {
|
|
|
|
|
if (!isAllowedWebhookUrl(webhookUrl)) {
|
|
|
|
|
return c.json({ error: 'webhookUrl must not point to private/internal addresses.' }, 400)
|
|
|
|
|
}
|
|
|
|
|
const testResult = await testWebhook(webhookUrl)
|
|
|
|
|
if (!testResult.reachable || !testResult.validResponse) {
|
|
|
|
|
return c.json({
|
|
|
|
|
error: 'Webhook verification failed.',
|
|
|
|
|
details: testResult.error,
|
|
|
|
|
}, 422)
|
|
|
|
|
}
|
|
|
|
|
updates.webhookUrl = webhookUrl
|
|
|
|
|
updates.consecutiveErrors = 0
|
|
|
|
|
updates.isActive = true
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-06 22:13:19 +00:00
|
|
|
if (profilePicUrl) updates.profilePicUrl = profilePicUrl
|
|
|
|
|
|
2026-03-07 08:52:24 +00:00
|
|
|
if (rawCustomization !== undefined) {
|
|
|
|
|
const custResult = validateCustomization(rawCustomization)
|
|
|
|
|
if (!custResult.valid) {
|
|
|
|
|
return c.json({ error: custResult.error }, 400)
|
|
|
|
|
}
|
|
|
|
|
// Merge with existing customization
|
|
|
|
|
const existing = rows[0].customization ? JSON.parse(rows[0].customization) : {}
|
|
|
|
|
const merged = { ...existing, ...custResult.data }
|
|
|
|
|
updates.customization = JSON.stringify(merged)
|
|
|
|
|
// Update archetype if set in customization
|
|
|
|
|
if (custResult.data.archetype) {
|
|
|
|
|
updates.archetype = custResult.data.archetype
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-06 22:13:19 +00:00
|
|
|
if (Object.keys(updates).length > 0) {
|
|
|
|
|
await db.update(schema.bots).set(updates).where(eq(schema.bots.id, rows[0].id))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return c.json({ updated: true })
|
|
|
|
|
})
|