fix: should allow folks to register

This commit is contained in:
silver 2023-09-27 23:47:37 +01:00
parent f60345493c
commit 9db8a238d2
7 changed files with 136 additions and 217 deletions

View file

@ -94,9 +94,9 @@ async fn update_ldap(config: &Config, db: &Pool<Sqlite>) {
if !tmp_account.user.is_empty() { if !tmp_account.user.is_empty() {
sqlx::query_as::<_, Accounts>( sqlx::query_as::<_, Accounts>(
" "
INSERT OR REPLACE INTO accounts (user, uid, discord, mail, student_id, enabled, secure) INSERT OR REPLACE INTO accounts (user, uid, discord, mail, student_id, enabled, secure)
VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7)
", ",
) )
.bind(&tmp_account.user) .bind(&tmp_account.user)
.bind(tmp_account.uid) .bind(tmp_account.uid)
@ -135,8 +135,7 @@ struct RecordCSV {
impl From<RecordCSV> for AccountWolves { impl From<RecordCSV> for AccountWolves {
fn from(input: RecordCSV) -> Self { fn from(input: RecordCSV) -> Self {
AccountWolves { AccountWolves {
id_wolves: "".to_string(), id_wolves: input.mem_id,
id_member: input.mem_id,
id_student: if input.id_student.is_empty() { None } else { Some(input.id_student) }, id_student: if input.id_student.is_empty() { None } else { Some(input.id_student) },
email: input.email, email: input.email,
expiry: input.expiry, expiry: input.expiry,
@ -169,12 +168,11 @@ fn get_csv(config: &Config) -> Result<Vec<RecordCSV>, Box<dyn std::error::Error>
async fn update_account(db: &Pool<Sqlite>, account: &AccountWolves) { async fn update_account(db: &Pool<Sqlite>, account: &AccountWolves) {
sqlx::query_as::<_, AccountWolves>( sqlx::query_as::<_, AccountWolves>(
" "
INSERT OR REPLACE INTO accounts_wolves (id_wolves, id_member, id_student, email, expiry, name_first, name_second) INSERT OR REPLACE INTO accounts_wolves (id_wolves, id_student, email, expiry, name_first, name_second)
VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7) VALUES (?1, ?2, ?3, ?4, ?5, ?6)
", ",
) )
.bind(&account.id_wolves) .bind(&account.id_wolves)
.bind(&account.id_member)
.bind(&account.id_student) .bind(&account.id_student)
.bind(&account.email) .bind(&account.email)
.bind(&account.expiry) .bind(&account.expiry)

View file

@ -90,10 +90,10 @@ async fn from_csv(db: &Pool<Sqlite>) -> Result<HashSet<String>, Box<dyn Error>>
async fn account_mail_get_uid(db: &Pool<Sqlite>, mail: &str) -> Option<String> { async fn account_mail_get_uid(db: &Pool<Sqlite>, mail: &str) -> Option<String> {
match sqlx::query_as::<_, Accounts>( match sqlx::query_as::<_, Accounts>(
r#" r#"
SELECT * SELECT *
FROM accounts FROM accounts
WHERE mail == ? WHERE mail == ?
"#, "#,
) )
.bind(mail) .bind(mail)
.fetch_one(db) .fetch_one(db)
@ -107,10 +107,10 @@ async fn account_mail_get_uid(db: &Pool<Sqlite>, mail: &str) -> Option<String> {
async fn account_id_get_uid(db: &Pool<Sqlite>, id: &str) -> Option<String> { async fn account_id_get_uid(db: &Pool<Sqlite>, id: &str) -> Option<String> {
match sqlx::query_as::<_, Accounts>( match sqlx::query_as::<_, Accounts>(
r#" r#"
SELECT * SELECT *
FROM accounts FROM accounts
WHERE student_id == ? WHERE student_id == ?
"#, "#,
) )
.bind(id) .bind(id)
.fetch_one(db) .fetch_one(db)
@ -160,10 +160,10 @@ async fn get_secure_sub(db: &Pool<Sqlite>, group: &HashSet<String>, cache: &mut
async fn is_secure(db: &Pool<Sqlite>, user: &str) -> bool { async fn is_secure(db: &Pool<Sqlite>, user: &str) -> bool {
match sqlx::query_as::<_, Accounts>( match sqlx::query_as::<_, Accounts>(
r#" r#"
SELECT * SELECT *
FROM accounts FROM accounts
WHERE user == ? AND secure == 1 WHERE user == ? AND secure == 1
"#, "#,
) )
.bind(user) .bind(user)
.fetch_all(db) .fetch_all(db)

View file

@ -1,4 +1,5 @@
pub mod methods; pub mod methods;
use chrono::{Datelike, SecondsFormat, Utc}; use chrono::{Datelike, SecondsFormat, Utc};
use dotenvy::dotenv; use dotenvy::dotenv;
use ldap3::{LdapConn, Mod}; use ldap3::{LdapConn, Mod};
@ -17,7 +18,6 @@ use tide::prelude::*;
#[derive(Debug, Deserialize, Serialize, sqlx::FromRow)] #[derive(Debug, Deserialize, Serialize, sqlx::FromRow)]
pub struct AccountWolves { pub struct AccountWolves {
pub id_wolves: String, pub id_wolves: String,
pub id_member: String,
pub id_student: Option<String>, pub id_student: Option<String>,
pub email: String, pub email: String,
pub expiry: String, pub expiry: String,
@ -71,29 +71,27 @@ pub async fn db_init(config: &Config) -> Result<Pool<Sqlite>, Error> {
sqlx::query( sqlx::query(
"CREATE TABLE IF NOT EXISTS accounts_wolves ( "CREATE TABLE IF NOT EXISTS accounts_wolves (
id_wolves text DEFAULT '', id_wolves text PRIMARY KEY,
id_member text DEFAULT '', id_student text,
id_student text, email text NOT NULL,
email text not null, expiry text NOT NULL,
expiry text not null, name_first text,
name_first text, name_second text
name_second text, )",
PRIMARY KEY (id_wolves, id_member)
)",
) )
.execute(&pool) .execute(&pool)
.await?; .await?;
sqlx::query( sqlx::query(
"CREATE TABLE IF NOT EXISTS accounts_new ( "CREATE TABLE IF NOT EXISTS accounts_new (
mail text primary key, mail text PRIMARY KEY,
auth_code text not null, auth_code text NOT NULL,
date_iso text not null, date_iso text NOT NULL,
date_expiry text not null, date_expiry text NOT NULL,
name_first text not null, name_first text NOT NULL,
name_surname integer not null, name_surname integer NOT NULL,
id_student text not null id_student text NOT NULL
)", )",
) )
.execute(&pool) .execute(&pool)
.await?; .await?;
@ -107,20 +105,20 @@ pub async fn db_init(config: &Config) -> Result<Pool<Sqlite>, Error> {
sqlx::query( sqlx::query(
"CREATE TABLE IF NOT EXISTS accounts_ssh ( "CREATE TABLE IF NOT EXISTS accounts_ssh (
user text primary key, user text PRIMARY KEY,
auth_code text not null, auth_code text NOT NULL,
email text not null email text NOT NULL
)", )",
) )
.execute(&pool) .execute(&pool)
.await?; .await?;
sqlx::query( sqlx::query(
"CREATE TABLE IF NOT EXISTS accounts_reset ( "CREATE TABLE IF NOT EXISTS accounts_reset (
user text primary key, user text PRIMARY KEY,
auth_code text not null, auth_code text NOT NULL,
date_expiry text not null date_expiry text NOT NULL
)", )",
) )
.execute(&pool) .execute(&pool)
.await?; .await?;
@ -135,14 +133,14 @@ pub async fn db_init(config: &Config) -> Result<Pool<Sqlite>, Error> {
// this is for active use // this is for active use
sqlx::query( sqlx::query(
"CREATE TABLE IF NOT EXISTS accounts ( "CREATE TABLE IF NOT EXISTS accounts (
user text primary key, user text PRIMARY KEY,
uid integer not null, uid integer NOT NULL,
discord text, discord text,
mail text not null, mail text NOT NULL,
student_id text not null, student_id text NOT NULL,
enabled integer not null, enabled integer NOT NULL,
secure integer not null secure integer NOT NULL
)", )",
) )
.execute(&pool) .execute(&pool)
.await?; .await?;
@ -271,9 +269,9 @@ pub fn random_string(len: usize) -> String {
pub async fn get_wolves(db: &Pool<Sqlite>) -> Vec<AccountWolves> { pub async fn get_wolves(db: &Pool<Sqlite>) -> Vec<AccountWolves> {
sqlx::query_as::<_, AccountWolves>( sqlx::query_as::<_, AccountWolves>(
r#" r#"
SELECT * SELECT *
FROM accounts_wolves FROM accounts_wolves
"#, "#,
) )
.fetch_all(db) .fetch_all(db)
.await .await

View file

@ -1,6 +1,6 @@
use skynet_ldap_backend::{ use skynet_ldap_backend::{
db_init, get_config, db_init, get_config,
methods::{account_new, account_recover, account_update, discord}, methods::{account_new, account_recover, account_update},
State, State,
}; };
@ -34,9 +34,6 @@ async fn main() -> tide::Result<()> {
app.at("/ldap/recover/ssh/request").post(account_recover::ssh::request); app.at("/ldap/recover/ssh/request").post(account_recover::ssh::request);
app.at("/ldap/recover/ssh/verify").post(account_recover::ssh::verify); app.at("/ldap/recover/ssh/verify").post(account_recover::ssh::verify);
// for discord
app.at("/ldap/discord").get(discord::account::get);
app.listen(host_port).await?; app.listen(host_port).await?;
Ok(()) Ok(())
} }

View file

@ -231,47 +231,47 @@ pub mod password {
// Create the html we want to send. // Create the html we want to send.
let html = html! { let html = html! {
head { head {
title { "Hello from Skynet!" } title { "Hello from Skynet!" }
style type="text/css" { style type="text/css" {
"h2, h4 { font-family: Arial, Helvetica, sans-serif; }" "h2, h4 { font-family: Arial, Helvetica, sans-serif; }"
}
} }
div style="display: flex; flex-direction: column; align-items: center;" { }
h2 { "Hello from Skynet!" } div style="display: flex; flex-direction: column; align-items: center;" {
// Substitute in the name of our recipient. h2 { "Hello from Skynet!" }
p { "Hi " (recipient) "," } // Substitute in the name of our recipient.
p { p { "Hi " (recipient) "," }
"Here is your password reset link:" p {
br; "Here is your password reset link:"
a href=(link_new) { (link_new) } br;
} a href=(link_new) { (link_new) }
p {
"If did not request this please ignore."
}
p {
"UL Computer Society"
br;
"Skynet Team"
br;
a href=(discord) { (discord) }
}
} }
p {
"If did not request this please ignore."
}
p {
"UL Computer Society"
br;
"Skynet Team"
br;
a href=(discord) { (discord) }
}
}
}; };
let body_text = format!( let body_text = format!(
r#" r#"
Hi {recipient} Hi {recipient}
Here is your password reset link: Here is your password reset link:
{link_new} {link_new}
If did not request this please ignore. If did not request this please ignore.
UL Computer Society UL Computer Society
Skynet Team Skynet Team
{discord} {discord}
"# "#
); );
// Build the message. // Build the message.
@ -368,44 +368,44 @@ pub mod username {
// Create the html we want to send. // Create the html we want to send.
let html = html! { let html = html! {
head { head {
title { "Hello from Skynet!" } title { "Hello from Skynet!" }
style type="text/css" { style type="text/css" {
"h2, h4 { font-family: Arial, Helvetica, sans-serif; }" "h2, h4 { font-family: Arial, Helvetica, sans-serif; }"
}
} }
div style="display: flex; flex-direction: column; align-items: center;" { }
h2 { "Hello from Skynet!" } div style="display: flex; flex-direction: column; align-items: center;" {
// Substitute in the name of our recipient. h2 { "Hello from Skynet!" }
p { "Hi there," } // Substitute in the name of our recipient.
p { p { "Hi there," }
"You requested a username reminder: " (recipient) p {
} "You requested a username reminder: " (recipient)
p {
"If did not request this please ignore."
}
p {
"UL Computer Society"
br;
"Skynet Team"
br;
a href=(discord) { (discord) }
}
} }
p {
"If did not request this please ignore."
}
p {
"UL Computer Society"
br;
"Skynet Team"
br;
a href=(discord) { (discord) }
}
}
}; };
let body_text = format!( let body_text = format!(
r#" r#"
Hi there, Hi there,
You requested a username reminder: {recipient} You requested a username reminder: {recipient}
If did not request this please ignore. If did not request this please ignore.
UL Computer Society UL Computer Society
Skynet Team Skynet Team
{discord} {discord}
"# "#
); );
// Build the message. // Build the message.
@ -485,10 +485,10 @@ pub mod ssh {
// check if there is ane listing entry, use that auth if exists // check if there is ane listing entry, use that auth if exists
if let Ok(result) = sqlx::query_as::<_, AccountsSSH>( if let Ok(result) = sqlx::query_as::<_, AccountsSSH>(
r#" r#"
SELECT * SELECT *
FROM accounts_ssh FROM accounts_ssh
WHERE user == ? WHERE user == ?
"#, "#,
) )
.bind(&user) .bind(&user)
.fetch_one(db) .fetch_one(db)
@ -501,9 +501,9 @@ pub mod ssh {
let auth = random_string(50); let auth = random_string(50);
if sqlx::query_as::<_, AccountsSSH>( if sqlx::query_as::<_, AccountsSSH>(
" "
INSERT OR REPLACE INTO accounts_ssh (user, auth_code, email) INSERT OR REPLACE INTO accounts_ssh (user, auth_code, email)
VALUES (?1, ?2, ?3) VALUES (?1, ?2, ?3)
", ",
) )
.bind(&user) .bind(&user)
.bind(&auth) .bind(&auth)
@ -537,10 +537,10 @@ pub mod ssh {
let config = &req.state().config; let config = &req.state().config;
let details = if let Ok(result) = sqlx::query_as::<_, AccountsSSH>( let details = if let Ok(result) = sqlx::query_as::<_, AccountsSSH>(
r#" r#"
SELECT * SELECT *
FROM accounts_ssh FROM accounts_ssh
WHERE user == ? WHERE user == ?
"#, "#,
) )
.bind(&user) .bind(&user)
.fetch_one(db) .fetch_one(db)
@ -596,9 +596,9 @@ pub mod ssh {
// delete from tmp // delete from tmp
sqlx::query_as::<_, AccountsSSH>( sqlx::query_as::<_, AccountsSSH>(
r#" r#"
DELETE FROM accounts_ssh DELETE FROM accounts_ssh
WHERE user == ? WHERE user == ?
"#, "#,
) )
.bind(&user) .bind(&user)
.fetch_optional(db) .fetch_optional(db)

View file

@ -1,73 +0,0 @@
use crate::{Accounts, State};
use sqlx::{Pool, Sqlite};
use tide::{
prelude::{json, Deserialize},
Request,
};
pub mod account {
use super::*;
use crate::methods::account_new::email::get_wolves_mail;
use serde::Serialize;
#[derive(Debug, Deserialize)]
struct Auth {
auth: String,
}
pub async fn get(req: Request<State>) -> tide::Result {
let config = &req.state().config;
if let Ok(auth) = req.query::<Auth>() {
if auth.auth != config.auth_discord {
return Ok(json!([]).into());
}
} else {
return Ok(json!([]).into());
};
let db = &req.state().db;
let result = get_discord_users(db).await;
Ok(json!(result).into())
}
#[derive(Debug, Deserialize, Serialize)]
pub struct DiscordResult {
discord: String,
id_wolves: String,
id_member: String,
}
pub async fn get_discord_users(db: &Pool<Sqlite>) -> Vec<DiscordResult> {
let results = sqlx::query_as::<_, Accounts>(
r#"
SELECT *
FROM accounts
WHERE discord IS NOT NULL AND enabled = 1
"#,
)
.fetch_all(db)
.await
.unwrap_or(vec![]);
let mut result = vec![];
for item in results {
if let Some(discord) = item.discord {
let accounts = get_wolves_mail(db, &item.mail).await;
if !accounts.is_empty() {
let tmp = DiscordResult {
discord,
id_wolves: accounts[0].id_wolves.to_owned(),
id_member: accounts[0].id_member.to_owned(),
};
result.push(tmp);
}
}
}
result
}
}

View file

@ -1,4 +1,3 @@
pub mod account_new; pub mod account_new;
pub mod account_recover; pub mod account_recover;
pub mod account_update; pub mod account_update;
pub mod discord;