#11 signup email #36

Merged
silver merged 5 commits from #11_signup_email into main 2023-08-06 12:02:40 +00:00
2 changed files with 186 additions and 178 deletions
Showing only changes of commit 158292be05 - Show all commits

View file

@ -1,6 +1,9 @@
use skynet_ldap_backend::{ use skynet_ldap_backend::{
db_init, get_config, db_init, get_config,
methods::{account_new::post_new_account, account_update::post_update_ldap}, methods::{
account_new::post::{account, email},
account_update::post_update_ldap,
},
State, State,
}; };
@ -21,7 +24,8 @@ async fn main() -> tide::Result<()> {
let mut app = tide::with_state(state); let mut app = tide::with_state(state);
app.at("/ldap/update").post(post_update_ldap); app.at("/ldap/update").post(post_update_ldap);
app.at("/ldap/new").post(post_new_account); app.at("/ldap/new/email").post(email::submit);
app.at("/ldap/new/account").post(account::submit);
app.listen(host_port).await?; app.listen(host_port).await?;
Ok(()) Ok(())

View file

@ -232,19 +232,21 @@ pub mod post {
.await .await
} }
} }
}
#[derive(Debug, Deserialize)] pub mod account {
struct LdapNewUser { use super::*;
#[derive(Debug, Deserialize)]
struct LdapNewUser {
auth: String, auth: String,
user: String, user: String,
pass: String, pass: String,
} }
/// Handles initial detail entering page /// Handles initial detail entering page
/// Verify users have access to said email /// Verify users have access to said email
/// Get users to set username and password. /// Get users to set username and password.
pub async fn post_new_account(mut req: Request<State>) -> tide::Result { pub async fn submit(mut req: Request<State>) -> tide::Result {
let LdapNewUser { let LdapNewUser {
auth, auth,
user, user,
@ -291,10 +293,10 @@ pub async fn post_new_account(mut req: Request<State>) -> tide::Result {
account_verification_clear_pending(db, &auth).await?; account_verification_clear_pending(db, &auth).await?;
Ok(json!({"result": "success"}).into()) Ok(json!({"result": "success"}).into())
} }
// clear the db of expired ones before checking for username and validating inputs // clear the db of expired ones before checking for username and validating inputs
async fn db_pending_clear_expired(pool: &Pool<Sqlite>) -> Result<Vec<AccountsNew>, Error> { async fn db_pending_clear_expired(pool: &Pool<Sqlite>) -> Result<Vec<AccountsNew>, Error> {
sqlx::query_as::<_, AccountsNew>( sqlx::query_as::<_, AccountsNew>(
r#" r#"
DELETE DELETE
@ -305,9 +307,9 @@ async fn db_pending_clear_expired(pool: &Pool<Sqlite>) -> Result<Vec<AccountsNew
.bind(get_now_iso(true)) .bind(get_now_iso(true))
.fetch_all(pool) .fetch_all(pool)
.await .await
} }
fn is_valid_name(name: &str) -> Option<String> { fn is_valid_name(name: &str) -> Option<String> {
// max length is 31 chars // max length is 31 chars
if name.len() >= 32 { if name.len() >= 32 {
return Some(String::from("Too long, max len 31")); return Some(String::from("Too long, max len 31"));
@ -333,9 +335,9 @@ fn is_valid_name(name: &str) -> Option<String> {
} }
None None
} }
async fn db_get_user(pool: &Pool<Sqlite>, auth: &str) -> Option<AccountsNew> { async fn db_get_user(pool: &Pool<Sqlite>, auth: &str) -> Option<AccountsNew> {
if let Ok(res) = sqlx::query_as::<_, AccountsNew>( if let Ok(res) = sqlx::query_as::<_, AccountsNew>(
r#" r#"
SELECT * SELECT *
@ -353,9 +355,9 @@ async fn db_get_user(pool: &Pool<Sqlite>, auth: &str) -> Option<AccountsNew> {
} }
None None
} }
async fn ldap_create_account(config: &Config, db: &Pool<Sqlite>, user: AccountsNew, username: &str, pass: &str) -> Result<(), ldap3::LdapError> { async fn ldap_create_account(config: &Config, db: &Pool<Sqlite>, user: AccountsNew, username: &str, pass: &str) -> Result<(), ldap3::LdapError> {
let mut ldap = LdapConn::new(&config.ldap_host)?; let mut ldap = LdapConn::new(&config.ldap_host)?;
ldap.simple_bind(&config.ldap_admin, &config.ldap_admin_pw)?.success()?; ldap.simple_bind(&config.ldap_admin, &config.ldap_admin_pw)?.success()?;
@ -412,16 +414,16 @@ async fn ldap_create_account(config: &Config, db: &Pool<Sqlite>, user: AccountsN
ldap.unbind()?; ldap.unbind()?;
Ok(()) Ok(())
} }
fn get_sk_created() -> String { fn get_sk_created() -> String {
use chrono::Utc; use chrono::Utc;
let now = Utc::now(); let now = Utc::now();
format!("{}", now.format("%Y%m%d%H%M%SZ")) format!("{}", now.format("%Y%m%d%H%M%SZ"))
} }
async fn get_max_uid_number(db: &Pool<Sqlite>) -> i64 { async fn get_max_uid_number(db: &Pool<Sqlite>) -> i64 {
if let Ok(results) = sqlx::query_as::<_, Accounts>( if let Ok(results) = sqlx::query_as::<_, Accounts>(
r#" r#"
SELECT * SELECT *
@ -439,9 +441,9 @@ async fn get_max_uid_number(db: &Pool<Sqlite>) -> i64 {
} }
9999 9999
} }
async fn account_verification_clear_pending(db: &Pool<Sqlite>, auth_code: &str) -> Result<Vec<AccountsNew>, Error> { async fn account_verification_clear_pending(db: &Pool<Sqlite>, auth_code: &str) -> Result<Vec<AccountsNew>, Error> {
sqlx::query_as::<_, AccountsNew>( sqlx::query_as::<_, AccountsNew>(
r#" r#"
DELETE FROM accounts_new DELETE FROM accounts_new
@ -451,4 +453,6 @@ async fn account_verification_clear_pending(db: &Pool<Sqlite>, auth_code: &str)
.bind(auth_code) .bind(auth_code)
.fetch_all(db) .fetch_all(db)
.await .await
}
}
} }