|
| 1 | +/** |
| 2 | + * Ticket Controller |
| 3 | + * |
| 4 | + * Creates one-time-use tickets for WebSocket authentication. |
| 5 | + * Tickets are stored in the appropriate DO and consumed on connection. |
| 6 | + */ |
| 7 | + |
| 8 | +import { BaseController } from '../baseController'; |
| 9 | +import { RouteContext } from '../../types/route-context'; |
| 10 | +import { createLogger } from '../../../logger'; |
| 11 | +import { checkAppOwnership } from '../../../middleware/auth/routeAuth'; |
| 12 | +import { generateTicketToken, getResourceStub } from '../../../middleware/auth/ticketAuth'; |
| 13 | +import type { TicketResourceType } from '../../../middleware/auth/routeAuth'; |
| 14 | +import type { PendingWsTicket, AuthUser } from '../../../types/auth-types'; |
| 15 | + |
| 16 | +const TICKET_TTL_MS = 15_000; |
| 17 | + |
| 18 | +interface CreateTicketRequest { |
| 19 | + resourceType: TicketResourceType; |
| 20 | + resourceId?: string; |
| 21 | +} |
| 22 | + |
| 23 | +// ============================================================================ |
| 24 | +// Helpers |
| 25 | +// ============================================================================ |
| 26 | + |
| 27 | +async function verifyOwnership( |
| 28 | + user: AuthUser, |
| 29 | + resourceType: TicketResourceType, |
| 30 | + resourceId: string, |
| 31 | + env: Env |
| 32 | +): Promise<boolean> { |
| 33 | + switch (resourceType) { |
| 34 | + case 'agent': |
| 35 | + return checkAppOwnership(user, { agentId: resourceId }, env); |
| 36 | + case 'vault': |
| 37 | + return resourceId === user.id; |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +function resolveResourceId( |
| 42 | + body: CreateTicketRequest, |
| 43 | + userId: string |
| 44 | +): { resourceId: string } | { error: string } { |
| 45 | + if (body.resourceType === 'vault') { |
| 46 | + return { resourceId: userId }; |
| 47 | + } |
| 48 | + if (!body.resourceId) { |
| 49 | + return { error: 'resourceId is required for agent tickets' }; |
| 50 | + } |
| 51 | + return { resourceId: body.resourceId }; |
| 52 | +} |
| 53 | + |
| 54 | +// ============================================================================ |
| 55 | +// Controller |
| 56 | +// ============================================================================ |
| 57 | + |
| 58 | +export class TicketController extends BaseController { |
| 59 | + static readonly logger = createLogger('TicketController'); |
| 60 | + |
| 61 | + /** |
| 62 | + * Create a WebSocket ticket |
| 63 | + * POST /api/ws-ticket |
| 64 | + */ |
| 65 | + static async createTicket( |
| 66 | + request: Request, |
| 67 | + env: Env, |
| 68 | + _ctx: ExecutionContext, |
| 69 | + context: RouteContext |
| 70 | + ): Promise<Response> { |
| 71 | + const user = context.user; |
| 72 | + if (!user) { |
| 73 | + return this.createErrorResponse('Authentication required', 401); |
| 74 | + } |
| 75 | + |
| 76 | + // Parse and validate request |
| 77 | + let body: CreateTicketRequest; |
| 78 | + try { |
| 79 | + body = await request.json() as CreateTicketRequest; |
| 80 | + } catch { |
| 81 | + return this.createErrorResponse('Invalid JSON body', 400); |
| 82 | + } |
| 83 | + |
| 84 | + if (!body.resourceType || !['agent', 'vault'].includes(body.resourceType)) { |
| 85 | + return this.createErrorResponse('Invalid resourceType', 400); |
| 86 | + } |
| 87 | + |
| 88 | + // Resolve resource ID |
| 89 | + const resolved = resolveResourceId(body, user.id); |
| 90 | + if ('error' in resolved) { |
| 91 | + return this.createErrorResponse(resolved.error, 400); |
| 92 | + } |
| 93 | + const { resourceId } = resolved; |
| 94 | + |
| 95 | + // Verify ownership |
| 96 | + if (!await verifyOwnership(user, body.resourceType, resourceId, env)) { |
| 97 | + this.logger.warn('Ticket creation denied', { userId: user.id, resourceType: body.resourceType, resourceId }); |
| 98 | + return this.createErrorResponse('Access denied', 403); |
| 99 | + } |
| 100 | + |
| 101 | + // Create and store ticket |
| 102 | + const now = Date.now(); |
| 103 | + const ticket: PendingWsTicket = { |
| 104 | + token: generateTicketToken(body.resourceType, resourceId), |
| 105 | + user, |
| 106 | + sessionId: context.sessionId ?? `ticket:${body.resourceType}:${resourceId}`, |
| 107 | + createdAt: now, |
| 108 | + expiresAt: now + TICKET_TTL_MS, |
| 109 | + }; |
| 110 | + |
| 111 | + try { |
| 112 | + const stub = await getResourceStub(env, body.resourceType, resourceId); |
| 113 | + await stub.storeWsTicket(ticket); |
| 114 | + |
| 115 | + this.logger.info('Ticket created', { resourceType: body.resourceType, resourceId, userId: user.id }); |
| 116 | + |
| 117 | + return this.createSuccessResponse({ |
| 118 | + ticket: ticket.token, |
| 119 | + expiresIn: Math.floor(TICKET_TTL_MS / 1000), |
| 120 | + expiresAt: new Date(ticket.expiresAt).toISOString(), |
| 121 | + }); |
| 122 | + } catch (error) { |
| 123 | + this.logger.error('Failed to create ticket', { resourceType: body.resourceType, resourceId, error }); |
| 124 | + return this.createErrorResponse('Failed to create ticket', 500); |
| 125 | + } |
| 126 | + } |
| 127 | +} |
0 commit comments