diff --git a/src/callers/login.rs b/src/callers/login.rs index 11fc6a4..6f94daf 100644 --- a/src/callers/login.rs +++ b/src/callers/login.rs @@ -78,7 +78,8 @@ pub mod endpoint { if hashing::verify_password(&payload.password, user.password.clone()).unwrap() { // Create token 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) { let current_time = time::OffsetDateTime::now_utc(); @@ -122,7 +123,8 @@ pub mod endpoint { match repo::service::valid_passphrase(&pool, &payload.passphrase).await { Ok((id, _passphrase, _date_created)) => { 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) { let login_result = icarus_models::login_result::LoginResult { diff --git a/src/token_stuff/mod.rs b/src/token_stuff/mod.rs index aa9454f..0a94595 100644 --- a/src/token_stuff/mod.rs +++ b/src/token_stuff/mod.rs @@ -20,7 +20,10 @@ pub fn get_expiration(issued: &time::OffsetDateTime) -> Result 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 { message: String::from(MESSAGE), 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)) } -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 { message: String::from("Service random"), 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 { match get_payload(key, token) { - Ok((payload, _header)) => { - match payload.subject() { - Some(_sub) => true, - None => false, - } - } - Err(_err) => { - false - } + Ok((payload, _header)) => match payload.subject() { + Some(_sub) => true, + None => false, + }, + Err(_err) => false, } } pub fn extract_id_from_token(key: &String, token: &String) -> Result { match get_payload(key, token) { - Ok((payload, _header)) => { - match payload.claim("id") { - Some(id) => { - match uuid::Uuid::parse_str(id.as_str().unwrap()) { - Ok(extracted) => { - Ok(extracted) - } - Err(err) => { - 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())) - } + Ok((payload, _header)) => match payload.claim("id") { + Some(id) => match uuid::Uuid::parse_str(id.as_str().unwrap()) { + Ok(extracted) => Ok(extracted), + Err(err) => 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(); jwt::decode_with_verifier(token, &ver) }