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

changed form is_active to is_revoked #28

Merged
merged 1 commit into from
May 3, 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
41 changes: 27 additions & 14 deletions src/core/session.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use crate::{
errors::{Error, Result},
traits::{decryption::Decrypt, encryption::Encrypt},
utils::{encryption_utils::Encryption, session_utils::{IDToken, RefreshToken}},
errors::{Error, Result}, models::session_model::SessionResponse, traits::{decryption::Decrypt, encryption::Encrypt}, utils::{encryption_utils::Encryption, session_utils::{IDToken, RefreshToken}}
};
use bson::{doc, DateTime};
use futures::StreamExt;
Expand All @@ -17,7 +15,7 @@ pub struct Session {
pub id_token: String,
pub refresh_token: String,
pub user_agent: String,
pub is_active: bool,
pub is_revoked: bool,
pub created_at: DateTime,
pub updated_at: DateTime,
}
Expand All @@ -40,7 +38,7 @@ impl Session {
id_token,
refresh_token,
user_agent: user_agent.to_string(),
is_active: true,
is_revoked: false,
created_at: DateTime::now(),
updated_at: DateTime::now(),
}
Expand Down Expand Up @@ -81,12 +79,12 @@ impl Session {
.count_documents(doc! {
"uid": encrypted_id,
"id_token": encrypted_id_token,
"is_active": true,
"is_revoked": false,
}, None)
.await
{
Ok(count) => {
if count > 0 {
if count == 1 {
Ok(())
} else {
Err(Error::SessionExpired {
Expand All @@ -111,7 +109,7 @@ impl Session {
token_data
}

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

Expand All @@ -126,24 +124,39 @@ impl Session {
.find(
doc! {
"uid": encrypted_uid,
"is_active": true,
"is_revoked": false,
},
None,
)
.await
.unwrap();

let mut sessions: Vec<Session> = Vec::new();
let mut sessions_res: Vec<SessionResponse> = Vec::new();
while let Some(session) = cursor.next().await {
match session {
Ok(data) => {
let decrypted_session = data.decrypt(&dek_data.dek);
sessions.push(decrypted_session);
match IDToken::verify(&decrypted_session.id_token) {
Ok(token) => {
println!("{:?}", token);
sessions_res.push(
SessionResponse {
uid: decrypted_session.uid,
email: decrypted_session.email,
user_agent: decrypted_session.user_agent,
is_revoked: decrypted_session.is_revoked,
created_at: decrypted_session.created_at,
updated_at: decrypted_session.updated_at,
}
);
}
Err(_) => continue,
}
}
Err(e) => return Err(Error::ServerError { message: e.to_string() }),
}
}
Ok(sessions)
Ok(sessions_res)
}

pub async fn revoke_all(mongo_client: &Client, uid: &str) -> Result<()> {
Expand All @@ -153,7 +166,7 @@ impl Session {
match collection_session
.update_many(
doc! {"uid": uid},
doc! {"$set": {"is_active": false}},
doc! {"$set": {"is_revoked": true}},
None,
)
.await
Expand All @@ -176,7 +189,7 @@ impl Session {
match collection_session
.update_one(
doc! {"id_token": id_token, "refresh_token": refresh_token },
doc! {"$set": {"is_active": false}},
doc! {"$set": {"is_revoked": true}},
None,
)
.await
Expand Down
7 changes: 3 additions & 4 deletions src/handlers/session_handler.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use axum::{extract::State, Json};
use axum_macros::debug_handler;

use crate::{core::session::Session, errors::{Error, Result}, models::{session_model::VerifyJwt, user_model::UserIdPayload}, utils::session_utils::IDToken, AppState};
use crate::{core::session::Session, errors::{Error, Result}, models::{session_model::{SessionResponse, VerifySession}, user_model::UserIdPayload}, utils::session_utils::IDToken, AppState};

#[debug_handler]
pub async fn verify_session(
State(state): State<AppState>,
payload: Json<VerifyJwt>,
payload: Json<VerifySession>,
) -> Result<Json<IDToken>> {
// check if the token is not empty
if payload.token.is_empty() {
Expand All @@ -27,7 +27,7 @@ pub async fn verify_session(
pub async fn get_all_from_uid(
State(state): State<AppState>,
payload: Json<UserIdPayload>,
) -> Result<Json<Vec<Session>>> {
) -> Result<Json<Vec<SessionResponse>>> {
// check if the token is not empty
if payload.uid.is_empty() {
return Err(Error::InvalidPayload { message: "Invalid payload passed".to_string() });
Expand All @@ -39,6 +39,5 @@ pub async fn get_all_from_uid(
return Ok(Json(data));
}
Err(e) => return Err(e),

};
}
13 changes: 12 additions & 1 deletion src/models/session_model.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
use bson::DateTime;
use serde::{Deserialize, Serialize};

#[derive(Deserialize, Debug, Clone, Serialize)]
pub struct VerifyJwt {
pub struct VerifySession {
pub token: String,
}

#[derive(Deserialize, Debug, Clone, Serialize)]
pub struct SessionResponse {
pub uid : String,
pub email : String,
pub user_agent : String,
pub is_revoked : bool,
pub created_at : DateTime,
pub updated_at : DateTime,
}
4 changes: 1 addition & 3 deletions src/utils/auth_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ use crate::{
core::{dek::Dek, session::Session, user::User},
errors::{Error, Result},
models::auth_model::{SignInPayload, SignUpPayload},
utils::{
hashing_utils::verify_password_hash, session_utils::IDToken,
},
utils::hashing_utils::verify_password_hash,
};

pub async fn sign_up(mongo_client: &Client, payload: Json<SignUpPayload>) -> Result<Json<Value>> {
Expand Down
2 changes: 1 addition & 1 deletion src/utils/session_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ impl IDToken {
uid: user.uid.to_string(),
iss: server_url,
iat: chrono::Utc::now().timestamp() as usize,
exp: chrono::Utc::now().timestamp() as usize + (3600 * 12), // 12h
exp: chrono::Utc::now().timestamp() as usize + 3600, // 1h
token_type: "id".to_string(),
data : Some(
[
Expand Down