Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add authentication for RPC service #89

Merged
merged 7 commits into from
May 9, 2023
Merged

feat: add authentication for RPC service #89

merged 7 commits into from
May 9, 2023

Conversation

lauti7
Copy link
Contributor

@lauti7 lauti7 commented May 5, 2023

This PR:

@lauti7 lauti7 changed the title auth wip feat: add authentication for RPC service May 5, 2023
@lauti7 lauti7 marked this pull request as ready for review May 8, 2023 15:42
@lauti7 lauti7 requested a review from guidota May 8, 2023 15:43
Copy link
Contributor

@guidota guidota left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be great to move the authentication stuff to its own module.

Ok(ws) => Ok((ws, address)),
Err(err) => Err(AuthenticationErrors::UnexpectedError(Box::new(err))),
}
} else if let Err(err) = ws_write.close().await {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you say if we close the websocket in the upgrade handler and return the error first?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay but the error would be Err((Websocket,AuthenticationErrors)) because we have to consume the ws


match tokio::time::timeout(Duration::from_secs(30), ws_read.next()).await {
Ok(client_response) => {
let response = client_response.unwrap().unwrap();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we handle the possible errors here?

WrongSignature,
Timeout,
NotTextMessage,
UnexpectedError(Box<dyn std::error::Error + Send + Sync>),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we move the socket close to the handler, then we don't need this one.

@@ -121,3 +146,60 @@ async fn handle_rejection(err: Rejection) -> Result<impl Reply, std::convert::In
))
}
}

pub enum AuthenticationErrors {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
pub enum AuthenticationErrors {
pub enum AuthenticationError {

Ok(client_response) => {
let response = client_response.unwrap().unwrap();
if let Ok(auth_chain) = response.to_str() {
let auth_chain = AuthChain::from_json(auth_chain).unwrap();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we handle possible errors here?

Comment on lines 166 to 172
if ws_write
.send(Message::text(&message_to_be_firmed))
.await
.is_err()
{
return Err(AuthenticationErrors::FailedToSendChallenge);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if ws_write
.send(Message::text(&message_to_be_firmed))
.await
.is_err()
{
return Err(AuthenticationErrors::FailedToSendChallenge);
}
ws_write
.send(Message::text(&message_to_be_firmed))
.await
.map_err(|_| AuthenticationErrors::FailedToSendChallenge)?;

@lauti7 lauti7 linked an issue May 9, 2023 that may be closed by this pull request
@lauti7 lauti7 merged commit 2aaff1d into main May 9, 2023
@lauti7 lauti7 deleted the feat/ws-auth branch May 9, 2023 13:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Middleware for websocket server (RPC Server)
2 participants