2023-06-04 11:17:16 +00:00
|
|
|
pub mod methods;
|
2023-07-30 01:50:13 +00:00
|
|
|
use chrono::{Datelike, SecondsFormat, Utc};
|
2023-07-29 21:19:15 +00:00
|
|
|
use dotenvy::dotenv;
|
2023-07-30 01:50:13 +00:00
|
|
|
use rand::{distributions::Alphanumeric, thread_rng, Rng};
|
|
|
|
use sqlx::{
|
|
|
|
sqlite::{SqliteConnectOptions, SqlitePoolOptions},
|
|
|
|
Error, Pool, Sqlite,
|
|
|
|
};
|
|
|
|
use std::{
|
|
|
|
env,
|
|
|
|
str::FromStr,
|
|
|
|
time::{SystemTime, UNIX_EPOCH},
|
|
|
|
};
|
2023-06-04 13:21:12 +00:00
|
|
|
use tide::prelude::*;
|
|
|
|
|
2023-08-05 21:00:18 +00:00
|
|
|
#[derive(Debug, Deserialize, Serialize, sqlx::FromRow)]
|
|
|
|
pub struct AccountWolves {
|
|
|
|
pub id_wolves: String,
|
|
|
|
pub id_student: String,
|
|
|
|
pub email: String,
|
|
|
|
pub expiry: String,
|
|
|
|
pub name_first: String,
|
|
|
|
pub name_second: String,
|
|
|
|
}
|
|
|
|
|
2023-07-30 01:02:56 +00:00
|
|
|
#[derive(Debug, Clone, Deserialize, Serialize, sqlx::FromRow)]
|
2023-07-29 20:32:16 +00:00
|
|
|
pub struct AccountsNew {
|
2023-07-29 22:10:19 +00:00
|
|
|
pub mail: String,
|
|
|
|
pub auth_code: String,
|
|
|
|
pub date_iso: String,
|
|
|
|
pub date_expiry: String,
|
|
|
|
pub name_first: String,
|
|
|
|
pub name_surname: String,
|
2023-07-30 01:50:13 +00:00
|
|
|
pub id_student: String,
|
2023-06-04 13:21:12 +00:00
|
|
|
}
|
2023-05-25 23:02:12 +00:00
|
|
|
|
2023-06-04 20:16:24 +00:00
|
|
|
#[derive(Debug, Deserialize, Serialize, sqlx::FromRow)]
|
|
|
|
pub struct Accounts {
|
2023-07-30 00:19:06 +00:00
|
|
|
pub user: String,
|
|
|
|
pub uid: i64,
|
|
|
|
pub discord: Option<String>,
|
|
|
|
pub mail: String,
|
|
|
|
pub student_id: String,
|
|
|
|
pub enabled: bool,
|
|
|
|
pub secure: bool,
|
2023-06-04 20:16:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn db_init(config: &Config) -> Result<Pool<Sqlite>, Error> {
|
2023-07-17 00:24:52 +00:00
|
|
|
let database = format!("{}/{}", &config.home, &config.database);
|
2023-08-05 19:21:04 +00:00
|
|
|
println!("Database: {:?}", &database);
|
2023-05-25 23:02:12 +00:00
|
|
|
let pool = SqlitePoolOptions::new()
|
|
|
|
.max_connections(5)
|
|
|
|
.connect_with(SqliteConnectOptions::from_str(&format!("sqlite://{}", database))?.create_if_missing(true))
|
|
|
|
.await?;
|
|
|
|
|
2023-08-05 20:51:09 +00:00
|
|
|
sqlx::query(
|
|
|
|
"CREATE TABLE IF NOT EXISTS accounts_wolves (
|
|
|
|
id_wolves text primary key,
|
|
|
|
id_student text not null,
|
|
|
|
email text not null,
|
|
|
|
expiry text not null,
|
|
|
|
name_first text not null,
|
2023-08-05 23:03:46 +00:00
|
|
|
name_second integer not null
|
2023-08-05 20:51:09 +00:00
|
|
|
)",
|
|
|
|
)
|
2023-08-05 21:00:18 +00:00
|
|
|
.execute(&pool)
|
|
|
|
.await?;
|
2023-08-05 20:51:09 +00:00
|
|
|
|
2023-07-29 20:32:16 +00:00
|
|
|
sqlx::query(
|
|
|
|
"CREATE TABLE IF NOT EXISTS accounts_new (
|
|
|
|
mail text primary key,
|
|
|
|
auth_code text not null,
|
|
|
|
date_iso text not null,
|
|
|
|
date_expiry text not null,
|
|
|
|
name_first text not null,
|
2023-07-30 01:50:13 +00:00
|
|
|
name_surname integer not null,
|
|
|
|
id_student text not null
|
2023-07-29 20:32:16 +00:00
|
|
|
)",
|
|
|
|
)
|
2023-07-29 23:48:15 +00:00
|
|
|
.execute(&pool)
|
|
|
|
.await?;
|
2023-07-29 20:32:16 +00:00
|
|
|
|
2023-07-30 19:46:44 +00:00
|
|
|
sqlx::query("CREATE INDEX IF NOT EXISTS index_auth_code ON accounts_new (auth_code)").execute(&pool).await?;
|
|
|
|
sqlx::query("CREATE INDEX IF NOT EXISTS index_date_expiry ON accounts_new (date_expiry)").execute(&pool).await?;
|
2023-07-29 20:32:16 +00:00
|
|
|
|
2023-06-04 20:16:24 +00:00
|
|
|
// this is for active use
|
|
|
|
sqlx::query(
|
|
|
|
"CREATE TABLE IF NOT EXISTS accounts (
|
|
|
|
user text primary key,
|
2023-07-29 18:48:44 +00:00
|
|
|
uid integer not null,
|
2023-06-04 20:16:24 +00:00
|
|
|
discord text,
|
2023-07-29 18:48:44 +00:00
|
|
|
mail text not null,
|
|
|
|
student_id text not null,
|
|
|
|
enabled integer not null,
|
|
|
|
secure integer not null
|
2023-06-04 20:16:24 +00:00
|
|
|
)",
|
2023-06-04 21:06:34 +00:00
|
|
|
)
|
|
|
|
.execute(&pool)
|
|
|
|
.await?;
|
2023-06-04 20:16:24 +00:00
|
|
|
|
2023-07-29 23:48:15 +00:00
|
|
|
sqlx::query("CREATE INDEX IF NOT EXISTS index_uid_number ON accounts (uid)").execute(&pool).await?;
|
2023-07-30 01:50:13 +00:00
|
|
|
sqlx::query("CREATE INDEX IF NOT EXISTS index_mail ON accounts (mail)").execute(&pool).await?;
|
|
|
|
sqlx::query("CREATE INDEX IF NOT EXISTS index_student_id ON accounts (student_id)")
|
|
|
|
.execute(&pool)
|
|
|
|
.await?;
|
2023-06-04 20:16:24 +00:00
|
|
|
|
2023-05-25 23:02:12 +00:00
|
|
|
Ok(pool)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_now() -> i64 {
|
|
|
|
if let Ok(x) = SystemTime::now().duration_since(UNIX_EPOCH) {
|
|
|
|
x.as_secs() as i64
|
|
|
|
} else {
|
|
|
|
0
|
|
|
|
}
|
|
|
|
}
|
2023-06-04 11:17:16 +00:00
|
|
|
|
2023-07-30 00:32:01 +00:00
|
|
|
pub fn get_now_iso(short: bool) -> String {
|
|
|
|
let now = Utc::now();
|
|
|
|
if short {
|
|
|
|
format!("{}-{:02}-{:02}", now.year(), now.month(), now.day())
|
|
|
|
} else {
|
|
|
|
now.to_rfc3339_opts(SecondsFormat::Millis, true)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-04 11:17:16 +00:00
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct State {
|
|
|
|
pub db: Pool<Sqlite>,
|
|
|
|
pub config: Config,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct Config {
|
2023-06-18 16:19:59 +00:00
|
|
|
pub ldap_host: String,
|
|
|
|
pub ldap_admin: String,
|
|
|
|
pub ldap_admin_pw: String,
|
2023-07-17 00:24:52 +00:00
|
|
|
pub home: String,
|
2023-06-04 11:17:16 +00:00
|
|
|
pub database: String,
|
2023-07-17 00:24:52 +00:00
|
|
|
pub csv: String,
|
2023-06-04 11:17:16 +00:00
|
|
|
pub host_port: String,
|
2023-07-29 20:38:03 +00:00
|
|
|
pub mail_smtp: String,
|
|
|
|
pub mail_user: String,
|
|
|
|
pub mail_pass: String,
|
2023-06-04 11:17:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_config() -> Config {
|
|
|
|
dotenv().ok();
|
|
|
|
|
|
|
|
// reasonable defaults
|
|
|
|
let mut config = Config {
|
|
|
|
ldap_host: "".to_string(),
|
2023-06-04 18:39:01 +00:00
|
|
|
ldap_admin: "".to_string(),
|
|
|
|
ldap_admin_pw: "".to_string(),
|
2023-07-17 00:24:52 +00:00
|
|
|
home: ".".to_string(),
|
2023-06-04 11:17:16 +00:00
|
|
|
database: "database.db".to_string(),
|
2023-07-17 00:24:52 +00:00
|
|
|
csv: "wolves.csv".to_string(),
|
2023-06-04 11:17:16 +00:00
|
|
|
host_port: "127.0.0.1:8087".to_string(),
|
2023-07-29 20:38:03 +00:00
|
|
|
mail_smtp: "".to_string(),
|
|
|
|
mail_user: "".to_string(),
|
|
|
|
mail_pass: "".to_string(),
|
2023-06-04 11:17:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
if let Ok(x) = env::var("LDAP_HOST") {
|
|
|
|
config.ldap_host = x.trim().to_string();
|
|
|
|
}
|
2023-06-04 18:39:01 +00:00
|
|
|
if let Ok(x) = env::var("LDAP_ADMIN") {
|
|
|
|
config.ldap_admin = x.trim().to_string();
|
|
|
|
}
|
|
|
|
if let Ok(x) = env::var("LDAP_ADMIN_PW") {
|
|
|
|
config.ldap_admin_pw = x.trim().to_string();
|
|
|
|
}
|
2023-07-17 00:24:52 +00:00
|
|
|
if let Ok(x) = env::var("HOME") {
|
|
|
|
config.home = x.trim().to_string();
|
|
|
|
}
|
2023-06-04 11:17:16 +00:00
|
|
|
if let Ok(x) = env::var("DATABASE") {
|
|
|
|
config.database = x.trim().to_string();
|
|
|
|
}
|
2023-07-17 00:24:52 +00:00
|
|
|
if let Ok(x) = env::var("CSV") {
|
|
|
|
config.csv = x.trim().to_string();
|
|
|
|
}
|
2023-06-04 11:17:16 +00:00
|
|
|
if let Ok(x) = env::var("HOST_PORT") {
|
|
|
|
config.host_port = x.trim().to_string();
|
|
|
|
}
|
2023-07-29 20:38:03 +00:00
|
|
|
if let Ok(x) = env::var("EMAIL_SMTP") {
|
|
|
|
config.mail_smtp = x.trim().to_string();
|
|
|
|
}
|
|
|
|
if let Ok(x) = env::var("EMAIL_USER") {
|
|
|
|
config.mail_user = x.trim().to_string();
|
|
|
|
}
|
|
|
|
if let Ok(x) = env::var("EMAIL_PASS") {
|
|
|
|
config.mail_pass = x.trim().to_string();
|
|
|
|
}
|
2023-06-04 11:17:16 +00:00
|
|
|
|
|
|
|
config
|
|
|
|
}
|
2023-06-04 20:16:24 +00:00
|
|
|
|
2023-07-30 01:02:56 +00:00
|
|
|
// from https://rust-lang-nursery.github.io/rust-cookbook/algorithms/randomness.html#create-random-passwords-from-a-set-of-alphanumeric-characters
|
|
|
|
pub fn random_string(len: usize) -> String {
|
|
|
|
thread_rng().sample_iter(&Alphanumeric).take(len).map(char::from).collect()
|
|
|
|
}
|
2023-08-05 21:00:18 +00:00
|
|
|
|
|
|
|
pub async fn get_wolves(db: &Pool<Sqlite>) -> Vec<AccountWolves> {
|
|
|
|
sqlx::query_as::<_, AccountWolves>(
|
|
|
|
r#"
|
|
|
|
SELECT *
|
|
|
|
FROM accounts_wolves
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.fetch_all(db)
|
|
|
|
.await
|
|
|
|
.unwrap_or(vec![])
|
|
|
|
}
|