fn authenticate(&self) -> Result<Self::CookieType, AuthFail> {
        let conn = PGCONN.lock().unwrap();
        let authstr = format!(r#"
            SELECT u.userid, u.username, u.display FROM users u WHERE u.username = '{username}' AND 
                u.salt_hash = crypt('{password}', u.salt_hash)"#, username=&self.username, password=&self.password);
        let is_user_qrystr = format!("SELECT userid FROM users WHERE username = '{}'", &self.username);
        let is_admin_qrystr = format!("SELECT userid FROM users WHERE username = '{}' AND is_admin = '1'", &self.username);
        let password_qrystr = format!("SELECT u.userid FROM users u WHERE u.username = '{}' AND u.salt_hash = crypt('{}', u.salt_hash)", &self.username, &self.password);
        println!("Attempting query: {}", authstr);
        if let Ok(qry) = conn.query(&authstr, &[]) {
            if !qry.is_empty() && qry.len() == 1 {
                let row = qry.get(0);
                
                let display_opt = row.get_opt(2);
                let display = match display_opt {
                    Some(Ok(d)) => Some(d),
                    _ => None,
                };
                
                return Ok(AdministratorCookie {
                    userid: row.get(0),
                    username: row.get(1),
                    display,
                });
            }
        }
        if let Ok(eqry) = conn.query(&is_user_qrystr, &[]) {
            if eqry.is_empty() || eqry.len() == 0 {
                return Err(AuthFail::new(self.username.clone(), "Username was not found.".to_string()));
            }
        }
        if let Ok(eqry) = conn.query(&is_admin_qrystr, &[]) {
            if eqry.is_empty() || eqry.len() == 0 {
                // In production this message may be more harmful than useful as it
                // would be able to tell anyone who is an administrator and thus the
                // message should be changed to something like Unkown error or Invalid username/password
                return Err(AuthFail::new(self.username.clone(), "User does not have administrator priveleges.".to_string()));
            }
        }
        if let Ok(eqry) = conn.query(&password_qrystr, &[]) {
            if eqry.is_empty() || eqry.len() == 0 {
                return Err(AuthFail::new(self.username.clone(), "Invalid username / password combination.".to_string()));
            }
        }
        Err(AuthFail::new(self.username.clone(), "Unknown error..".to_string()))
    }
/// Authenticate the credentials inside the login form
Authenticate the credentials inside the login form
Authenticate the credentials inside the login form
fn authenticate(&self) -> Result<Self::CookieType, AuthFail> {
        let conn = PGCONN.lock().unwrap();
        let authstr = format!(r#"
            SELECT u.userid, u.username, u.display FROM users u WHERE u.username = '{username}' AND 
                u.salt_hash = crypt('{password}', u.salt_hash)"#, username=&self.username, password=&self.password);
        let is_user_qrystr = format!("SELECT userid FROM users WHERE username = '{}'", &self.username);
        let is_admin_qrystr = format!("SELECT userid FROM users WHERE username = '{}' AND is_admin = '1'", &self.username);
        let password_qrystr = format!("SELECT u.userid FROM users u WHERE u.username = '{}' AND u.salt_hash = crypt('{}', u.salt_hash)", &self.username, &self.password);
        println!("Attempting query: {}", authstr);
        if let Ok(qry) = conn.query(&authstr, &[]) {
            if !qry.is_empty() && qry.len() == 1 {
                let row = qry.get(0);
                let display_opt = row.get_opt(2);
                let display = match display_opt {
                    Some(Ok(d)) => Some(d),
                    _ => None,
                };
                return Ok(AdministratorCookie {
                    userid: row.get(0),
                    username: row.get(1),
                    display,
                });
            }
        }
        if let Ok(eqry) = conn.query(&is_user_qrystr, &[]) {
            if eqry.is_empty() || eqry.len() == 0 {
                return Err(AuthFail::new(self.username.clone(), "Username was not found.".to_string()));
            }
        }
        if let Ok(eqry) = conn.query(&is_admin_qrystr, &[]) {
            if eqry.is_empty() || eqry.len() == 0 {
                // In production this message may be more harmful than useful as it
                // would be able to tell anyone who is an administrator and thus the
                // message should be changed to something like Unkown error or Invalid username/password
                return Err(AuthFail::new(self.username.clone(), "User does not have administrator priveleges.".to_string()));
            }
        }
        if let Ok(eqry) = conn.query(&password_qrystr, &[]) {
            if eqry.is_empty() || eqry.len() == 0 {
                return Err(AuthFail::new(self.username.clone(), "Invalid username / password combination.".to_string()));
            }
        }
        Err(AuthFail::new(self.username.clone(), "Unknown error..".to_string()))
    }
fn authenticate ( & self ) -> Result < Self :: CookieType , AuthFail > { let conn = PGCONN . lock ( ) . unwrap ( ) ; let authstr = format ! ( r#"
            SELECT u.userid, u.username, u.display FROM users u WHERE u.username = '{username}' AND 
                u.salt_hash = crypt('{password}', u.salt_hash)"# username self username password self password ) ; let is_user_qrystr = format ! ( " " self username ) ; let is_admin_qrystr = format ! ( " " self username ) ; let password_qrystr = format ! ( " " self username self password ) ; println ! ( " " authstr ) ; if let Ok ( qry ) = conn . query ( & authstr , & [ ] ) { if ! qry . is_empty ( ) && qry . len ( ) == 1 { let row = qry . get ( 0 ) ; let display_opt = row . get_opt ( 2 ) ; let display = match display_opt { Some ( Ok ( d ) ) => Some ( d ) , _ => None , } ; return Ok ( AdministratorCookie { userid : row . get ( 0 ) , username : row . get ( 1 ) , display , } ) ; } } if let Ok ( eqry ) = conn . query ( & is_user_qrystr , & [ ] ) { if eqry . is_empty ( ) || eqry . len ( ) == 0 { return Err ( AuthFail :: new ( self . username . clone ( ) , " " . to_string ( ) ) ) ; } } if let Ok ( eqry ) = conn . query ( & is_admin_qrystr , & [ ] ) { if eqry . is_empty ( ) || eqry . len ( ) == 0 { // In production this message may be more harmful than useful as it // would be able to tell anyone who is an administrator and thus the // message should be changed to something like Unkown error or Invalid username/password return Err ( AuthFail :: new ( self . username . clone ( ) , " " . to_string ( ) ) ) ; } } if let Ok ( eqry ) = conn . query ( & password_qrystr , & [ ] ) { if eqry . is_empty ( ) || eqry . len ( ) == 0 { return Err ( AuthFail :: new ( self . username . clone ( ) , " " . to_string ( ) ) ) ; } } Err ( AuthFail :: new ( self . username . clone ( ) , " " . to_string ( ) ) ) }
Authenticate the credentials inside the login form
Authenticate the credentials inside the login form
&self
&self