Update rust #20

Merged
phoenix merged 12 commits from update_rust into main 2026-07-06 22:03:17 -04:00
5 changed files with 33 additions and 25 deletions
Showing only changes of commit 2c0011cf6c - Show all commits
+23 -9
View File
@@ -96,11 +96,20 @@ pub mod response {
pub async fn extract(
response: axum::response::Response,
) -> Result<LoginResponse, std::io::Error> {
let body = axum::body::to_bytes(response.into_body(), usize::MAX)
.await
.unwrap();
let _parsed_body: LoginResponse = serde_json::from_slice(&body).unwrap();
todo!("Add code to convert axum::Response to this type");
let body = match axum::body::to_bytes(response.into_body(), usize::MAX)
.await {
Ok(body) => body,
Err(err) => {
return Err(std::io::Error::other(err));
}
};
let parsed_body: LoginResponse = match serde_json::from_slice(&body) {
Ok(body) => body,
Err(err) => {
return Err(std::io::Error::other(err));
}
};
Ok(parsed_body)
}
#[derive(Deserialize, Serialize, utoipa::ToSchema)]
@@ -193,9 +202,16 @@ pub async fn user_login(
if matches {
// Create token
let key = textsender_models::envy::environment::get_secret_key()
.await
.value;
let cst = token_stuff::create_token(&key, &user.id).unwrap();
let cst = match token_stuff::create_token(&key, &user.id) {
Ok(token) => token,
Err(_err) => {
return (axum::http::StatusCode::INTERNAL_SERVER_ERROR, axum::Json(response::LoginResponse {
message: String::from("Error creating token"),
data: Vec::new()
}));
}
};
if token_stuff::verify_token(&key, &cst.access_token) {
let current_time = time::OffsetDateTime::now_utc();
@@ -333,7 +349,6 @@ pub async fn service_user_login(
// Create token
println!("Creating token");
let key = textsender_models::envy::environment::get_secret_key()
.await
.value;
let cst =
token_stuff::create_token(&key, &service_user.id).unwrap();
@@ -440,7 +455,6 @@ pub async fn refresh_token(
)
} 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) {
+2 -4
View File
@@ -80,7 +80,7 @@ pub fn generate_the_salt() -> (
responses(
(status = 201, description = "User created", body = response::Response),
(status = 404, description = "User already exists", body = response::Response),
(status = 400, description = "Issue creating user", body = response::Response)
(status = 500, description = "Issue creating user", body = response::Response)
)
)]
pub async fn register_user(
@@ -105,9 +105,7 @@ pub async fn register_user(
let mut user = textsender_models::user::User {
username: payload.username.clone(),
password: payload.password.clone(),
// email: payload.email.clone(),
phone_number: payload.phone_number.clone(),
// email_verified: true,
..Default::default()
};
@@ -183,7 +181,7 @@ pub async fn register_user(
/// Checks to see if registration is enabled
async fn is_registration_enabled() -> Result<bool, std::io::Error> {
let key = String::from("ENABLE_REGISTRATION");
let var = textsender_models::envy::environment::get_env(&key).await;
let var = textsender_models::envy::environment::get_env(&key);
let parsed_value = var.value.to_uppercase();
if parsed_value == "TRUE" {
-1
View File
@@ -2,7 +2,6 @@ use sqlx::postgres::PgPoolOptions;
pub async fn create_pool() -> Result<sqlx::PgPool, sqlx::Error> {
let database_url = textsender_models::envy::environment::get_db_url()
.await
.value;
println!("Database url: {database_url}");
+3 -5
View File
@@ -38,7 +38,6 @@ pub mod init {
mod cors {
pub async fn configure_cors() -> tower_http::cors::CorsLayer {
// Start building the CORS layer with common settings
let cors = tower_http::cors::CorsLayer::new()
.allow_methods([
axum::http::Method::GET,
@@ -46,11 +45,11 @@ pub mod init {
axum::http::Method::POST,
axum::http::Method::PUT,
axum::http::Method::DELETE,
]) // Specify allowed methods:cite[2]
])
.allow_headers([
axum::http::header::CONTENT_TYPE,
axum::http::header::AUTHORIZATION,
]) // Specify allowed headers:cite[2]
])
.allow_credentials(true) // If you need to send cookies or authentication headers:cite[2]
.max_age(std::time::Duration::from_secs(3600)); // Cache the preflight response for 1 hour:cite[2]
@@ -58,7 +57,7 @@ pub mod init {
match std::env::var(textsender_models::envy::keys::APP_ENV).as_deref() {
Ok("production") => {
let allowed_origins_env =
textsender_models::envy::environment::get_allowed_origins().await;
textsender_models::envy::environment::get_allowed_origins();
match textsender_models::envy::utility::delimitize(&allowed_origins_env) {
Ok(alwd) => {
let allowed_origins: Vec<axum::http::HeaderValue> = alwd
@@ -93,7 +92,6 @@ pub mod init {
}
pub async fn routes() -> Router {
// build our application with a route
Router::new()
.route(
callers::endpoints::DBTEST,
+5 -6
View File
@@ -26,7 +26,7 @@ pub fn create_token(
audiences: vec![String::from(AUDIENCE)],
user_id: *id,
};
textsender_models::token::create_token(provided_key, &resource, time::Duration::hours(4))
textsender_models::token::create_token(provided_key, resource, time::Duration::hours(4))
}
pub fn create_service_token(
@@ -39,7 +39,7 @@ pub fn create_service_token(
audiences: vec![String::from(AUDIENCE)],
user_id: *id,
};
textsender_models::token::create_token(provided, &resource, time::Duration::hours(1))
textsender_models::token::create_token(provided, resource, time::Duration::hours(1))
}
pub fn create_service_refresh_token(
@@ -52,7 +52,7 @@ pub fn create_service_refresh_token(
audiences: vec![String::from(AUDIENCE)],
user_id: *id,
};
textsender_models::token::create_token(key, &resource, time::Duration::hours(4))
textsender_models::token::create_token(key, resource, time::Duration::hours(4))
}
pub fn verify_token(key: &String, token: &String) -> bool {
@@ -119,9 +119,8 @@ mod tests {
#[test]
fn test_tokenize() {
let rt = tokio::runtime::Runtime::new().unwrap();
let special_key = rt
.block_on(textsender_models::envy::environment::get_secret_key())
let special_key =
textsender_models::envy::environment::get_secret_key()
.value;
let id = uuid::Uuid::new_v4();
match create_token(&special_key, &id) {