Refresh endpoint (#7)
Rust Build / Rustfmt (push) Successful in 1m0s
Rust Build / Check (push) Successful in 1m10s
Rust Build / Clippy (push) Successful in 1m4s
Rust Build / Test Suite (push) Successful in 2m25s
Rust Build / build (push) Successful in 1m41s
Rust Build / Rustfmt (pull_request) Successful in 29s
Rust Build / Test Suite (pull_request) Successful in 1m1s
Rust Build / Check (pull_request) Successful in 1m45s
Rust Build / Clippy (pull_request) Successful in 1m48s
Rust Build / build (pull_request) Successful in 3m11s

Reviewed-on: phoenix/textsender-auth#7
This commit was merged in pull request #7.
This commit is contained in:
2026-06-12 10:51:40 -04:00
parent e5e34b4ad3
commit 8e0b8b737b
8 changed files with 535 additions and 118 deletions
+172
View File
@@ -32,6 +32,11 @@ pub mod request {
}
}
}
#[derive(Deserialize, utoipa::ToSchema)]
pub struct RefreshTokenRequest {
pub access_token: String,
}
}
pub mod response {
@@ -57,6 +62,12 @@ pub mod response {
let _parsed_body: LoginResponse = serde_json::from_slice(&body).unwrap();
todo!("Add code to convert axum::Response to this type");
}
#[derive(Deserialize, Serialize, utoipa::ToSchema)]
pub struct RefreshTokenResponse {
pub message: String,
pub data: Vec<textsender_models::token::LoginResult>,
}
}
/// Endpoint for a user login
@@ -337,3 +348,164 @@ pub async fn service_user_login(
}
}
}
/// Endpoint for service user login
#[utoipa::path(
post,
path = super::endpoints::REFRESH_TOKEN,
request_body(
content = request::RefreshTokenRequest,
description = "Data required refresh token",
content_type = "application/json"
),
responses(
(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)
)
)]
pub async fn refresh_token(
axum::Extension(pool): axum::Extension<sqlx::PgPool>,
axum::Json(payload): axum::Json<request::RefreshTokenRequest>,
) -> (
axum::http::StatusCode,
axum::Json<response::RefreshTokenResponse>,
) {
if payload.access_token.is_empty() {
let reason = String::from("Access token not provided");
(
axum::http::StatusCode::BAD_REQUEST,
axum::Json(response::RefreshTokenResponse {
message: reason,
data: Vec::new(),
}),
)
} else {
let key = textsender_models::envy::environment::get_secret_key()
.await
.value;
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 mut response = response::RefreshTokenResponse {
message: String::new(),
data: Vec::new(),
};
match repo::user::get_with_id(&pool, &id).await {
Ok(_user) => {
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(),
}),
),
},
None => (
axum::http::StatusCode::INTERNAL_SERVER_ERROR,
axum::Json(response::RefreshTokenResponse {
message: String::from("Refresh token not generated"),
data: Vec::new(),
}),
),
}
}
Err(err) => {
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);
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(),
}),
),
},
None => (
axum::http::StatusCode::INTERNAL_SERVER_ERROR,
axum::Json(response::RefreshTokenResponse {
message: String::from(
"Refresh token not generated",
),
data: Vec::new(),
}),
),
}
}
Err(err) => (
axum::http::StatusCode::INTERNAL_SERVER_ERROR,
axum::Json(response::RefreshTokenResponse {
message: err.to_string(),
data: Vec::new(),
}),
),
}
}
}
}
Err(err) => (
axum::http::StatusCode::INTERNAL_SERVER_ERROR,
axum::Json(response::RefreshTokenResponse {
message: err.to_string(),
data: Vec::new(),
}),
),
}
} else {
(
axum::http::StatusCode::INTERNAL_SERVER_ERROR,
axum::Json(response::RefreshTokenResponse {
message: String::from("Unable to verify token"),
data: Vec::new(),
}),
)
}
}
}
+2
View File
@@ -13,4 +13,6 @@ pub mod endpoints {
pub const REGISTER_SERVICE_USER: &str = "/api/v1/service/register";
/// Endpoint constant for service login user
pub const LOGIN_SERVICE_USER: &str = "/api/v1/service/login";
/// Endpoint constant for refresh token
pub const REFRESH_TOKEN: &str = "/api/v1/token/refresh";
}