forked from Plume/Plume
12efe721cc
* Big refactoring of the Inbox We now have a type that routes an activity through the registered handlers until one of them matches. Each Actor/Activity/Object combination is represented by an implementation of AsObject These combinations are then registered on the Inbox type, which will try to deserialize the incoming activity in the requested types. Advantages: - nicer syntax: the final API is clearer and more idiomatic - more generic: only two traits (`AsActor` and `AsObject`) instead of one for each kind of activity - it is easier to see which activities we handle and which one we don't * Small fixes - Avoid panics - Don't search for AP ID infinitely - Code style issues * Fix tests * Introduce a new trait: FromId It should be implemented for any AP object. It allows to look for an object in database using its AP ID, or to dereference it if it was not present in database Also moves the inbox code to plume-models to test it (and write a basic test for each activity type we handle) * Use if let instead of match * Don't require PlumeRocket::intl for tests * Return early and remove a forgotten dbg! * Add more tests to try to understand where the issues come from * Also add a test for comment federation * Don't check creation_date is the same for blogs * Make user and blog federation more tolerant to errors/missing fields * Make clippy happy * Use the correct Accept header when dereferencing * Fix follow approval with Mastodon * Add spaces to characters that should not be in usernames And validate blog names too * Smarter dereferencing: only do it once for each actor/object * Forgot some files * Cargo fmt * Delete plume_test * Delete plume_tests * Update get_id docs + Remove useless : Sized * Appease cargo fmt * Remove dbg! + Use as_ref instead of clone when possible + Use and_then instead of map when possible * Remove .po~ * send unfollow to local instance * read cover from update activity * Make sure "cc" and "to" are never empty and fix a typo in a constant name * Cargo fmt
287 lines
8.2 KiB
Rust
287 lines
8.2 KiB
Rust
use lettre::Transport;
|
|
use rocket::http::ext::IntoOwned;
|
|
use rocket::{
|
|
http::{uri::Uri, Cookie, Cookies, SameSite},
|
|
request::{FlashMessage, Form, LenientForm},
|
|
response::Redirect,
|
|
State,
|
|
};
|
|
use rocket_i18n::I18n;
|
|
use std::{
|
|
borrow::Cow,
|
|
sync::{Arc, Mutex},
|
|
time::Instant,
|
|
};
|
|
use template_utils::Ructe;
|
|
use validator::{Validate, ValidationError, ValidationErrors};
|
|
|
|
use mail::{build_mail, Mailer};
|
|
use plume_models::{
|
|
db_conn::DbConn,
|
|
users::{User, AUTH_COOKIE},
|
|
Error, PlumeRocket, CONFIG,
|
|
};
|
|
use routes::errors::ErrorPage;
|
|
|
|
#[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)]
|
|
pub struct LoginForm {
|
|
#[validate(length(min = "1", message = "We need an email, or a username to identify you"))]
|
|
pub email_or_name: String,
|
|
#[validate(length(min = "1", message = "Your password can't be empty"))]
|
|
pub password: String,
|
|
}
|
|
|
|
#[post("/login", data = "<form>")]
|
|
pub fn create(
|
|
form: LenientForm<LoginForm>,
|
|
flash: Option<FlashMessage>,
|
|
mut cookies: Cookies,
|
|
rockets: PlumeRocket,
|
|
) -> Result<Redirect, Ructe> {
|
|
let conn = &*rockets.conn;
|
|
let user = User::find_by_email(&*conn, &form.email_or_name)
|
|
.or_else(|_| User::find_by_fqn(&rockets, &form.email_or_name));
|
|
let mut errors = match form.validate() {
|
|
Ok(_) => ValidationErrors::new(),
|
|
Err(e) => e,
|
|
};
|
|
|
|
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");
|
|
|
|
let mut err = ValidationError::new("invalid_login");
|
|
err.message = Some(Cow::from("Invalid username, or password"));
|
|
errors.add("email_or_name", err);
|
|
String::new()
|
|
};
|
|
|
|
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(IntoOwned::into_owned)
|
|
.map_err(|_| {
|
|
render!(session::login(
|
|
&(&*conn, &rockets.intl.catalog, None),
|
|
None,
|
|
&*form,
|
|
errors
|
|
))
|
|
})?;
|
|
|
|
Ok(Redirect::to(uri))
|
|
} else {
|
|
Err(render!(session::login(
|
|
&(&*conn, &rockets.intl.catalog, None),
|
|
None,
|
|
&*form,
|
|
errors
|
|
)))
|
|
}
|
|
}
|
|
|
|
#[get("/logout")]
|
|
pub fn delete(mut cookies: Cookies) -> Redirect {
|
|
if let Some(cookie) = cookies.get_private(AUTH_COOKIE) {
|
|
cookies.remove_private(cookie);
|
|
}
|
|
Redirect::to("/")
|
|
}
|
|
|
|
#[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>,
|
|
requests: State<Arc<Mutex<Vec<ResetRequest>>>>,
|
|
) -> 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);
|
|
|
|
if User::find_by_email(&*conn, &form.email).is_ok()
|
|
&& !requests.iter().any(|x| x.mail == form.email.clone())
|
|
{
|
|
let id = plume_common::utils::random_hex();
|
|
|
|
requests.push(ResetRequest {
|
|
mail: form.email.clone(),
|
|
id: id.clone(),
|
|
creation_date: Instant::now(),
|
|
});
|
|
|
|
let link = format!("https://{}/password-reset/{}", CONFIG.base_url, id);
|
|
if let Some(message) = build_mail(
|
|
form.email.clone(),
|
|
i18n!(intl.catalog, "Password reset"),
|
|
i18n!(intl.catalog, "Here is the link to reset your password: {0}"; link),
|
|
) {
|
|
if let Some(ref mut mail) = *mail.lock().unwrap() {
|
|
mail.send(message.into())
|
|
.map_err(|_| eprintln!("Couldn't send password reset email"))
|
|
.ok();
|
|
}
|
|
}
|
|
}
|
|
render!(session::password_reset_request_ok(&(
|
|
&*conn,
|
|
&intl.catalog,
|
|
None
|
|
)))
|
|
}
|
|
|
|
#[get("/password-reset/<token>")]
|
|
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)?;
|
|
Ok(render!(session::password_reset(
|
|
&(&*conn, &intl.catalog, None),
|
|
&NewPasswordForm::default(),
|
|
ValidationErrors::default()
|
|
)))
|
|
}
|
|
|
|
#[derive(FromForm, Default, Validate)]
|
|
#[validate(schema(
|
|
function = "passwords_match",
|
|
skip_on_field_errors = "false",
|
|
message = "Passwords are not matching"
|
|
))]
|
|
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>>>>,
|
|
form: Form<NewPasswordForm>,
|
|
) -> Result<Redirect, Ructe> {
|
|
form.validate()
|
|
.and_then(|_| {
|
|
let mut requests = requests.lock().unwrap();
|
|
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
|
|
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();
|
|
Ok(Redirect::to(uri!(
|
|
new: m = i18n!(intl.catalog, "Your password was successfully reset.")
|
|
)))
|
|
} else {
|
|
Ok(Redirect::to(uri!(
|
|
new: m = i18n!(intl.catalog, "Sorry, but the link expired. Try again")
|
|
)))
|
|
}
|
|
})
|
|
.map_err(|err| {
|
|
render!(session::password_reset(
|
|
&(&*conn, &intl.catalog, None),
|
|
&form,
|
|
err
|
|
))
|
|
})
|
|
}
|
|
|
|
fn to_validation<T>(_: T) -> ValidationErrors {
|
|
let mut errors = ValidationErrors::new();
|
|
errors.add(
|
|
"",
|
|
ValidationError {
|
|
code: Cow::from("server_error"),
|
|
message: Some(Cow::from("An unknown error occured")),
|
|
params: std::collections::HashMap::new(),
|
|
},
|
|
);
|
|
errors
|
|
}
|