From 4732867ffa8041a3406dade3ae3ad32cf95f0a0c Mon Sep 17 00:00:00 2001 From: salmonik Date: Sat, 28 Feb 2026 19:19:28 +0100 Subject: add authentication barebone --- src/bin/client/connection.rs | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) (limited to 'src/bin/client/connection.rs') diff --git a/src/bin/client/connection.rs b/src/bin/client/connection.rs index d03dc1a..497ed36 100644 --- a/src/bin/client/connection.rs +++ b/src/bin/client/connection.rs @@ -3,8 +3,18 @@ use std::net::TcpStream; use std::sync::mpsc; use std::thread; use std::time::{Duration, Instant}; +use rusqlite::Connection; +use bcrypt::hash_with_salt; +use crate::database; -pub fn connect(addr: &str) -> Result> { +const SALT: [u8; 16] = *b"fixedsalt1234567"; + +fn hash_password(password: &str) -> String { + let result = hash_with_salt(password, 10, SALT).unwrap(); + result.to_string() +} + +pub fn connect(addr: &str, conn: &Connection) -> Result> { let (tx, rx) = mpsc::channel(); let addr_str = addr.to_string(); thread::spawn(move || { @@ -23,6 +33,21 @@ pub fn connect(addr: &str) -> Result> { ping_reader.read_line(&mut response).unwrap(); let elapsed = start.elapsed(); println!("\n\rConnected to {} with ping delay: {:?}", addr, elapsed); + + // Check for credentials and auto-auth + if let Some((username, password)) = database::get_credentials(conn, addr) { + let hashed = hash_password(&password); + let auth_msg = format!("AUTH {} {}", username, hashed); + writeln!(ping_writer, "{}", auth_msg).unwrap(); + let mut auth_response = String::new(); + ping_reader.read_line(&mut auth_response).unwrap(); + if auth_response.trim().starts_with("OK") { + println!("Authenticated as {}", username); + } else { + println!("Authentication failed: {}", auth_response.trim()); + // Do not fail, allow manual login + } + } Ok(s) } Ok(Err(e)) => Err(Box::new(e)), -- cgit v1.2.3