From 759d27111129a43a5a6855cbef4dcdb3eb4282ff Mon Sep 17 00:00:00 2001 From: Araozu Date: Fri, 21 Jun 2024 19:48:27 -0500 Subject: [PATCH] Add guard for user --- Cargo.lock | 158 +++++++++++++++++++++++++++++++++++----- Cargo.toml | 2 +- package.json | 1 + src/controller/mod.rs | 11 +++ src/controller/utils.rs | 29 ++++++++ src/main.rs | 2 +- src/view/mod.rs | 7 ++ static/tailwind.css | 3 +- tailwind.config.js | 1 + 9 files changed, 191 insertions(+), 23 deletions(-) create mode 100644 src/controller/utils.rs diff --git a/Cargo.lock b/Cargo.lock index 3018feb..2775aca 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -17,6 +17,41 @@ version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" +[[package]] +name = "aead" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d122413f284cf2d62fb1b7db97e02edb8cda96d769b16e443a4f6195e35662b0" +dependencies = [ + "crypto-common", + "generic-array", +] + +[[package]] +name = "aes" +version = "0.8.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b169f7a6d4742236a0a00c541b845991d0ac43e546831af1249753ab4c3aa3a0" +dependencies = [ + "cfg-if", + "cipher", + "cpufeatures", +] + +[[package]] +name = "aes-gcm" +version = "0.10.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "831010a0f742e1209b3bcea8fab6a8e149051ba6099432c8cb2cc117dec3ead1" +dependencies = [ + "aead", + "aes", + "cipher", + "ctr", + "ghash", + "subtle", +] + [[package]] name = "ahash" version = "0.8.7" @@ -208,6 +243,16 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "cipher" +version = "0.4.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "773f3b9af64447d2ce9850330c473515014aa235e6a783b02db81ff39e4a3dad" +dependencies = [ + "crypto-common", + "inout", +] + [[package]] name = "const-oid" version = "0.9.6" @@ -220,7 +265,13 @@ version = "0.18.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3cd91cf61412820176e137621345ee43b3f4423e589e7ae4e50d601d93e35ef8" dependencies = [ + "aes-gcm", + "base64", + "hkdf", "percent-encoding", + "rand", + "sha2", + "subtle", "time", "version_check", ] @@ -271,9 +322,19 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1bfb12502f3fc46cca1bb51ac28df9d618d813cdc3d2f25b9fe775a34af26bb3" dependencies = [ "generic-array", + "rand_core", "typenum", ] +[[package]] +name = "ctr" +version = "0.9.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0369ee1ad671834580515889b80f2ea915f23b8be8d0daa4bbaf2ac5c7590835" +dependencies = [ + "cipher", +] + [[package]] name = "der" version = "0.7.8" @@ -569,6 +630,16 @@ dependencies = [ "wasi", ] +[[package]] +name = "ghash" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f0d8a4362ccb29cb0b265253fb0a2728f592895ee6854fd9bc13f2ffda266ff1" +dependencies = [ + "opaque-debug", + "polyval", +] + [[package]] name = "gimli" version = "0.28.1" @@ -592,7 +663,7 @@ dependencies = [ "futures-core", "futures-sink", "futures-util", - "http", + "http 0.2.11", "indexmap", "slab", "tokio", @@ -678,6 +749,17 @@ dependencies = [ "itoa", ] +[[package]] +name = "http" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "21b9ddb458710bc376481b842f5da65cdf31522de232c1ca8146abce2a358258" +dependencies = [ + "bytes", + "fnv", + "itoa", +] + [[package]] name = "http-body" version = "0.4.6" @@ -685,7 +767,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7ceab25649e9960c0311ea418d17bee82c0dcec1bd053b5f9a66e265a693bed2" dependencies = [ "bytes", - "http", + "http 0.2.11", "pin-project-lite", ] @@ -712,7 +794,7 @@ dependencies = [ "futures-core", "futures-util", "h2", - "http", + "http 0.2.11", "http-body", "httparse", "httpdate", @@ -753,13 +835,22 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c8fae54786f62fb2918dcfae3d568594e50eb9b5c25bf04371af6fe7516452fb" [[package]] -name = "is-terminal" -version = "0.4.10" +name = "inout" +version = "0.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0bad00257d07be169d870ab665980b06cdb366d792ad690bf2e76876dc503455" +checksum = "a0c10553d664a4d0bcff9f4215d0aac67a639cc68ef660840afe309b807bc9f5" +dependencies = [ + "generic-array", +] + +[[package]] +name = "is-terminal" +version = "0.4.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f23ff5ef2b80d608d61efee834934d862cd92461afc0560dedf493e4c033738b" dependencies = [ "hermit-abi", - "rustix", + "libc", "windows-sys 0.52.0", ] @@ -938,16 +1029,15 @@ dependencies = [ [[package]] name = "multer" -version = "2.1.0" +version = "3.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "01acbdc23469fd8fe07ab135923371d5f5a422fbf9c522158677c8eb15bc51c2" +checksum = "83e87776546dc87511aa5ee218730c92b666d7264ab6ed41f9d215af9cd5224b" dependencies = [ "bytes", "encoding_rs", "futures-util", - "http", + "http 1.1.0", "httparse", - "log", "memchr", "mime", "spin 0.9.8", @@ -1066,6 +1156,12 @@ version = "1.19.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92" +[[package]] +name = "opaque-debug" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c08d65885ee38876c4f86fa503fb49d7b507c2b62552df7c70b2fce627e06381" + [[package]] name = "overload" version = "0.1.1" @@ -1178,6 +1274,18 @@ version = "0.3.29" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2900ede94e305130c13ddd391e0ab7cbaeb783945ae07a279c268cb05109c6cb" +[[package]] +name = "polyval" +version = "0.6.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9d1fe60d06143b2430aa532c94cfe9e29783047f06c0d7fd359a9a51b729fa25" +dependencies = [ + "cfg-if", + "cpufeatures", + "opaque-debug", + "universal-hash", +] + [[package]] name = "powerfmt" version = "0.2.0" @@ -1349,9 +1457,9 @@ checksum = "c08c74e62047bb2de4ff487b251e4a92e24f48745648451635cec7d591162d9f" [[package]] name = "rocket" -version = "0.5.0" +version = "0.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9e7bb57ccb26670d73b6a47396c83139447b9e7878cab627fdfe9ea8da489150" +checksum = "a516907296a31df7dc04310e7043b61d71954d703b603cc6867a026d7e72d73f" dependencies = [ "async-stream", "async-trait", @@ -1386,9 +1494,9 @@ dependencies = [ [[package]] name = "rocket_codegen" -version = "0.5.0" +version = "0.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a2238066abf75f21be6cd7dc1a09d5414a671f4246e384e49fe3f8a4936bd04c" +checksum = "575d32d7ec1a9770108c879fc7c47815a80073f96ca07ff9525a94fcede1dd46" dependencies = [ "devise", "glob", @@ -1403,14 +1511,14 @@ dependencies = [ [[package]] name = "rocket_http" -version = "0.5.0" +version = "0.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "37a1663694d059fe5f943ea5481363e48050acedd241d46deb2e27f71110389e" +checksum = "e274915a20ee3065f611c044bd63c40757396b6dbc057d6046aec27f14f882b9" dependencies = [ "cookie", "either", "futures", - "http", + "http 0.2.11", "hyper", "indexmap", "log", @@ -2208,6 +2316,16 @@ version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "39ec24b3121d976906ece63c9daad25b85969647682eee313cb5779fdd69e14e" +[[package]] +name = "universal-hash" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fc1de2c688dc15305988b563c3854064043356019f97a4b46276fe734c4f07ea" +dependencies = [ + "crypto-common", + "subtle", +] + [[package]] name = "url" version = "2.5.0" @@ -2438,9 +2556,9 @@ dependencies = [ [[package]] name = "yansi" -version = "1.0.0-rc.1" +version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1367295b8f788d371ce2dbc842c7b709c73ee1364d30351dd300ec2203b12377" +checksum = "cfe53a6657fd280eaa890a3bc59152892ffa3e30101319d168b781ed6529b049" dependencies = [ "is-terminal", ] diff --git a/Cargo.toml b/Cargo.toml index 01377c9..588ac4e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,5 +7,5 @@ edition = "2021" [dependencies] maud = { version = "0.26.0", features = ["rocket"] } -rocket = "0.5.0" +rocket = { version = "0.5.1", features = ["secrets"] } sqlx = { version = "0.7.3", features = ["postgres"] } diff --git a/package.json b/package.json index 7ffccfb..b2d873b 100644 --- a/package.json +++ b/package.json @@ -5,6 +5,7 @@ "main": "index.js", "scripts": { "build": "echo \"Error: no test specified\" && exit 1", + "dev": "tailwindcss -i ./static/tailwind.css -o ./static/out.css --watch", "tailwind": "tailwindcss -i ./static/tailwind.css -o ./static/out.css --watch" }, "keywords": [], diff --git a/src/controller/mod.rs b/src/controller/mod.rs index 18947b9..a155805 100644 --- a/src/controller/mod.rs +++ b/src/controller/mod.rs @@ -1,6 +1,17 @@ use maud::Markup; +use rocket::http::Status; + +mod utils; + #[get("/")] pub fn homepage() -> Markup { crate::view::homepage() } + +#[get("/new")] +pub fn new_definition(user: utils::User) -> (Status, Markup) { + + todo!() +} + diff --git a/src/controller/utils.rs b/src/controller/utils.rs new file mode 100644 index 0000000..f850188 --- /dev/null +++ b/src/controller/utils.rs @@ -0,0 +1,29 @@ +use rocket::{ + http::Status, + request::{FromRequest, Outcome, Request}, +}; + +/// Name of the header that stores the session ID of an user +const RS_SESSION_ID: &str = "x-rs-session-id"; + +pub struct User { +} + +#[rocket::async_trait] +impl<'r> FromRequest<'r> for User { + type Error = String; + + async fn from_request(req: &'r Request<'_>) -> Outcome { + let session_cookie = req.cookies().get_private(RS_SESSION_ID); + + let _session_cookie = match session_cookie { + Some(cookie) => cookie.value().to_string(), + None => return Outcome::Error((Status::Unauthorized, "Unauthorized".into())), + }; + + // Check if session cookie is valid + // TODO + + Outcome::Success(User {}) + } +} diff --git a/src/main.rs b/src/main.rs index e73746b..aeae0fa 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,6 +9,6 @@ use rocket::fs::{relative, FileServer}; #[launch] fn rocket() -> _ { rocket::build() - .mount("/", routes![controller::homepage]) + .mount("/", routes![controller::homepage, controller::new_definition]) .mount("/static", FileServer::from(relative!("static"))) } diff --git a/src/view/mod.rs b/src/view/mod.rs index a2f65a5..e700fe7 100644 --- a/src/view/mod.rs +++ b/src/view/mod.rs @@ -88,6 +88,13 @@ pub fn homepage() -> Markup { div class="bg-accent text-accent-content py-2 m-1 rounded-md text-center font-bold text-lg" { "Jerguero" } + div class="text-center p-2" { + a class="py-1 px-2 rounded bg-c-primary text-c-on-primary hover:underline" + href="/new" + { + "Agregá una nueva definición" + } + } (post()) (post()) (post()) diff --git a/static/tailwind.css b/static/tailwind.css index c7d4872..27ef632 100644 --- a/static/tailwind.css +++ b/static/tailwind.css @@ -4,6 +4,7 @@ :root { --c-primary: #0ea5e9; + --c-on-primary: white; } @@ -20,7 +21,7 @@ --c-bg: #0a0a0a; --c-on-bg: white; - --c-bg-2: #051c2c; + --c-bg-2: #0f1c25; } } diff --git a/tailwind.config.js b/tailwind.config.js index deaf55f..2c5304c 100644 --- a/tailwind.config.js +++ b/tailwind.config.js @@ -14,6 +14,7 @@ module.exports = { }, colors: { "c-primary": "var(--c-primary)", + "c-on-primary": "var(--c-on-primary)", "c-bg": "var(--c-bg)", "c-bg-2": "var(--c-bg-2)", "c-on-bg": "var(--c-on-bg)",