2019-02-27 13:29:26 +01:00
|
|
|
use lettre::Transport;
|
2019-03-20 17:56:17 +01:00
|
|
|
use rocket::http::ext::IntoOwned;
|
2018-05-19 09:39:59 +02:00
|
|
|
use rocket::{
|
2019-03-20 17:56:17 +01:00
|
|
|
http::{uri::Uri, Cookie, Cookies, SameSite},
|
|
|
|
request::{FlashMessage, Form, LenientForm},
|
2018-07-06 11:51:19 +02:00
|
|
|
response::Redirect,
|
2019-03-20 17:56:17 +01:00
|
|
|
State,
|
2018-05-19 09:39:59 +02:00
|
|
|
};
|
2018-12-06 18:54:16 +01:00
|
|
|
use rocket_i18n::I18n;
|
2019-03-20 17:56:17 +01:00
|
|
|
use std::{
|
|
|
|
borrow::Cow,
|
|
|
|
sync::{Arc, Mutex},
|
|
|
|
time::Instant,
|
|
|
|
};
|
2018-12-06 18:54:16 +01:00
|
|
|
use template_utils::Ructe;
|
2019-03-20 17:56:17 +01:00
|
|
|
use validator::{Validate, ValidationError, ValidationErrors};
|
2018-04-24 11:21:39 +02:00
|
|
|
|
2019-03-20 17:56:17 +01:00
|
|
|
use mail::{build_mail, Mailer};
|
2018-06-23 18:36:11 +02:00
|
|
|
use plume_models::{
|
|
|
|
db_conn::DbConn,
|
2019-03-20 17:56:17 +01:00
|
|
|
users::{User, AUTH_COOKIE},
|
2019-03-21 10:30:33 +01:00
|
|
|
Error, CONFIG,
|
2018-06-23 18:36:11 +02:00
|
|
|
};
|
2019-02-27 13:29:26 +01:00
|
|
|
use routes::errors::ErrorPage;
|
2018-12-29 09:36:07 +01:00
|
|
|
|
2018-12-06 18:54:16 +01:00
|
|
|
#[get("/login?<m>")]
|
2018-12-13 22:20:19 +01:00
|
|
|
pub fn new(user: Option<User>, conn: DbConn, m: Option<String>, intl: I18n) -> Ructe {
|
2018-12-06 18:54:16 +01:00
|
|
|
render!(session::login(
|
|
|
|
&(&*conn, &intl.catalog, user),
|
2018-12-13 22:20:19 +01:00
|
|
|
m,
|
2018-12-06 18:54:16 +01:00
|
|
|
&LoginForm::default(),
|
|
|
|
ValidationErrors::default()
|
|
|
|
))
|
2018-06-04 20:21:43 +02:00
|
|
|
}
|
|
|
|
|
2019-03-12 19:40:54 +01:00
|
|
|
#[derive(Default, FromForm, Validate)]
|
2018-12-06 18:54:16 +01:00
|
|
|
pub struct LoginForm {
|
2019-04-01 20:09:29 +02:00
|
|
|
#[validate(length(min = "1", message = "We need an email, or a username to identify you"))]
|
2018-12-06 18:54:16 +01:00
|
|
|
pub email_or_name: String,
|
2018-08-18 12:37:40 +02:00
|
|
|
#[validate(length(min = "1", message = "Your password can't be empty"))]
|
2019-03-20 17:56:17 +01:00
|
|
|
pub password: String,
|
2018-04-23 11:52:44 +02:00
|
|
|
}
|
|
|
|
|
2018-12-06 18:54:16 +01:00
|
|
|
#[post("/login", data = "<form>")]
|
2019-03-20 17:56:17 +01:00
|
|
|
pub fn create(
|
|
|
|
conn: DbConn,
|
|
|
|
form: LenientForm<LoginForm>,
|
|
|
|
flash: Option<FlashMessage>,
|
|
|
|
mut cookies: Cookies,
|
|
|
|
intl: I18n,
|
|
|
|
) -> Result<Redirect, Ructe> {
|
2018-11-26 10:21:52 +01:00
|
|
|
let user = User::find_by_email(&*conn, &form.email_or_name)
|
2019-03-06 18:28:10 +01:00
|
|
|
.or_else(|_| User::find_by_fqn(&*conn, &form.email_or_name));
|
2018-07-06 11:51:19 +02:00
|
|
|
let mut errors = match form.validate() {
|
|
|
|
Ok(_) => ValidationErrors::new(),
|
2019-03-20 17:56:17 +01:00
|
|
|
Err(e) => e,
|
2018-04-23 11:52:44 +02:00
|
|
|
};
|
2018-12-06 18:54:16 +01:00
|
|
|
|
2018-12-29 09:36:07 +01:00
|
|
|
let user_id = if let Ok(user) = user {
|
2018-11-26 10:21:52 +01:00
|
|
|
if !user.auth(&form.password) {
|
2018-10-20 11:04:20 +02:00
|
|
|
let mut err = ValidationError::new("invalid_login");
|
|
|
|
err.message = Some(Cow::from("Invalid username or password"));
|
2018-12-29 09:36:07 +01:00
|
|
|
errors.add("email_or_name", err);
|
|
|
|
String::new()
|
2018-12-31 11:45:59 +01:00
|
|
|
} else {
|
|
|
|
user.id.to_string()
|
2018-10-20 11:04:20 +02:00
|
|
|
}
|
|
|
|
} else {
|
2018-09-03 19:04:21 +02:00
|
|
|
// Fake password verification, only to avoid different login times
|
|
|
|
// that could be used to see if an email adress is registered or not
|
2019-03-20 17:56:17 +01:00
|
|
|
User::get(&*conn, 1)
|
|
|
|
.map(|u| u.auth(&form.password))
|
|
|
|
.expect("No user is registered");
|
2018-09-03 19:04:21 +02:00
|
|
|
|
2018-08-18 12:37:40 +02:00
|
|
|
let mut err = ValidationError::new("invalid_login");
|
2019-04-01 20:09:29 +02:00
|
|
|
err.message = Some(Cow::from("Invalid username, or password"));
|
2018-12-29 09:36:07 +01:00
|
|
|
errors.add("email_or_name", err);
|
|
|
|
String::new()
|
|
|
|
};
|
2018-08-18 12:37:40 +02:00
|
|
|
|
2018-07-06 11:51:19 +02:00
|
|
|
if errors.is_empty() {
|
2019-03-20 17:56:17 +01:00
|
|
|
cookies.add_private(
|
|
|
|
Cookie::build(AUTH_COOKIE, user_id)
|
|
|
|
.same_site(SameSite::Lax)
|
|
|
|
.finish(),
|
|
|
|
);
|
2018-09-08 01:11:27 +02:00
|
|
|
let destination = flash
|
2019-03-20 17:56:17 +01:00
|
|
|
.and_then(|f| {
|
|
|
|
if f.name() == "callback" {
|
|
|
|
Some(f.msg().to_owned())
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
2018-09-08 01:11:27 +02:00
|
|
|
})
|
2018-11-26 10:21:52 +01:00
|
|
|
.unwrap_or_else(|| "/".to_owned());
|
2018-09-08 01:11:27 +02:00
|
|
|
|
|
|
|
let uri = Uri::parse(&destination)
|
2019-04-01 20:28:23 +02:00
|
|
|
.map(IntoOwned::into_owned)
|
2019-03-20 17:56:17 +01:00
|
|
|
.map_err(|_| {
|
|
|
|
render!(session::login(
|
|
|
|
&(&*conn, &intl.catalog, None),
|
|
|
|
None,
|
|
|
|
&*form,
|
|
|
|
errors
|
|
|
|
))
|
|
|
|
})?;
|
2018-09-08 01:11:27 +02:00
|
|
|
|
|
|
|
Ok(Redirect::to(uri))
|
2018-07-06 11:51:19 +02:00
|
|
|
} else {
|
2018-12-06 18:54:16 +01:00
|
|
|
Err(render!(session::login(
|
|
|
|
&(&*conn, &intl.catalog, None),
|
|
|
|
None,
|
|
|
|
&*form,
|
|
|
|
errors
|
|
|
|
)))
|
2018-04-23 11:52:44 +02:00
|
|
|
}
|
|
|
|
}
|
2018-04-23 13:13:49 +02:00
|
|
|
|
|
|
|
#[get("/logout")]
|
2018-12-06 18:54:16 +01:00
|
|
|
pub fn delete(mut cookies: Cookies) -> Redirect {
|
2018-11-26 10:21:52 +01:00
|
|
|
if let Some(cookie) = cookies.get_private(AUTH_COOKIE) {
|
|
|
|
cookies.remove_private(cookie);
|
|
|
|
}
|
2018-04-23 13:13:49 +02:00
|
|
|
Redirect::to("/")
|
|
|
|
}
|
2019-02-27 13:29:26 +01:00
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct ResetRequest {
|
|
|
|
pub mail: String,
|
|
|
|
pub id: String,
|
|
|
|
pub creation_date: Instant,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PartialEq for ResetRequest {
|
|
|
|
fn eq(&self, other: &Self) -> bool {
|
|
|
|
self.id == other.id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[get("/password-reset")]
|
|
|
|
pub fn password_reset_request_form(conn: DbConn, intl: I18n) -> Ructe {
|
|
|
|
render!(session::password_reset_request(
|
|
|
|
&(&*conn, &intl.catalog, None),
|
|
|
|
&ResetForm::default(),
|
|
|
|
ValidationErrors::default()
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(FromForm, Validate, Default)]
|
|
|
|
pub struct ResetForm {
|
|
|
|
#[validate(email)]
|
|
|
|
pub email: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[post("/password-reset", data = "<form>")]
|
|
|
|
pub fn password_reset_request(
|
|
|
|
conn: DbConn,
|
|
|
|
intl: I18n,
|
|
|
|
mail: State<Arc<Mutex<Mailer>>>,
|
|
|
|
form: Form<ResetForm>,
|
2019-03-20 17:56:17 +01:00
|
|
|
requests: State<Arc<Mutex<Vec<ResetRequest>>>>,
|
2019-02-27 13:29:26 +01:00
|
|
|
) -> Ructe {
|
|
|
|
let mut requests = requests.lock().unwrap();
|
|
|
|
// Remove outdated requests (more than 1 day old) to avoid the list to grow too much
|
|
|
|
requests.retain(|r| r.creation_date.elapsed().as_secs() < 24 * 60 * 60);
|
|
|
|
|
2019-03-20 17:56:17 +01:00
|
|
|
if User::find_by_email(&*conn, &form.email).is_ok()
|
|
|
|
&& !requests.iter().any(|x| x.mail == form.email.clone())
|
|
|
|
{
|
2019-02-27 13:29:26 +01:00
|
|
|
let id = plume_common::utils::random_hex();
|
|
|
|
|
|
|
|
requests.push(ResetRequest {
|
|
|
|
mail: form.email.clone(),
|
|
|
|
id: id.clone(),
|
|
|
|
creation_date: Instant::now(),
|
|
|
|
});
|
|
|
|
|
2019-03-21 10:30:33 +01:00
|
|
|
let link = format!("https://{}/password-reset/{}", CONFIG.base_url, id);
|
2019-02-27 13:29:26 +01:00
|
|
|
if let Some(message) = build_mail(
|
|
|
|
form.email.clone(),
|
|
|
|
i18n!(intl.catalog, "Password reset"),
|
2019-03-20 17:56:17 +01:00
|
|
|
i18n!(intl.catalog, "Here is the link to reset your password: {0}"; link),
|
2019-02-27 13:29:26 +01:00
|
|
|
) {
|
2019-03-19 14:37:56 +01:00
|
|
|
if let Some(ref mut mail) = *mail.lock().unwrap() {
|
2019-03-20 17:56:17 +01:00
|
|
|
mail.send(message.into())
|
2019-04-01 20:09:29 +02:00
|
|
|
.map_err(|_| eprintln!("Couldn't send password reset email"))
|
2019-03-20 17:56:17 +01:00
|
|
|
.ok();
|
|
|
|
}
|
2019-02-27 13:29:26 +01:00
|
|
|
}
|
|
|
|
}
|
2019-03-20 17:56:17 +01:00
|
|
|
render!(session::password_reset_request_ok(&(
|
|
|
|
&*conn,
|
|
|
|
&intl.catalog,
|
|
|
|
None
|
|
|
|
)))
|
2019-02-27 13:29:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[get("/password-reset/<token>")]
|
2019-03-20 17:56:17 +01:00
|
|
|
pub fn password_reset_form(
|
|
|
|
conn: DbConn,
|
|
|
|
intl: I18n,
|
|
|
|
token: String,
|
|
|
|
requests: State<Arc<Mutex<Vec<ResetRequest>>>>,
|
|
|
|
) -> Result<Ructe, ErrorPage> {
|
|
|
|
requests
|
|
|
|
.lock()
|
|
|
|
.unwrap()
|
|
|
|
.iter()
|
|
|
|
.find(|x| x.id == token.clone())
|
|
|
|
.ok_or(Error::NotFound)?;
|
2019-02-27 13:29:26 +01:00
|
|
|
Ok(render!(session::password_reset(
|
|
|
|
&(&*conn, &intl.catalog, None),
|
|
|
|
&NewPasswordForm::default(),
|
|
|
|
ValidationErrors::default()
|
|
|
|
)))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(FromForm, Default, Validate)]
|
2019-03-20 17:56:17 +01:00
|
|
|
#[validate(schema(
|
|
|
|
function = "passwords_match",
|
|
|
|
skip_on_field_errors = "false",
|
|
|
|
message = "Passwords are not matching"
|
|
|
|
))]
|
2019-02-27 13:29:26 +01:00
|
|
|
pub struct NewPasswordForm {
|
|
|
|
pub password: String,
|
|
|
|
pub password_confirmation: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn passwords_match(form: &NewPasswordForm) -> Result<(), ValidationError> {
|
|
|
|
if form.password != form.password_confirmation {
|
|
|
|
Err(ValidationError::new("password_match"))
|
|
|
|
} else {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[post("/password-reset/<token>", data = "<form>")]
|
|
|
|
pub fn password_reset(
|
|
|
|
conn: DbConn,
|
|
|
|
intl: I18n,
|
|
|
|
token: String,
|
|
|
|
requests: State<Arc<Mutex<Vec<ResetRequest>>>>,
|
2019-03-20 17:56:17 +01:00
|
|
|
form: Form<NewPasswordForm>,
|
2019-02-27 13:29:26 +01:00
|
|
|
) -> Result<Redirect, Ructe> {
|
|
|
|
form.validate()
|
|
|
|
.and_then(|_| {
|
|
|
|
let mut requests = requests.lock().unwrap();
|
2019-03-20 17:56:17 +01:00
|
|
|
let req = requests
|
|
|
|
.iter()
|
|
|
|
.find(|x| x.id == token.clone())
|
|
|
|
.ok_or_else(|| to_validation(0))?
|
|
|
|
.clone();
|
|
|
|
if req.creation_date.elapsed().as_secs() < 60 * 60 * 2 {
|
|
|
|
// Reset link is only valid for 2 hours
|
2019-02-27 13:29:26 +01:00
|
|
|
requests.retain(|r| *r != req);
|
|
|
|
let user = User::find_by_email(&*conn, &req.mail).map_err(to_validation)?;
|
|
|
|
user.reset_password(&*conn, &form.password).ok();
|
2019-03-20 17:56:17 +01:00
|
|
|
Ok(Redirect::to(uri!(
|
|
|
|
new: m = i18n!(intl.catalog, "Your password was successfully reset.")
|
|
|
|
)))
|
2019-02-27 13:29:26 +01:00
|
|
|
} else {
|
2019-03-20 17:56:17 +01:00
|
|
|
Ok(Redirect::to(uri!(
|
|
|
|
new: m = i18n!(intl.catalog, "Sorry, but the link expired. Try again")
|
|
|
|
)))
|
2019-02-27 13:29:26 +01:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.map_err(|err| {
|
|
|
|
render!(session::password_reset(
|
|
|
|
&(&*conn, &intl.catalog, None),
|
|
|
|
&form,
|
|
|
|
err
|
|
|
|
))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn to_validation<T>(_: T) -> ValidationErrors {
|
|
|
|
let mut errors = ValidationErrors::new();
|
2019-03-20 17:56:17 +01:00
|
|
|
errors.add(
|
|
|
|
"",
|
|
|
|
ValidationError {
|
|
|
|
code: Cow::from("server_error"),
|
|
|
|
message: Some(Cow::from("An unknown error occured")),
|
|
|
|
params: std::collections::HashMap::new(),
|
|
|
|
},
|
|
|
|
);
|
2019-02-27 13:29:26 +01:00
|
|
|
errors
|
|
|
|
}
|