From 3d8b9805a40e57d977bc48daba1f208dd1bfcbc4 Mon Sep 17 00:00:00 2001 From: Tim Fritzen Date: Fri, 18 Oct 2024 21:27:19 +0200 Subject: [PATCH] Updated to newest rocket version and fixed errors --- Cargo.toml | 25 +++++++++++++------------ src/cookies.rs | 3 +-- src/forms/mod.rs | 9 ++++----- src/session/redis/mod.rs | 6 +++--- src/user/auth.rs | 11 ++++++----- src/user/user_impl.rs | 15 ++++++++------- 6 files changed, 35 insertions(+), 34 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 32749e5..b1be021 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,13 +24,13 @@ sqlx-mysql = ["sqlx/mysql"] [dependencies.rusqlite] -version = "0.27.0" +version = "0.32.1" optional = true [dependencies] rand = "0.8.5" -rust-argon2 = "1.0.0" +rust-argon2 = "2.1.0" lazy_static = "1.4.0" regex = "1.5.6" serde_json = "1.0.82" @@ -39,17 +39,18 @@ thiserror = "1.0.31" async-trait = "0.1.56" fehler = "1.0.0" chrono = "0.4.19" -validator = { version = "0.15.0", features = ["derive"] } -futures= "0.3.21" +validator = { version = "0.18.1", features = ["derive"] } +futures = "0.3.21" [dependencies.sqlx] -version = "0.6.0" +version = "0.8.2" +features = ["runtime-tokio", "tls-native-tls"] optional = true [dependencies.rocket] -version = "0.5.0-rc.2" +version = "0.5.1" features = ["secrets"] [dependencies.serde] @@ -62,7 +63,7 @@ optional = true [dependencies.redis] -version = "0.21.5" +version = "0.27.4" features = ["aio", "tokio-comp"] optional = true @@ -71,25 +72,25 @@ version = "1.19.2" features = ["rt", "rt-multi-thread"] [dev-dependencies] -tokio-postgres= "0.7.6" +tokio-postgres = "0.7.6" [dev-dependencies.rocket] -version = "0.5.0-rc.2" +version = "0.5.1" features = ["secrets", "json"] [dev-dependencies.redis] -version = "0.21.5" +version = "0.27.4" features = ["aio", "tokio-comp"] [dev-dependencies.rocket_dyn_templates] -version = "0.1.0-rc.2" +version = "0.2.0" features = ["tera"] [dev-dependencies.sqlx] -version = "0.6.0" +version = "0.8.2" features = ["runtime-tokio-rustls"] [dev-dependencies.rocket_auth] diff --git a/src/cookies.rs b/src/cookies.rs index aed56c9..700ae98 100644 --- a/src/cookies.rs +++ b/src/cookies.rs @@ -24,7 +24,6 @@ pub struct Session { pub auth_key: String, } - #[async_trait] impl<'r> FromRequest<'r> for Session { type Error = Error; @@ -34,7 +33,7 @@ impl<'r> FromRequest<'r> for Session { if let Some(session) = get_session(cookies) { Outcome::Success(session) } else { - Outcome::Failure((Status::Unauthorized, Error::UnauthorizedError)) + Outcome::Error((Status::Unauthorized, Error::UnauthorizedError)) } } } diff --git a/src/forms/mod.rs b/src/forms/mod.rs index 1158ba6..0fec788 100644 --- a/src/forms/mod.rs +++ b/src/forms/mod.rs @@ -1,6 +1,5 @@ use crate::prelude::*; - /// The `Login` form is used along with the [`Auth`] guard to authenticate users. #[derive(FromForm, Deserialize, Clone, Hash, PartialEq, Eq, Validate)] pub struct Login { @@ -15,10 +14,10 @@ pub struct Signup { #[validate(email)] pub email: String, #[validate( - custom = "is_long", - custom = "has_number", - custom = "has_lowercase", - custom = "has_uppercase" + custom(function = "is_long"), + custom(function = "has_number"), + custom(function = "has_lowercase"), + custom(function = "has_uppercase") )] pub(crate) password: String, } diff --git a/src/session/redis/mod.rs b/src/session/redis/mod.rs index 41a2348..c236055 100644 --- a/src/session/redis/mod.rs +++ b/src/session/redis/mod.rs @@ -3,7 +3,7 @@ use crate::prelude::*; use redis::{Client, Commands}; -const YEAR_IN_SECS: usize = 365 * 60 * 60 * 24; +const YEAR_IN_SECS: u64 = 365 * 60 * 60 * 24; impl SessionManager for Client { #[throws(Error)] @@ -14,7 +14,7 @@ impl SessionManager for Client { #[throws(Error)] fn insert_for(&self, id: i32, key: String, time: Duration) { let mut cnn = self.get_connection()?; - cnn.set_ex(id, key, time.as_secs() as usize)?; + cnn.set_ex(id, key, time.as_secs() as u64)?; } #[throws(Error)] fn remove(&self, id: i32) { @@ -30,7 +30,7 @@ impl SessionManager for Client { #[throws(Error)] fn clear_all(&self) { let mut cnn = self.get_connection()?; - redis::Cmd::new().arg("FLUSHDB").execute(&mut cnn); + redis::Cmd::new().arg("FLUSHDB").exec(&mut cnn)?; } #[throws(Error)] fn clear_expired(&self) {} diff --git a/src/user/auth.rs b/src/user/auth.rs index d626cbf..ff79428 100644 --- a/src/user/auth.rs +++ b/src/user/auth.rs @@ -7,6 +7,7 @@ use rocket::Request; use rocket::State; use serde_json::json; use std::time::Duration; +use validator::ValidateEmail; /// The [`Auth`] guard allows to log in, log out, sign up, modify, and delete the currently (un)authenticated user. /// For more information see [`Auth`]. @@ -64,7 +65,7 @@ impl<'r> FromRequest<'r> for Auth<'r> { let users: &State = if let Outcome::Success(users) = req.guard().await { users } else { - return Outcome::Failure((Status::InternalServerError, Error::UnmanagedStateError)); + return Outcome::Error((Status::InternalServerError, Error::UnmanagedStateError)); }; Outcome::Success(Auth { @@ -222,7 +223,7 @@ impl<'a> Auth<'a> { pub fn logout(&self) { let session = self.get_session()?; self.users.logout(session)?; - self.cookies.remove_private(Cookie::named("rocket_auth")); + self.cookies.remove_private(Cookie::build("rocket_auth")); } /// Deletes the account of the currently authenticated user. /// ```rust @@ -238,7 +239,7 @@ impl<'a> Auth<'a> { if self.is_auth() { let session = self.get_session()?; self.users.delete(session.id).await?; - self.cookies.remove_private(Cookie::named("rocket_auth")); + self.cookies.remove_private(Cookie::build("rocket_auth")); } else { throw!(Error::UnauthenticatedError) } @@ -275,7 +276,7 @@ impl<'a> Auth<'a> { #[throws(Error)] pub async fn change_email(&self, email: String) { if self.is_auth() { - if !validator::validate_email(&email) { + if !email.validate_email() { throw!(Error::InvalidEmailAddressError) } let session = self.get_session()?; @@ -308,7 +309,7 @@ impl<'a> Auth<'a> { #[throws(Error)] pub async fn compare_password(&self, password: &str) -> bool { if self.is_auth() { - let session = self.get_session()?; + let session = self.get_session()?; let user: User = self.users.get_by_id(session.id).await?; user.compare_password(password)? } else { diff --git a/src/user/user_impl.rs b/src/user/user_impl.rs index e5ebe05..181e4e4 100644 --- a/src/user/user_impl.rs +++ b/src/user/user_impl.rs @@ -4,6 +4,7 @@ use super::rand_string; use crate::prelude::*; use rocket::http::Status; use rocket::request::{FromRequest, Outcome, Request}; +use validator::ValidateEmail; impl User { /// This method allows to reset the password of a user. @@ -12,7 +13,7 @@ impl User { /// In case the user is authenticated, /// you can change it more easily with [`change_password`](`super::auth::Auth::change_password`). /// This function will fail in case the password is not secure enough. - /// + /// /// ```rust /// # use rocket::{State, post}; /// # use rocket_auth::{Error, Users}; @@ -85,7 +86,7 @@ impl User { /// ``` #[throws(Error)] pub fn set_email(&mut self, email: &str) { - if validator::validate_email(email) { + if email.validate_email() { self.email = email.to_lowercase(); } else { throw!(Error::InvalidEmailAddressError) @@ -113,13 +114,13 @@ impl<'r> FromRequest<'r> for User { let guard = request.guard().await; let auth: Auth = match guard { Success(auth) => auth, - Failure(x) => return Failure(x), + Error(x) => return Error(x), Forward(x) => return Forward(x), }; if let Some(user) = auth.get_user().await { Outcome::Success(user) } else { - Outcome::Failure((Status::Unauthorized, Error::UnauthorizedError)) + Outcome::Error((Status::Unauthorized, Self::Error::UnauthorizedError)) } } } @@ -132,7 +133,7 @@ impl<'r> FromRequest<'r> for AdminUser { let guard = request.guard().await; let auth: Auth = match guard { Success(auth) => auth, - Failure(x) => return Failure(x), + Error(x) => return Error(x), Forward(x) => return Forward(x), }; if let Some(user) = auth.get_user().await { @@ -140,12 +141,12 @@ impl<'r> FromRequest<'r> for AdminUser { return Outcome::Success(AdminUser(user)); } } - Outcome::Failure((Status::Unauthorized, Error::UnauthorizedError)) + Outcome::Error((Status::Unauthorized, Self::Error::UnauthorizedError)) } } -use std::ops::*; use argon2::verify_encoded; +use std::ops::*; impl Deref for AdminUser { type Target = User;