|
| 1 | +//! LiveKit huddle token endpoint. |
| 2 | +//! |
| 3 | +//! ## Routes |
| 4 | +//! - `POST /api/huddles/{channel_id}/token` — generate a LiveKit access token |
| 5 | +//! for the authenticated user to join the channel's huddle room. |
| 6 | +
|
| 7 | +use std::sync::Arc; |
| 8 | + |
| 9 | +use axum::{ |
| 10 | + extract::{Path, Query, State}, |
| 11 | + http::{HeaderMap, StatusCode}, |
| 12 | + response::Json, |
| 13 | +}; |
| 14 | +use uuid::Uuid; |
| 15 | + |
| 16 | +use super::{ |
| 17 | + check_channel_membership, check_token_channel_access, extract_auth_context, internal_error, |
| 18 | + scope_error, |
| 19 | +}; |
| 20 | +use crate::state::AppState; |
| 21 | + |
| 22 | +/// Query parameters for [`huddle_token`]. |
| 23 | +#[derive(serde::Deserialize)] |
| 24 | +pub struct HuddleTokenQuery { |
| 25 | + /// The parent (non-ephemeral) channel this huddle belongs to. |
| 26 | + /// |
| 27 | + /// When provided and the caller is a member of the parent channel, the relay |
| 28 | + /// will auto-add them to the private ephemeral huddle channel so they can |
| 29 | + /// obtain a token without requiring an explicit invite. |
| 30 | + pub parent_channel_id: Option<Uuid>, |
| 31 | +} |
| 32 | + |
| 33 | +/// `POST /api/huddles/{channel_id}/token` — generate a LiveKit access token. |
| 34 | +/// |
| 35 | +/// Returns `{ "token": "<jwt>", "url": "<livekit_url>", "room": "sprout-<channel_id>" }`. |
| 36 | +/// Returns 501 if LiveKit is not configured on this relay. |
| 37 | +/// Returns 403 if the caller is not a member of the channel. |
| 38 | +pub async fn huddle_token( |
| 39 | + State(state): State<Arc<AppState>>, |
| 40 | + headers: HeaderMap, |
| 41 | + Path(channel_id): Path<Uuid>, |
| 42 | + Query(query): Query<HuddleTokenQuery>, |
| 43 | +) -> Result<Json<serde_json::Value>, (StatusCode, Json<serde_json::Value>)> { |
| 44 | + let ctx = extract_auth_context(&headers, &state).await?; |
| 45 | + sprout_auth::require_scope(&ctx.scopes, sprout_auth::Scope::MessagesRead) |
| 46 | + .map_err(scope_error)?; |
| 47 | + check_token_channel_access(&ctx, &channel_id)?; |
| 48 | + |
| 49 | + // Require LiveKit to be configured. |
| 50 | + let huddle_service = state.huddle_service.as_ref().ok_or_else(|| { |
| 51 | + ( |
| 52 | + StatusCode::NOT_IMPLEMENTED, |
| 53 | + Json(serde_json::json!({ |
| 54 | + "error": "huddles_not_configured", |
| 55 | + "message": "LiveKit is not configured on this relay" |
| 56 | + })), |
| 57 | + ) |
| 58 | + })?; |
| 59 | + |
| 60 | + // Verify the caller is a member of the channel (or it's an open channel). |
| 61 | + // If they're not a member, attempt relay-side auto-add when: |
| 62 | + // 1. parent_channel_id was provided |
| 63 | + // 2. The target channel is private + ephemeral (ttl_seconds IS NOT NULL) |
| 64 | + // 3. The caller IS a member of the parent channel |
| 65 | + let membership_result = check_channel_membership(&state, channel_id, &ctx.pubkey_bytes).await; |
| 66 | + |
| 67 | + if let Err(ref membership_err) = membership_result { |
| 68 | + if let Some(parent_id) = query.parent_channel_id { |
| 69 | + // Gate 1: caller must be a member of the parent channel. |
| 70 | + check_channel_membership(&state, parent_id, &ctx.pubkey_bytes).await?; |
| 71 | + |
| 72 | + // Gate 2: target channel must be private + ephemeral. |
| 73 | + let channel = state |
| 74 | + .db |
| 75 | + .get_channel(channel_id) |
| 76 | + .await |
| 77 | + .map_err(|e| internal_error(&format!("db error: {e}")))?; |
| 78 | + |
| 79 | + if channel.visibility == "private" && channel.ttl_seconds.is_some() { |
| 80 | + // Auto-add: use the channel creator as invited_by (truthful attribution). |
| 81 | + // The creator is always an active owner, satisfying add_member's invite check. |
| 82 | + state |
| 83 | + .db |
| 84 | + .add_member( |
| 85 | + channel_id, |
| 86 | + &ctx.pubkey_bytes, |
| 87 | + sprout_db::channel::MemberRole::Member, |
| 88 | + Some(&channel.created_by), |
| 89 | + ) |
| 90 | + .await |
| 91 | + .map_err(|e| internal_error(&format!("auto-add failed: {e}")))?; |
| 92 | + |
| 93 | + tracing::info!( |
| 94 | + "Huddle auto-add: added {} to ephemeral channel {} (parent: {})", |
| 95 | + ctx.pubkey.to_hex(), |
| 96 | + channel_id, |
| 97 | + parent_id |
| 98 | + ); |
| 99 | + // Fall through to token generation. |
| 100 | + } else { |
| 101 | + // Not a private ephemeral channel — return the original 403. |
| 102 | + return Err(membership_err.clone()); |
| 103 | + } |
| 104 | + } else { |
| 105 | + // No parent_channel_id provided — return the original 403. |
| 106 | + membership_result?; |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + // Generate the LiveKit token. |
| 111 | + let room = sprout_huddle::HuddleService::create_room_name(channel_id); |
| 112 | + let identity = ctx.pubkey.to_hex(); |
| 113 | + let lk_token = huddle_service |
| 114 | + .generate_token(&room, &identity, &identity) |
| 115 | + .map_err(|e| internal_error(&format!("token generation failed: {e}")))?; |
| 116 | + |
| 117 | + let livekit_url = state.livekit_url.as_deref().unwrap_or_default(); |
| 118 | + |
| 119 | + if livekit_url.is_empty() { |
| 120 | + return Err(internal_error("livekit_url is not configured")); |
| 121 | + } |
| 122 | + |
| 123 | + Ok(Json(serde_json::json!({ |
| 124 | + "token": lk_token.token, |
| 125 | + "url": livekit_url, |
| 126 | + "room": room, |
| 127 | + }))) |
| 128 | +} |
0 commit comments