Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
novacrazy committed Oct 4, 2024
1 parent 0a4dc67 commit a164ec6
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 28 deletions.
8 changes: 1 addition & 7 deletions bin/nexus/src/rpc/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,7 @@ pub async fn do_auth(state: ServerState, token: &Archived<RawAuthToken>) -> Resu

match auth {
Some(auth @ Authorization::Bot { .. }) => Ok(auth),
Some(auth @ Authorization::User { expires, .. }) => {
if expires > Timestamp::now_utc() {
Ok(auth)
} else {
Err(Error::NoSession) // TODO: Better error type for bad bot auth
}
}
Some(auth @ Authorization::User { expires, .. }) if expires > Timestamp::now_utc() => Ok(auth),
_ => Err(Error::NoSession),
}
}
Expand Down
26 changes: 10 additions & 16 deletions bin/nexus/src/rpc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,44 +115,40 @@ where
// avoid inlining every async state machine by boxing them inside a lazy future/async block
macro_rules! c {
($([$size:literal])? $first:ident$(::$frag:ident)+($($args:expr),*)) => {
Box::pin(async move { ::rpc::stream::encode_item::<_, Error, _>(
out, crate::rpc::$first$(::$frag)+($($args),*).await).await.map_err(Error::from) })
return Ok(Box::pin(async move { ::rpc::stream::encode_item::<_, Error, _>(
out, crate::rpc::$first$(::$frag)+($($args),*).await).await.map_err(Error::from) }))
};
}
macro_rules! s {
($([$size:literal])? $first:ident$(::$frag:ident)+($($args:expr),*)) => {
Box::pin(async move { ::rpc::stream::encode_stream::<_, Error, _>(
out, crate::rpc::$first$(::$frag)+($($args),*).await).await.map_err(Error::from) })
return Ok(Box::pin(async move { ::rpc::stream::encode_stream::<_, Error, _>(
out, crate::rpc::$first$(::$frag)+($($args),*).await).await.map_err(Error::from) }))
};
}

use core::{future::Future, pin::Pin};

#[allow(clippy::type_complexity)]
#[allow(clippy::type_complexity)] #[rustfmt::skip]
// using a closure here allows for early returns of the future for auth and others
let gen_dispatch = move || -> Result<Pin<Box<dyn Future<Output = Result<(), Error>> + Send>>, Error> {
let (addr, auth, proc) = match cmd {
ArchivedRpcRequest::Procedure { addr, auth, proc } => (addr, auth, proc),
ArchivedRpcRequest::Authorize { token } => return Ok(c!(auth::do_auth(state, token))),
ArchivedRpcRequest::Authorize { token } => c!(auth::do_auth(state, token)),
_ => unimplemented!(),
};

// prepare fields
let addr = addr.as_ipaddr();
let auth = || match auth.as_ref() {
Some(auth) => Ok(auth.get().deserialize_simple().expect("Unable to deserialize auth")),
None => Err(Error::Unauthorized),
};

use rpc::procedure::ArchivedProcedure as Proc;

// assigning to a variable is apparently requires to get the type coalescing to work
#[allow(unused_variables)]
#[rustfmt::skip]
let running: Pin<Box<dyn Future<Output = Result<(), Error>> + Send>> = match proc {
match proc {
Proc::GetServerConfig(cmd) => todo!("GetServerConfig"),
Proc::UserRegister(cmd) => c!(user::user_register::register_user(state, addr, cmd)),
Proc::UserLogin(cmd) => c!(user::user_login::login(state, addr, cmd)),
Proc::UserRegister(cmd) => c!(user::user_register::register_user(state, addr.as_ipaddr(), cmd)),
Proc::UserLogin(cmd) => c!(user::user_login::login(state, addr.as_ipaddr(), cmd)),
Proc::UserLogout(_) => c!(user::me::user_logout::logout_user(state, auth()?)),
Proc::Enable2FA(cmd) => c!(user::me::user_mfa::enable_2fa(state, auth()?, cmd)),
Proc::Confirm2FA(cmd) => c!(user::me::user_mfa::confirm_2fa(state, auth()?, cmd)),
Expand Down Expand Up @@ -207,9 +203,7 @@ where
Proc::PatchRoom(cmd) => c!(room::modify_room::modify_room(state, auth()?, cmd)),
Proc::DeleteRoom(cmd) => c!(room::remove_room::remove_room(state, auth()?, cmd)),
Proc::GetRoom(cmd) => c!(room::get_room::get_room(state, auth()?, cmd)),
};

Ok(running)
}
};

gen_dispatch()?.await
Expand Down
1 change: 0 additions & 1 deletion components/auth/src/bot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ impl SplitBotToken {
}

pub fn verify(&self, key: &BotTokenKey) -> bool {
//token_mac(self, key).verify_slice(&self.hmac).is_ok()
self.hmac == self.token_mac(key).finalize_fixed().as_slice()
}

Expand Down
2 changes: 1 addition & 1 deletion components/rpc/src/procedure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ decl_procs! {
303 = RedeemInvite,

// Party stuff, goes to faction servers
401 = CreateParty,
401 = CreateParty, // goes to the nexus first
402 = GetParty @ party.party_id,
403 = PatchParty @ party.party_id,
404 = DeleteParty @ party.party_id,
Expand Down
6 changes: 3 additions & 3 deletions components/rpc/src/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ where
E: core::fmt::Debug,
ApiError: From<E>,
{
// stream::iter is more efficient
encode_stream(out, Ok(futures_util::stream::iter([item]))).await
// stream::iter is more efficient for non-future values
encode_stream(out, Ok(futures_util::stream::iter(Some(item)))).await
}

pub async fn encode_stream<T, E, W>(
Expand All @@ -54,7 +54,7 @@ where

let mut stream = std::pin::pin!(match stream {
Ok(stream) => Either::Left(stream),
Err(err) => Either::Right(futures_util::stream::iter([Err(err)])),
Err(err) => Either::Right(futures_util::stream::iter(Some(Err(err)))),
});

while let Some(item) = stream.next().await {
Expand Down

0 comments on commit a164ec6

Please sign in to comment.