Skip to content

Commit

Permalink
Session id response and new session route
Browse files Browse the repository at this point in the history
  • Loading branch information
Rajdip019 committed Jun 9, 2024
1 parent bedfd29 commit fb7d152
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 11 deletions.
Binary file modified .DS_Store
Binary file not shown.
8 changes: 3 additions & 5 deletions src/core/dek.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,14 @@ impl Dek {
let email_regex =
regex::Regex::new(r"^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$").unwrap();
let is_email = email_regex.is_match(identifier);
let encrypted_identifier = Encryption::encrypt_data(&identifier, &server_kek);
match is_email {
true => {
// encrypt the email using kek
let encrypted_email_kek = Encryption::encrypt_data(&identifier, &server_kek);
let cursor_dek = collection_dek
.find_one(
Some(doc! {
"email": encrypted_email_kek.clone(),
"email": encrypted_identifier.clone(),
}),
None,
)
Expand All @@ -106,12 +106,10 @@ impl Dek {
};
}
false => {
// encrypt the uid using kek
let encrypted_uid_kek = Encryption::encrypt_data(&identifier, &server_kek);
let cursor_dek = collection_dek
.find_one(
Some(doc! {
"uid": encrypted_uid_kek.clone(),
"uid": encrypted_identifier.clone(),
}),
None,
)
Expand Down
49 changes: 49 additions & 0 deletions src/core/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,7 @@ impl Session {
println!("{:?}", token);
sessions_res.push(SessionResponse {
uid: decrypted_session.uid,
session_id: decrypted_session.session_id,
email: decrypted_session.email,
user_agent: decrypted_session.user_agent,
is_revoked: decrypted_session.is_revoked,
Expand All @@ -326,6 +327,54 @@ impl Session {
Ok(sessions_res)
}

pub async fn get_details(mongo_client: &Client, uid: &str, session_id: &str) -> Result<SessionResponse> {
let db = mongo_client.database("auth");
let collection_session: Collection<Session> = db.collection("sessions");

let dek_data = match Dek::get(mongo_client, uid).await {
Ok(dek) => dek,
Err(e) => return Err(e),
};

let encrypted_uid = Encryption::encrypt_data(uid, &dek_data.dek);
let encrypted_session_id = Encryption::encrypt_data(session_id, &dek_data.dek);

let session = match collection_session
.find_one(doc! {"uid": encrypted_uid, "session_id": encrypted_session_id}, None)
.await
{
Ok(session) => {
match session {
Some(data) => {
let decrypted_session = data.decrypt(&dek_data.dek);
Ok(decrypted_session)
}
None => Err(Error::SessionNotFound {
message: "Session not found".to_string(),
}),
}
}
Err(e) => Err(Error::ServerError {
message: e.to_string(),
}),
};

match session {
Ok(data) => {
Ok(SessionResponse {
uid: data.uid,
session_id: data.session_id,
email: data.email,
user_agent: data.user_agent,
is_revoked: data.is_revoked,
created_at: data.created_at,
updated_at: data.updated_at,
})
}
Err(e) => Err(e),
}
}

pub async fn revoke_all(mongo_client: &Client, uid: &str) -> Result<()> {
let db = mongo_client.database("auth");
let collection_session: Collection<Session> = db.collection("sessions");
Expand Down
6 changes: 6 additions & 0 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ pub enum Error {
ExpiredSignature { message: String },
SessionExpired { message: String },
ActiveSessionExists { message: String },
SessionNotFound { message: String },

// -- Validation Errors
InvalidEmail { message: String },
Expand Down Expand Up @@ -153,6 +154,10 @@ impl Error {
(StatusCode::CONFLICT, ClientError::ACTIVE_SESSION_EXISTS)
}

Self::SessionNotFound { message: _ } => {
(StatusCode::NOT_FOUND, ClientError::SESSION_NOT_FOUND)
}

_ => (
StatusCode::INTERNAL_SERVER_ERROR,
ClientError::SERVICE_ERROR,
Expand All @@ -177,6 +182,7 @@ pub enum ClientError {
EXPIRED_SIGNATURE,
SESSION_EXPIRED,
ACTIVE_SESSION_EXISTS,
SESSION_NOT_FOUND,
}

// region: --- Error Boilerplate
Expand Down
25 changes: 21 additions & 4 deletions src/handlers/session_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@ use crate::{
errors::{Error, Result},
models::{
session_model::{
DeleteAllSessionsPayload, DeleteAllSessionsResult, DeleteSessionsPayload,
DeleteSessionsResult, RevokeAllSessionsPayload, RevokeAllSessionsResult,
RevokeSessionsPayload, RevokeSessionsResult, SessionRefreshPayload,
SessionRefreshResult, SessionResponse, VerifySession,
DeleteAllSessionsPayload, DeleteAllSessionsResult, DeleteSessionsPayload, DeleteSessionsResult, RevokeAllSessionsPayload, RevokeAllSessionsResult, RevokeSessionsPayload, RevokeSessionsResult, SessionDetailsPayload, SessionRefreshPayload, SessionRefreshResult, SessionResponse, VerifySession
},
user_model::UserIdPayload,
},
Expand Down Expand Up @@ -71,6 +68,26 @@ pub async fn get_all_from_uid(
};
}

#[debug_handler]
pub async fn get_details(
State(state): State<AppState>,
payload: Json<SessionDetailsPayload>,
) -> Result<Json<SessionResponse>> {
// check if the token is not empty
if payload.uid.is_empty() | payload.session_id.is_empty() {
return Err(Error::InvalidPayload {
message: "Invalid payload passed".to_string(),
});
}

match Session::get_details(&state.mongo_client, &payload.uid, &payload.session_id).await {
Ok(data) => {
return Ok(Json(data));
}
Err(e) => return Err(e),
};
}

#[debug_handler]
pub async fn refresh_session(
State(state): State<AppState>,
Expand Down
7 changes: 7 additions & 0 deletions src/models/session_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub struct VerifySession {
#[derive(Deserialize, Debug, Clone, Serialize)]
pub struct SessionResponse {
pub uid : String,
pub session_id : String,
pub email : String,
pub user_agent : String,
pub is_revoked : bool,
Expand Down Expand Up @@ -69,4 +70,10 @@ pub struct DeleteSessionsPayload {
#[derive(Serialize, Debug, Clone)]
pub struct DeleteSessionsResult {
pub message: String,
}

#[derive(Deserialize, Debug, Clone)]
pub struct SessionDetailsPayload {
pub uid: String,
pub session_id: String,
}
5 changes: 3 additions & 2 deletions src/routes/session_routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@ use axum::{extract::State, routing::post, Router};

use crate::{
handlers::session_handler::{
delete, delete_all, get_all_from_uid, refresh_session, revoke, revoke_all, verify_session,
delete, delete_all, get_all_from_uid, get_details, refresh_session, revoke, revoke_all, verify_session
},
AppState,
};

pub fn routes(State(state): State<AppState>) -> Router {
let session_routes = Router::new()
.route("/verify", post(verify_session))
.route("/get_all_from_uid", post(get_all_from_uid))
.route("/get-all-from-uid", post(get_all_from_uid))
.route("/get-details", post(get_details))
.route("/refresh-session", post(refresh_session))
.route("/revoke", post(revoke))
.route("/revoke-all", post(revoke_all))
Expand Down

0 comments on commit fb7d152

Please sign in to comment.