Plume/src/routes/session.rs

104 lines
3.2 KiB
Rust
Raw Normal View History

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