2018-05-19 09:39:59 +02:00
|
|
|
use rocket::{
|
2018-09-30 11:56:12 +02:00
|
|
|
http::{Cookie, Cookies, SameSite, uri::Uri},
|
2018-07-06 11:51:19 +02:00
|
|
|
response::Redirect,
|
2018-06-24 18:58:57 +02:00
|
|
|
request::{LenientForm,FlashMessage}
|
2018-05-19 09:39:59 +02:00
|
|
|
};
|
2018-09-08 01:11:27 +02:00
|
|
|
use rocket::http::ext::IntoOwned;
|
2018-12-06 18:54:16 +01:00
|
|
|
use rocket_i18n::I18n;
|
2018-08-18 12:37:40 +02:00
|
|
|
use std::borrow::Cow;
|
2018-07-06 11:51:19 +02:00
|
|
|
use validator::{Validate, ValidationError, ValidationErrors};
|
2018-12-06 18:54:16 +01:00
|
|
|
use template_utils::Ructe;
|
2018-04-24 11:21:39 +02:00
|
|
|
|
2018-06-23 18:36:11 +02:00
|
|
|
use plume_models::{
|
|
|
|
db_conn::DbConn,
|
|
|
|
users::{User, AUTH_COOKIE}
|
|
|
|
};
|
2018-04-23 11:52:44 +02:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-12-06 18:54:16 +01:00
|
|
|
#[derive(Default, FromForm, Validate, Serialize)]
|
|
|
|
pub struct LoginForm {
|
2018-07-07 22:51:48 +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"))]
|
2018-12-06 18:54:16 +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>")]
|
|
|
|
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)
|
2018-12-29 09:36:07 +01:00
|
|
|
.or_else(|_| User::find_local(&*conn, &form.email_or_name));
|
2018-07-06 11:51:19 +02:00
|
|
|
let mut errors = match form.validate() {
|
|
|
|
Ok(_) => ValidationErrors::new(),
|
|
|
|
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
|
2018-12-29 09:36:07 +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");
|
|
|
|
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() {
|
2018-12-29 09:36:07 +01:00
|
|
|
cookies.add_private(Cookie::build(AUTH_COOKIE, user_id)
|
2018-09-30 11:56:12 +02:00
|
|
|
.same_site(SameSite::Lax)
|
|
|
|
.finish());
|
2018-09-08 01:11:27 +02:00
|
|
|
let destination = flash
|
|
|
|
.and_then(|f| if f.name() == "callback" {
|
|
|
|
Some(f.msg().to_owned())
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
})
|
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)
|
|
|
|
.map(|x| x.into_owned())
|
2018-12-07 21:00:12 +01:00
|
|
|
.map_err(|_| render!(session::login(
|
2018-12-06 18:54:16 +01:00
|
|
|
&(&*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("/")
|
|
|
|
}
|