WIP: Update token module to support server api

This commit is contained in:
Nathan Sobo 2022-10-14 10:00:50 -06:00 committed by Antonio Scandurra
parent 5d433b1666
commit 19a2752674
5 changed files with 47 additions and 34 deletions

View file

@ -39,8 +39,14 @@ fn main() {
let user1_token = live_kit_server::token::create( let user1_token = live_kit_server::token::create(
&live_kit_key, &live_kit_key,
&live_kit_secret, &live_kit_secret,
"test-room", Some("test-participant-1"),
"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(); .unwrap();
let room1 = Room::new(); let room1 = Room::new();
@ -49,10 +55,17 @@ fn main() {
let user2_token = live_kit_server::token::create( let user2_token = live_kit_server::token::create(
&live_kit_key, &live_kit_key,
&live_kit_secret, &live_kit_secret,
"test-room", Some("test-participant-2"),
"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(); .unwrap();
let room2 = Room::new(); let room2 = Room::new();
room2.connect(&live_kit_url, &user2_token).await.unwrap(); room2.connect(&live_kit_url, &user2_token).await.unwrap();
cx.add_window(Default::default(), |cx| ScreenCaptureView::new(room2, cx)); cx.add_window(Default::default(), |cx| ScreenCaptureView::new(room2, cx));

View file

@ -12,7 +12,7 @@ doctest = false
anyhow = "1.0.38" anyhow = "1.0.38"
hmac = "0.12" hmac = "0.12"
jwt = "0.16" jwt = "0.16"
hyper = "0.14" hyper = { version = "0.14", features = ["client", "http1"] }
prost = "0.8" prost = "0.8"
prost-types = "0.8" prost-types = "0.8"
serde = { version = "1.0", features = ["derive", "rc"] } serde = { version = "1.0", features = ["derive", "rc"] }

View file

@ -31,6 +31,6 @@ impl Client {
.build(); .build();
// token::create(api_key, secret_key, room_name, participant_name) // token::create(api_key, secret_key, room_name, participant_name)
self.http.request(req) // self.http.request(req)
} }
} }

View file

@ -1,3 +1,3 @@
mod api; pub mod api;
mod proto; mod proto;
mod token; pub mod token;

View file

@ -1,4 +1,4 @@
use anyhow::Result; use anyhow::{anyhow, Result};
use hmac::{Hmac, Mac}; use hmac::{Hmac, Mac};
use jwt::SignWithKey; use jwt::SignWithKey;
use serde::Serialize; use serde::Serialize;
@ -14,43 +14,49 @@ static DEFAULT_TTL: Duration = Duration::from_secs(6 * 60 * 60); // 6 hours
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
struct ClaimGrants<'a> { struct ClaimGrants<'a> {
iss: &'a str, iss: &'a str,
sub: &'a str, sub: Option<&'a str>,
iat: u64, iat: u64,
exp: u64, exp: u64,
nbf: u64, nbf: u64,
jwtid: &'a str, jwtid: Option<&'a str>,
video: VideoGrant<'a>, video: VideoGrant<'a>,
} }
#[derive(Default, Serialize)] #[derive(Default, Serialize)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
struct VideoGrant<'a> { pub struct VideoGrant<'a> {
room_create: Option<bool>, pub room_create: Option<bool>,
room_join: Option<bool>, pub room_join: Option<bool>,
room_list: Option<bool>, pub room_list: Option<bool>,
room_record: Option<bool>, pub room_record: Option<bool>,
room_admin: Option<bool>, pub room_admin: Option<bool>,
room: Option<&'a str>, pub room: Option<&'a str>,
can_publish: Option<bool>, pub can_publish: Option<bool>,
can_subscribe: Option<bool>, pub can_subscribe: Option<bool>,
can_publish_data: Option<bool>, pub can_publish_data: Option<bool>,
hidden: Option<bool>, pub hidden: Option<bool>,
recorder: Option<bool>, pub recorder: Option<bool>,
} }
pub fn create( pub fn create(
api_key: &str, api_key: &str,
secret_key: &str, secret_key: &str,
room_name: &str, identity: Option<&str>,
participant_name: &str, video_grant: VideoGrant,
) -> Result<String> { ) -> Result<String> {
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<Sha256> = Hmac::new_from_slice(secret_key.as_bytes())?; let secret_key: Hmac<Sha256> = Hmac::new_from_slice(secret_key.as_bytes())?;
let now = SystemTime::now(); let now = SystemTime::now();
let claims = ClaimGrants { let claims = ClaimGrants {
iss: api_key, iss: api_key,
sub: participant_name, sub: identity,
iat: now.duration_since(UNIX_EPOCH).unwrap().as_secs(), iat: now.duration_since(UNIX_EPOCH).unwrap().as_secs(),
exp: now exp: now
.add(DEFAULT_TTL) .add(DEFAULT_TTL)
@ -58,14 +64,8 @@ pub fn create(
.unwrap() .unwrap()
.as_secs(), .as_secs(),
nbf: 0, nbf: 0,
jwtid: participant_name, jwtid: identity,
video: VideoGrant { video: video_grant,
room: Some(room_name),
room_join: Some(true),
can_publish: Some(true),
can_subscribe: Some(true),
..Default::default()
},
}; };
Ok(claims.sign_with_key(&secret_key)?) Ok(claims.sign_with_key(&secret_key)?)
} }