From 957ff739bb2aff0e4c0456d031f2875de419b680 Mon Sep 17 00:00:00 2001 From: Gerard Guillemas Martos Date: Thu, 19 Sep 2024 15:17:29 +0100 Subject: [PATCH] Fix failed `authenticate` resulting in session reset (#4839) --- core/src/rpc/rpc_context.rs | 7 +++++-- tests/common/tests.rs | 19 +++++++++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/core/src/rpc/rpc_context.rs b/core/src/rpc/rpc_context.rs index ce962224..1a572fc9 100644 --- a/core/src/rpc/rpc_context.rs +++ b/core/src/rpc/rpc_context.rs @@ -172,9 +172,12 @@ pub trait RpcContext { return Err(RpcError::InvalidParams); }; let mut tmp_session = mem::take(self.session_mut()); - crate::iam::verify::token(self.kvs(), &mut tmp_session, &token.0).await?; + let out: Result<(), RpcError> = + crate::iam::verify::token(self.kvs(), &mut tmp_session, &token.0) + .await + .map_err(Into::into); *self.session_mut() = tmp_session; - Ok(Value::None.into()) + out.map(|_| Value::None.into()) } // ------------------------------ diff --git a/tests/common/tests.rs b/tests/common/tests.rs index 7d602557..38bd2539 100644 --- a/tests/common/tests.rs +++ b/tests/common/tests.rs @@ -1505,6 +1505,25 @@ async fn session_reauthentication_expired() { server.finish().unwrap(); } +#[test(tokio::test)] +async fn session_failed_reauthentication() { + // Setup database server without authentication + let (addr, mut server) = common::start_server_without_auth().await.unwrap(); + // Connect to WebSocket + let mut socket = Socket::connect(&addr, SERVER, FORMAT).await.unwrap(); + // Specify a namespace and database to use + socket.send_message_use(Some(NS), Some(DB)).await.unwrap(); + // Check that we have are have a database and namespace selected + socket.send_message_query("INFO FOR DB").await.unwrap(); + // Authenticate using an invalid token + socket.send_request("authenticate", json!(["invalid",])).await.unwrap(); + // Check to see if we still have a namespace and database selected + let res = socket.send_message_query("INFO FOR DB").await.unwrap(); + assert_eq!(res[0]["status"], "OK", "result: {res:?}"); + // Test passed + server.finish().unwrap(); +} + #[test(tokio::test)] async fn session_use_change_database() { // Setup database server