2023-06-04 11:17:16 +00:00
|
|
|
pub mod methods;
|
|
|
|
use dotenv::dotenv;
|
2023-06-04 21:06:34 +00:00
|
|
|
use ldap3::{LdapConn, Scope, SearchEntry};
|
2023-05-25 23:02:12 +00:00
|
|
|
use sqlx::sqlite::{SqliteConnectOptions, SqlitePoolOptions};
|
|
|
|
use sqlx::{Error, Pool, Sqlite};
|
2023-06-04 11:17:16 +00:00
|
|
|
use std::env;
|
2023-05-25 23:02:12 +00:00
|
|
|
use std::str::FromStr;
|
|
|
|
use std::time::{SystemTime, UNIX_EPOCH};
|
2023-06-04 13:21:12 +00:00
|
|
|
use tide::prelude::*;
|
|
|
|
|
|
|
|
#[derive(Debug, Deserialize, Serialize, sqlx::FromRow)]
|
|
|
|
pub struct AccountsPending {
|
|
|
|
user: String,
|
|
|
|
mail: String,
|
2023-06-04 22:42:09 +00:00
|
|
|
cn: String,
|
|
|
|
sn: String,
|
2023-06-04 21:06:34 +00:00
|
|
|
auth_code: String,
|
2023-06-04 22:42:09 +00:00
|
|
|
|
|
|
|
// action will be what to do with it
|
|
|
|
action: String,
|
2023-06-04 13:21:12 +00:00
|
|
|
// will only last for a few hours
|
2023-06-04 21:06:34 +00:00
|
|
|
expiry: i64,
|
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 {
|
|
|
|
user: String,
|
2023-07-29 18:48:44 +00:00
|
|
|
uid: i64,
|
2023-06-04 20:16:24 +00:00
|
|
|
discord: Option<String>,
|
2023-07-29 18:48:44 +00:00
|
|
|
mail: String,
|
|
|
|
student_id: String,
|
2023-06-04 21:06:34 +00:00
|
|
|
enabled: bool,
|
2023-07-29 18:48:44 +00:00
|
|
|
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-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?;
|
|
|
|
|
|
|
|
sqlx::query(
|
2023-06-04 13:21:12 +00:00
|
|
|
"CREATE TABLE IF NOT EXISTS accounts_pending (
|
|
|
|
user text primary key,
|
|
|
|
mail text not null,
|
2023-06-04 22:42:09 +00:00
|
|
|
cn text not null,
|
|
|
|
sn text not null,
|
|
|
|
action text not null,
|
2023-06-04 13:21:12 +00:00
|
|
|
auth_code text not null,
|
|
|
|
expiry integer not null
|
2023-05-25 23:02:12 +00:00
|
|
|
)",
|
|
|
|
)
|
|
|
|
.execute(&pool)
|
|
|
|
.await?;
|
2023-05-25 23:51:36 +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 18:48:44 +00:00
|
|
|
sqlx::query("CREATE INDEX IF NOT EXISTS index_uid_number ON accounts (uid)")
|
2023-06-04 21:06:34 +00:00
|
|
|
.execute(&pool)
|
|
|
|
.await?;
|
2023-06-04 20:16:24 +00:00
|
|
|
|
|
|
|
update_accounts(&pool, config).await;
|
2023-06-04 21:06:34 +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
|
|
|
|
|
|
|
#[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,
|
|
|
|
}
|
|
|
|
|
|
|
|
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(),
|
|
|
|
};
|
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
config
|
|
|
|
}
|
2023-06-04 20:16:24 +00:00
|
|
|
|
|
|
|
async fn update_accounts(pool: &Pool<Sqlite>, config: &Config) {
|
|
|
|
let mut ldap = LdapConn::new(&config.ldap_host).unwrap();
|
|
|
|
|
|
|
|
ldap.simple_bind(&config.ldap_admin, &config.ldap_admin_pw).unwrap().success().unwrap();
|
|
|
|
|
2023-07-29 18:48:44 +00:00
|
|
|
// use this to pre load a large chunk of data
|
|
|
|
if let Ok(x) = ldap.search(
|
|
|
|
"ou=users,dc=skynet,dc=ie",
|
|
|
|
Scope::OneLevel,
|
|
|
|
"(objectClass=*)",
|
|
|
|
vec!["uid", "uidNumber", "skDiscord", "skMemberOf", "mail", "skID", "skSecure"]
|
|
|
|
) {
|
2023-06-04 20:16:24 +00:00
|
|
|
if let Ok((rs, _res)) = x.success() {
|
|
|
|
for entry in rs {
|
|
|
|
let tmp = SearchEntry::construct(entry);
|
|
|
|
|
|
|
|
let mut tmp_account = Accounts {
|
|
|
|
user: "".to_string(),
|
2023-07-29 18:48:44 +00:00
|
|
|
uid: 0,
|
2023-06-04 20:16:24 +00:00
|
|
|
discord: None,
|
2023-07-29 18:48:44 +00:00
|
|
|
mail: "".to_string(),
|
|
|
|
student_id: "".to_string(),
|
2023-06-04 20:16:24 +00:00
|
|
|
enabled: false,
|
2023-07-29 18:48:44 +00:00
|
|
|
secure: false,
|
2023-06-04 20:16:24 +00:00
|
|
|
};
|
2023-06-04 21:06:34 +00:00
|
|
|
|
2023-06-04 20:16:24 +00:00
|
|
|
// pull out the required info
|
|
|
|
if tmp.attrs.contains_key("uid") && !tmp.attrs["uid"].is_empty() {
|
|
|
|
tmp_account.user = tmp.attrs["uid"][0].clone();
|
|
|
|
}
|
|
|
|
if tmp.attrs.contains_key("uidNumber") && !tmp.attrs["uidNumber"].is_empty() {
|
2023-07-29 18:48:44 +00:00
|
|
|
tmp_account.uid = tmp.attrs["uidNumber"][0].clone().parse().unwrap_or(0);
|
2023-06-04 20:16:24 +00:00
|
|
|
}
|
|
|
|
if tmp.attrs.contains_key("skDiscord") && !tmp.attrs["skDiscord"].is_empty() {
|
2023-07-29 17:41:53 +00:00
|
|
|
tmp_account.discord = Option::from(tmp.attrs["skDiscord"][0].clone());
|
2023-06-04 20:16:24 +00:00
|
|
|
}
|
2023-07-29 18:48:44 +00:00
|
|
|
if tmp.attrs.contains_key("mail") && !tmp.attrs["mail"].is_empty() {
|
|
|
|
tmp_account.mail = tmp.attrs["mail"][0].clone();
|
|
|
|
}
|
|
|
|
if tmp.attrs.contains_key("skID") && !tmp.attrs["skID"].is_empty() {
|
|
|
|
tmp_account.student_id = tmp.attrs["skID"][0].clone();
|
|
|
|
}
|
2023-07-29 18:02:56 +00:00
|
|
|
if tmp.attrs.contains_key("skMemberOf")
|
|
|
|
&& !tmp.attrs["skMemberOf"].is_empty()
|
|
|
|
&& tmp.attrs["skMemberOf"].contains(&String::from("cn=skynet-users-linux,ou=groups,dc=skynet,dc=ie"))
|
|
|
|
{
|
2023-06-04 20:16:24 +00:00
|
|
|
tmp_account.enabled = true;
|
|
|
|
}
|
2023-07-29 18:48:44 +00:00
|
|
|
if tmp.attrs.contains_key("skSecure") && !tmp.attrs["skSecure"].is_empty() {
|
|
|
|
tmp_account.secure = true;
|
|
|
|
}
|
2023-06-04 20:16:24 +00:00
|
|
|
|
2023-06-04 21:06:34 +00:00
|
|
|
if !tmp_account.user.is_empty() {
|
2023-06-04 20:16:24 +00:00
|
|
|
sqlx::query_as::<_, Accounts>(
|
2023-06-04 21:06:34 +00:00
|
|
|
"
|
2023-07-29 18:48:44 +00:00
|
|
|
INSERT OR REPLACE INTO accounts (user, uid, discord, mail, student_id, enabled, secure)
|
|
|
|
VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7)
|
2023-06-04 20:16:24 +00:00
|
|
|
",
|
|
|
|
)
|
2023-06-04 21:06:34 +00:00
|
|
|
.bind(&tmp_account.user)
|
2023-07-29 18:48:44 +00:00
|
|
|
.bind(tmp_account.uid)
|
2023-06-04 21:06:34 +00:00
|
|
|
.bind(&tmp_account.discord)
|
2023-07-29 18:48:44 +00:00
|
|
|
.bind(&tmp_account.mail)
|
|
|
|
.bind(&tmp_account.student_id)
|
2023-06-04 21:06:34 +00:00
|
|
|
.bind(tmp_account.enabled)
|
2023-07-29 18:48:44 +00:00
|
|
|
.bind(tmp_account.secure)
|
2023-06-04 21:06:34 +00:00
|
|
|
.fetch_optional(pool)
|
|
|
|
.await
|
|
|
|
.ok();
|
2023-06-04 20:16:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// done with ldap
|
|
|
|
ldap.unbind().unwrap();
|
|
|
|
}
|