diff --git a/src/api/v1/bench.rs b/src/api/v1/bench.rs index f4404f6..0e82cce 100644 --- a/src/api/v1/bench.rs +++ b/src/api/v1/bench.rs @@ -129,8 +129,20 @@ async fn register( session: Session, path: web::Query, ) -> ServiceResult { - let uuid = runners::register_runner(&data).await?; - session.insert(SURVEY_USER_ID, uuid.to_string()).unwrap(); + let is_authenticated = || { + if let Ok(Some(_)) = session.get::(SURVEY_USER_ID) { + log::info!("user already registered"); + true + } else { + false + } + }; + + if !is_authenticated() { + let uuid = runners::register_runner(&data).await?; + session.insert(SURVEY_USER_ID, uuid.to_string()).unwrap(); + session.get::(SURVEY_USER_ID).unwrap().unwrap(); + } let path = path.into_inner(); if let Some(redirect_to) = path.redirect_to { Ok(HttpResponse::Found() diff --git a/src/main.rs b/src/main.rs index c9b6fd9..bf44f1a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -109,8 +109,8 @@ async fn main() -> std::io::Result<()> { actix_middleware::DefaultHeaders::new() .header("Permissions-Policy", "interest-cohort=()"), ) - .wrap(get_identity_service()) .wrap(get_survey_session()) + .wrap(get_identity_service()) .wrap(actix_middleware::NormalizePath::new( actix_middleware::TrailingSlash::Trim, )) @@ -134,12 +134,13 @@ pub fn get_json_err() -> JsonConfig { #[cfg(not(tarpaulin_include))] pub fn get_survey_session() -> actix_session::CookieSession { let cookie_secret = &SETTINGS.server.cookie_secret2; - actix_session::CookieSession::private(cookie_secret.as_bytes()) + actix_session::CookieSession::signed(cookie_secret.as_bytes()) + .lazy(true) .domain(&SETTINGS.server.domain) .name("survey-id") - .path("/survey") + .http_only(true) + .path("/") .max_age(30 * 60) - .domain(&SETTINGS.server.domain) .secure(false) } diff --git a/templates/api/v1/routes.ts b/templates/api/v1/routes.ts new file mode 100644 index 0000000..9e4f4bf --- /dev/null +++ b/templates/api/v1/routes.ts @@ -0,0 +1,22 @@ +/* + * Copyright (C) 2021 Aravinth Manivannan + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +const ROUTES = { + register: "/survey/api/v1/benches/register", +}; + +export default ROUTES; diff --git a/templates/bench/index.ts b/templates/bench/index.ts index dd15527..b8e9474 100644 --- a/templates/bench/index.ts +++ b/templates/bench/index.ts @@ -15,10 +15,15 @@ * along with this program. If not, see . */ import { Perf } from "./types"; +import ROUTES from "../api/v1/routes"; export const index = () => { - console.log("running bench init"); const FACTOR = 500000; + + const initSession = async () => { + fetch(ROUTES.register); + }; + const worker = new Worker("/bench.js"); const res: Array = []; const stats = document.getElementById("stats"); @@ -71,8 +76,9 @@ export const index = () => { s.innerHTML = "Benchmark finished"; }; - const run = (e: Event) => { + const run = async (e: Event) => { e.preventDefault(); + await initSession(); document.getElementById("pre-bench").style.display = "none"; document.getElementById("bench").style.display = "flex"; @@ -100,5 +106,7 @@ export const index = () => { addDeviceInfo(); }; - document.getElementById("start").addEventListener("click", (e) => run(e)); + document + .getElementById("start") + .addEventListener("click", async (e) => await run(e)); };