Compare commits

...
Author SHA1 Message Date
phoenix 7736f79649 Test is workng
textsender_models PR / Rustfmt (pull_request) Successful in 53s
textsender_models PR / Check (pull_request) Successful in 1m5s
textsender_models PR / Clippy (pull_request) Successful in 1m22s
Release Tagging / release (pull_request) Successful in 34s
2026-06-23 00:19:32 -04:00
phoenix 1eb659d865 Added test
textsender_models PR / Rustfmt (pull_request) Successful in 36s
textsender_models PR / Check (pull_request) Successful in 1m5s
textsender_models PR / Clippy (pull_request) Successful in 57s
Release Tagging / release (pull_request) Failing after 33s
2026-06-22 23:58:44 -04:00
+52 -9
View File
@@ -54,7 +54,7 @@ mod util {
pub fn time_to_std_time( pub fn time_to_std_time(
provided_time: &time::OffsetDateTime, provided_time: &time::OffsetDateTime,
) -> Result<std::time::SystemTime, std::time::SystemTimeError> { ) -> Result<std::time::SystemTime, std::time::SystemTimeError> {
let converted = std::time::SystemTime::from(*provided_time); let converted = std::time::SystemTime::from(provided_time.to_utc());
Ok(converted) Ok(converted)
} }
} }
@@ -74,6 +74,7 @@ pub struct CreateTokenResult {
pub access_token: String, pub access_token: String,
pub issued: i64, pub issued: i64,
pub expires_in: i64, pub expires_in: i64,
pub token_issued: time::OffsetDateTime,
} }
pub fn create_token( pub fn create_token(
@@ -85,12 +86,9 @@ pub fn create_token(
header.set_token_type(TOKEN_TYPE); header.set_token_type(TOKEN_TYPE);
let mut payload = josekit::jwt::JwtPayload::new(); let mut payload = josekit::jwt::JwtPayload::new();
let message = &token_resource.message; payload.set_subject(&token_resource.message);
let issuer = &token_resource.issuer; payload.set_issuer(&token_resource.issuer);
let audiences: &Vec<String> = &token_resource.audiences; payload.set_audience(token_resource.audiences.clone());
payload.set_subject(message);
payload.set_issuer(issuer);
payload.set_audience(audiences.clone());
if !token_resource.user_id.is_nil() { if !token_resource.user_id.is_nil() {
match payload.set_claim("user_id", Some(serde_json::json!(token_resource.user_id))) { match payload.set_claim("user_id", Some(serde_json::json!(token_resource.user_id))) {
Ok(_) => {} Ok(_) => {}
@@ -111,10 +109,55 @@ pub fn create_token(
Ok(CreateTokenResult { Ok(CreateTokenResult {
access_token: josekit::jwt::encode_with_signer(&payload, &header, &signer).unwrap(), access_token: josekit::jwt::encode_with_signer(&payload, &header, &signer).unwrap(),
issued: (expire - time::OffsetDateTime::UNIX_EPOCH).whole_seconds(), issued: issued.unix_timestamp(),
expires_in: (issued - time::OffsetDateTime::UNIX_EPOCH).whole_seconds(), expires_in: expire.unix_timestamp(),
token_issued: issued,
}) })
} }
Err(e) => Err(josekit::JoseError::InvalidClaim(e.into())), Err(e) => Err(josekit::JoseError::InvalidClaim(e.into())),
} }
} }
#[cfg(test)]
mod tests {
const TEST_MESSAGE: &str = "Testing for textsender";
const TEST_ISSUER: &str = "textsender-test";
const TEST_AUDIENCE: &str = "area-test";
const TEST_USER_ID: uuid::Uuid = uuid::uuid!("9ab1c75f-d184-4913-ae99-544b4dcfcb41");
const TEST_KEY: &str = "8342nhf7ycrt4983q7ryfc93w478ryfc3w9487ryfc342w98i7cy";
#[test]
fn test_create_token() {
let token_resource = super::TokenResource {
message: String::from(TEST_MESSAGE),
issuer: String::from(TEST_ISSUER),
audiences: vec![String::from(TEST_AUDIENCE)],
user_id: TEST_USER_ID,
};
let token_duration = time::Duration::minutes(30);
let key = String::from(TEST_KEY);
match super::create_token(&key, &token_resource, token_duration) {
Ok(cst) => match time::OffsetDateTime::from_unix_timestamp(cst.issued) {
Ok(d_result) => {
let they_match = {
d_result.year() == cst.token_issued.year()
&& d_result.month() == cst.token_issued.month()
&& d_result.day() == cst.token_issued.day()
&& d_result.hour() == cst.token_issued.hour()
&& d_result.minute() == cst.token_issued.minute()
&& d_result.second() == cst.token_issued.second()
};
assert!(they_match, "Issued times do not match");
}
Err(err) => {
assert!(false, "Error: {err:?}");
}
},
Err(err) => {
assert!(false, "Error: {err:?}");
}
}
}
}