Refresh endpoint #7

Merged
phoenix merged 8 commits from refresh_token into main 2026-06-12 10:51:40 -04:00
Showing only changes of commit ffd87f7226 - Show all commits
+56 -164
View File
@@ -63,7 +63,7 @@ pub mod response {
todo!("Add code to convert axum::Response to this type");
}
#[derive(Deserialize, utoipa::ToSchema)]
#[derive(Deserialize, Serialize, utoipa::ToSchema)]
pub struct RefreshTokenResponse {
pub message: String,
pub data: Vec<textsender_models::token::LoginResult>,
@@ -359,7 +359,7 @@ pub async fn service_user_login(
content_type = "application/json"
),
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 = 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) {
match token_stuff::extract_id_from_token(&key, &payload.access_token) {
Ok(id) => {
let generate_service_token =
|id, key| -> (Option<String>, Option<i64>) {
match token_stuff::create_service_refresh_token(key, id) {
Ok((token, issued)) => (Some(token), Some(issued)),
Err(_err) => (None, None),
}
};
let generate_service_token = |id, key| -> (Option<String>, Option<i64>) {
match token_stuff::create_service_refresh_token(key, id) {
Ok((token, issued)) => (Some(token), Some(issued)),
Err(_err) => (None, None),
}
};
let mut response = response::RefreshTokenResponse {
message: String::new(),
@@ -407,9 +406,8 @@ pub async fn refresh_token(
match refresh_token {
Some(token) => match issued {
Some(issued_at) => {
response.message = String::from(
super::messages::SUCCESSFUL_MESSAGE,
);
response.message =
String::from(super::messages::SUCCESSFUL_MESSAGE);
let lr = textsender_models::token::LoginResult {
user_id: id,
access_token: token,
@@ -433,9 +431,7 @@ pub async fn refresh_token(
None => (
axum::http::StatusCode::INTERNAL_SERVER_ERROR,
axum::Json(response::RefreshTokenResponse {
message: String::from(
"Refresh token not generated",
),
message: String::from("Refresh token not generated"),
data: Vec::new(),
}),
),
@@ -445,40 +441,42 @@ pub async fn refresh_token(
println!("Unable to find user, checking service user: {err:?}");
match repo::service::get(&pool, &id).await {
Ok(_service_user) => {
let (refresh_token, issued) =
generate_service_token(&id, &key);
let (refresh_token, issued) = generate_service_token(&id, &key);
match refresh_token {
Some(token) => {
match issued {
Some(issued_at) => {
response.message = String::from(super::messages::SUCCESSFUL_MESSAGE);
let lr = textsender_models::token::LoginResult {
user_id: id,
access_token: token,
issued_at,
token_type: String::from(
textsender_models::token::TOKEN_TYPE,
),
..Default::default()
};
response.data.push(lr);
(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()
}))
}
Some(token) => match issued {
Some(issued_at) => {
response.message = String::from(
super::messages::SUCCESSFUL_MESSAGE,
);
let lr = textsender_models::token::LoginResult {
user_id: id,
access_token: token,
issued_at,
token_type: String::from(
textsender_models::token::TOKEN_TYPE,
),
..Default::default()
};
response.data.push(lr);
(axum::http::StatusCode::OK, axum::Json(response))
}
}
None => {
(axum::http::StatusCode::INTERNAL_SERVER_ERROR, axum::Json(response::RefreshTokenResponse {
message: String::from("Refresh token not generated"),
data: Vec::new()
}))
}
None => (
axum::http::StatusCode::INTERNAL_SERVER_ERROR,
axum::Json(response::RefreshTokenResponse {
message: String::from("Issued at not returned"),
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) => (
@@ -487,133 +485,27 @@ pub async fn refresh_token(
message: err.to_string(),
data: Vec::new(),
}),
)
),
}
}
}
}
Err(err) => {
(axum::http::StatusCode::INTERNAL_SERVER_ERROR, axum::Json(response::RefreshTokenResponse {
Err(err) => (
axum::http::StatusCode::INTERNAL_SERVER_ERROR,
axum::Json(response::RefreshTokenResponse {
message: err.to_string(),
data: Vec::new()
}))
}
data: Vec::new(),
}),
),
}
} 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::Json(response::ServiceUserLoginResponse {
message: err.to_string(),
axum::Json(response::RefreshTokenResponse {
message: String::from("Unable to verify token"),
data: Vec::new(),
}),
),
)
}
*/
}
}