Validate User guard
This commit is contained in:
parent
da4019ec61
commit
e1b9daa931
40
Cargo.lock
generated
40
Cargo.lock
generated
@ -471,6 +471,9 @@ name = "eeg_internal"
|
|||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"argon2",
|
"argon2",
|
||||||
|
"dotenvy",
|
||||||
|
"env_logger",
|
||||||
|
"log",
|
||||||
"maud",
|
"maud",
|
||||||
"rocket",
|
"rocket",
|
||||||
"rocket_db_pools",
|
"rocket_db_pools",
|
||||||
@ -496,6 +499,19 @@ dependencies = [
|
|||||||
"cfg-if",
|
"cfg-if",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "env_logger"
|
||||||
|
version = "0.10.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "4cd405aab171cb85d6735e5c8d9db038c17d3ca007a4d2c25f337935c3d90580"
|
||||||
|
dependencies = [
|
||||||
|
"humantime",
|
||||||
|
"is-terminal",
|
||||||
|
"log",
|
||||||
|
"regex",
|
||||||
|
"termcolor",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "equivalent"
|
name = "equivalent"
|
||||||
version = "1.0.1"
|
version = "1.0.1"
|
||||||
@ -844,6 +860,12 @@ version = "1.0.3"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "df3b46402a9d5adb4c86a0cf463f42e19994e3ee891101b1841f30a545cb49a9"
|
checksum = "df3b46402a9d5adb4c86a0cf463f42e19994e3ee891101b1841f30a545cb49a9"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "humantime"
|
||||||
|
version = "2.1.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "hyper"
|
name = "hyper"
|
||||||
version = "0.14.28"
|
version = "0.14.28"
|
||||||
@ -2195,6 +2217,15 @@ dependencies = [
|
|||||||
"windows-sys 0.52.0",
|
"windows-sys 0.52.0",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "termcolor"
|
||||||
|
version = "1.4.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "06794f8f6c5c898b3275aebefa6b8a1cb24cd2c6c79397ab15774837a0bc5755"
|
||||||
|
dependencies = [
|
||||||
|
"winapi-util",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "thiserror"
|
name = "thiserror"
|
||||||
version = "1.0.56"
|
version = "1.0.56"
|
||||||
@ -2645,6 +2676,15 @@ version = "0.4.0"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
|
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "winapi-util"
|
||||||
|
version = "0.1.6"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "f29e6f9198ba0d26b4c9f07dbe6f9ed633e1f3d5b8b414090084349e46a52596"
|
||||||
|
dependencies = [
|
||||||
|
"winapi",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "winapi-x86_64-pc-windows-gnu"
|
name = "winapi-x86_64-pc-windows-gnu"
|
||||||
version = "0.4.0"
|
version = "0.4.0"
|
||||||
|
@ -11,6 +11,9 @@ maud = { version = "0.26.0", features = ["rocket"] }
|
|||||||
rocket = {version = "0.5.0", features = ["secrets"] }
|
rocket = {version = "0.5.0", features = ["secrets"] }
|
||||||
serde = "1.0.196"
|
serde = "1.0.196"
|
||||||
sqlx = { version = "0.7.3", features = ["mysql", "macros", "chrono"] }
|
sqlx = { version = "0.7.3", features = ["mysql", "macros", "chrono"] }
|
||||||
|
log = "0.4.20"
|
||||||
|
env_logger = "0.10.0"
|
||||||
|
dotenvy = "0.15.7"
|
||||||
|
|
||||||
[dependencies.rocket_db_pools]
|
[dependencies.rocket_db_pools]
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
|
@ -1,2 +1,5 @@
|
|||||||
|
[release]
|
||||||
|
secret_key = "CEvBmqk0FHgrtiIbitcTe+citXkWgo2fUfya+EoFiQ0="
|
||||||
|
|
||||||
[default.databases.main]
|
[default.databases.main]
|
||||||
url = "mysql://root:123456789@localhost:33306/eegsac_manager"
|
url = "mysql://root:123456789@localhost:33306/eegsac_manager"
|
||||||
|
@ -10,10 +10,10 @@ use session::SessionData;
|
|||||||
use session::Sessions;
|
use session::Sessions;
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct User(SessionData);
|
pub struct RegularUser(pub SessionData);
|
||||||
|
|
||||||
#[rocket::async_trait]
|
#[rocket::async_trait]
|
||||||
impl<'r> FromRequest<'r> for User {
|
impl<'r> FromRequest<'r> for RegularUser {
|
||||||
type Error = ();
|
type Error = ();
|
||||||
|
|
||||||
async fn from_request(req: &'r Request<'_>) -> Outcome<Self, ()> {
|
async fn from_request(req: &'r Request<'_>) -> Outcome<Self, ()> {
|
||||||
@ -22,12 +22,12 @@ impl<'r> FromRequest<'r> for User {
|
|||||||
|
|
||||||
let session_id_str = match session_opt {
|
let session_id_str = match session_opt {
|
||||||
Some(s) => s.value().to_owned(),
|
Some(s) => s.value().to_owned(),
|
||||||
None => return Outcome::Error((Status::Unauthorized, ())),
|
None => return Outcome::Forward(Status::Unauthorized),
|
||||||
};
|
};
|
||||||
|
|
||||||
let session_id = match session_id_str.parse::<usize>() {
|
let session_id = match session_id_str.parse::<usize>() {
|
||||||
Ok(s) => s,
|
Ok(s) => s,
|
||||||
Err(_) => return Outcome::Error((Status::Unauthorized, ())),
|
Err(_) => return Outcome::Forward(Status::Unauthorized),
|
||||||
};
|
};
|
||||||
|
|
||||||
let sessions_outcome = req.guard::<&State<Sessions>>().await;
|
let sessions_outcome = req.guard::<&State<Sessions>>().await;
|
||||||
@ -37,8 +37,8 @@ impl<'r> FromRequest<'r> for User {
|
|||||||
};
|
};
|
||||||
|
|
||||||
match sessions.get(session_id) {
|
match sessions.get(session_id) {
|
||||||
Some(s) => Outcome::Success(User(s.clone())),
|
Some(s) => Outcome::Success(RegularUser(s.clone())),
|
||||||
None => Outcome::Error((Status::Unauthorized, ())),
|
None => Outcome::Forward(Status::Unauthorized),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -64,8 +64,8 @@ impl Sessions {
|
|||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug, Clone)]
|
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||||
pub struct SessionData {
|
pub struct SessionData {
|
||||||
user_id: i32,
|
pub user_id: i32,
|
||||||
expires_at: u64,
|
pub expires_at: u64,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl SessionData {
|
impl SessionData {
|
||||||
|
@ -1,13 +1,22 @@
|
|||||||
use maud::Markup;
|
use maud::{html, Markup};
|
||||||
use rocket::http::CookieJar;
|
use rocket::http::CookieJar;
|
||||||
|
|
||||||
|
use crate::auth::RegularUser;
|
||||||
|
|
||||||
pub mod login;
|
pub mod login;
|
||||||
pub mod register;
|
pub mod register;
|
||||||
pub mod user;
|
pub mod user;
|
||||||
|
|
||||||
#[get("/")]
|
#[get("/")]
|
||||||
pub fn index(cookies: &CookieJar<'_>) -> Markup {
|
pub fn index(user: RegularUser) -> Markup {
|
||||||
cookies.add_private(("rocket_session_id", "123456"));
|
let RegularUser(user) = user;
|
||||||
|
|
||||||
|
html! {
|
||||||
|
"Logged in with user id: " (user.user_id) " :D"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[get("/", rank = 2)]
|
||||||
|
pub fn index_login(cookies: &CookieJar<'_>) -> Markup {
|
||||||
crate::view::login::login()
|
crate::view::login::login()
|
||||||
}
|
}
|
||||||
|
@ -20,7 +20,7 @@ fn rocket() -> _ {
|
|||||||
.manage(auth::session::Sessions::new())
|
.manage(auth::session::Sessions::new())
|
||||||
.register("/", catchers![view::not_authorized])
|
.register("/", catchers![view::not_authorized])
|
||||||
.attach(DefaultDB::init())
|
.attach(DefaultDB::init())
|
||||||
.mount("/", routes![controller::index,])
|
.mount("/", routes![controller::index, controller::index_login,])
|
||||||
.mount(
|
.mount(
|
||||||
"/f",
|
"/f",
|
||||||
routes![controller::user::create_user, controller::login::login,],
|
routes![controller::user::create_user, controller::login::login,],
|
||||||
|
@ -4,6 +4,6 @@ use super::default_skeleton;
|
|||||||
|
|
||||||
pub fn login() -> Markup {
|
pub fn login() -> Markup {
|
||||||
default_skeleton(html! {
|
default_skeleton(html! {
|
||||||
"TODO"
|
"NOT LOGGED IN! :O"
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user