diff --git a/crates/capture/src/main.rs b/crates/capture/src/main.rs index 912e1e0be2..79cb0df922 100644 --- a/crates/capture/src/main.rs +++ b/crates/capture/src/main.rs @@ -39,8 +39,14 @@ fn main() { let user1_token = live_kit_server::token::create( &live_kit_key, &live_kit_secret, - "test-room", - "test-participant-1", + Some("test-participant-1"), + live_kit_server::token::VideoGrant { + room: Some("test-room"), + room_join: Some(true), + can_publish: Some(true), + can_subscribe: Some(true), + ..Default::default() + }, ) .unwrap(); let room1 = Room::new(); @@ -49,10 +55,17 @@ fn main() { let user2_token = live_kit_server::token::create( &live_kit_key, &live_kit_secret, - "test-room", - "test-participant-2", + Some("test-participant-2"), + live_kit_server::token::VideoGrant { + room: Some("test-room"), + room_join: Some(true), + can_publish: Some(true), + can_subscribe: Some(true), + ..Default::default() + }, ) .unwrap(); + let room2 = Room::new(); room2.connect(&live_kit_url, &user2_token).await.unwrap(); cx.add_window(Default::default(), |cx| ScreenCaptureView::new(room2, cx)); diff --git a/crates/live_kit_server/Cargo.toml b/crates/live_kit_server/Cargo.toml index 17eb83f83e..51d134ca95 100644 --- a/crates/live_kit_server/Cargo.toml +++ b/crates/live_kit_server/Cargo.toml @@ -12,7 +12,7 @@ doctest = false anyhow = "1.0.38" hmac = "0.12" jwt = "0.16" -hyper = "0.14" +hyper = { version = "0.14", features = ["client", "http1"] } prost = "0.8" prost-types = "0.8" serde = { version = "1.0", features = ["derive", "rc"] } diff --git a/crates/live_kit_server/src/api.rs b/crates/live_kit_server/src/api.rs index acdb80aaf9..cec360a30d 100644 --- a/crates/live_kit_server/src/api.rs +++ b/crates/live_kit_server/src/api.rs @@ -31,6 +31,6 @@ impl Client { .build(); // token::create(api_key, secret_key, room_name, participant_name) - self.http.request(req) + // self.http.request(req) } } diff --git a/crates/live_kit_server/src/live_kit_server.rs b/crates/live_kit_server/src/live_kit_server.rs index 7b4a741355..7471a96ec4 100644 --- a/crates/live_kit_server/src/live_kit_server.rs +++ b/crates/live_kit_server/src/live_kit_server.rs @@ -1,3 +1,3 @@ -mod api; +pub mod api; mod proto; -mod token; +pub mod token; diff --git a/crates/live_kit_server/src/token.rs b/crates/live_kit_server/src/token.rs index f6ac2945b5..ae03cb3469 100644 --- a/crates/live_kit_server/src/token.rs +++ b/crates/live_kit_server/src/token.rs @@ -1,4 +1,4 @@ -use anyhow::Result; +use anyhow::{anyhow, Result}; use hmac::{Hmac, Mac}; use jwt::SignWithKey; use serde::Serialize; @@ -14,43 +14,49 @@ static DEFAULT_TTL: Duration = Duration::from_secs(6 * 60 * 60); // 6 hours #[serde(rename_all = "camelCase")] struct ClaimGrants<'a> { iss: &'a str, - sub: &'a str, + sub: Option<&'a str>, iat: u64, exp: u64, nbf: u64, - jwtid: &'a str, + jwtid: Option<&'a str>, video: VideoGrant<'a>, } #[derive(Default, Serialize)] #[serde(rename_all = "camelCase")] -struct VideoGrant<'a> { - room_create: Option, - room_join: Option, - room_list: Option, - room_record: Option, - room_admin: Option, - room: Option<&'a str>, - can_publish: Option, - can_subscribe: Option, - can_publish_data: Option, - hidden: Option, - recorder: Option, +pub struct VideoGrant<'a> { + pub room_create: Option, + pub room_join: Option, + pub room_list: Option, + pub room_record: Option, + pub room_admin: Option, + pub room: Option<&'a str>, + pub can_publish: Option, + pub can_subscribe: Option, + pub can_publish_data: Option, + pub hidden: Option, + pub recorder: Option, } pub fn create( api_key: &str, secret_key: &str, - room_name: &str, - participant_name: &str, + identity: Option<&str>, + video_grant: VideoGrant, ) -> Result { + if video_grant.room_join.is_some() && identity.is_none() { + Err(anyhow!( + "identity is required for room_join grant, but it is none" + ))?; + } + let secret_key: Hmac = Hmac::new_from_slice(secret_key.as_bytes())?; let now = SystemTime::now(); let claims = ClaimGrants { iss: api_key, - sub: participant_name, + sub: identity, iat: now.duration_since(UNIX_EPOCH).unwrap().as_secs(), exp: now .add(DEFAULT_TTL) @@ -58,14 +64,8 @@ pub fn create( .unwrap() .as_secs(), nbf: 0, - jwtid: participant_name, - video: VideoGrant { - room: Some(room_name), - room_join: Some(true), - can_publish: Some(true), - can_subscribe: Some(true), - ..Default::default() - }, + jwtid: identity, + video: video_grant, }; Ok(claims.sign_with_key(&secret_key)?) }