Compare commits
18
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9ea37e56f0
|
||
|
|
6e91320709
|
||
|
|
7736f79649
|
||
|
|
1eb659d865
|
||
|
|
3ea92ae64e
|
||
|
|
0e8e0db284
|
||
|
|
720f3d8f75
|
||
|
|
7e5bc016b1
|
||
|
|
384dd41690
|
||
|
|
f246676203
|
||
|
|
47e6c85fc7
|
||
|
|
82370ad63a
|
||
|
|
e5ab4aa426
|
||
|
|
ad632fca16
|
||
|
|
86febdab0d
|
||
|
|
7534e8652f
|
||
|
|
c4b2c19f77
|
||
|
|
890d03bdf3
|
Generated
+10
-10
@@ -732,14 +732,14 @@ checksum = "b6d2cec3eae94f9f509c767b45932f1ada8350c4bdb85af2fcab4a3c14807981"
|
||||
|
||||
[[package]]
|
||||
name = "libredox"
|
||||
version = "0.1.18"
|
||||
version = "0.1.17"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c943259e342f1e06ff2da7a83eabdfe7f92ce10262688dbf1895ff0b3e6e4652"
|
||||
checksum = "f02ab6bace2054fb888a3c16f990117b579d14a3088e472d63c6011fa185c9d3"
|
||||
dependencies = [
|
||||
"bitflags",
|
||||
"libc",
|
||||
"plain",
|
||||
"redox_syscall 0.9.0",
|
||||
"redox_syscall 0.8.1",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -1106,9 +1106,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "redox_syscall"
|
||||
version = "0.9.0"
|
||||
version = "0.8.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c5102a6aaa05aa011a238e178e6bca86d2cb56fc9f586d37cb80f5bca6e07759"
|
||||
checksum = "5b44b894f2a6e36457d665d1e08c3866add6ed5e70050c1b4ba8a8ddedb02ce7"
|
||||
dependencies = [
|
||||
"bitflags",
|
||||
]
|
||||
@@ -1626,7 +1626,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "textsender_models"
|
||||
version = "0.4.11"
|
||||
version = "0.4.10"
|
||||
dependencies = [
|
||||
"const_format",
|
||||
"dotenvy",
|
||||
@@ -1661,9 +1661,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "time"
|
||||
version = "0.3.53"
|
||||
version = "0.3.51"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "18dfaaeddcb932337b5e7866ee7d0ce9b76d2fd092997146f187ec09b4558a50"
|
||||
checksum = "85c17d80feb7334b40c484e45ed1a5273dfd8bfda537c3be2e74a06a6686f327"
|
||||
dependencies = [
|
||||
"deranged",
|
||||
"num-conv",
|
||||
@@ -1681,9 +1681,9 @@ checksum = "9e1c906769ad99c88eaa54e728060edef082f8e358ff32030cb7c7d315e81109"
|
||||
|
||||
[[package]]
|
||||
name = "time-macros"
|
||||
version = "0.2.31"
|
||||
version = "0.2.30"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c431b87111666e491a90baa837f914fb45cd5dc3c268591b0220ff5057f2085f"
|
||||
checksum = "dcef1a61bdb119096e153208ec5cbec23944ce8bca13be5c7f60c634f7403935"
|
||||
dependencies = [
|
||||
"num-conv",
|
||||
"time-core",
|
||||
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "textsender_models"
|
||||
version = "0.4.11"
|
||||
version = "0.4.10"
|
||||
edition = "2024"
|
||||
rust-version = "1.96"
|
||||
description = "Models used for the textsender project"
|
||||
|
||||
@@ -20,11 +20,12 @@ impl TwilioConfig {
|
||||
}
|
||||
}
|
||||
|
||||
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");
|
||||
// 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()
|
||||
|
||||
+16
-14
@@ -1,4 +1,6 @@
|
||||
pub fn get_db_url() -> crate::envy::EnvVar {
|
||||
// TODO: Functions does not need to be async
|
||||
|
||||
pub async 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 +8,7 @@ pub fn get_db_url() -> crate::envy::EnvVar {
|
||||
crate::envy::init_envvar(key, &value)
|
||||
}
|
||||
|
||||
pub fn get_secret_main_key() -> crate::envy::EnvVar {
|
||||
pub async 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 +16,7 @@ pub fn get_secret_main_key() -> crate::envy::EnvVar {
|
||||
crate::envy::init_envvar(key, &value)
|
||||
}
|
||||
|
||||
pub fn get_service_passphrase() -> crate::envy::EnvVar {
|
||||
pub async 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 +24,7 @@ pub fn get_service_passphrase() -> crate::envy::EnvVar {
|
||||
crate::envy::init_envvar(key, &value)
|
||||
}
|
||||
|
||||
pub fn get_secret_key() -> crate::envy::EnvVar {
|
||||
pub async 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 +32,7 @@ pub fn get_secret_key() -> crate::envy::EnvVar {
|
||||
crate::envy::init_envvar(key, &value)
|
||||
}
|
||||
|
||||
pub fn get_root_directory() -> crate::envy::EnvVar {
|
||||
pub async 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 +40,7 @@ pub fn get_root_directory() -> crate::envy::EnvVar {
|
||||
crate::envy::init_envvar(key, &value)
|
||||
}
|
||||
|
||||
pub fn get_app_base_api_url() -> crate::envy::EnvVar {
|
||||
pub async 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 +48,7 @@ pub fn get_app_base_api_url() -> crate::envy::EnvVar {
|
||||
crate::envy::init_envvar(key, &value)
|
||||
}
|
||||
|
||||
pub fn get_app_auth_base_api_url() -> crate::envy::EnvVar {
|
||||
pub async 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 +56,7 @@ pub fn get_app_auth_base_api_url() -> crate::envy::EnvVar {
|
||||
crate::envy::init_envvar(key, &value)
|
||||
}
|
||||
|
||||
pub fn get_app_env() -> crate::envy::EnvVar {
|
||||
pub async 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 +64,14 @@ pub fn get_app_env() -> crate::envy::EnvVar {
|
||||
crate::envy::init_envvar(key, &value)
|
||||
}
|
||||
|
||||
pub fn get_backend_port() -> crate::envy::EnvVar {
|
||||
pub async 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 fn get_frontend_url() -> crate::envy::EnvVar {
|
||||
pub async 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 +79,7 @@ pub fn get_frontend_url() -> crate::envy::EnvVar {
|
||||
crate::envy::init_envvar(key, &value)
|
||||
}
|
||||
|
||||
pub fn get_rust_log() -> crate::envy::EnvVar {
|
||||
pub async 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 +87,7 @@ pub fn get_rust_log() -> crate::envy::EnvVar {
|
||||
crate::envy::init_envvar(key, &value)
|
||||
}
|
||||
|
||||
pub fn get_allowed_origins() -> crate::envy::EnvVar {
|
||||
pub async 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 +98,8 @@ pub fn get_allowed_origins() -> crate::envy::EnvVar {
|
||||
envvar
|
||||
}
|
||||
|
||||
/// Get environment not specified in the library
|
||||
pub fn get_env(environment: &str) -> crate::envy::EnvVar {
|
||||
/// Get environment not specified in the code
|
||||
pub async 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);
|
||||
|
||||
+8
-26
@@ -20,17 +20,16 @@ pub fn init_envvar(key: &str, value: &str) -> EnvVar {
|
||||
}
|
||||
|
||||
pub fn init_delimiter(envvar: &mut EnvVar, delimiter: char) {
|
||||
let amount_of_delimiters_found: i32 = {
|
||||
let mut count = 0;
|
||||
for v in envvar.value.chars() {
|
||||
if v == delimiter {
|
||||
count += 1;
|
||||
}
|
||||
}
|
||||
let mut amount_of_delimiters_found: i32 = 0;
|
||||
|
||||
for v in envvar.value.chars() {
|
||||
if v == delimiter {
|
||||
amount_of_delimiters_found += 1;
|
||||
}
|
||||
}
|
||||
|
||||
count
|
||||
};
|
||||
let has_delimiter = amount_of_delimiters_found >= 1;
|
||||
|
||||
if has_delimiter {
|
||||
envvar.has_delimiter = has_delimiter;
|
||||
envvar.delimiter = delimiter;
|
||||
@@ -38,20 +37,3 @@ 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");
|
||||
}
|
||||
}
|
||||
|
||||
+3
-41
@@ -2,52 +2,14 @@
|
||||
/// extract it into some strings
|
||||
pub fn delimitize(var: &crate::envy::EnvVar) -> Result<Vec<String>, std::io::Error> {
|
||||
if var.has_delimiter {
|
||||
let values: Vec<String> = match var
|
||||
Ok(var
|
||||
.value
|
||||
.split(var.delimiter)
|
||||
.map(|c| c.parse::<String>())
|
||||
.collect()
|
||||
{
|
||||
Ok(v) => v,
|
||||
Err(err) => {
|
||||
return Err(std::io::Error::other(err));
|
||||
}
|
||||
};
|
||||
|
||||
Ok(values)
|
||||
.map(|c| c.parse::<String>().unwrap())
|
||||
.collect())
|
||||
} 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:?}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+14
-68
@@ -54,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 = (*provided_time).into();
|
||||
let converted: std::time::SystemTime = provided_time.clone().into();
|
||||
Ok(converted)
|
||||
}
|
||||
}
|
||||
@@ -85,50 +85,10 @@ pub fn create_token(
|
||||
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: 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());
|
||||
|
||||
if !token_resource.user_id.is_nil() {
|
||||
match payload.set_claim("user_id", Some(serde_json::json!(token_resource.user_id))) {
|
||||
Ok(_) => {}
|
||||
@@ -137,38 +97,24 @@ fn init_payload(
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
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()));
|
||||
}
|
||||
};
|
||||
payload.set_issued_at(&util::time_to_std_time(&issued).unwrap());
|
||||
payload.set_expires_at(&util::time_to_std_time(&expire).unwrap());
|
||||
|
||||
payload.set_issued_at(&issued_at);
|
||||
payload.set_expires_at(&expires_at);
|
||||
Ok((issued, expire))
|
||||
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,
|
||||
})
|
||||
}
|
||||
Err(err) => Err(josekit::JoseError::InvalidClaim(err.into())),
|
||||
Err(e) => Err(josekit::JoseError::InvalidClaim(e.into())),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user