an http based roguelike rpg about jelqing
Blame: src/http/shame.rs
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 1 | use axum::http::HeaderValue; |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 2 | |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 3 | use super::ctx::Ctx; |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 4 | use crate::{game::Run, secret::Secret}; |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 5 | |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 6 | pub const COOKIE_ATTRIBUTES: &str = "Max-Age=31536000; Path=/; HttpOnly; SameSite=Strict"; |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 7 | |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 8 | pub fn settle(run: &mut Run, ctx: &Ctx, secret: &Secret) -> (u32, HeaderValue) { |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 9 | let presented = ctx |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 10 | .cookie("shame") |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 11 | .and_then(|signed| secret.verify("shame", signed)) |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 12 | .and_then(|total| total.parse::<u32>().ok()); |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 13 | let total = run.settle_shame(presented); |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 14 | let cookie = format!( |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 15 | "shame={}; {COOKIE_ATTRIBUTES}", |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 16 | secret.sign("shame", &total.to_string()) |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 17 | ); |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 18 | ( |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 19 | total, |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 20 | HeaderValue::from_str(&cookie).expect("a signed number is a valid header value"), |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 21 | ) |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 22 | } |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 23 | |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 24 | pub fn ruler_cookie(secret: &Secret) -> HeaderValue { |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 25 | HeaderValue::from_str(&format!( |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 26 | "ruler={}; {COOKIE_ATTRIBUTES}", |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 27 | secret.sign("ruler", "1") |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 28 | )) |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 29 | .expect("a signed constant is a valid header value") |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 30 | } |