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