Ready to try
Rust Build / Rustfmt (pull_request) Successful in 1m10s
Rust Build / Test Suite (pull_request) Successful in 1m13s
Rust Build / Check (pull_request) Successful in 1m18s
Rust Build / Clippy (pull_request) Successful in 30s
Rust Build / build (pull_request) Successful in 3m14s

This commit is contained in:
2026-06-12 10:23:32 -04:00
parent 19427114fc
commit ffd87f7226
+56 -164
View File
@@ -63,7 +63,7 @@ pub mod response {
todo!("Add code to convert axum::Response to this type"); todo!("Add code to convert axum::Response to this type");
} }
#[derive(Deserialize, utoipa::ToSchema)] #[derive(Deserialize, Serialize, utoipa::ToSchema)]
pub struct RefreshTokenResponse { pub struct RefreshTokenResponse {
pub message: String, pub message: String,
pub data: Vec<textsender_models::token::LoginResult>, pub data: Vec<textsender_models::token::LoginResult>,
@@ -359,7 +359,7 @@ pub async fn service_user_login(
content_type = "application/json" content_type = "application/json"
), ),
responses( responses(
(status = 201, description = "Service uuser login successful", body = response::RefreshTokenResponse), (status = 200, description = "Service uuser login successful", body = response::RefreshTokenResponse),
(status = 400, description = "Bad data", body = response::RefreshTokenResponse), (status = 400, description = "Bad data", body = response::RefreshTokenResponse),
(status = 500, description = "Something went wrong", body = response::RefreshTokenResponse) (status = 500, description = "Something went wrong", body = response::RefreshTokenResponse)
) )
@@ -388,13 +388,12 @@ pub async fn refresh_token(
if token_stuff::verify_token(&key, &payload.access_token) { if token_stuff::verify_token(&key, &payload.access_token) {
match token_stuff::extract_id_from_token(&key, &payload.access_token) { match token_stuff::extract_id_from_token(&key, &payload.access_token) {
Ok(id) => { Ok(id) => {
let generate_service_token = let generate_service_token = |id, key| -> (Option<String>, Option<i64>) {
|id, key| -> (Option<String>, Option<i64>) { match token_stuff::create_service_refresh_token(key, id) {
match token_stuff::create_service_refresh_token(key, id) { Ok((token, issued)) => (Some(token), Some(issued)),
Ok((token, issued)) => (Some(token), Some(issued)), Err(_err) => (None, None),
Err(_err) => (None, None), }
} };
};
let mut response = response::RefreshTokenResponse { let mut response = response::RefreshTokenResponse {
message: String::new(), message: String::new(),
@@ -407,9 +406,8 @@ pub async fn refresh_token(
match refresh_token { match refresh_token {
Some(token) => match issued { Some(token) => match issued {
Some(issued_at) => { Some(issued_at) => {
response.message = String::from( response.message =
super::messages::SUCCESSFUL_MESSAGE, String::from(super::messages::SUCCESSFUL_MESSAGE);
);
let lr = textsender_models::token::LoginResult { let lr = textsender_models::token::LoginResult {
user_id: id, user_id: id,
access_token: token, access_token: token,
@@ -433,9 +431,7 @@ pub async fn refresh_token(
None => ( None => (
axum::http::StatusCode::INTERNAL_SERVER_ERROR, axum::http::StatusCode::INTERNAL_SERVER_ERROR,
axum::Json(response::RefreshTokenResponse { axum::Json(response::RefreshTokenResponse {
message: String::from( message: String::from("Refresh token not generated"),
"Refresh token not generated",
),
data: Vec::new(), data: Vec::new(),
}), }),
), ),
@@ -445,40 +441,42 @@ pub async fn refresh_token(
println!("Unable to find user, checking service user: {err:?}"); println!("Unable to find user, checking service user: {err:?}");
match repo::service::get(&pool, &id).await { match repo::service::get(&pool, &id).await {
Ok(_service_user) => { Ok(_service_user) => {
let (refresh_token, issued) = let (refresh_token, issued) = generate_service_token(&id, &key);
generate_service_token(&id, &key);
match refresh_token { match refresh_token {
Some(token) => { Some(token) => match issued {
match issued { Some(issued_at) => {
Some(issued_at) => { response.message = String::from(
response.message = String::from(super::messages::SUCCESSFUL_MESSAGE); super::messages::SUCCESSFUL_MESSAGE,
let lr = textsender_models::token::LoginResult { );
user_id: id, let lr = textsender_models::token::LoginResult {
access_token: token, user_id: id,
issued_at, access_token: token,
token_type: String::from( issued_at,
textsender_models::token::TOKEN_TYPE, token_type: String::from(
), textsender_models::token::TOKEN_TYPE,
..Default::default() ),
}; ..Default::default()
response.data.push(lr); };
(axum::http::StatusCode::OK, response.data.push(lr);
axum::Json(response)) (axum::http::StatusCode::OK, axum::Json(response))
}
None => {
(axum::http::StatusCode::INTERNAL_SERVER_ERROR, axum::Json(response::RefreshTokenResponse {
message: String::from("Issued at not returned"),
data: Vec::new()
}))
}
} }
} None => (
None => { axum::http::StatusCode::INTERNAL_SERVER_ERROR,
(axum::http::StatusCode::INTERNAL_SERVER_ERROR, axum::Json(response::RefreshTokenResponse { axum::Json(response::RefreshTokenResponse {
message: String::from("Refresh token not generated"), message: String::from("Issued at not returned"),
data: Vec::new() data: Vec::new(),
})) }),
} ),
},
None => (
axum::http::StatusCode::INTERNAL_SERVER_ERROR,
axum::Json(response::RefreshTokenResponse {
message: String::from(
"Refresh token not generated",
),
data: Vec::new(),
}),
),
} }
} }
Err(err) => ( Err(err) => (
@@ -487,133 +485,27 @@ pub async fn refresh_token(
message: err.to_string(), message: err.to_string(),
data: Vec::new(), data: Vec::new(),
}), }),
) ),
} }
} }
} }
} }
Err(err) => { Err(err) => (
(axum::http::StatusCode::INTERNAL_SERVER_ERROR, axum::Json(response::RefreshTokenResponse { axum::http::StatusCode::INTERNAL_SERVER_ERROR,
axum::Json(response::RefreshTokenResponse {
message: err.to_string(), message: err.to_string(),
data: Vec::new() data: Vec::new(),
})) }),
} ),
} }
} else { } else {
(axum::http::StatusCode::INTERNAL_SERVER_ERROR, axum::Json(response::RefreshTokenResponse { (
message: String::from("Unable to verify token"),
data: Vec::new()
}))
}
/*
match repo::service::exists(&pool, &payload.username).await {
Ok(exists) => {
if !exists {
println!("User does not exists");
(
axum::http::StatusCode::INTERNAL_SERVER_ERROR,
axum::Json(response::ServiceUserLoginResponse {
message: String::from("Unable to login"),
data: Vec::new(),
}),
)
} else {
println!("Good to create");
let service_user =
match repo::service::get_with_username(&pool, &payload.username).await {
Ok(service_user) => service_user,
Err(err) => {
eprintln!("Error: {err:?}");
return (
axum::http::StatusCode::INTERNAL_SERVER_ERROR,
axum::Json(response::ServiceUserLoginResponse {
message: String::from("Unable to login"),
data: Vec::new(),
}),
);
}
};
println!("Service user: {service_user:?}");
println!("Payload: {:?}", payload.passphrase);
let hashed_password = service_user.passphrase.clone();
println!("Hash password: {hashed_password:?}");
match hashing::verify_password(&payload.passphrase, hashed_password) {
Ok(matches) => {
if matches {
// Create token
println!("Creating token");
let key = textsender_models::envy::environment::get_secret_key()
.await
.value;
let (token_literal, duration) =
token_stuff::create_token(&key, &service_user.id).unwrap();
if token_stuff::verify_token(&key, &token_literal) {
let current_time = time::OffsetDateTime::now_utc();
let _ = repo::service::update_last_login(
&pool,
&service_user,
&current_time,
)
.await;
(
axum::http::StatusCode::OK,
axum::Json(response::ServiceUserLoginResponse {
message: String::from(
super::messages::SUCCESSFUL_MESSAGE,
),
data: vec![textsender_models::token::LoginResult {
user_id: service_user.id,
access_token: token_literal,
token_type: String::from(
textsender_models::token::TOKEN_TYPE,
),
issued_at: duration,
..Default::default()
}],
}),
)
} else {
eprintln!("Invalid token");
(
axum::http::StatusCode::INTERNAL_SERVER_ERROR,
axum::Json(response::ServiceUserLoginResponse {
message: String::from("Invalid attempt"),
data: Vec::new(),
}),
)
}
} else {
eprintln!("No match");
(
axum::http::StatusCode::INTERNAL_SERVER_ERROR,
axum::Json(response::ServiceUserLoginResponse {
message: String::from("Invalid attempt"),
data: Vec::new(),
}),
)
}
}
Err(err) => (
axum::http::StatusCode::INTERNAL_SERVER_ERROR,
axum::Json(response::ServiceUserLoginResponse {
message: err.to_string(),
data: Vec::new(),
}),
),
}
}
}
Err(err) => (
axum::http::StatusCode::INTERNAL_SERVER_ERROR, axum::http::StatusCode::INTERNAL_SERVER_ERROR,
axum::Json(response::ServiceUserLoginResponse { axum::Json(response::RefreshTokenResponse {
message: err.to_string(), message: String::from("Unable to verify token"),
data: Vec::new(), data: Vec::new(),
}), }),
), )
} }
*/
} }
} }