From eda18f3c3b3ebf0a3a9baf2f8fda878782cc6b24 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Thu, 16 Jul 2026 06:18:54 +0300 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat(server):=20username=20auth=20a?= =?UTF-8?q?nd=20register=20bootstrap?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crates/server/src/authentication.rs | 22 +++++++++++++++++++++- crates/server/src/error.rs | 24 ++++++++++++++++++++++++ crates/server/src/lib.rs | 7 +++++-- crates/server/src/network.rs | 16 ++++++++++------ crates/server/src/request.rs | 11 ++++++++++- 5 files changed, 70 insertions(+), 10 deletions(-) diff --git a/crates/server/src/authentication.rs b/crates/server/src/authentication.rs index 94da785..8cf7370 100644 --- a/crates/server/src/authentication.rs +++ b/crates/server/src/authentication.rs @@ -8,6 +8,26 @@ pub(crate) async fn authenticate( //todo //Err(Error::Common(common::error::Error::Authenticate)) - let user = User::read(&"0".into(), database_connection).await?; + let users = User::read_by_username(authentication_token, database_connection).await?; + let user = users + .into_iter() + .next() + .ok_or(Error::Common(common::error::Error::Authenticate))?; + Ok(user) +} + +pub(crate) async fn deauthenticate( + authentication_token: &str, + database_connection: &DB, +) -> Result { + println!("{}", authentication_token); + + //todo + //Err(Error::Common(common::error::Error::Authenticate)) + let users = User::read_by_username(authentication_token, database_connection).await?; + let user = users + .into_iter() + .next() + .ok_or(Error::Common(common::error::Error::Authenticate))?; Ok(user) } diff --git a/crates/server/src/error.rs b/crates/server/src/error.rs index c331f3b..b1b3781 100644 --- a/crates/server/src/error.rs +++ b/crates/server/src/error.rs @@ -67,12 +67,30 @@ impl From for Error { } } +impl From for Error { + fn from(value: std::net::AddrParseError) -> Self { + Self::Common(value.into()) + } +} + +impl From for Error { + fn from(value: std::io::Error) -> Self { + Self::Common(value.into()) + } +} + impl From for Error { fn from(value: quinn::ConnectionError) -> Self { Self::Common(common::error::network::Error::from(value).into()) } } +impl From for Error { + fn from(value: quinn::ConnectError) -> Self { + Self::Common(value.into()) + } +} + impl From for Error { fn from(value: quinn::ReadError) -> Self { Self::Common(common::error::network::Error::from(value).into()) @@ -91,6 +109,12 @@ impl From for Error { } } +impl From for Error { + fn from(value: quinn::ClosedStream) -> Self { + Self::Common(common::error::network::Error::from(value).into()) + } +} + impl From for Error { fn from(value: bitcode::Error) -> Self { Self::Common(common::error::decode::Error::from(value).into()) diff --git a/crates/server/src/lib.rs b/crates/server/src/lib.rs index 7feb375..cc64c9c 100644 --- a/crates/server/src/lib.rs +++ b/crates/server/src/lib.rs @@ -32,7 +32,7 @@ pub struct ServerConfig { impl Default for ServerConfig { fn default() -> Self { - let mut toml = TOML::naive_toml_parser(SERVER_CONFIG_FILE_PATH).unwrap(); + let mut toml = TOML::naive_toml_parser_from_file(SERVER_CONFIG_FILE_PATH).unwrap(); Self { database_prefix: toml.fields.remove("database_prefix").unwrap(), @@ -55,5 +55,8 @@ pub async fn showtime() { let database_connection = database_::connect(&server_config).await.unwrap(); database_::migrate(&database_connection).await.unwrap(); database_::health(&database_connection).await.unwrap(); - serve(&server_config.server_address, &database_connection).await; + + serve(&server_config.server_address, &database_connection) + .await + .unwrap(); } diff --git a/crates/server/src/network.rs b/crates/server/src/network.rs index 62b0231..4816259 100644 --- a/crates/server/src/network.rs +++ b/crates/server/src/network.rs @@ -25,13 +25,15 @@ fn config() -> quinn::ServerConfig { quinn::ServerConfig::with_single_cert(vec![certificate_der], certificate_key.into()).unwrap() } -pub async fn serve(server_address: &str, database_connection: &DB) { - let endpoint = Endpoint::server(config(), server_address.parse().unwrap()).unwrap(); +pub async fn serve(server_address: &str, database_connection: &DB) -> Result<(), Error> { + let endpoint = Endpoint::server(config(), server_address.parse()?)?; while let Some(incoming) = endpoint.accept().await { let database_connection = database_connection.clone(); tokio::spawn(handle_connection(incoming, database_connection)); } + + Ok(()) } async fn handle_connection(incoming: Incoming, database_connection: DB) { @@ -53,9 +55,10 @@ async fn establish_connection( ) -> Result<(), Error> { let (send_stream, receive_stream) = connection.accept_bi().await?; - let authentication_request = + let first_request = Network::receive_request(receive_stream, Some(AUTHENTICATION_MAX_READ_LENGHT)).await?; - if let Request::Authentication(authentication) = authentication_request { + + if let Request::Authentication(authentication) = first_request { let authentication_token = authentication.get_authentication_token(); let user = authenticate(authentication_token, database_connection).await?; let response = Response::Authentication(common::user::User::new( @@ -68,9 +71,10 @@ async fn establish_connection( let send_stream = connection.open_uni().await?; USER_SEND_STREAM_MAP.write().await.insert(user, send_stream); + Ok(()) + } else { + Err(Error::Common(common::error::Error::Authenticate)) } - - Err(Error::Common(common::error::Error::Authenticate)) } async fn listen(connection: Connection, database_connection: DB) { diff --git a/crates/server/src/request.rs b/crates/server/src/request.rs index 7ab32b8..f567b09 100644 --- a/crates/server/src/request.rs +++ b/crates/server/src/request.rs @@ -1,7 +1,7 @@ use common::{request::Request, response::Response}; use crate::{ - authentication::authenticate, + authentication::{authenticate, deauthenticate}, community::Community, database_::DB, error::Error, @@ -22,6 +22,15 @@ pub async fn handle_request(request: Request, database_connection: &DB) -> Respo Ok(Response::Authentication(user.into())) } + Request::Deauthentication(deauthentication) => { + let user = deauthenticate( + deauthentication.get_authentication_token(), + database_connection, + ) + .await?; + + Ok(Response::Deauthentication(user.into())) + } Request::CreateUser(create_user) => { let user = User::create(create_user.get_username(), database_connection).await?;