Compare commits

..
Author SHA1 Message Date
phoenix 137398405f Code refactoring (#32)
Release Tagging / release (push) Successful in 52s
Rust Build / Check (push) Successful in 1m9s
Rust Build / Rustfmt (push) Successful in 30s
Rust Build / Test Suite (push) Successful in 1m29s
Rust Build / Clippy (push) Successful in 1m2s
Rust Build / build (push) Successful in 57s
Reviewed-on: phoenix/textsender_models#32
2026-07-02 13:18:41 -04:00
phoenix 6508c6c535 tsk-30: Remove async from envy emodukle (#31)
Release Tagging / release (push) Successful in 58s
Rust Build / Check (push) Successful in 1m9s
Rust Build / Test Suite (push) Successful in 1m21s
Rust Build / Rustfmt (push) Successful in 46s
Rust Build / Clippy (push) Successful in 1m3s
Rust Build / build (push) Successful in 1m16s
Closes #30

Reviewed-on: phoenix/textsender_models#31
2026-07-01 23:00:39 -04:00
phoenix c1d391939b Update models (#29)
Release Tagging / release (push) Successful in 48s
Rust Build / Rustfmt (push) Successful in 35s
Rust Build / Test Suite (push) Successful in 1m26s
Rust Build / Clippy (push) Successful in 1m27s
Rust Build / build (push) Successful in 1m29s
Rust Build / Check (push) Successful in 1m12s
textsender_models PR / Rustfmt (pull_request) Successful in 54s
textsender_models PR / Check (pull_request) Successful in 1m20s
Release Tagging / release (pull_request) Successful in 37s
textsender_models PR / Clippy (pull_request) Successful in 1m5s
Reviewed-on: phoenix/textsender_models#29
2026-06-27 17:20:20 -04:00
9 changed files with 316 additions and 491 deletions
Generated
+156 -431
View File
File diff suppressed because it is too large Load Diff
+2 -2
View File
@@ -1,6 +1,6 @@
[package]
name = "textsender_models"
version = "0.4.10"
version = "0.5.0"
edition = "2024"
rust-version = "1.96"
description = "Models used for the textsender project"
@@ -9,7 +9,7 @@ description = "Models used for the textsender project"
serde = { version = "1.0.228", features = ["derive"] }
serde_json = { version = "1.0.150" }
time = { version = "0.3.49", features = ["formatting", "macros", "parsing", "serde"] }
sqlx = { version = "0.8.6", features = ["postgres", "runtime-tokio-native-tls", "time", "uuid"] }
sqlx = { version = "0.9.0", features = ["runtime-tokio", "tls-native-tls", "postgres", "time", "uuid"] }
uuid = { version = "1.23.3", features = ["v4", "serde"] }
dotenvy = { version = "0.15.7" }
const_format = { version = "0.2.36" }
+5 -6
View File
@@ -20,12 +20,11 @@ impl TwilioConfig {
}
}
// TODO: Remove the async at a later point
pub async fn load_config() -> Result<TwilioConfig, std::io::Error> {
let auth_sid_var = crate::envy::environment::get_env("TWILIO_AUTH_SID").await;
let service_sid_var = crate::envy::environment::get_env("TWILIO_SERVICE_SID").await;
let auth_token_var = crate::envy::environment::get_env("TWILIO_AUTH_TOKEN").await;
let phone_number_var = crate::envy::environment::get_env("TWILIO_PHONE_NUMBER").await;
pub fn load_config() -> Result<TwilioConfig, std::io::Error> {
let auth_sid_var = crate::envy::environment::get_env("TWILIO_AUTH_SID");
let service_sid_var = crate::envy::environment::get_env("TWILIO_SERVICE_SID");
let auth_token_var = crate::envy::environment::get_env("TWILIO_AUTH_TOKEN");
let phone_number_var = crate::envy::environment::get_env("TWILIO_PHONE_NUMBER");
if auth_sid_var.value.is_empty()
|| service_sid_var.value.is_empty()
-3
View File
@@ -4,13 +4,10 @@
pub struct Contact {
#[serde(skip_serializing_if = "crate::init::is_uuid_nil")]
pub id: Option<uuid::Uuid>,
// empty?
#[serde(skip_serializing_if = "Option::is_none")]
pub firstname: Option<String>,
// empty?
#[serde(skip_serializing_if = "Option::is_none")]
pub lastname: Option<String>,
// empty?
#[serde(skip_serializing_if = "Option::is_none")]
pub nickname: Option<String>,
pub phone_number: String,
+14 -16
View File
@@ -1,6 +1,4 @@
// TODO: Functions does not need to be async
pub async fn get_db_url() -> crate::envy::EnvVar {
pub fn get_db_url() -> crate::envy::EnvVar {
dotenvy::dotenv().ok();
let key = crate::envy::keys::DB_URL;
let value = std::env::var(key).expect(key);
@@ -8,7 +6,7 @@ pub async fn get_db_url() -> crate::envy::EnvVar {
crate::envy::init_envvar(key, &value)
}
pub async fn get_secret_main_key() -> crate::envy::EnvVar {
pub fn get_secret_main_key() -> crate::envy::EnvVar {
dotenvy::dotenv().ok();
let key = crate::envy::keys::SECRET_MAIN_KEY;
let value = std::env::var(key).expect(key);
@@ -16,7 +14,7 @@ pub async fn get_secret_main_key() -> crate::envy::EnvVar {
crate::envy::init_envvar(key, &value)
}
pub async fn get_service_passphrase() -> crate::envy::EnvVar {
pub fn get_service_passphrase() -> crate::envy::EnvVar {
dotenvy::dotenv().ok();
let key = crate::envy::keys::SERVICE_PASSPHRASE;
let value = std::env::var(key).expect(key);
@@ -24,7 +22,7 @@ pub async fn get_service_passphrase() -> crate::envy::EnvVar {
crate::envy::init_envvar(key, &value)
}
pub async fn get_secret_key() -> crate::envy::EnvVar {
pub fn get_secret_key() -> crate::envy::EnvVar {
dotenvy::dotenv().ok();
let key = crate::envy::keys::SECRET_KEY;
let value = std::env::var(key).expect(key);
@@ -32,7 +30,7 @@ pub async fn get_secret_key() -> crate::envy::EnvVar {
crate::envy::init_envvar(key, &value)
}
pub async fn get_root_directory() -> crate::envy::EnvVar {
pub fn get_root_directory() -> crate::envy::EnvVar {
dotenvy::dotenv().ok();
let key = crate::envy::keys::ROOT_DIRECTORY;
let value = std::env::var(key).expect(key);
@@ -40,7 +38,7 @@ pub async fn get_root_directory() -> crate::envy::EnvVar {
crate::envy::init_envvar(key, &value)
}
pub async fn get_app_base_api_url() -> crate::envy::EnvVar {
pub fn get_app_base_api_url() -> crate::envy::EnvVar {
dotenvy::dotenv().ok();
let key = crate::envy::keys::TEXTSENDER_BASE_API_URL;
let value = std::env::var(key).expect(key);
@@ -48,7 +46,7 @@ pub async fn get_app_base_api_url() -> crate::envy::EnvVar {
crate::envy::init_envvar(key, &value)
}
pub async fn get_app_auth_base_api_url() -> crate::envy::EnvVar {
pub fn get_app_auth_base_api_url() -> crate::envy::EnvVar {
dotenvy::dotenv().ok();
let key = crate::envy::keys::TEXTSENDER_AUTH_BASE_API_URL;
let value = std::env::var(key).expect(key);
@@ -56,7 +54,7 @@ pub async fn get_app_auth_base_api_url() -> crate::envy::EnvVar {
crate::envy::init_envvar(key, &value)
}
pub async fn get_app_env() -> crate::envy::EnvVar {
pub fn get_app_env() -> crate::envy::EnvVar {
dotenvy::dotenv().ok();
let key = crate::envy::keys::APP_ENV;
let value = std::env::var(key).expect(key);
@@ -64,14 +62,14 @@ pub async fn get_app_env() -> crate::envy::EnvVar {
crate::envy::init_envvar(key, &value)
}
pub async fn get_backend_port() -> crate::envy::EnvVar {
pub fn get_backend_port() -> crate::envy::EnvVar {
dotenvy::dotenv().ok();
let key = crate::envy::keys::BACKEND_PORT;
let value = std::env::var(key).expect(key);
crate::envy::init_envvar(key, &value)
}
pub async fn get_frontend_url() -> crate::envy::EnvVar {
pub fn get_frontend_url() -> crate::envy::EnvVar {
dotenvy::dotenv().ok();
let key = crate::envy::keys::FRONTEND_URL;
let value = std::env::var(key).expect(key);
@@ -79,7 +77,7 @@ pub async fn get_frontend_url() -> crate::envy::EnvVar {
crate::envy::init_envvar(key, &value)
}
pub async fn get_rust_log() -> crate::envy::EnvVar {
pub fn get_rust_log() -> crate::envy::EnvVar {
dotenvy::dotenv().ok();
let key = crate::envy::keys::RUST_LOG;
let value = std::env::var(key).expect(key);
@@ -87,7 +85,7 @@ pub async fn get_rust_log() -> crate::envy::EnvVar {
crate::envy::init_envvar(key, &value)
}
pub async fn get_allowed_origins() -> crate::envy::EnvVar {
pub fn get_allowed_origins() -> crate::envy::EnvVar {
dotenvy::dotenv().ok();
let key = crate::envy::keys::ALLOWED_ORIGINS;
let value = std::env::var(key).expect(key);
@@ -98,8 +96,8 @@ pub async fn get_allowed_origins() -> crate::envy::EnvVar {
envvar
}
/// Get environment not specified in the code
pub async fn get_env(environment: &str) -> crate::envy::EnvVar {
/// Get environment not specified in the library
pub fn get_env(environment: &str) -> crate::envy::EnvVar {
dotenvy::dotenv().ok();
let my_error = format!("{environment} {}", crate::envy::keys::error::GENERAL_ERROR);
let value = std::env::var(environment).expect(&my_error);
+25 -7
View File
@@ -20,16 +20,17 @@ pub fn init_envvar(key: &str, value: &str) -> EnvVar {
}
pub fn init_delimiter(envvar: &mut EnvVar, delimiter: char) {
let mut amount_of_delimiters_found: i32 = 0;
for v in envvar.value.chars() {
if v == delimiter {
amount_of_delimiters_found += 1;
let amount_of_delimiters_found: i32 = {
let mut count = 0;
for v in envvar.value.chars() {
if v == delimiter {
count += 1;
}
}
}
count
};
let has_delimiter = amount_of_delimiters_found >= 1;
if has_delimiter {
envvar.has_delimiter = has_delimiter;
envvar.delimiter = delimiter;
@@ -37,3 +38,20 @@ pub fn init_delimiter(envvar: &mut EnvVar, delimiter: char) {
envvar.has_delimiter = has_delimiter;
}
}
#[cfg(test)]
mod tests {
#[test]
fn test_init_delimiter() {
let delimiter: char = ',';
let value = format!("red,green,white,black");
let mut env_var = super::EnvVar {
key: String::from("COLORS"),
value: value.clone(),
..Default::default()
};
super::init_delimiter(&mut env_var, delimiter);
assert_eq!(value, env_var.value, "Colors do not match");
}
}
+41 -3
View File
@@ -2,14 +2,52 @@
/// extract it into some strings
pub fn delimitize(var: &crate::envy::EnvVar) -> Result<Vec<String>, std::io::Error> {
if var.has_delimiter {
Ok(var
let values: Vec<String> = match var
.value
.split(var.delimiter)
.map(|c| c.parse::<String>().unwrap())
.collect())
.map(|c| c.parse::<String>())
.collect()
{
Ok(v) => v,
Err(err) => {
return Err(std::io::Error::other(err));
}
};
Ok(values)
} else {
Err(std::io::Error::other(
"Environment variable does not have a delimiter",
))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_delimitize() {
let envvar = crate::envy::EnvVar {
key: "OCEANS".to_string(),
value: "Atlantic|Pacific|Indian|Arctic".to_string(),
has_delimiter: true,
delimiter: '|',
};
let all_values = vec![
"Atlantic".to_string(),
"Pacific".to_string(),
"Indian".to_string(),
"Arctic".to_string(),
];
match delimitize(&envvar) {
Ok(values) => {
assert_eq!(values, all_values, "Failure in delimitizing EnvVar values");
}
Err(err) => {
assert!(false, "Error delimitizing: {err:?}");
}
}
}
}
+73 -19
View File
@@ -78,17 +78,57 @@ pub struct CreateTokenResult {
}
pub fn create_token(
key: &String,
token_resource: &TokenResource,
key: &str,
token_resource: TokenResource,
duration: time::Duration,
) -> Result<CreateTokenResult, josekit::JoseError> {
let mut header = josekit::jws::JwsHeader::new();
header.set_token_type(TOKEN_TYPE);
match init_payload(token_resource, duration) {
Ok((payload, issued, expire)) => {
let signer = match josekit::jws::alg::hmac::HmacJwsAlgorithm::Hs256
.signer_from_bytes(key.as_bytes())
{
Ok(signer) => signer,
Err(err) => {
return Err(err);
}
};
let access_token = match josekit::jwt::encode_with_signer(&payload, &header, &signer) {
Ok(access_token) => access_token,
Err(err) => {
return Err(err);
}
};
Ok(CreateTokenResult {
access_token,
issued: issued.unix_timestamp(),
expires_in: expire.unix_timestamp(),
token_issued: issued,
})
}
Err(e) => Err(josekit::JoseError::InvalidClaim(e.into())),
}
}
fn init_payload(
token_resource: TokenResource,
duration: time::Duration,
) -> Result<
(
josekit::jwt::JwtPayload,
time::OffsetDateTime,
time::OffsetDateTime,
),
josekit::JoseError,
> {
let mut payload = josekit::jwt::JwtPayload::new();
payload.set_subject(&token_resource.message);
payload.set_issuer(&token_resource.issuer);
payload.set_audience(token_resource.audiences.clone());
payload.set_subject(token_resource.message);
payload.set_issuer(token_resource.issuer);
payload.set_audience(token_resource.audiences);
if !token_resource.user_id.is_nil() {
match payload.set_claim("user_id", Some(serde_json::json!(token_resource.user_id))) {
Ok(_) => {}
@@ -97,24 +137,38 @@ pub fn create_token(
}
}
}
match init_payload_issued(&mut payload, duration) {
Ok((issued, expire)) => Ok((payload, issued, expire)),
Err(err) => Err(err),
}
}
fn init_payload_issued(
payload: &mut josekit::jwt::JwtPayload,
duration: time::Duration,
) -> Result<(time::OffsetDateTime, time::OffsetDateTime), josekit::JoseError> {
match get_issued() {
Ok(issued) => {
let expire = issued + duration;
payload.set_issued_at(&util::time_to_std_time(&issued).unwrap());
payload.set_expires_at(&util::time_to_std_time(&expire).unwrap());
let issued_at = match util::time_to_std_time(&issued) {
Ok(issued_at) => issued_at,
Err(err) => {
return Err(josekit::JoseError::InvalidClaim(err.into()));
}
};
let expires_at = match util::time_to_std_time(&expire) {
Ok(expires_at) => expires_at,
Err(err) => {
return Err(josekit::JoseError::InvalidClaim(err.into()));
}
};
let signer = josekit::jws::alg::hmac::HmacJwsAlgorithm::Hs256
.signer_from_bytes(key.as_bytes())
.unwrap();
Ok(CreateTokenResult {
access_token: josekit::jwt::encode_with_signer(&payload, &header, &signer).unwrap(),
issued: issued.unix_timestamp(),
expires_in: expire.unix_timestamp(),
token_issued: issued,
})
payload.set_issued_at(&issued_at);
payload.set_expires_at(&expires_at);
Ok((issued, expire))
}
Err(e) => Err(josekit::JoseError::InvalidClaim(e.into())),
Err(err) => Err(josekit::JoseError::InvalidClaim(err.into())),
}
}
@@ -138,7 +192,7 @@ mod tests {
let token_duration = time::Duration::minutes(30);
let key = String::from(TEST_KEY);
match super::create_token(&key, &token_resource, token_duration) {
match super::create_token(&key, token_resource, token_duration) {
Ok(cst) => match time::OffsetDateTime::from_unix_timestamp(cst.issued) {
Ok(d_result) => {
let they_match = {
-4
View File
@@ -6,9 +6,7 @@ pub struct User {
pub phone_number: String,
pub username: String,
pub password: String,
// Firstname is a pointer
pub firstname: String,
// Lastname is a pointer
pub lastname: String,
#[serde(with = "time::serde::rfc3339::option")]
pub created: Option<time::OffsetDateTime>,
@@ -27,7 +25,6 @@ pub struct ServiceUser {
pub passphrase: String,
#[serde(with = "time::serde::rfc3339::option")]
pub created: Option<time::OffsetDateTime>,
// When serializing to json, if empty do not populate
#[serde(with = "time::serde::rfc3339::option")]
pub last_login: Option<time::OffsetDateTime>,
#[serde(skip)]
@@ -81,7 +78,6 @@ pub struct UserProfile {
#[derive(Clone, Debug, Default, serde::Deserialize, serde::Serialize, sqlx::FromRow)]
pub struct Salt {
// #[serde(skip_serializing_if = "crate::init::is_uuid_nil")]
pub id: uuid::Uuid,
#[serde(skip_serializing_if = "String::is_empty")]
pub salt: String,