Compare commits

..
Author SHA1 Message Date
phoenix 403a941ece cargo update
textsender_models PR / Rustfmt (pull_request) Successful in 1m1s
textsender_models PR / Clippy (pull_request) Successful in 1m14s
Release Tagging / release (pull_request) Successful in 27s
textsender_models PR / Check (pull_request) Successful in 1m42s
2026-07-01 22:58:27 -04:00
phoenix 904e50f981 Some refactoring
textsender_models PR / Rustfmt (pull_request) Successful in 32s
textsender_models PR / Check (pull_request) Has been cancelled
textsender_models PR / Clippy (pull_request) Has been cancelled
Release Tagging / release (pull_request) Successful in 35s
2026-07-01 22:57:45 -04:00
phoenix 6b84cc42d6 Removing async from functions
textsender_models PR / Rustfmt (pull_request) Successful in 35s
Release Tagging / release (pull_request) Successful in 39s
textsender_models PR / Clippy (pull_request) Successful in 1m23s
textsender_models PR / Check (pull_request) Successful in 1m29s
2026-07-01 22:53:09 -04:00
phoenix b25b651241 bump: textsender_models
textsender_models PR / Rustfmt (pull_request) Successful in 48s
textsender_models PR / Check (pull_request) Successful in 1m2s
Release Tagging / release (pull_request) Successful in 31s
textsender_models PR / Clippy (pull_request) Successful in 1m38s
2026-07-01 22:49:48 -04:00
phoenix cc6e6286c7 Adding test
textsender_models PR / Rustfmt (pull_request) Successful in 1m9s
textsender_models PR / Check (pull_request) Successful in 1m20s
textsender_models PR / Clippy (pull_request) Successful in 1m27s
Release Tagging / release (pull_request) Successful in 34s
2026-07-01 22:47:36 -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
phoenix 2722af5589 Feature usage (#28)
Release Tagging / release (push) Successful in 51s
Rust Build / Check (push) Successful in 1m7s
Rust Build / Test Suite (push) Successful in 1m9s
Rust Build / Rustfmt (push) Successful in 56s
Rust Build / Clippy (push) Successful in 47s
Rust Build / build (push) Successful in 1m3s
Reviewed-on: phoenix/textsender_models#28
2026-06-14 17:43:09 -04:00
phoenix c09fa36b15 Claim fix (#27)
Release Tagging / release (push) Successful in 35s
Rust Build / Rustfmt (push) Successful in 39s
Rust Build / Test Suite (push) Successful in 1m13s
Rust Build / Check (push) Successful in 1m30s
Rust Build / build (push) Successful in 47s
Rust Build / Clippy (push) Successful in 1m23s
Reviewed-on: phoenix/textsender_models#27
2026-06-13 19:00:22 -04:00
phoenix 8317f41e3c Claim update (#26)
Release Tagging / release (push) Successful in 47s
Rust Build / Check (push) Successful in 56s
Rust Build / Rustfmt (push) Successful in 27s
Rust Build / Test Suite (push) Successful in 1m34s
Rust Build / Clippy (push) Successful in 50s
Rust Build / build (push) Successful in 40s
Release Tagging / release (pull_request) Successful in 46s
Rust Build / Rustfmt (pull_request) Successful in 28s
Rust Build / Check (pull_request) Successful in 1m15s
Rust Build / Test Suite (pull_request) Successful in 1m20s
Rust Build / Clippy (pull_request) Successful in 43s
Rust Build / build (pull_request) Successful in 1m2s
Reviewed-on: phoenix/textsender_models#26
2026-06-13 18:02:25 -04:00
phoenix ba998096cb Contact update (#25)
Release Tagging / release (push) Successful in 52s
Rust Build / Check (push) Successful in 1m4s
Rust Build / Test Suite (push) Successful in 1m7s
Rust Build / Clippy (push) Successful in 34s
Rust Build / build (push) Successful in 51s
Rust Build / Rustfmt (push) Successful in 1m30s
Release Tagging / release (pull_request) Successful in 58s
Rust Build / Test Suite (pull_request) Successful in 1m14s
Rust Build / Check (pull_request) Successful in 1m24s
Rust Build / Rustfmt (pull_request) Successful in 49s
Rust Build / build (pull_request) Successful in 47s
Rust Build / Clippy (pull_request) Successful in 1m6s
Reviewed-on: phoenix/textsender_models#25
2026-06-13 16:25:48 -04:00
phoenix d504108745 Merge pull request 'V0.3.0' (#21) from v0.3.0-migrate into main
Rust Build / Check (push) Successful in 27s
Release Tagging / release (push) Successful in 55s
Rust Build / Test Suite (push) Successful in 52s
Rust Build / Rustfmt (push) Successful in 27s
Rust Build / build (push) Successful in 39s
Rust Build / Clippy (push) Successful in 44s
Rust Build / Check (pull_request) Successful in 52s
Release Tagging / release (pull_request) Successful in 1m1s
Rust Build / Test Suite (pull_request) Successful in 1m8s
Rust Build / Rustfmt (pull_request) Successful in 41s
Rust Build / build (pull_request) Successful in 57s
Rust Build / Clippy (pull_request) Successful in 1m58s
Reviewed-on: phoenix/textsender-models#21
2026-06-07 14:54:19 -04:00
phoenix c6d2d19619 Get ready (#23)
Release Tagging / release (pull_request) Failing after 39s
Rust Build / Check (pull_request) Successful in 39s
Rust Build / Test Suite (pull_request) Successful in 55s
Rust Build / Rustfmt (pull_request) Successful in 1m27s
Rust Build / Clippy (pull_request) Successful in 37s
Rust Build / build (pull_request) Successful in 36s
Reviewed-on: phoenix/textsender-models#23
2026-06-07 14:51:07 -04:00
phoenix 38dbcb09f7 Model changes (#22)
Release Tagging / release (push) Successful in 33s
Rust Build / Test Suite (push) Successful in 27s
Rust Build / Check (push) Successful in 1m3s
Rust Build / Rustfmt (push) Successful in 38s
Rust Build / Clippy (push) Successful in 37s
Rust Build / build (push) Successful in 27s
Release Tagging / release (pull_request) Successful in 2m29s
Rust Build / Check (pull_request) Successful in 3m42s
Rust Build / Test Suite (pull_request) Successful in 1m41s
Rust Build / Clippy (pull_request) Successful in 43s
Rust Build / Rustfmt (pull_request) Successful in 1m40s
Rust Build / build (pull_request) Successful in 41s
Reviewed-on: phoenix/textsender-models#22
2026-06-03 21:34:09 -04:00
phoenix 766d38ea0e Skipping serialization of fields in Contact (#20)
Release Tagging / release (push) Successful in 40s
Rust Build / Test Suite (push) Successful in 26s
Rust Build / Check (push) Successful in 1m28s
Rust Build / Rustfmt (push) Successful in 26s
Rust Build / Clippy (push) Successful in 40s
Rust Build / build (push) Successful in 1m11s
Reviewed-on: phoenix/textsender-models#20
2026-06-01 22:15:17 -04:00
phoenix 2be0337da0 User utoipa (#19)
Rust Build / Check (push) Successful in 36s
Rust Build / Rustfmt (push) Successful in 31s
Rust Build / Test Suite (push) Successful in 41s
Rust Build / build (push) Successful in 31s
Rust Build / Clippy (push) Successful in 45s
Release Tagging / release (push) Successful in 40s
Reviewed-on: phoenix/textsender-models#19
2026-05-31 17:48:09 -04:00
15 changed files with 1704 additions and 309 deletions
+46
View File
@@ -0,0 +1,46 @@
name: textsender_models PR
on:
pull_request:
branches:
- main
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
jobs:
check:
name: Check
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@v5
- uses: actions-rust-lang/setup-rust-toolchain@v1
with:
toolchain: 1.96
- uses: Swatinem/rust-cache@v2
- run: cargo check
fmt:
name: Rustfmt
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@v5
- uses: actions-rust-lang/setup-rust-toolchain@v1
with:
toolchain: 1.96
- uses: Swatinem/rust-cache@v2
- run: rustup component add rustfmt
- run: cargo fmt --all -- --check
clippy:
name: Clippy
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@v5
- uses: actions-rust-lang/setup-rust-toolchain@v1
with:
toolchain: 1.96
- uses: Swatinem/rust-cache@v2
- run: rustup component add clippy
- run: cargo clippy -- -D warnings
+3 -3
View File
@@ -3,10 +3,10 @@ name: Release Tagging
on:
push:
branches:
- v0.3.0-migrate
- main
pull_request:
branches:
- v0.3.0-migrate
- main
jobs:
release:
@@ -20,7 +20,7 @@ jobs:
- name: Install Rust
uses: actions-rs/toolchain@v1
with:
toolchain: 1.95
toolchain: 1.96
components: cargo
- name: Extract Version from Cargo.toml
+15 -9
View File
@@ -3,10 +3,11 @@ name: Rust Build
on:
push:
branches:
- v0.3.0-migrate
pull_request:
branches:
- v0.3.0-migrate
- main
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
jobs:
check:
@@ -16,7 +17,8 @@ jobs:
- uses: actions/checkout@v5
- uses: actions-rust-lang/setup-rust-toolchain@v1
with:
toolchain: 1.95
toolchain: 1.96
- uses: Swatinem/rust-cache@v2
- run: cargo check
test:
@@ -26,7 +28,8 @@ jobs:
- uses: actions/checkout@v5
- uses: actions-rust-lang/setup-rust-toolchain@v1
with:
toolchain: 1.95
toolchain: 1.96
- uses: Swatinem/rust-cache@v2
- run: cargo test
fmt:
@@ -36,7 +39,8 @@ jobs:
- uses: actions/checkout@v5
- uses: actions-rust-lang/setup-rust-toolchain@v1
with:
toolchain: 1.95
toolchain: 1.96
- uses: Swatinem/rust-cache@v2
- run: rustup component add rustfmt
- run: cargo fmt --all -- --check
@@ -47,7 +51,8 @@ jobs:
- uses: actions/checkout@v5
- uses: actions-rust-lang/setup-rust-toolchain@v1
with:
toolchain: 1.95
toolchain: 1.96
- uses: Swatinem/rust-cache@v2
- run: rustup component add clippy
- run: cargo clippy -- -D warnings
@@ -58,5 +63,6 @@ jobs:
- uses: actions/checkout@v5
- uses: actions-rust-lang/setup-rust-toolchain@v1
with:
toolchain: 1.95
toolchain: 1.96
- uses: Swatinem/rust-cache@v2
- run: cargo build
Generated
+1395 -196
View File
File diff suppressed because it is too large Load Diff
+18 -10
View File
@@ -1,21 +1,29 @@
[package]
name = "textsender_models"
version = "0.3.0"
version = "0.4.11"
edition = "2024"
rust-version = "1.95"
description = "models used for the textsender project"
# license = "MIT"
rust-version = "1.96"
description = "Models used for the textsender project"
[dependencies]
serde = { version = "1.0.228", features = ["derive"] }
serde_json = { version = "1.0.149" }
rand = { version = "0.10.1" }
time = { version = "0.3.47", features = ["formatting", "macros", "parsing", "serde"] }
uuid = { version = "1.23.1", features = ["v4", "serde"] }
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"] }
uuid = { version = "1.23.3", features = ["v4", "serde"] }
dotenvy = { version = "0.15.7" }
const_format = { version = "0.2.36" }
josekit = { version = "0.10.3" }
utoipa = { version = "5.4.0", features = ["uuid", "time"] }
utoipa = { version = "5.5.0", features = ["uuid", "time"] }
[features]
default = ["full"]
full = ["user", "message", "contact", "config", "envy", "token"]
user = []
message = []
contact = []
config = []
envy = []
token = []
[dev-dependencies]
tempfile = { version = "3.27.0" }
+24 -4
View File
@@ -1,9 +1,7 @@
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
#[derive(Clone, Debug, Default, serde::Deserialize, serde::Serialize)]
pub struct TwilioConfig {
#[serde(alias = "auth_id")]
pub account_sid: uuid::Uuid,
pub account_sid: String,
#[serde(skip_serializing_if = "String::is_empty")]
pub service_sid: String,
#[serde(skip_serializing_if = "String::is_empty")]
@@ -21,3 +19,25 @@ impl TwilioConfig {
println!("Number: {:?}", self.phone_number);
}
}
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()
|| auth_token_var.value.is_empty()
|| phone_number_var.value.is_empty()
{
Err(std::io::Error::other("Config not set"))
} else {
Ok(TwilioConfig {
service_sid: service_sid_var.value,
auth_token: auth_token_var.value,
account_sid: auth_sid_var.value,
phone_number: phone_number_var.value,
})
}
}
+13 -9
View File
@@ -1,15 +1,19 @@
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
#[derive(
Clone, Debug, Default, serde::Deserialize, serde::Serialize, sqlx::FromRow, utoipa::ToSchema,
)]
pub struct Contact {
pub id: uuid::Uuid,
#[serde(skip_serializing_if = "crate::init::is_uuid_nil")]
pub id: Option<uuid::Uuid>,
// empty?
pub firstname: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub firstname: Option<String>,
// empty?
pub lastname: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub lastname: Option<String>,
// empty?
pub nickname: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub nickname: Option<String>,
pub phone_number: String,
// empty
pub user_id: uuid::Uuid,
#[serde(skip_serializing_if = "crate::init::is_uuid_nil")]
pub user_id: Option<uuid::Uuid>,
}
+14 -14
View File
@@ -1,4 +1,4 @@
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);
@@ -6,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);
@@ -14,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);
@@ -22,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);
@@ -30,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);
@@ -38,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);
@@ -46,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);
@@ -54,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);
@@ -62,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);
@@ -77,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);
@@ -85,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);
@@ -96,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");
}
}
+5 -2
View File
@@ -6,7 +6,10 @@ pub mod token;
pub mod user;
pub mod init {
pub fn is_uuid_nil(uuid: &uuid::Uuid) -> bool {
uuid.is_nil()
pub fn is_uuid_nil(uuid: &Option<uuid::Uuid>) -> bool {
match uuid {
Some(id) => id.is_nil(),
None => true,
}
}
}
+9 -6
View File
@@ -1,14 +1,17 @@
use serde::{Deserialize, Serialize};
/// Message status - Instant
pub const MESSAGE_EVENT_RESPONSE_STATUS_INSTANT: &str = "INSTANT";
/// Message status - Scheduled
pub const MESSAGE_EVENT_RESPONSE_STATUS_SCHEDULED: &str = "SCHEDULED";
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
#[derive(
Clone, Debug, Default, serde::Deserialize, serde::Serialize, sqlx::FromRow, utoipa::ToSchema,
)]
pub struct MessageEventResponse {
pub id: uuid::Uuid,
pub scheduled_message_event_id: uuid::Uuid,
// Json field
pub response: String,
#[serde(skip_serializing_if = "crate::init::is_uuid_nil")]
pub scheduled_message_event_id: Option<uuid::Uuid>,
/// Stores a json response of the sent message
pub response: serde_json::Value,
pub user_id: uuid::Uuid,
pub contact_id: uuid::Uuid,
pub message_id: uuid::Uuid,
+7 -5
View File
@@ -1,11 +1,13 @@
pub mod event;
pub mod scheduling;
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, Default, Deserialize, Serialize, utoipa::ToSchema)]
#[derive(
Clone, Debug, Default, serde::Deserialize, serde::Serialize, sqlx::FromRow, utoipa::ToSchema,
)]
pub struct Message {
pub id: uuid::Uuid,
#[serde(skip_serializing_if = "crate::init::is_uuid_nil")]
pub id: Option<uuid::Uuid>,
pub content: String,
pub user_id: uuid::Uuid,
#[serde(skip_serializing_if = "crate::init::is_uuid_nil")]
pub user_id: Option<uuid::Uuid>,
}
+16 -14
View File
@@ -1,24 +1,26 @@
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, Default, Deserialize, Serialize, utoipa::ToSchema)]
#[derive(
Clone, Debug, Default, serde::Deserialize, serde::Serialize, sqlx::FromRow, utoipa::ToSchema,
)]
pub struct ScheduledMessageEvent {
id: uuid::Uuid,
contact_id: uuid::Uuid,
message_id: uuid::Uuid,
scheduled_message_id: uuid::Uuid,
pub id: uuid::Uuid,
pub contact_id: uuid::Uuid,
pub message_id: uuid::Uuid,
pub scheduled_message_id: uuid::Uuid,
#[serde(with = "time::serde::rfc3339::option")]
created: Option<time::OffsetDateTime>,
pub created: Option<time::OffsetDateTime>,
}
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
#[derive(
Clone, Debug, Default, serde::Deserialize, serde::Serialize, sqlx::FromRow, utoipa::ToSchema,
)]
pub struct ScheduledMessage {
id: uuid::Uuid,
pub id: uuid::Uuid,
#[serde(with = "time::serde::rfc3339::option")]
scheduled: Option<time::OffsetDateTime>,
pub scheduled: Option<time::OffsetDateTime>,
#[serde(with = "time::serde::rfc3339::option")]
created: Option<time::OffsetDateTime>,
status: String,
user_id: uuid::Uuid,
pub created: Option<time::OffsetDateTime>,
pub status: String,
pub user_id: uuid::Uuid,
}
pub const READY: &str = "READY";
+94 -20
View File
@@ -1,18 +1,40 @@
use serde::{Deserialize, Serialize};
use serde::Deserialize;
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
#[derive(Clone, Debug, Default, serde::Deserialize, serde::Serialize)]
pub struct Claims {
pub user_id: uuid::Uuid,
pub role: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub role: Option<String>,
#[serde(alias = "iss")]
pub issued: String,
#[serde(alias = "exp")]
#[serde(deserialize_with = "deserialize_i64_from_f64")]
pub expired: i64,
#[serde(alias = "iat")]
#[serde(deserialize_with = "deserialize_i64_from_f64")]
pub issued_at: i64,
}
#[derive(Clone, Debug, Default, Deserialize, Serialize, utoipa::ToSchema)]
fn deserialize_i64_from_f64<'de, D>(deserializer: D) -> Result<i64, D::Error>
where
D: serde::Deserializer<'de>,
{
let val = f64::deserialize(deserializer)?;
// Handle NaN and infinity cases
if val.is_nan() || val.is_infinite() {
return Err(serde::de::Error::custom("invalid float value"));
}
// Round to nearest integer and convert
let rounded = val.round();
// Check if the rounded value can fit in i64
if rounded < (i64::MIN as f64) || rounded > (i64::MAX as f64) {
Err(serde::de::Error::custom("float out of i64 range"))
} else {
Ok(rounded as i64)
}
}
#[derive(Clone, Debug, Default, serde::Deserialize, serde::Serialize, utoipa::ToSchema)]
pub struct LoginResult {
pub user_id: uuid::Uuid,
pub access_token: String,
@@ -21,7 +43,7 @@ pub struct LoginResult {
pub issued_at: i64,
}
// Alias for LoginResult
/// Alias for LoginResult
pub type Login = LoginResult;
pub fn get_issued() -> time::Result<time::OffsetDateTime> {
@@ -32,7 +54,7 @@ mod util {
pub fn time_to_std_time(
provided_time: &time::OffsetDateTime,
) -> Result<std::time::SystemTime, std::time::SystemTimeError> {
let converted = std::time::SystemTime::from(*provided_time);
let converted: std::time::SystemTime = (*provided_time).into();
Ok(converted)
}
}
@@ -42,28 +64,33 @@ pub struct TokenResource {
pub message: String,
pub issuer: String,
pub audiences: Vec<String>,
pub id: uuid::Uuid,
pub user_id: uuid::Uuid,
}
/// Token type
pub const TOKEN_TYPE: &str = "JWT";
pub struct CreateTokenResult {
pub access_token: String,
pub issued: i64,
pub expires_in: i64,
pub token_issued: time::OffsetDateTime,
}
pub fn create_token(
key: &String,
token_resource: &TokenResource,
duration: time::Duration,
) -> Result<(String, i64), josekit::JoseError> {
) -> Result<CreateTokenResult, josekit::JoseError> {
let mut header = josekit::jws::JwsHeader::new();
header.set_token_type(TOKEN_TYPE);
let mut payload = josekit::jwt::JwtPayload::new();
let message = &token_resource.message;
let issuer = &token_resource.issuer;
let audiences: &Vec<String> = &token_resource.audiences;
payload.set_subject(message);
payload.set_issuer(issuer);
payload.set_audience(audiences.clone());
if !token_resource.id.is_nil() {
match payload.set_claim("id", Some(serde_json::json!(token_resource.id))) {
payload.set_subject(&token_resource.message);
payload.set_issuer(&token_resource.issuer);
payload.set_audience(token_resource.audiences.clone());
if !token_resource.user_id.is_nil() {
match payload.set_claim("user_id", Some(serde_json::json!(token_resource.user_id))) {
Ok(_) => {}
Err(err) => {
return Err(err);
@@ -79,11 +106,58 @@ pub fn create_token(
let signer = josekit::jws::alg::hmac::HmacJwsAlgorithm::Hs256
.signer_from_bytes(key.as_bytes())
.unwrap();
Ok((
josekit::jwt::encode_with_signer(&payload, &header, &signer).unwrap(),
(expire - time::OffsetDateTime::UNIX_EPOCH).whole_seconds(),
))
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,
})
}
Err(e) => Err(josekit::JoseError::InvalidClaim(e.into())),
}
}
#[cfg(test)]
mod tests {
const TEST_MESSAGE: &str = "Testing for textsender";
const TEST_ISSUER: &str = "textsender-test";
const TEST_AUDIENCE: &str = "area-test";
const TEST_USER_ID: uuid::Uuid = uuid::uuid!("9ab1c75f-d184-4913-ae99-544b4dcfcb41");
const TEST_KEY: &str = "8342nhf7ycrt4983q7ryfc93w478ryfc3w9487ryfc342w98i7cy";
#[test]
fn test_create_token() {
let token_resource = super::TokenResource {
message: String::from(TEST_MESSAGE),
issuer: String::from(TEST_ISSUER),
audiences: vec![String::from(TEST_AUDIENCE)],
user_id: TEST_USER_ID,
};
let token_duration = time::Duration::minutes(30);
let key = String::from(TEST_KEY);
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 = {
d_result.year() == cst.token_issued.year()
&& d_result.month() == cst.token_issued.month()
&& d_result.day() == cst.token_issued.day()
&& d_result.hour() == cst.token_issued.hour()
&& d_result.minute() == cst.token_issued.minute()
&& d_result.second() == cst.token_issued.second()
};
assert!(they_match, "Issued times do not match");
}
Err(err) => {
assert!(false, "Error: {err:?}");
}
},
Err(err) => {
assert!(false, "Error: {err:?}");
}
}
}
}
+20 -10
View File
@@ -1,6 +1,6 @@
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, Default, Deserialize, Serialize, utoipa::ToSchema)]
#[derive(
Clone, Debug, Default, serde::Deserialize, serde::Serialize, sqlx::FromRow, utoipa::ToSchema,
)]
pub struct User {
pub id: uuid::Uuid,
pub phone_number: String,
@@ -18,7 +18,9 @@ pub struct User {
pub salt_id: uuid::Uuid,
}
#[derive(Clone, Debug, Default, Deserialize, Serialize, utoipa::ToSchema)]
#[derive(
Clone, Debug, Default, serde::Deserialize, serde::Serialize, sqlx::FromRow, utoipa::ToSchema,
)]
pub struct ServiceUser {
pub id: uuid::Uuid,
pub username: String,
@@ -32,7 +34,9 @@ pub struct ServiceUser {
pub salt_id: uuid::Uuid,
}
#[derive(Clone, Debug, Default, Deserialize, Serialize, utoipa::ToSchema)]
#[derive(
Clone, Debug, Default, serde::Deserialize, serde::Serialize, sqlx::FromRow, utoipa::ToSchema,
)]
pub struct Organization {
pub id: uuid::Uuid,
pub name: String,
@@ -40,7 +44,9 @@ pub struct Organization {
pub created: Option<time::OffsetDateTime>,
}
#[derive(Clone, Debug, Default, Deserialize, Serialize, utoipa::ToSchema)]
#[derive(
Clone, Debug, Default, serde::Deserialize, serde::Serialize, sqlx::FromRow, utoipa::ToSchema,
)]
pub struct UserLoginHistory {
pub id: uuid::Uuid,
#[serde(with = "time::serde::rfc3339::option")]
@@ -48,7 +54,9 @@ pub struct UserLoginHistory {
pub user_id: uuid::Uuid,
}
#[derive(Clone, Debug, Default, Deserialize, Serialize, utoipa::ToSchema)]
#[derive(
Clone, Debug, Default, serde::Deserialize, serde::Serialize, sqlx::FromRow, utoipa::ToSchema,
)]
pub struct ServiceUserLoginHistory {
pub id: uuid::Uuid,
#[serde(with = "time::serde::rfc3339::option")]
@@ -56,7 +64,9 @@ pub struct ServiceUserLoginHistory {
pub service_user_id: uuid::Uuid,
}
#[derive(Clone, Debug, Default, Deserialize, Serialize, utoipa::ToSchema)]
#[derive(
Clone, Debug, Default, serde::Deserialize, serde::Serialize, sqlx::FromRow, utoipa::ToSchema,
)]
pub struct UserProfile {
pub user_id: uuid::Uuid,
pub phone_number: String,
@@ -69,9 +79,9 @@ pub struct UserProfile {
pub last_login: Option<time::OffsetDateTime>,
}
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
#[derive(Clone, Debug, Default, serde::Deserialize, serde::Serialize, sqlx::FromRow)]
pub struct Salt {
#[serde(skip_serializing_if = "crate::init::is_uuid_nil")]
// #[serde(skip_serializing_if = "crate::init::is_uuid_nil")]
pub id: uuid::Uuid,
#[serde(skip_serializing_if = "String::is_empty")]
pub salt: String,