Fake auth
This commit is contained in:
+20
@@ -0,0 +1,20 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use serde::Deserialize;
|
||||
|
||||
use crate::{Server, utils::client::Client};
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
struct AuthApiRes {
|
||||
uuid: u32,
|
||||
}
|
||||
|
||||
pub fn auth(_server: &Arc<Server>, client: &mut Client, token: &str) -> crate::Result<u32> {
|
||||
// let mut res = ureq::get(format!("https://api.voxa.org/server-auth?token={token}")).call()?;
|
||||
|
||||
// let api_res: AuthApiRes = serde_json::from_str(&res.body_mut().read_to_string()?)?;
|
||||
|
||||
client.set_uuid(232);
|
||||
|
||||
Ok(232)
|
||||
}
|
||||
+8
-6
@@ -5,6 +5,7 @@ use std::{
|
||||
sync::{Arc, Mutex},
|
||||
};
|
||||
|
||||
pub mod auth;
|
||||
pub mod macros;
|
||||
pub mod requests;
|
||||
pub mod types;
|
||||
@@ -110,7 +111,7 @@ impl Server {
|
||||
fn handle_client(self: &Arc<Self>, stream: TcpStream) -> anyhow::Result<()> {
|
||||
Self::LOGGER.info(format!("New connection: {}", stream.peer_addr()?));
|
||||
// Initialize client
|
||||
let client = Client::new(stream)?;
|
||||
let mut client = Client::new(stream)?;
|
||||
|
||||
// Initialize handshake
|
||||
self.wrap_err(
|
||||
@@ -123,13 +124,14 @@ impl Server {
|
||||
)?;
|
||||
|
||||
match self.wrap_err(&client, client.read_t::<types::handshake::ClientDetails>())? {
|
||||
Some(types::WsMessage::Message(_)) => {
|
||||
// Do auth stuff
|
||||
Some(types::WsMessage::Message(types::handshake::ClientDetails {
|
||||
auth_token, ..
|
||||
})) => {
|
||||
let auth_res = auth::auth(self, &mut client, &auth_token);
|
||||
let uuid = self.wrap_err(&client, auth_res)?;
|
||||
self.wrap_err(
|
||||
&client,
|
||||
client.send(types::ServerMessage::Authenticated {
|
||||
user_id: format!("<placeholder>"),
|
||||
}),
|
||||
client.send(types::ServerMessage::Authenticated { uuid }),
|
||||
)?;
|
||||
}
|
||||
Some(_) => {
|
||||
|
||||
@@ -26,7 +26,7 @@ pub fn send(
|
||||
&client,
|
||||
server.db.insert_message(
|
||||
&channel_id,
|
||||
"idk",
|
||||
client.get_uuid()?,
|
||||
&contents,
|
||||
chrono::Utc::now().timestamp(),
|
||||
),
|
||||
|
||||
+4
-2
@@ -25,7 +25,9 @@ pub enum ClientMessage {
|
||||
#[serde(tag = "type", content = "params", rename_all = "snake_case")]
|
||||
pub enum ServerMessage {
|
||||
/// Successful authentication
|
||||
Authenticated { user_id: String },
|
||||
Authenticated {
|
||||
uuid: u32,
|
||||
},
|
||||
|
||||
TempMessage {
|
||||
message: String,
|
||||
@@ -82,7 +84,7 @@ pub mod data {
|
||||
pub struct Message {
|
||||
pub id: i64,
|
||||
pub channel_id: String,
|
||||
pub from: String,
|
||||
pub from: u32,
|
||||
pub contents: String,
|
||||
pub timestamp: i64,
|
||||
}
|
||||
|
||||
+12
-4
@@ -100,13 +100,13 @@ pub mod handshake {
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Client(pub TcpStream);
|
||||
pub struct Client(TcpStream, Option<u32>);
|
||||
|
||||
impl Client {
|
||||
/// Create a client with no timeouts
|
||||
pub fn new(mut stream: TcpStream) -> crate::Result<Self> {
|
||||
handshake::handle_websocket_handshake(&mut stream)?;
|
||||
Ok(Client(stream))
|
||||
Ok(Client(stream, None))
|
||||
}
|
||||
|
||||
/// Create a client and set read/write timeouts (useful in prod)
|
||||
@@ -122,7 +122,7 @@ impl Client {
|
||||
stream.set_write_timeout(Some(t))?;
|
||||
}
|
||||
handshake::handle_websocket_handshake(&mut stream)?;
|
||||
Ok(Client(stream))
|
||||
Ok(Client(stream, None))
|
||||
}
|
||||
|
||||
/// Send a close frame and flush. `code` is a WebSocket close code (e.g., 1000 normal).
|
||||
@@ -323,11 +323,19 @@ impl Client {
|
||||
pub fn read(&self) -> crate::Result<Option<WsMessage<ClientMessage>>> {
|
||||
self.read_t()
|
||||
}
|
||||
|
||||
pub fn get_uuid(&self) -> crate::Result<u32> {
|
||||
self.1.ok_or(anyhow!("Invalid UUID"))
|
||||
}
|
||||
|
||||
pub fn set_uuid(&mut self, uuid: u32) {
|
||||
self.1 = Some(uuid)
|
||||
}
|
||||
}
|
||||
|
||||
impl Clone for Client {
|
||||
fn clone(&self) -> Self {
|
||||
Client(self.0.try_clone().expect("failed to clone TcpStream"))
|
||||
Client(self.0.try_clone().expect("failed to clone TcpStream"), None)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+26
-5
@@ -1,7 +1,7 @@
|
||||
use crate::{ServerConfig, types::data::Message};
|
||||
use rusqlite::{Connection, Result, params};
|
||||
|
||||
pub struct Database(pub Connection);
|
||||
pub struct Database(Connection);
|
||||
|
||||
// General use case
|
||||
impl Database {
|
||||
@@ -30,13 +30,13 @@ impl Database {
|
||||
pub fn insert_message(
|
||||
&self,
|
||||
channel_id: &str,
|
||||
user_id: &str,
|
||||
user_id: u32,
|
||||
contents: &str,
|
||||
timestamp: i64,
|
||||
) -> Result<Message> {
|
||||
self.0.execute(
|
||||
"INSERT INTO chat (channel_id, user_id, contents, timestamp)
|
||||
VALUES (?1, ?2, ?3, ?4)",
|
||||
VALUES (?1, ?2, ?3, ?4)",
|
||||
params![channel_id, user_id, contents, timestamp],
|
||||
)?;
|
||||
|
||||
@@ -45,12 +45,33 @@ impl Database {
|
||||
Ok(Message {
|
||||
id,
|
||||
channel_id: channel_id.to_string(),
|
||||
from: user_id.to_string(),
|
||||
from: user_id,
|
||||
contents: contents.to_string(),
|
||||
timestamp,
|
||||
})
|
||||
}
|
||||
|
||||
/// Delete a message from the DB
|
||||
pub fn delete_message(&self, message_id: usize) -> Result<()> {
|
||||
self.0
|
||||
.execute("DELETE FROM chat WHERE id = ?1;", params![message_id])?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Delete a message from the DB
|
||||
pub fn edit_message(&self, message_id: usize, contents: &str) -> Result<()> {
|
||||
self.0.execute(
|
||||
"UPDATE table_name
|
||||
SET contents = ?2
|
||||
WHERE id = ?1;
|
||||
",
|
||||
params![message_id, contents],
|
||||
)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Get a message by its ID
|
||||
pub fn get_message_by_id(&self, message_id: usize) -> Result<Option<Message>> {
|
||||
let mut stmt = self.0.prepare(
|
||||
@@ -63,7 +84,7 @@ impl Database {
|
||||
Ok((
|
||||
row.get::<_, i64>(0)?, // id
|
||||
row.get::<_, String>(1)?, // channel_id
|
||||
row.get::<_, String>(2)?, // user_id
|
||||
row.get::<_, u32>(2)?, // user_id
|
||||
row.get::<_, String>(3)?, // contents
|
||||
row.get::<_, i64>(4)?, // timestamp
|
||||
))
|
||||
|
||||
Reference in New Issue
Block a user