Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
eebd4db4d4
|
@@ -1,46 +0,0 @@
|
|||||||
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,10 +3,10 @@ name: Release Tagging
|
|||||||
on:
|
on:
|
||||||
push:
|
push:
|
||||||
branches:
|
branches:
|
||||||
- main
|
- v0.3.0-migrate
|
||||||
pull_request:
|
pull_request:
|
||||||
branches:
|
branches:
|
||||||
- main
|
- v0.3.0-migrate
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
release:
|
release:
|
||||||
@@ -20,7 +20,7 @@ jobs:
|
|||||||
- name: Install Rust
|
- name: Install Rust
|
||||||
uses: actions-rs/toolchain@v1
|
uses: actions-rs/toolchain@v1
|
||||||
with:
|
with:
|
||||||
toolchain: 1.96
|
toolchain: 1.95
|
||||||
components: cargo
|
components: cargo
|
||||||
|
|
||||||
- name: Extract Version from Cargo.toml
|
- name: Extract Version from Cargo.toml
|
||||||
|
|||||||
@@ -3,11 +3,10 @@ name: Rust Build
|
|||||||
on:
|
on:
|
||||||
push:
|
push:
|
||||||
branches:
|
branches:
|
||||||
- main
|
- v0.3.0-migrate
|
||||||
|
pull_request:
|
||||||
concurrency:
|
branches:
|
||||||
group: ${{ github.workflow }}-${{ github.ref }}
|
- v0.3.0-migrate
|
||||||
cancel-in-progress: true
|
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
check:
|
check:
|
||||||
@@ -17,8 +16,7 @@ jobs:
|
|||||||
- uses: actions/checkout@v5
|
- uses: actions/checkout@v5
|
||||||
- uses: actions-rust-lang/setup-rust-toolchain@v1
|
- uses: actions-rust-lang/setup-rust-toolchain@v1
|
||||||
with:
|
with:
|
||||||
toolchain: 1.96
|
toolchain: 1.95
|
||||||
- uses: Swatinem/rust-cache@v2
|
|
||||||
- run: cargo check
|
- run: cargo check
|
||||||
|
|
||||||
test:
|
test:
|
||||||
@@ -28,8 +26,7 @@ jobs:
|
|||||||
- uses: actions/checkout@v5
|
- uses: actions/checkout@v5
|
||||||
- uses: actions-rust-lang/setup-rust-toolchain@v1
|
- uses: actions-rust-lang/setup-rust-toolchain@v1
|
||||||
with:
|
with:
|
||||||
toolchain: 1.96
|
toolchain: 1.95
|
||||||
- uses: Swatinem/rust-cache@v2
|
|
||||||
- run: cargo test
|
- run: cargo test
|
||||||
|
|
||||||
fmt:
|
fmt:
|
||||||
@@ -39,8 +36,7 @@ jobs:
|
|||||||
- uses: actions/checkout@v5
|
- uses: actions/checkout@v5
|
||||||
- uses: actions-rust-lang/setup-rust-toolchain@v1
|
- uses: actions-rust-lang/setup-rust-toolchain@v1
|
||||||
with:
|
with:
|
||||||
toolchain: 1.96
|
toolchain: 1.95
|
||||||
- uses: Swatinem/rust-cache@v2
|
|
||||||
- run: rustup component add rustfmt
|
- run: rustup component add rustfmt
|
||||||
- run: cargo fmt --all -- --check
|
- run: cargo fmt --all -- --check
|
||||||
|
|
||||||
@@ -51,8 +47,7 @@ jobs:
|
|||||||
- uses: actions/checkout@v5
|
- uses: actions/checkout@v5
|
||||||
- uses: actions-rust-lang/setup-rust-toolchain@v1
|
- uses: actions-rust-lang/setup-rust-toolchain@v1
|
||||||
with:
|
with:
|
||||||
toolchain: 1.96
|
toolchain: 1.95
|
||||||
- uses: Swatinem/rust-cache@v2
|
|
||||||
- run: rustup component add clippy
|
- run: rustup component add clippy
|
||||||
- run: cargo clippy -- -D warnings
|
- run: cargo clippy -- -D warnings
|
||||||
|
|
||||||
@@ -63,6 +58,5 @@ jobs:
|
|||||||
- uses: actions/checkout@v5
|
- uses: actions/checkout@v5
|
||||||
- uses: actions-rust-lang/setup-rust-toolchain@v1
|
- uses: actions-rust-lang/setup-rust-toolchain@v1
|
||||||
with:
|
with:
|
||||||
toolchain: 1.96
|
toolchain: 1.95
|
||||||
- uses: Swatinem/rust-cache@v2
|
|
||||||
- run: cargo build
|
- run: cargo build
|
||||||
|
|||||||
Generated
+188
-1387
File diff suppressed because it is too large
Load Diff
+10
-18
@@ -1,29 +1,21 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "textsender_models"
|
name = "textsender_models"
|
||||||
version = "0.4.10"
|
version = "0.3.0"
|
||||||
edition = "2024"
|
edition = "2024"
|
||||||
rust-version = "1.96"
|
rust-version = "1.95"
|
||||||
description = "Models used for the textsender project"
|
description = "models used for the textsender project"
|
||||||
|
# license = "MIT"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
serde = { version = "1.0.228", features = ["derive"] }
|
serde = { version = "1.0.228", features = ["derive"] }
|
||||||
serde_json = { version = "1.0.150" }
|
serde_json = { version = "1.0.149" }
|
||||||
time = { version = "0.3.49", features = ["formatting", "macros", "parsing", "serde"] }
|
rand = { version = "0.10.1" }
|
||||||
sqlx = { version = "0.8.6", features = ["postgres", "runtime-tokio-native-tls", "time", "uuid"] }
|
time = { version = "0.3.47", features = ["formatting", "macros", "parsing", "serde"] }
|
||||||
uuid = { version = "1.23.3", features = ["v4", "serde"] }
|
uuid = { version = "1.23.1", features = ["v4", "serde"] }
|
||||||
dotenvy = { version = "0.15.7" }
|
dotenvy = { version = "0.15.7" }
|
||||||
const_format = { version = "0.2.36" }
|
const_format = { version = "0.2.36" }
|
||||||
josekit = { version = "0.10.3" }
|
josekit = { version = "0.10.3" }
|
||||||
utoipa = { version = "5.5.0", features = ["uuid", "time"] }
|
utoipa = { version = "5.4.0", features = ["uuid", "time"] }
|
||||||
|
|
||||||
[features]
|
|
||||||
default = ["full"]
|
|
||||||
full = ["user", "message", "contact", "config", "envy", "token"]
|
|
||||||
user = []
|
|
||||||
message = []
|
|
||||||
contact = []
|
|
||||||
config = []
|
|
||||||
envy = []
|
|
||||||
token = []
|
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
|
tempfile = { version = "3.27.0" }
|
||||||
|
|||||||
@@ -1,7 +1,9 @@
|
|||||||
#[derive(Clone, Debug, Default, serde::Deserialize, serde::Serialize)]
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
|
||||||
pub struct TwilioConfig {
|
pub struct TwilioConfig {
|
||||||
#[serde(alias = "auth_id")]
|
#[serde(alias = "auth_id")]
|
||||||
pub account_sid: String,
|
pub account_sid: uuid::Uuid,
|
||||||
#[serde(skip_serializing_if = "String::is_empty")]
|
#[serde(skip_serializing_if = "String::is_empty")]
|
||||||
pub service_sid: String,
|
pub service_sid: String,
|
||||||
#[serde(skip_serializing_if = "String::is_empty")]
|
#[serde(skip_serializing_if = "String::is_empty")]
|
||||||
@@ -19,26 +21,3 @@ impl TwilioConfig {
|
|||||||
println!("Number: {:?}", self.phone_number);
|
println!("Number: {:?}", self.phone_number);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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;
|
|
||||||
|
|
||||||
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,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
+12
-11
@@ -1,19 +1,20 @@
|
|||||||
#[derive(
|
use serde::{Deserialize, Serialize};
|
||||||
Clone, Debug, Default, serde::Deserialize, serde::Serialize, sqlx::FromRow, utoipa::ToSchema,
|
|
||||||
)]
|
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
|
||||||
pub struct Contact {
|
pub struct Contact {
|
||||||
#[serde(skip_serializing_if = "crate::init::is_uuid_nil")]
|
#[serde(skip_serializing_if = "crate::init::is_uuid_nil")]
|
||||||
pub id: Option<uuid::Uuid>,
|
pub id: uuid::Uuid,
|
||||||
// empty?
|
// empty?
|
||||||
#[serde(skip_serializing_if = "Option::is_none")]
|
#[serde(skip_serializing_if = "String::is_empty")]
|
||||||
pub firstname: Option<String>,
|
pub firstname: String,
|
||||||
// empty?
|
// empty?
|
||||||
#[serde(skip_serializing_if = "Option::is_none")]
|
#[serde(skip_serializing_if = "String::is_empty")]
|
||||||
pub lastname: Option<String>,
|
pub lastname: String,
|
||||||
// empty?
|
// empty?
|
||||||
#[serde(skip_serializing_if = "Option::is_none")]
|
#[serde(skip_serializing_if = "String::is_empty")]
|
||||||
pub nickname: Option<String>,
|
pub nickname: String,
|
||||||
pub phone_number: String,
|
pub phone_number: String,
|
||||||
|
// empty
|
||||||
#[serde(skip_serializing_if = "crate::init::is_uuid_nil")]
|
#[serde(skip_serializing_if = "crate::init::is_uuid_nil")]
|
||||||
pub user_id: Option<uuid::Uuid>,
|
pub user_id: uuid::Uuid,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,3 @@
|
|||||||
// TODO: Functions does not need to be async
|
|
||||||
|
|
||||||
pub async fn get_db_url() -> crate::envy::EnvVar {
|
pub async fn get_db_url() -> crate::envy::EnvVar {
|
||||||
dotenvy::dotenv().ok();
|
dotenvy::dotenv().ok();
|
||||||
let key = crate::envy::keys::DB_URL;
|
let key = crate::envy::keys::DB_URL;
|
||||||
|
|||||||
+2
-5
@@ -6,10 +6,7 @@ pub mod token;
|
|||||||
pub mod user;
|
pub mod user;
|
||||||
|
|
||||||
pub mod init {
|
pub mod init {
|
||||||
pub fn is_uuid_nil(uuid: &Option<uuid::Uuid>) -> bool {
|
pub fn is_uuid_nil(uuid: &uuid::Uuid) -> bool {
|
||||||
match uuid {
|
uuid.is_nil()
|
||||||
Some(id) => id.is_nil(),
|
|
||||||
None => true,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,17 +1,14 @@
|
|||||||
/// Message status - Instant
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
pub const MESSAGE_EVENT_RESPONSE_STATUS_INSTANT: &str = "INSTANT";
|
pub const MESSAGE_EVENT_RESPONSE_STATUS_INSTANT: &str = "INSTANT";
|
||||||
/// Message status - Scheduled
|
|
||||||
pub const MESSAGE_EVENT_RESPONSE_STATUS_SCHEDULED: &str = "SCHEDULED";
|
pub const MESSAGE_EVENT_RESPONSE_STATUS_SCHEDULED: &str = "SCHEDULED";
|
||||||
|
|
||||||
#[derive(
|
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
|
||||||
Clone, Debug, Default, serde::Deserialize, serde::Serialize, sqlx::FromRow, utoipa::ToSchema,
|
|
||||||
)]
|
|
||||||
pub struct MessageEventResponse {
|
pub struct MessageEventResponse {
|
||||||
pub id: uuid::Uuid,
|
pub id: uuid::Uuid,
|
||||||
#[serde(skip_serializing_if = "crate::init::is_uuid_nil")]
|
pub scheduled_message_event_id: uuid::Uuid,
|
||||||
pub scheduled_message_event_id: Option<uuid::Uuid>,
|
// Json field
|
||||||
/// Stores a json response of the sent message
|
pub response: String,
|
||||||
pub response: serde_json::Value,
|
|
||||||
pub user_id: uuid::Uuid,
|
pub user_id: uuid::Uuid,
|
||||||
pub contact_id: uuid::Uuid,
|
pub contact_id: uuid::Uuid,
|
||||||
pub message_id: uuid::Uuid,
|
pub message_id: uuid::Uuid,
|
||||||
|
|||||||
+5
-7
@@ -1,13 +1,11 @@
|
|||||||
pub mod event;
|
pub mod event;
|
||||||
pub mod scheduling;
|
pub mod scheduling;
|
||||||
|
|
||||||
#[derive(
|
use serde::{Deserialize, Serialize};
|
||||||
Clone, Debug, Default, serde::Deserialize, serde::Serialize, sqlx::FromRow, utoipa::ToSchema,
|
|
||||||
)]
|
#[derive(Clone, Debug, Default, Deserialize, Serialize, utoipa::ToSchema)]
|
||||||
pub struct Message {
|
pub struct Message {
|
||||||
#[serde(skip_serializing_if = "crate::init::is_uuid_nil")]
|
pub id: uuid::Uuid,
|
||||||
pub id: Option<uuid::Uuid>,
|
|
||||||
pub content: String,
|
pub content: String,
|
||||||
#[serde(skip_serializing_if = "crate::init::is_uuid_nil")]
|
pub user_id: uuid::Uuid,
|
||||||
pub user_id: Option<uuid::Uuid>,
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,26 +1,24 @@
|
|||||||
#[derive(
|
use serde::{Deserialize, Serialize};
|
||||||
Clone, Debug, Default, serde::Deserialize, serde::Serialize, sqlx::FromRow, utoipa::ToSchema,
|
|
||||||
)]
|
#[derive(Clone, Debug, Default, Deserialize, Serialize, utoipa::ToSchema)]
|
||||||
pub struct ScheduledMessageEvent {
|
pub struct ScheduledMessageEvent {
|
||||||
pub id: uuid::Uuid,
|
id: uuid::Uuid,
|
||||||
pub contact_id: uuid::Uuid,
|
contact_id: uuid::Uuid,
|
||||||
pub message_id: uuid::Uuid,
|
message_id: uuid::Uuid,
|
||||||
pub scheduled_message_id: uuid::Uuid,
|
scheduled_message_id: uuid::Uuid,
|
||||||
#[serde(with = "time::serde::rfc3339::option")]
|
#[serde(with = "time::serde::rfc3339::option")]
|
||||||
pub created: Option<time::OffsetDateTime>,
|
created: Option<time::OffsetDateTime>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(
|
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
|
||||||
Clone, Debug, Default, serde::Deserialize, serde::Serialize, sqlx::FromRow, utoipa::ToSchema,
|
|
||||||
)]
|
|
||||||
pub struct ScheduledMessage {
|
pub struct ScheduledMessage {
|
||||||
pub id: uuid::Uuid,
|
id: uuid::Uuid,
|
||||||
#[serde(with = "time::serde::rfc3339::option")]
|
#[serde(with = "time::serde::rfc3339::option")]
|
||||||
pub scheduled: Option<time::OffsetDateTime>,
|
scheduled: Option<time::OffsetDateTime>,
|
||||||
#[serde(with = "time::serde::rfc3339::option")]
|
#[serde(with = "time::serde::rfc3339::option")]
|
||||||
pub created: Option<time::OffsetDateTime>,
|
created: Option<time::OffsetDateTime>,
|
||||||
pub status: String,
|
status: String,
|
||||||
pub user_id: uuid::Uuid,
|
user_id: uuid::Uuid,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub const READY: &str = "READY";
|
pub const READY: &str = "READY";
|
||||||
|
|||||||
+20
-94
@@ -1,40 +1,18 @@
|
|||||||
use serde::Deserialize;
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
#[derive(Clone, Debug, Default, serde::Deserialize, serde::Serialize)]
|
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
|
||||||
pub struct Claims {
|
pub struct Claims {
|
||||||
pub user_id: uuid::Uuid,
|
pub user_id: uuid::Uuid,
|
||||||
#[serde(skip_serializing_if = "Option::is_none")]
|
pub role: String,
|
||||||
pub role: Option<String>,
|
|
||||||
#[serde(alias = "iss")]
|
#[serde(alias = "iss")]
|
||||||
pub issued: String,
|
pub issued: String,
|
||||||
#[serde(alias = "exp")]
|
#[serde(alias = "exp")]
|
||||||
#[serde(deserialize_with = "deserialize_i64_from_f64")]
|
|
||||||
pub expired: i64,
|
pub expired: i64,
|
||||||
#[serde(alias = "iat")]
|
#[serde(alias = "iat")]
|
||||||
#[serde(deserialize_with = "deserialize_i64_from_f64")]
|
|
||||||
pub issued_at: i64,
|
pub issued_at: i64,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn deserialize_i64_from_f64<'de, D>(deserializer: D) -> Result<i64, D::Error>
|
#[derive(Clone, Debug, Default, Deserialize, Serialize, utoipa::ToSchema)]
|
||||||
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 struct LoginResult {
|
||||||
pub user_id: uuid::Uuid,
|
pub user_id: uuid::Uuid,
|
||||||
pub access_token: String,
|
pub access_token: String,
|
||||||
@@ -43,7 +21,7 @@ pub struct LoginResult {
|
|||||||
pub issued_at: i64,
|
pub issued_at: i64,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Alias for LoginResult
|
// Alias for LoginResult
|
||||||
pub type Login = LoginResult;
|
pub type Login = LoginResult;
|
||||||
|
|
||||||
pub fn get_issued() -> time::Result<time::OffsetDateTime> {
|
pub fn get_issued() -> time::Result<time::OffsetDateTime> {
|
||||||
@@ -54,7 +32,7 @@ mod util {
|
|||||||
pub fn time_to_std_time(
|
pub fn time_to_std_time(
|
||||||
provided_time: &time::OffsetDateTime,
|
provided_time: &time::OffsetDateTime,
|
||||||
) -> Result<std::time::SystemTime, std::time::SystemTimeError> {
|
) -> Result<std::time::SystemTime, std::time::SystemTimeError> {
|
||||||
let converted: std::time::SystemTime = (*provided_time).into();
|
let converted = std::time::SystemTime::from(*provided_time);
|
||||||
Ok(converted)
|
Ok(converted)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -64,33 +42,28 @@ pub struct TokenResource {
|
|||||||
pub message: String,
|
pub message: String,
|
||||||
pub issuer: String,
|
pub issuer: String,
|
||||||
pub audiences: Vec<String>,
|
pub audiences: Vec<String>,
|
||||||
pub user_id: uuid::Uuid,
|
pub id: uuid::Uuid,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Token type
|
|
||||||
pub const TOKEN_TYPE: &str = "JWT";
|
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(
|
pub fn create_token(
|
||||||
key: &String,
|
key: &String,
|
||||||
token_resource: &TokenResource,
|
token_resource: &TokenResource,
|
||||||
duration: time::Duration,
|
duration: time::Duration,
|
||||||
) -> Result<CreateTokenResult, josekit::JoseError> {
|
) -> Result<(String, i64), josekit::JoseError> {
|
||||||
let mut header = josekit::jws::JwsHeader::new();
|
let mut header = josekit::jws::JwsHeader::new();
|
||||||
header.set_token_type(TOKEN_TYPE);
|
header.set_token_type(TOKEN_TYPE);
|
||||||
|
|
||||||
let mut payload = josekit::jwt::JwtPayload::new();
|
let mut payload = josekit::jwt::JwtPayload::new();
|
||||||
payload.set_subject(&token_resource.message);
|
let message = &token_resource.message;
|
||||||
payload.set_issuer(&token_resource.issuer);
|
let issuer = &token_resource.issuer;
|
||||||
payload.set_audience(token_resource.audiences.clone());
|
let audiences: &Vec<String> = &token_resource.audiences;
|
||||||
if !token_resource.user_id.is_nil() {
|
payload.set_subject(message);
|
||||||
match payload.set_claim("user_id", Some(serde_json::json!(token_resource.user_id))) {
|
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))) {
|
||||||
Ok(_) => {}
|
Ok(_) => {}
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
return Err(err);
|
return Err(err);
|
||||||
@@ -106,58 +79,11 @@ pub fn create_token(
|
|||||||
let signer = josekit::jws::alg::hmac::HmacJwsAlgorithm::Hs256
|
let signer = josekit::jws::alg::hmac::HmacJwsAlgorithm::Hs256
|
||||||
.signer_from_bytes(key.as_bytes())
|
.signer_from_bytes(key.as_bytes())
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
Ok((
|
||||||
Ok(CreateTokenResult {
|
josekit::jwt::encode_with_signer(&payload, &header, &signer).unwrap(),
|
||||||
access_token: josekit::jwt::encode_with_signer(&payload, &header, &signer).unwrap(),
|
(expire - time::OffsetDateTime::UNIX_EPOCH).whole_seconds(),
|
||||||
issued: issued.unix_timestamp(),
|
))
|
||||||
expires_in: expire.unix_timestamp(),
|
|
||||||
token_issued: issued,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
Err(e) => Err(josekit::JoseError::InvalidClaim(e.into())),
|
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:?}");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
+10
-20
@@ -1,6 +1,6 @@
|
|||||||
#[derive(
|
use serde::{Deserialize, Serialize};
|
||||||
Clone, Debug, Default, serde::Deserialize, serde::Serialize, sqlx::FromRow, utoipa::ToSchema,
|
|
||||||
)]
|
#[derive(Clone, Debug, Default, Deserialize, Serialize, utoipa::ToSchema)]
|
||||||
pub struct User {
|
pub struct User {
|
||||||
pub id: uuid::Uuid,
|
pub id: uuid::Uuid,
|
||||||
pub phone_number: String,
|
pub phone_number: String,
|
||||||
@@ -18,9 +18,7 @@ pub struct User {
|
|||||||
pub salt_id: uuid::Uuid,
|
pub salt_id: uuid::Uuid,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(
|
#[derive(Clone, Debug, Default, Deserialize, Serialize, utoipa::ToSchema)]
|
||||||
Clone, Debug, Default, serde::Deserialize, serde::Serialize, sqlx::FromRow, utoipa::ToSchema,
|
|
||||||
)]
|
|
||||||
pub struct ServiceUser {
|
pub struct ServiceUser {
|
||||||
pub id: uuid::Uuid,
|
pub id: uuid::Uuid,
|
||||||
pub username: String,
|
pub username: String,
|
||||||
@@ -34,9 +32,7 @@ pub struct ServiceUser {
|
|||||||
pub salt_id: uuid::Uuid,
|
pub salt_id: uuid::Uuid,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(
|
#[derive(Clone, Debug, Default, Deserialize, Serialize, utoipa::ToSchema)]
|
||||||
Clone, Debug, Default, serde::Deserialize, serde::Serialize, sqlx::FromRow, utoipa::ToSchema,
|
|
||||||
)]
|
|
||||||
pub struct Organization {
|
pub struct Organization {
|
||||||
pub id: uuid::Uuid,
|
pub id: uuid::Uuid,
|
||||||
pub name: String,
|
pub name: String,
|
||||||
@@ -44,9 +40,7 @@ pub struct Organization {
|
|||||||
pub created: Option<time::OffsetDateTime>,
|
pub created: Option<time::OffsetDateTime>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(
|
#[derive(Clone, Debug, Default, Deserialize, Serialize, utoipa::ToSchema)]
|
||||||
Clone, Debug, Default, serde::Deserialize, serde::Serialize, sqlx::FromRow, utoipa::ToSchema,
|
|
||||||
)]
|
|
||||||
pub struct UserLoginHistory {
|
pub struct UserLoginHistory {
|
||||||
pub id: uuid::Uuid,
|
pub id: uuid::Uuid,
|
||||||
#[serde(with = "time::serde::rfc3339::option")]
|
#[serde(with = "time::serde::rfc3339::option")]
|
||||||
@@ -54,9 +48,7 @@ pub struct UserLoginHistory {
|
|||||||
pub user_id: uuid::Uuid,
|
pub user_id: uuid::Uuid,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(
|
#[derive(Clone, Debug, Default, Deserialize, Serialize, utoipa::ToSchema)]
|
||||||
Clone, Debug, Default, serde::Deserialize, serde::Serialize, sqlx::FromRow, utoipa::ToSchema,
|
|
||||||
)]
|
|
||||||
pub struct ServiceUserLoginHistory {
|
pub struct ServiceUserLoginHistory {
|
||||||
pub id: uuid::Uuid,
|
pub id: uuid::Uuid,
|
||||||
#[serde(with = "time::serde::rfc3339::option")]
|
#[serde(with = "time::serde::rfc3339::option")]
|
||||||
@@ -64,9 +56,7 @@ pub struct ServiceUserLoginHistory {
|
|||||||
pub service_user_id: uuid::Uuid,
|
pub service_user_id: uuid::Uuid,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(
|
#[derive(Clone, Debug, Default, Deserialize, Serialize, utoipa::ToSchema)]
|
||||||
Clone, Debug, Default, serde::Deserialize, serde::Serialize, sqlx::FromRow, utoipa::ToSchema,
|
|
||||||
)]
|
|
||||||
pub struct UserProfile {
|
pub struct UserProfile {
|
||||||
pub user_id: uuid::Uuid,
|
pub user_id: uuid::Uuid,
|
||||||
pub phone_number: String,
|
pub phone_number: String,
|
||||||
@@ -79,9 +69,9 @@ pub struct UserProfile {
|
|||||||
pub last_login: Option<time::OffsetDateTime>,
|
pub last_login: Option<time::OffsetDateTime>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug, Default, serde::Deserialize, serde::Serialize, sqlx::FromRow)]
|
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
|
||||||
pub struct Salt {
|
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,
|
pub id: uuid::Uuid,
|
||||||
#[serde(skip_serializing_if = "String::is_empty")]
|
#[serde(skip_serializing_if = "String::is_empty")]
|
||||||
pub salt: String,
|
pub salt: String,
|
||||||
|
|||||||
Reference in New Issue
Block a user