Add Id to token (#55)
All checks were successful
Release Tagging / release (push) Successful in 39s
Rust Build / Check (push) Successful in 41s
Rust Build / Rustfmt (push) Successful in 36s
Rust Build / Test Suite (push) Successful in 37s
Rust Build / Clippy (push) Successful in 33s
Rust Build / build (push) Successful in 34s
Rust Build / Test Suite (pull_request) Successful in 31s
Rust Build / Check (pull_request) Successful in 33s
Rust Build / Clippy (pull_request) Successful in 31s
Rust Build / Rustfmt (pull_request) Successful in 32s
Rust Build / build (pull_request) Successful in 41s
All checks were successful
Release Tagging / release (push) Successful in 39s
Rust Build / Check (push) Successful in 41s
Rust Build / Rustfmt (push) Successful in 36s
Rust Build / Test Suite (push) Successful in 37s
Rust Build / Clippy (push) Successful in 33s
Rust Build / build (push) Successful in 34s
Rust Build / Test Suite (pull_request) Successful in 31s
Rust Build / Check (pull_request) Successful in 33s
Rust Build / Clippy (pull_request) Successful in 31s
Rust Build / Rustfmt (pull_request) Successful in 32s
Rust Build / build (pull_request) Successful in 41s
Reviewed-on: #55 Co-authored-by: phoenix <kundeng00@pm.me> Co-committed-by: phoenix <kundeng00@pm.me>
This commit is contained in:
2
Cargo.lock
generated
2
Cargo.lock
generated
@@ -142,7 +142,7 @@ checksum = "5971ac85611da7067dbfcabef3c70ebb5606018acd9e2a3903a0da507521e0d5"
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "icarus_models"
|
name = "icarus_models"
|
||||||
version = "0.5.4"
|
version = "0.5.5"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"josekit",
|
"josekit",
|
||||||
"rand",
|
"rand",
|
||||||
|
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "icarus_models"
|
name = "icarus_models"
|
||||||
version = "0.5.4"
|
version = "0.5.5"
|
||||||
edition = "2024"
|
edition = "2024"
|
||||||
rust-version = "1.88"
|
rust-version = "1.88"
|
||||||
description = "models used for the icarus project"
|
description = "models used for the icarus project"
|
||||||
|
53
src/token.rs
53
src/token.rs
@@ -80,6 +80,7 @@ pub struct TokenResource {
|
|||||||
pub message: String,
|
pub message: String,
|
||||||
pub issuer: String,
|
pub issuer: String,
|
||||||
pub audiences: Vec<String>,
|
pub audiences: Vec<String>,
|
||||||
|
pub id: uuid::Uuid,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub const TOKEN_TYPE: &str = "JWT";
|
pub const TOKEN_TYPE: &str = "JWT";
|
||||||
@@ -99,6 +100,14 @@ pub fn create_token(
|
|||||||
payload.set_subject(message);
|
payload.set_subject(message);
|
||||||
payload.set_issuer(issuer);
|
payload.set_issuer(issuer);
|
||||||
payload.set_audience(audiences.clone());
|
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() {
|
match get_issued() {
|
||||||
Ok(issued) => {
|
Ok(issued) => {
|
||||||
let expire = issued + duration;
|
let expire = issued + duration;
|
||||||
@@ -121,6 +130,19 @@ pub fn create_token(
|
|||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
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]
|
#[test]
|
||||||
fn test_token_scope_check() {
|
fn test_token_scope_check() {
|
||||||
let mut token = Token::default();
|
let mut token = Token::default();
|
||||||
@@ -138,14 +160,29 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_token_creation() {
|
fn test_token_creation() {
|
||||||
let key = String::from(
|
let key = test_key();
|
||||||
"c3092urmc2219ix320i40m293ic29IM09IN0u879Y8B98YB8yb86TN7B55R4yv4RCVU6Bi8YO8U",
|
let test_token_resource = test_resource();
|
||||||
);
|
let token_expiration_duration = time::Duration::hours(2);
|
||||||
let test_token_resource = TokenResource {
|
|
||||||
issuer: String::from("icarus_auth_test"),
|
match create_token(&key, &test_token_resource, token_expiration_duration) {
|
||||||
message: String::from("Authorization"),
|
Ok((token, expire_duration)) => {
|
||||||
audiences: vec![String::from("icarus_test")],
|
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:?}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[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);
|
let token_expiration_duration = time::Duration::hours(2);
|
||||||
|
|
||||||
match create_token(&key, &test_token_resource, token_expiration_duration) {
|
match create_token(&key, &test_token_resource, token_expiration_duration) {
|
||||||
|
Reference in New Issue
Block a user