diff --git a/src/callers/login.rs b/src/callers/login.rs index e58e077..efa1144 100644 --- a/src/callers/login.rs +++ b/src/callers/login.rs @@ -58,6 +58,10 @@ pub mod endpoint { use super::request; use super::response; + // TODO: At some point, get the username from the DB + // Name of service username when returning a login result + pub const SERVICE_USERNAME: &str = "service"; + async fn not_found(message: &str) -> (StatusCode, Json) { ( StatusCode::NOT_FOUND, @@ -129,7 +133,7 @@ pub mod endpoint { if token_stuff::verify_token(&key, &token_literal) { let login_result = icarus_models::login_result::LoginResult { id, - username: String::from("service"), + username: String::from(SERVICE_USERNAME), token: token_literal, token_type: String::from(icarus_models::token::TOKEN_TYPE), expiration: duration, @@ -172,38 +176,45 @@ pub mod endpoint { // Get passphrase record with id // match repo::service::get_passphrase match token_stuff::extract_id_from_token(&key, &payload.access_token) { - Ok(id) => { - match repo::service::get_passphrase(&pool, &id).await { - Ok((returned_id, _, _)) => { - match token_stuff::create_service_refresh_token(&key, &returned_id) - { - Ok((access_token, exp_dur)) => { - let login_result = - icarus_models::login_result::LoginResult { - id: returned_id, - token: access_token, - expiration: exp_dur, - token_type: String::from( - icarus_models::token::TOKEN_TYPE, - ), - username: String::from("service"), - }; - } - Err(err) => {} + Ok(id) => match repo::service::get_passphrase(&pool, &id).await { + Ok((returned_id, _, _)) => { + match token_stuff::create_service_refresh_token(&key, &returned_id) { + Ok((access_token, exp_dur)) => { + let login_result = icarus_models::login_result::LoginResult { + id: returned_id, + token: access_token, + expiration: exp_dur, + token_type: String::from(icarus_models::token::TOKEN_TYPE), + username: String::from(SERVICE_USERNAME), + }; + response.message = String::from("Successful"); + response.data.push(login_result); + + (axum::http::StatusCode::OK, axum::Json(response)) + } + Err(err) => { + response.message = err.to_string(); + ( + axum::http::StatusCode::INTERNAL_SERVER_ERROR, + axum::Json(response), + ) } - /* - */ - (axum::http::StatusCode::OK, axum::Json(response)) - } - Err(err) => { - response.message = err.to_string(); - (axum::http::StatusCode::OK, axum::Json(response)) } } - } + Err(err) => { + response.message = err.to_string(); + ( + axum::http::StatusCode::INTERNAL_SERVER_ERROR, + axum::Json(response), + ) + } + }, Err(err) => { response.message = err.to_string(); - (axum::http::StatusCode::OK, axum::Json(response)) + ( + axum::http::StatusCode::INTERNAL_SERVER_ERROR, + axum::Json(response), + ) } } } else { diff --git a/src/main.rs b/src/main.rs index 5d8a4ab..06fa5c9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -45,6 +45,10 @@ mod init { callers::endpoints::SERVICE_LOGIN, post(callers::login::endpoint::service_login), ) + .route( + callers::endpoints::REFRESH_TOKEN, + post(callers::login::endpoint::refresh_token), + ) } pub async fn app() -> Router {