diff --git a/server/src/main.rs b/server/src/main.rs index d6ba282..96b1619 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -7,6 +7,7 @@ extern crate chrono; extern crate dotenv; extern crate serde; +extern crate rand; use rocket_contrib::serve::StaticFiles; @@ -15,9 +16,11 @@ use dotenv::dotenv; use std::env::var; mod website; +mod users; mod schema; -mod invites; mod models; +mod invites; +mod payload; use website::*; use invites::*; @@ -25,7 +28,7 @@ use invites::*; #[database("freechat_sample_db")] pub struct DBConn(diesel::MysqlConnection); -fn rocket() -> rocket::Rocket { +pub fn rocket() -> rocket::Rocket { rocket::ignite() .mount("/static", StaticFiles::from("/static")) .mount("/", routes![ @@ -35,6 +38,9 @@ fn rocket() -> rocket::Rocket { .mount("/invite", routes![ generate_invite, use_invite ]) + .mount("/user", routes![ + users::create_user + ]) .attach(Template::fairing()) .attach(DBConn::fairing()) } diff --git a/server/src/payload.rs b/server/src/payload.rs new file mode 100644 index 0000000..f54fc3c --- /dev/null +++ b/server/src/payload.rs @@ -0,0 +1,14 @@ +/* Module containg various structure which we use to pass back + * and forth from client/server as auth tokens + */ + +// This structure allows us to provide some critical data for the client to reconnect to +// the server without having to go through a sign in process everytime + +#[derive(Serialize)] +pub struct NewUserResponse { + pub userid: u64, + pub token: String, + pub username: String, + pub email: Option, +} \ No newline at end of file diff --git a/server/src/users.rs b/server/src/users.rs new file mode 100644 index 0000000..9df27d9 --- /dev/null +++ b/server/src/users.rs @@ -0,0 +1,55 @@ +use rocket::request::Form; +use rocket_contrib::json::Json; +use crate::payload; +// TODO: authentication needs to get applied basically everywhere but right now its missing + +#[derive(FromForm)] +pub struct NewUserForm { + pub username: String, + pub display: Option, + pub email: Option, + pub password: String // this part is generated for the user if an invite is used +} + +#[post("/create", data = "")] +pub fn create_user(user_sign_up: Form) -> Json { + let email = match user_sign_up.email.clone() { + Some(val) => { + val + } + None => { + "None".to_string() + } + }; + + // Constructing the response to the user with stuff they'll need to reconnect to the server + let user_auth = payload::NewUserResponse { + userid: 1, + token: "random token".to_string(), + username: user_sign_up.username.clone(), + email: Some(email), + }; + Json(user_auth) +} + +#[cfg(test)] +mod user_tests { + use super::*; + use rocket; + use rocket::local::Client; + use rocket::http::{Status, ContentType}; + + #[test] + fn new_user() { + let rocket = rocket::ignite() + .mount("/user", routes![create_user]); + let client = Client::new(rocket).expect("Invalid rocket instance"); + let response = client.post("/user/create") + .header(ContentType::Form) + .body("username=testuser&password=testpass") + .dispatch(); + + assert_eq!(response.status(), Status::Ok); + } + +} diff --git a/server/todo.md b/server/todo.md index ab055cd..6a748c7 100644 --- a/server/todo.md +++ b/server/todo.md @@ -2,13 +2,21 @@ frontend js needs some testing if it's to be guaranteed to work at all -endpoints need to start existsing so that we can actually start testing on per instance basis # Creation of users this part we'll probably use an sql db or something that couples easily with rocket keep the user data designi as stupid simple as possible to enforce both security by surface and haxor simplicity +# Auth + +* Modules should serve as a collection of authentication payloads and functions to verify that data from the client is correct + + # Webpages Starting to think that we shouldn't even bother with this stuff tbh + +# Things to depracate + +* webpages - as they're just not important to the core utility of freechat at the moment