forge
Explore
Log in

Don't you dare go Hollow

Username auth and register bootstrap

Browse files at this commit

eda18f3c3b3ebf0a3a9baf2f8fda878782cc6b24Ahmet Kaan GümüşParent: 6ac98a9
  • feat
    • username auth and register bootstrap
Show full message
✨ feat(server): username auth and register bootstrap
crates/server/src/authentication.rsmodified+21 -1
@@ -8,6 +8,26 @@ pub(crate) async fn authenticate(
8 8
9 //todo9 //todo
10 //Err(Error::Common(common::error::Error::Authenticate))10 //Err(Error::Common(common::error::Error::Authenticate))
11- let user = User::read(&"0".into(), database_connection).await?;11+ let users = User::read_by_username(authentication_token, database_connection).await?;
12+ let user = users
13+ .into_iter()
14+ .next()
15+ .ok_or(Error::Common(common::error::Error::Authenticate))?;
16+ Ok(user)
17+}
18+
19+pub(crate) async fn deauthenticate(
20+ authentication_token: &str,
21+ database_connection: &DB,
22+) -> Result<User, Error> {
23+ println!("{}", authentication_token);
24+
25+ //todo
26+ //Err(Error::Common(common::error::Error::Authenticate))
27+ let users = User::read_by_username(authentication_token, database_connection).await?;
28+ let user = users
29+ .into_iter()
30+ .next()
31+ .ok_or(Error::Common(common::error::Error::Authenticate))?;
12 Ok(user)32 Ok(user)
13 }33 }
crates/server/src/error.rsmodified+24 -0
@@ -67,12 +67,30 @@ impl From<surrealdb::Error> for Error {
67 }67 }
68 }68 }
69 69
70+impl From<std::net::AddrParseError> for Error {
71+ fn from(value: std::net::AddrParseError) -> Self {
72+ Self::Common(value.into())
73+ }
74+}
75+
76+impl From<std::io::Error> for Error {
77+ fn from(value: std::io::Error) -> Self {
78+ Self::Common(value.into())
79+ }
80+}
81+
70 impl From<quinn::ConnectionError> for Error {82 impl From<quinn::ConnectionError> for Error {
71 fn from(value: quinn::ConnectionError) -> Self {83 fn from(value: quinn::ConnectionError) -> Self {
72 Self::Common(common::error::network::Error::from(value).into())84 Self::Common(common::error::network::Error::from(value).into())
73 }85 }
74 }86 }
75 87
88+impl From<quinn::ConnectError> for Error {
89+ fn from(value: quinn::ConnectError) -> Self {
90+ Self::Common(value.into())
91+ }
92+}
93+
76 impl From<quinn::ReadError> for Error {94 impl From<quinn::ReadError> for Error {
77 fn from(value: quinn::ReadError) -> Self {95 fn from(value: quinn::ReadError) -> Self {
78 Self::Common(common::error::network::Error::from(value).into())96 Self::Common(common::error::network::Error::from(value).into())
@@ -91,6 +109,12 @@ impl From<quinn::WriteError> for Error {
91 }109 }
92 }110 }
93 111
112+impl From<quinn::ClosedStream> for Error {
113+ fn from(value: quinn::ClosedStream) -> Self {
114+ Self::Common(common::error::network::Error::from(value).into())
115+ }
116+}
117+
94 impl From<bitcode::Error> for Error {118 impl From<bitcode::Error> for Error {
95 fn from(value: bitcode::Error) -> Self {119 fn from(value: bitcode::Error) -> Self {
96 Self::Common(common::error::decode::Error::from(value).into())120 Self::Common(common::error::decode::Error::from(value).into())
crates/server/src/lib.rsmodified+5 -2
@@ -32,7 +32,7 @@ pub struct ServerConfig {
32 32
33 impl Default for ServerConfig {33 impl Default for ServerConfig {
34 fn default() -> Self {34 fn default() -> Self {
35- let mut toml = TOML::naive_toml_parser(SERVER_CONFIG_FILE_PATH).unwrap();35+ let mut toml = TOML::naive_toml_parser_from_file(SERVER_CONFIG_FILE_PATH).unwrap();
36 36
37 Self {37 Self {
38 database_prefix: toml.fields.remove("database_prefix").unwrap(),38 database_prefix: toml.fields.remove("database_prefix").unwrap(),
@@ -55,5 +55,8 @@ pub async fn showtime() {
55 let database_connection = database_::connect(&server_config).await.unwrap();55 let database_connection = database_::connect(&server_config).await.unwrap();
56 database_::migrate(&database_connection).await.unwrap();56 database_::migrate(&database_connection).await.unwrap();
57 database_::health(&database_connection).await.unwrap();57 database_::health(&database_connection).await.unwrap();
58- serve(&server_config.server_address, &database_connection).await;58+
59+ serve(&server_config.server_address, &database_connection)
60+ .await
61+ .unwrap();
59 }62 }
crates/server/src/network.rsmodified+10 -6
@@ -25,13 +25,15 @@ fn config() -> quinn::ServerConfig {
25 quinn::ServerConfig::with_single_cert(vec![certificate_der], certificate_key.into()).unwrap()25 quinn::ServerConfig::with_single_cert(vec![certificate_der], certificate_key.into()).unwrap()
26 }26 }
27 27
28-pub async fn serve(server_address: &str, database_connection: &DB) {28+pub async fn serve(server_address: &str, database_connection: &DB) -> Result<(), Error> {
29- let endpoint = Endpoint::server(config(), server_address.parse().unwrap()).unwrap();29+ let endpoint = Endpoint::server(config(), server_address.parse()?)?;
30 30
31 while let Some(incoming) = endpoint.accept().await {31 while let Some(incoming) = endpoint.accept().await {
32 let database_connection = database_connection.clone();32 let database_connection = database_connection.clone();
33 tokio::spawn(handle_connection(incoming, database_connection));33 tokio::spawn(handle_connection(incoming, database_connection));
34 }34 }
35+
36+ Ok(())
35 }37 }
36 38
37 async fn handle_connection(incoming: Incoming, database_connection: DB) {39 async fn handle_connection(incoming: Incoming, database_connection: DB) {
@@ -53,9 +55,10 @@ async fn establish_connection(
53 ) -> Result<(), Error> {55 ) -> Result<(), Error> {
54 let (send_stream, receive_stream) = connection.accept_bi().await?;56 let (send_stream, receive_stream) = connection.accept_bi().await?;
55 57
56- let authentication_request =58+ let first_request =
57 Network::receive_request(receive_stream, Some(AUTHENTICATION_MAX_READ_LENGHT)).await?;59 Network::receive_request(receive_stream, Some(AUTHENTICATION_MAX_READ_LENGHT)).await?;
58- if let Request::Authentication(authentication) = authentication_request {60+
61+ if let Request::Authentication(authentication) = first_request {
59 let authentication_token = authentication.get_authentication_token();62 let authentication_token = authentication.get_authentication_token();
60 let user = authenticate(authentication_token, database_connection).await?;63 let user = authenticate(authentication_token, database_connection).await?;
61 let response = Response::Authentication(common::user::User::new(64 let response = Response::Authentication(common::user::User::new(
@@ -68,9 +71,10 @@ async fn establish_connection(
68 71
69 let send_stream = connection.open_uni().await?;72 let send_stream = connection.open_uni().await?;
70 USER_SEND_STREAM_MAP.write().await.insert(user, send_stream);73 USER_SEND_STREAM_MAP.write().await.insert(user, send_stream);
74+ Ok(())
75+ } else {
76+ Err(Error::Common(common::error::Error::Authenticate))
71 }77 }
72-
73- Err(Error::Common(common::error::Error::Authenticate))
74 }78 }
75 79
76 async fn listen(connection: Connection, database_connection: DB) {80 async fn listen(connection: Connection, database_connection: DB) {
crates/server/src/request.rsmodified+10 -1
@@ -1,7 +1,7 @@
1 use common::{request::Request, response::Response};1 use common::{request::Request, response::Response};
2 2
3 use crate::{3 use crate::{
4- authentication::authenticate,4+ authentication::{authenticate, deauthenticate},
5 community::Community,5 community::Community,
6 database_::DB,6 database_::DB,
7 error::Error,7 error::Error,
@@ -22,6 +22,15 @@ pub async fn handle_request(request: Request, database_connection: &DB) -> Respo
22 22
23 Ok(Response::Authentication(user.into()))23 Ok(Response::Authentication(user.into()))
24 }24 }
25+ Request::Deauthentication(deauthentication) => {
26+ let user = deauthenticate(
27+ deauthentication.get_authentication_token(),
28+ database_connection,
29+ )
30+ .await?;
31+
32+ Ok(Response::Deauthentication(user.into()))
33+ }
25 Request::CreateUser(create_user) => {34 Request::CreateUser(create_user) => {
26 let user = User::create(create_user.get_username(), database_connection).await?;35 let user = User::create(create_user.get_username(), database_connection).await?;
27 36