implement domain validation using rocket::State

this doesn't work (yet?), because i don't know how to store something
mutable in State<>
This commit is contained in:
Igor Galić 2019-08-15 17:47:19 +02:00
parent bf1673dda1
commit ed30284386
No known key found for this signature in database
GPG Key ID: ACFEFF7F6A123A86
2 changed files with 28 additions and 4 deletions

View File

@ -1,5 +1,5 @@
#![allow(clippy::too_many_arguments)] #![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 activitypub;
extern crate askama_escape; extern crate askama_escape;
@ -53,9 +53,10 @@ use plume_models::{
use rocket::{fairing::AdHoc, http::ext::IntoOwned, http::uri::Origin}; use rocket::{fairing::AdHoc, http::ext::IntoOwned, http::uri::Origin};
use rocket_csrf::CsrfFairingBuilder; use rocket_csrf::CsrfFairingBuilder;
use scheduled_thread_pool::ScheduledThreadPool; use scheduled_thread_pool::ScheduledThreadPool;
use std::collections::HashMap;
use std::process::exit; use std::process::exit;
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
use std::time::Duration; use std::time::{Duration, Instant};
init_i18n!( 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 "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()) let rocket = rocket::custom(CONFIG.rocket.clone().unwrap())
.mount( .mount(
"/custom_domains/", "/custom_domains/",
@ -322,6 +324,7 @@ Then try to restart Plume
.manage(dbpool) .manage(dbpool)
.manage(Arc::new(workpool)) .manage(Arc::new(workpool))
.manage(searcher) .manage(searcher)
.manage(valid_domains)
.manage(include_i18n!()) .manage(include_i18n!())
.attach( .attach(
CsrfFairingBuilder::new() CsrfFairingBuilder::new()

View File

@ -5,8 +5,10 @@ use rocket::{
http::ContentType, http::ContentType,
request::LenientForm, request::LenientForm,
response::{content::Content, Flash, Redirect}, response::{content::Content, Flash, Redirect},
State,
}; };
use rocket_i18n::I18n; use rocket_i18n::I18n;
use std::time::Instant;
use std::{borrow::Cow, collections::HashMap}; use std::{borrow::Cow, collections::HashMap};
use validator::{Validate, ValidationError, ValidationErrors}; use validator::{Validate, ValidationError, ValidationErrors};
@ -96,8 +98,27 @@ pub fn new(rockets: PlumeRocket, _user: User) -> Ructe {
} }
#[get("/validate/<validation_id>")] #[get("/validate/<validation_id>")]
pub fn validate(validation_id: String, rockets: PlumeRocket) -> Result { pub fn domain_validation(
unimplemented!("No idea what to do here yet") validation_id: String,
valid_domains: State<HashMap<&str, Instant>>,
) -> Result<String, String> {
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 { pub mod custom {