From cd1c0dd7b6fc8cf33cc983ba69a7c0cc904bee14 Mon Sep 17 00:00:00 2001 From: phoenix Date: Wed, 6 Aug 2025 18:32:07 -0400 Subject: [PATCH 1/5] Added id to the token --- src/token.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/token.rs b/src/token.rs index 7655671..7030475 100644 --- a/src/token.rs +++ b/src/token.rs @@ -80,6 +80,7 @@ pub struct TokenResource { pub message: String, pub issuer: String, pub audiences: Vec, + pub id: uuid::Uuid, } pub const TOKEN_TYPE: &str = "JWT"; @@ -99,6 +100,15 @@ pub fn create_token( payload.set_subject(message); payload.set_issuer(issuer); payload.set_audience(audiences.clone()); + if token_resource.id.is_nil() { + match payload.set_claim("id", Some(serde_json::json!(token_resource.id))) { + Ok(_) => { + } + Err(err) => { + return Err(err); + } + } + } match get_issued() { Ok(issued) => { let expire = issued + duration; @@ -145,6 +155,7 @@ mod tests { issuer: String::from("icarus_auth_test"), message: String::from("Authorization"), audiences: vec![String::from("icarus_test")], + id: uuid::Uuid::nil(), }; let token_expiration_duration = time::Duration::hours(2); -- 2.43.0 From c8edff82b60e9f7668f57aecc896be721fd3b475 Mon Sep 17 00:00:00 2001 From: phoenix Date: Wed, 6 Aug 2025 18:35:34 -0400 Subject: [PATCH 2/5] Test refactor --- src/token.rs | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/src/token.rs b/src/token.rs index 7030475..ffc5004 100644 --- a/src/token.rs +++ b/src/token.rs @@ -131,6 +131,19 @@ pub fn create_token( mod tests { use super::*; + fn test_key() -> String { + String::from("c3092urmc2219ix320i40m293ic29IM09IN0u879Y8B98YB8yb86TN7B55R4yv4RCVU6Bi8YO8U") + } + + fn test_resource() -> TokenResource { + TokenResource { + issuer: String::from("icarus_auth_test"), + message: String::from("Authorization"), + audiences: vec![String::from("icarus_test")], + id: uuid::Uuid::nil(), + } + } + #[test] fn test_token_scope_check() { let mut token = Token::default(); @@ -148,15 +161,8 @@ mod tests { #[test] fn test_token_creation() { - let key = String::from( - "c3092urmc2219ix320i40m293ic29IM09IN0u879Y8B98YB8yb86TN7B55R4yv4RCVU6Bi8YO8U", - ); - let test_token_resource = TokenResource { - issuer: String::from("icarus_auth_test"), - message: String::from("Authorization"), - audiences: vec![String::from("icarus_test")], - id: uuid::Uuid::nil(), - }; + let key = test_key(); + let test_token_resource = test_resource(); let token_expiration_duration = time::Duration::hours(2); match create_token(&key, &test_token_resource, token_expiration_duration) { -- 2.43.0 From 7bd7d9b96bdca6ba5e6202796e4870d5848d7b1a Mon Sep 17 00:00:00 2001 From: phoenix Date: Wed, 6 Aug 2025 18:37:34 -0400 Subject: [PATCH 3/5] Added test and fixed conditional statement --- src/token.rs | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/src/token.rs b/src/token.rs index ffc5004..eaceb9a 100644 --- a/src/token.rs +++ b/src/token.rs @@ -100,7 +100,7 @@ pub fn create_token( payload.set_subject(message); payload.set_issuer(issuer); payload.set_audience(audiences.clone()); - if token_resource.id.is_nil() { + if !token_resource.id.is_nil() { match payload.set_claim("id", Some(serde_json::json!(token_resource.id))) { Ok(_) => { } @@ -178,4 +178,25 @@ mod tests { } } } + + #[test] + fn test_token_creation_with_id() { + let key = test_key(); + let mut test_token_resource = test_resource(); + test_token_resource.id = uuid::Uuid::new_v4(); + let token_expiration_duration = time::Duration::hours(2); + + match create_token(&key, &test_token_resource, token_expiration_duration) { + Ok((token, expire_duration)) => { + assert_eq!(false, token.is_empty(), "Error: Token is empty"); + assert!( + expire_duration > 0, + "Token expire duration is invalid {expire_duration:?}" + ); + } + Err(err) => { + assert!(false, "Error: {err:?}"); + } + } + } } -- 2.43.0 From 42119a5a6de8789f39e840741ed4a370b78af0e3 Mon Sep 17 00:00:00 2001 From: phoenix Date: Wed, 6 Aug 2025 18:38:49 -0400 Subject: [PATCH 4/5] Version bump --- Cargo.lock | 2 +- Cargo.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 86e4c76..30c190e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -142,7 +142,7 @@ checksum = "5971ac85611da7067dbfcabef3c70ebb5606018acd9e2a3903a0da507521e0d5" [[package]] name = "icarus_models" -version = "0.5.4" +version = "0.5.5" dependencies = [ "josekit", "rand", diff --git a/Cargo.toml b/Cargo.toml index ffd2480..1680962 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "icarus_models" -version = "0.5.4" +version = "0.5.5" edition = "2024" rust-version = "1.88" description = "models used for the icarus project" -- 2.43.0 From 070bfbaaf330fe0129ba77b7c6cba24ad5461bce Mon Sep 17 00:00:00 2001 From: phoenix Date: Wed, 6 Aug 2025 18:43:22 -0400 Subject: [PATCH 5/5] Code formatting --- src/token.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/token.rs b/src/token.rs index eaceb9a..5ade1d0 100644 --- a/src/token.rs +++ b/src/token.rs @@ -102,8 +102,7 @@ pub fn create_token( payload.set_audience(audiences.clone()); if !token_resource.id.is_nil() { match payload.set_claim("id", Some(serde_json::json!(token_resource.id))) { - Ok(_) => { - } + Ok(_) => {} Err(err) => { return Err(err); } -- 2.43.0