From b120327c051f403574a118d8e27795cc38c9f410 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Axel=20=C3=96rn=20Sigur=C3=B0sson?= Date: Tue, 2 Apr 2024 18:40:39 +0100 Subject: [PATCH] feat: Update JWTs to support multiple topics (#12) --- scripts/sign.py | 4 ++-- src/auth.rs | 22 +++++++++++----------- src/server.rs | 8 ++++---- src/session.rs | 2 +- 4 files changed, 18 insertions(+), 18 deletions(-) diff --git a/scripts/sign.py b/scripts/sign.py index 40d1293..1d14d69 100644 --- a/scripts/sign.py +++ b/scripts/sign.py @@ -13,14 +13,14 @@ def main( private_key_path: str = typer.Option("private_key.pem"), verbose: bool = typer.Option(False), scope: Scopes = typer.Argument(), - topic: str = typer.Argument(), + topics: list[str] = typer.Argument(), ): private_key = open(private_key_path, "r").read() payload = { "sub": "notiflux", "exp": dt.datetime.now(dt.timezone.utc) + dt.timedelta(days=365*100), - "topic": topic, + "topics": topics, "scope": scope.value, } if verbose: diff --git a/src/auth.rs b/src/auth.rs index eebaa79..a1b0d2a 100644 --- a/src/auth.rs +++ b/src/auth.rs @@ -5,14 +5,14 @@ use serde::{Deserialize, Serialize}; pub struct Claims { sub: String, exp: u64, - topic: String, + topics: Vec, scope: String, } #[derive(Debug, PartialEq)] pub enum Action { - Subscribe(String), - Broadcast(String), + Subscribe(Vec), + Broadcast(Vec), } pub fn get_action(token: &str, public_key: &[u8]) -> Result { @@ -30,14 +30,14 @@ pub fn get_action(token: &str, public_key: &[u8]) -> Result for Server { log::debug!("handling Broadcast: {:?}", msg); match get_action(&msg.token, &self.jwt_public_key) { - Ok(Action::Broadcast(topic)) => { - if topic == msg.topic { + Ok(Action::Broadcast(topics)) => { + if topics.contains(&msg.topic) { log::debug!("Broadcasting message to topic: {}", msg.topic); self.broadcast(&msg.topic, &msg.msg); } else { @@ -82,8 +82,8 @@ impl Handler for Server { log::debug!("{:?} subscribing topic {}", msg.id, msg.topic); match get_action(&msg.token, &self.jwt_public_key) { - Ok(Action::Subscribe(topic)) => { - if topic == msg.topic { + Ok(Action::Subscribe(topics)) => { + if topics.contains(&msg.topic) { log::debug!("{:?} is allowed to subscribe topic {}", msg.id, msg.topic); self.topics .entry(msg.topic.clone()) diff --git a/src/session.rs b/src/session.rs index 577d259..6481bcc 100644 --- a/src/session.rs +++ b/src/session.rs @@ -104,7 +104,7 @@ impl StreamHandler> for WSSession { let args: Vec<&str> = m.splitn(2, ' ').collect(); match args[..] { ["/subscribe", sub_args] => { - let sub_args: Vec<&str> = sub_args.splitn(2, ' ').collect(); + let sub_args: Vec<&str> = sub_args.split(' ').collect(); if let [topic, token] = sub_args[..] { self.addr.do_send(message::SubscribeToTopic { id: self.id,