Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated to newest rocket version and fixed errors #71

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 13 additions & 12 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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]
Expand All @@ -62,7 +63,7 @@ optional = true


[dependencies.redis]
version = "0.21.5"
version = "0.27.4"
features = ["aio", "tokio-comp"]
optional = true

Expand All @@ -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]
Expand Down
3 changes: 1 addition & 2 deletions src/cookies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ pub struct Session {
pub auth_key: String,
}


#[async_trait]
impl<'r> FromRequest<'r> for Session {
type Error = Error;
Expand All @@ -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))
}
}
}
Expand Down
9 changes: 4 additions & 5 deletions src/forms/mod.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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,
}
Expand Down
6 changes: 3 additions & 3 deletions src/session/redis/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -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) {
Expand All @@ -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) {}
Expand Down
11 changes: 6 additions & 5 deletions src/user/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`].
Expand Down Expand Up @@ -64,7 +65,7 @@ impl<'r> FromRequest<'r> for Auth<'r> {
let users: &State<Users> = 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 {
Expand Down Expand Up @@ -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
Expand All @@ -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)
}
Expand Down Expand Up @@ -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()?;
Expand Down Expand Up @@ -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 {
Expand Down
15 changes: 8 additions & 7 deletions src/user/user_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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};
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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))
}
}
}
Expand All @@ -132,20 +133,20 @@ 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 {
if user.is_admin {
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;
Expand Down