tsk-51: Code formatting
Some checks failed
Rust Build / Rustfmt (pull_request) Successful in 40s
Rust Build / Test Suite (pull_request) Failing after 39s
Rust Build / Check (pull_request) Failing after 1m3s
Rust Build / build (pull_request) Failing after 40s
Rust Build / Clippy (pull_request) Failing after 1m3s

This commit is contained in:
2025-08-06 19:14:15 -04:00
parent cfd61e203f
commit 34d3e66c7b
2 changed files with 29 additions and 34 deletions

View File

@@ -78,7 +78,8 @@ pub mod endpoint {
if hashing::verify_password(&payload.password, user.password.clone()).unwrap() { if hashing::verify_password(&payload.password, user.password.clone()).unwrap() {
// Create token // Create token
let key = icarus_envy::environment::get_secret_key().await; let key = icarus_envy::environment::get_secret_key().await;
let (token_literal, duration) = token_stuff::create_token(&key, &user.id).unwrap(); let (token_literal, duration) =
token_stuff::create_token(&key, &user.id).unwrap();
if token_stuff::verify_token(&key, &token_literal) { if token_stuff::verify_token(&key, &token_literal) {
let current_time = time::OffsetDateTime::now_utc(); let current_time = time::OffsetDateTime::now_utc();
@@ -122,7 +123,8 @@ pub mod endpoint {
match repo::service::valid_passphrase(&pool, &payload.passphrase).await { match repo::service::valid_passphrase(&pool, &payload.passphrase).await {
Ok((id, _passphrase, _date_created)) => { Ok((id, _passphrase, _date_created)) => {
let key = icarus_envy::environment::get_secret_key().await; let key = icarus_envy::environment::get_secret_key().await;
let (token_literal, duration) = token_stuff::create_service_token(&key, &id).unwrap(); let (token_literal, duration) =
token_stuff::create_service_token(&key, &id).unwrap();
if token_stuff::verify_token(&key, &token_literal) { if token_stuff::verify_token(&key, &token_literal) {
let login_result = icarus_models::login_result::LoginResult { let login_result = icarus_models::login_result::LoginResult {

View File

@@ -20,7 +20,10 @@ pub fn get_expiration(issued: &time::OffsetDateTime) -> Result<time::OffsetDateT
Ok(*issued + duration_expire) Ok(*issued + duration_expire)
} }
pub fn create_token(provided_key: &String, id: &uuid::Uuid) -> Result<(String, i64), josekit::JoseError> { pub fn create_token(
provided_key: &String,
id: &uuid::Uuid,
) -> Result<(String, i64), josekit::JoseError> {
let resource = icarus_models::token::TokenResource { let resource = icarus_models::token::TokenResource {
message: String::from(MESSAGE), message: String::from(MESSAGE),
issuer: String::from(ISSUER), issuer: String::from(ISSUER),
@@ -30,7 +33,10 @@ pub fn create_token(provided_key: &String, id: &uuid::Uuid) -> Result<(String, i
icarus_models::token::create_token(provided_key, &resource, time::Duration::hours(4)) icarus_models::token::create_token(provided_key, &resource, time::Duration::hours(4))
} }
pub fn create_service_token(provided: &String, id: &uuid::Uuid) -> Result<(String, i64), josekit::JoseError> { pub fn create_service_token(
provided: &String,
id: &uuid::Uuid,
) -> Result<(String, i64), josekit::JoseError> {
let resource = icarus_models::token::TokenResource { let resource = icarus_models::token::TokenResource {
message: String::from("Service random"), message: String::from("Service random"),
issuer: String::from(ISSUER), issuer: String::from(ISSUER),
@@ -42,44 +48,31 @@ pub fn create_service_token(provided: &String, id: &uuid::Uuid) -> Result<(Strin
pub fn verify_token(key: &String, token: &String) -> bool { pub fn verify_token(key: &String, token: &String) -> bool {
match get_payload(key, token) { match get_payload(key, token) {
Ok((payload, _header)) => { Ok((payload, _header)) => match payload.subject() {
match payload.subject() { Some(_sub) => true,
Some(_sub) => true, None => false,
None => false, },
} Err(_err) => false,
}
Err(_err) => {
false
}
} }
} }
pub fn extract_id_from_token(key: &String, token: &String) -> Result<uuid::Uuid, std::io::Error> { pub fn extract_id_from_token(key: &String, token: &String) -> Result<uuid::Uuid, std::io::Error> {
match get_payload(key, token) { match get_payload(key, token) {
Ok((payload, _header)) => { Ok((payload, _header)) => match payload.claim("id") {
match payload.claim("id") { Some(id) => match uuid::Uuid::parse_str(id.as_str().unwrap()) {
Some(id) => { Ok(extracted) => Ok(extracted),
match uuid::Uuid::parse_str(id.as_str().unwrap()) { Err(err) => Err(std::io::Error::other(err.to_string())),
Ok(extracted) => { },
Ok(extracted) None => Err(std::io::Error::other("No claim found")),
} },
Err(err) => { Err(err) => Err(std::io::Error::other(err.to_string())),
Err(std::io::Error::other(err.to_string()))
}
}
}
None => {
Err(std::io::Error::other("No claim found"))
}
}
}
Err(err) => {
Err(std::io::Error::other(err.to_string()))
}
} }
} }
fn get_payload(key: &String, token: &String) -> Result<(josekit::jwt::JwtPayload, josekit::jws::JwsHeader), josekit::JoseError> { fn get_payload(
key: &String,
token: &String,
) -> Result<(josekit::jwt::JwtPayload, josekit::jws::JwsHeader), josekit::JoseError> {
let ver = Hs256.verifier_from_bytes(key.as_bytes()).unwrap(); let ver = Hs256.verifier_from_bytes(key.as_bytes()).unwrap();
jwt::decode_with_verifier(token, &ver) jwt::decode_with_verifier(token, &ver)
} }