2018-05-19 07:39:59 +00:00
|
|
|
use rocket::{
|
2018-09-30 09:56:12 +00:00
|
|
|
http::{Cookie, Cookies, SameSite, uri::Uri},
|
2018-07-06 09:51:19 +00:00
|
|
|
response::Redirect,
|
2018-06-24 16:58:57 +00:00
|
|
|
request::{LenientForm,FlashMessage}
|
2018-05-19 07:39:59 +00:00
|
|
|
};
|
2018-09-07 23:11:27 +00:00
|
|
|
use rocket::http::ext::IntoOwned;
|
2018-12-06 17:54:16 +00:00
|
|
|
use rocket_i18n::I18n;
|
2018-08-18 10:37:40 +00:00
|
|
|
use std::borrow::Cow;
|
2018-07-06 09:51:19 +00:00
|
|
|
use validator::{Validate, ValidationError, ValidationErrors};
|
2018-12-06 17:54:16 +00:00
|
|
|
use template_utils::Ructe;
|
2018-04-24 09:21:39 +00:00
|
|
|
|
2018-06-23 16:36:11 +00:00
|
|
|
use plume_models::{
|
|
|
|
db_conn::DbConn,
|
|
|
|
users::{User, AUTH_COOKIE}
|
|
|
|
};
|
2018-04-23 09:52:44 +00:00
|
|
|
|
2018-12-29 08:36:07 +00:00
|
|
|
|
2018-12-06 17:54:16 +00:00
|
|
|
#[get("/login?<m>")]
|
2018-12-13 21:20:19 +00:00
|
|
|
pub fn new(user: Option<User>, conn: DbConn, m: Option<String>, intl: I18n) -> Ructe {
|
2018-12-06 17:54:16 +00:00
|
|
|
render!(session::login(
|
|
|
|
&(&*conn, &intl.catalog, user),
|
2018-12-13 21:20:19 +00:00
|
|
|
m,
|
2018-12-06 17:54:16 +00:00
|
|
|
&LoginForm::default(),
|
|
|
|
ValidationErrors::default()
|
|
|
|
))
|
2018-06-04 18:21:43 +00:00
|
|
|
}
|
|
|
|
|
2018-12-06 17:54:16 +00:00
|
|
|
#[derive(Default, FromForm, Validate, Serialize)]
|
|
|
|
pub struct LoginForm {
|
2018-07-07 20:51:48 +00:00
|
|
|
#[validate(length(min = "1", message = "We need an email or a username to identify you"))]
|
2018-12-06 17:54:16 +00:00
|
|
|
pub email_or_name: String,
|
2018-08-18 10:37:40 +00:00
|
|
|
#[validate(length(min = "1", message = "Your password can't be empty"))]
|
2018-12-06 17:54:16 +00:00
|
|
|
pub password: String
|
2018-04-23 09:52:44 +00:00
|
|
|
}
|
|
|
|
|
2018-12-06 17:54:16 +00:00
|
|
|
#[post("/login", data = "<form>")]
|
|
|
|
pub fn create(conn: DbConn, form: LenientForm<LoginForm>, flash: Option<FlashMessage>, mut cookies: Cookies, intl: I18n) -> Result<Redirect, Ructe> {
|
2018-11-26 09:21:52 +00:00
|
|
|
let user = User::find_by_email(&*conn, &form.email_or_name)
|
2018-12-29 08:36:07 +00:00
|
|
|
.or_else(|_| User::find_local(&*conn, &form.email_or_name));
|
2018-07-06 09:51:19 +00:00
|
|
|
let mut errors = match form.validate() {
|
|
|
|
Ok(_) => ValidationErrors::new(),
|
|
|
|
Err(e) => e
|
2018-04-23 09:52:44 +00:00
|
|
|
};
|
2018-12-06 17:54:16 +00:00
|
|
|
|
2018-12-29 08:36:07 +00:00
|
|
|
let user_id = if let Ok(user) = user {
|
2018-11-26 09:21:52 +00:00
|
|
|
if !user.auth(&form.password) {
|
2018-10-20 09:04:20 +00:00
|
|
|
let mut err = ValidationError::new("invalid_login");
|
|
|
|
err.message = Some(Cow::from("Invalid username or password"));
|
2018-12-29 08:36:07 +00:00
|
|
|
errors.add("email_or_name", err);
|
|
|
|
user.id.to_string()
|
|
|
|
} else {
|
|
|
|
String::new()
|
2018-10-20 09:04:20 +00:00
|
|
|
}
|
|
|
|
} else {
|
2018-09-03 17:04:21 +00:00
|
|
|
// Fake password verification, only to avoid different login times
|
|
|
|
// that could be used to see if an email adress is registered or not
|
2018-12-29 08:36:07 +00:00
|
|
|
User::get(&*conn, 1).map(|u| u.auth(&form.password)).expect("No user is registered");
|
2018-09-03 17:04:21 +00:00
|
|
|
|
2018-08-18 10:37:40 +00:00
|
|
|
let mut err = ValidationError::new("invalid_login");
|
|
|
|
err.message = Some(Cow::from("Invalid username or password"));
|
2018-12-29 08:36:07 +00:00
|
|
|
errors.add("email_or_name", err);
|
|
|
|
String::new()
|
|
|
|
};
|
2018-08-18 10:37:40 +00:00
|
|
|
|
2018-07-06 09:51:19 +00:00
|
|
|
if errors.is_empty() {
|
2018-12-29 08:36:07 +00:00
|
|
|
cookies.add_private(Cookie::build(AUTH_COOKIE, user_id)
|
2018-09-30 09:56:12 +00:00
|
|
|
.same_site(SameSite::Lax)
|
|
|
|
.finish());
|
2018-09-07 23:11:27 +00:00
|
|
|
|
|
|
|
let destination = flash
|
|
|
|
.and_then(|f| if f.name() == "callback" {
|
|
|
|
Some(f.msg().to_owned())
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
})
|
2018-11-26 09:21:52 +00:00
|
|
|
.unwrap_or_else(|| "/".to_owned());
|
2018-09-07 23:11:27 +00:00
|
|
|
|
|
|
|
let uri = Uri::parse(&destination)
|
|
|
|
.map(|x| x.into_owned())
|
2018-12-07 20:00:12 +00:00
|
|
|
.map_err(|_| render!(session::login(
|
2018-12-06 17:54:16 +00:00
|
|
|
&(&*conn, &intl.catalog, None),
|
|
|
|
None,
|
|
|
|
&*form,
|
|
|
|
errors
|
|
|
|
)))?;
|
2018-09-07 23:11:27 +00:00
|
|
|
|
|
|
|
Ok(Redirect::to(uri))
|
2018-07-06 09:51:19 +00:00
|
|
|
} else {
|
2018-12-06 17:54:16 +00:00
|
|
|
Err(render!(session::login(
|
|
|
|
&(&*conn, &intl.catalog, None),
|
|
|
|
None,
|
|
|
|
&*form,
|
|
|
|
errors
|
|
|
|
)))
|
2018-04-23 09:52:44 +00:00
|
|
|
}
|
|
|
|
}
|
2018-04-23 11:13:49 +00:00
|
|
|
|
|
|
|
#[get("/logout")]
|
2018-12-06 17:54:16 +00:00
|
|
|
pub fn delete(mut cookies: Cookies) -> Redirect {
|
2018-11-26 09:21:52 +00:00
|
|
|
if let Some(cookie) = cookies.get_private(AUTH_COOKIE) {
|
|
|
|
cookies.remove_private(cookie);
|
|
|
|
}
|
2018-04-23 11:13:49 +00:00
|
|
|
Redirect::to("/")
|
|
|
|
}
|