From ed302843868894e78ac60c95b00f549804c16347 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Igor=20Gali=C4=87?= Date: Thu, 15 Aug 2019 17:47:19 +0200 Subject: [PATCH] implement domain validation using rocket::State this doesn't work (yet?), because i don't know how to store something mutable in State<> --- src/main.rs | 7 +++++-- src/routes/blogs.rs | 25 +++++++++++++++++++++++-- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/src/main.rs b/src/main.rs index 7ff67b41..fc3ab79f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,5 @@ #![allow(clippy::too_many_arguments)] -#![feature(decl_macro, proc_macro_hygiene, try_trait)] +#![feature(decl_macro, proc_macro_hygiene, checked_duration_since, try_trait)] extern crate activitypub; extern crate askama_escape; @@ -53,9 +53,10 @@ use plume_models::{ use rocket::{fairing::AdHoc, http::ext::IntoOwned, http::uri::Origin}; use rocket_csrf::CsrfFairingBuilder; use scheduled_thread_pool::ScheduledThreadPool; +use std::collections::HashMap; use std::process::exit; use std::sync::{Arc, Mutex}; -use std::time::Duration; +use std::time::{Duration, Instant}; init_i18n!( "plume", ar, bg, ca, cs, de, en, eo, es, fr, gl, hi, hr, it, ja, nb, pl, pt, ro, ru, sr, sk, sv @@ -196,6 +197,7 @@ Then try to restart Plume } }); + let mut valid_domains: HashMap<&str, Instant> = HashMap::new(); let rocket = rocket::custom(CONFIG.rocket.clone().unwrap()) .mount( "/custom_domains/", @@ -322,6 +324,7 @@ Then try to restart Plume .manage(dbpool) .manage(Arc::new(workpool)) .manage(searcher) + .manage(valid_domains) .manage(include_i18n!()) .attach( CsrfFairingBuilder::new() diff --git a/src/routes/blogs.rs b/src/routes/blogs.rs index 8837640a..76736b34 100644 --- a/src/routes/blogs.rs +++ b/src/routes/blogs.rs @@ -5,8 +5,10 @@ use rocket::{ http::ContentType, request::LenientForm, response::{content::Content, Flash, Redirect}, + State, }; use rocket_i18n::I18n; +use std::time::Instant; use std::{borrow::Cow, collections::HashMap}; use validator::{Validate, ValidationError, ValidationErrors}; @@ -96,8 +98,27 @@ pub fn new(rockets: PlumeRocket, _user: User) -> Ructe { } #[get("/validate/")] -pub fn validate(validation_id: String, rockets: PlumeRocket) -> Result { - unimplemented!("No idea what to do here yet") +pub fn domain_validation( + validation_id: String, + valid_domains: State>, +) -> Result { + let value = valid_domains.inner().get(validation_id.as_str()); + if value.is_none() { + return Err(String::from("404")); + } + + // we have valid id, now check the time + let valid_until = value.unwrap(); + let now = Instant::now(); + + // nope, expired (410: gone) + if now.checked_duration_since(*valid_until).unwrap().as_secs() > 0 { + valid_domains.inner().remove(validation_id.as_str()); + return Err(String::from("410")); + } + + valid_domains.inner().remove(validation_id.as_str()); + return Ok(String::from("200")); } pub mod custom {