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(FTL-17164): re-generate session id after successful authentication #61

Merged
merged 2 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions affinidi-messaging-mediator/src/database/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@
impl DatabaseHandler {
/// Creates a new session in the database
/// Typically called when sending the initial challenge to the client
pub async fn create_session(&self, session: &Session) -> Result<(), MediatorError> {

Check warning on line 111 in affinidi-messaging-mediator/src/database/session.rs

View workflow job for this annotation

GitHub Actions / rust-pipeline / Cargo Check

this function depends on never type fallback being `()`

Check warning on line 111 in affinidi-messaging-mediator/src/database/session.rs

View workflow job for this annotation

GitHub Actions / rust-pipeline / Test Suite

this function depends on never type fallback being `()`
let mut con = self.get_async_connection().await?;

let sid = format!("SESSION:{}", session.session_id);
Expand Down Expand Up @@ -164,29 +164,37 @@
/// Updates the state, and the expiry time
pub async fn update_session_authenticated(
&self,
session_id: &str,
old_session_id: &str,
new_session_id: &str,
) -> Result<(), MediatorError> {
let mut con = self.get_async_connection().await?;

let sid = format!("SESSION:{}", session_id);
let old_sid = format!("SESSION:{}", old_session_id);
let new_sid = format!("SESSION:{}", new_session_id);

deadpool_redis::redis::pipe()
.atomic()
.cmd("RENAME")
.arg(&old_sid)
.arg(&new_sid)
.cmd("HSET")
.arg(&sid)
.arg(&new_sid)
.arg("state")
.arg(SessionState::Authenticated.to_string())
.cmd("HINCRBY")
.arg("GLOBAL")
.arg("SESSIONS_SUCCESS")
.arg(1)
.expire(&sid, 86400)
.expire(&new_sid, 86400)
.query_async(&mut con)
.await
.map_err(|err| {
MediatorError::SessionError(
session_id.into(),
format!("tried to retrieve session({}). Error: {}", session_id, err),
old_session_id.into(),
format!(
"tried to retrieve session({}). Error: {}",
old_session_id, err
),
)
})?;

Expand Down
14 changes: 6 additions & 8 deletions affinidi-messaging-mediator/src/handlers/authenticate.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
use std::{net::SocketAddr, time::SystemTime};

use affinidi_messaging_didcomm::{envelope::MetaEnvelope, Message, UnpackOptions};
use affinidi_messaging_sdk::messages::GenericDataStruct;
use axum::{
extract::{ConnectInfo, State},
Json,
};
use axum::{extract::State, Json};
use http::StatusCode;
use jsonwebtoken::{encode, Header};
use rand::{distributions::Alphanumeric, Rng};
use serde::{Deserialize, Serialize};
use std::time::SystemTime;
use tracing::{debug, info, warn};

use crate::{
Expand Down Expand Up @@ -169,7 +165,7 @@ pub async fn authentication_response(
})?;

// Retrieve the session info from the database
let session = state.database.get_session(&challenge.session_id).await?;
let mut session = state.database.get_session(&challenge.session_id).await?;

// check that the DID matches from what was given for the initial challenge request to what was used for the message response
if let Some(from_did) = msg.from {
Expand Down Expand Up @@ -203,6 +199,8 @@ pub async fn authentication_response(
)
.into());
}
let old_sid = session.session_id;
session.session_id = create_random_string(12);

// Passed all the checks, now create the JWT tokens
let access_claims = SessionClaims {
Expand Down Expand Up @@ -253,7 +251,7 @@ pub async fn authentication_response(
// Set the session state to Authorized
state
.database
.update_session_authenticated(&session.session_id)
.update_session_authenticated(&old_sid, &session.session_id)
.await?;

info!(
Expand Down
Loading