Skip to content

Commit

Permalink
Extract authentication into function
Browse files Browse the repository at this point in the history
  • Loading branch information
h3ndrk committed Dec 1, 2023
1 parent d8f0d05 commit d3cbc3e
Showing 1 changed file with 65 additions and 48 deletions.
113 changes: 65 additions & 48 deletions src/presentation/talks_ws.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,66 @@ async fn talks_ws_connection(
) -> Result<(), String> {
let mut updates = services.talks.register_for_updates();

let response = match receive(&mut socket).await? {
let (user_id, capabilities) = authenticate(&mut socket, &services).await?;

for talk in services
.talks
.get_all_talks()
.await
.map_err(|error| error.to_string())?
{
send(&mut socket, &Update::AddTalk(talk)).await?;
}

loop {
select! {
command = receive(&mut socket) => {
let command = command?;
services
.talks
.trigger(user_id, &capabilities, command)
.await
.map_err(|error| error.to_string())?;
},
update = updates.recv() => {
let update = update.map_err(|error| error.to_string())?;
send(&mut socket, &update).await?;
},
}
}
}

async fn send(socket: &mut WebSocket, message: &impl Serialize) -> Result<(), String> {
socket
.send(Message::Text(to_string(message).unwrap()))
.await
.map_err(|error| error.to_string())
}

async fn receive<T: DeserializeOwned>(socket: &mut WebSocket) -> Result<T, String> {
let Message::Text(message) = socket
.recv()
.await
.ok_or_else(|| "closed".to_string())?
.map_err(|error| error.to_string())?
else {
return Err("expected text message".to_string());
};
from_str(&message).map_err(|error| error.to_string())
}

async fn authenticate(
socket: &mut WebSocket,
services: &Arc<
Services<
impl AuthenticationService + Send + Sync,
impl CalendarService + Send + Sync,
impl TalksService + Send + Sync,
impl TeamsService + Send + Sync,
>,
>,
) -> Result<(i64, HashSet<Capability>), String> {
let response = match receive(socket).await? {
AuthenticationCommand::Register {
name,
team,
Expand Down Expand Up @@ -101,13 +160,14 @@ async fn talks_ws_connection(
Response::UnknownToken => "unknown token",
Response::Success { .. } => panic!("should not be reached"),
};
return send(&mut socket, &AuthenticationResponse::AuthenticationError {
send(socket, &AuthenticationResponse::AuthenticationError {
reason: reason.to_string(),
}).await;
}).await?;
return Err(format!("authentication error: {reason}"));
};

send(
&mut socket,
socket,
&AuthenticationResponse::AuthenticationSuccess {
user_id: user_id as usize,
capabilities: capabilities.clone(),
Expand All @@ -116,50 +176,7 @@ async fn talks_ws_connection(
)
.await?;

for talk in services
.talks
.get_all_talks()
.await
.map_err(|error| error.to_string())?
{
send(&mut socket, &Update::AddTalk(talk)).await?;
}

loop {
select! {
command = receive(&mut socket) => {
let command = command?;
services
.talks
.trigger(user_id, &capabilities, command)
.await
.map_err(|error| error.to_string())?;
},
update = updates.recv() => {
let update = update.map_err(|error| error.to_string())?;
send(&mut socket, &update).await?;
},
}
}
}

async fn send(socket: &mut WebSocket, message: &impl Serialize) -> Result<(), String> {
socket
.send(Message::Text(to_string(message).unwrap()))
.await
.map_err(|error| error.to_string())
}

async fn receive<T: DeserializeOwned>(socket: &mut WebSocket) -> Result<T, String> {
let Message::Text(message) = socket
.recv()
.await
.ok_or_else(|| "closed".to_string())?
.map_err(|error| error.to_string())?
else {
return Err("expected text message".to_string());
};
from_str(&message).map_err(|error| error.to_string())
Ok((user_id, capabilities))
}

#[derive(Clone, Debug, Deserialize)]
Expand Down

0 comments on commit d3cbc3e

Please sign in to comment.