// This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this // file, You can obtain one at https://mozilla.org/MPL/2.0/. //! The share-link visitor pages (docs/ui-api.md ยง4): one self-contained HTML page each, inline //! CSS, no JavaScript, no external requests, under 8 KB. Only the hostname and a date come //! from the request side, both escaped; the token is echoed only into the password form's //! hidden field (it is already in the visitor's address bar at that point), escaped too. use ianitor_core::share::PASSWORD_PATH; use ianitor_core::time::long_date; pub enum Problem { WrongPassword, TooMany, } pub enum Page<'a> { Password { host: &'a str, token: &'a str, until: u64, problem: Option, }, Expired { host: &'a str, until: u64, }, Invalid { host: &'a str, }, } impl Page<'_> { pub fn status(&self) -> u16 { match self { Page::Password { problem: Some(Problem::TooMany), .. } => 429, Page::Password { .. } => 401, Page::Expired { .. } => 410, Page::Invalid { .. } => 403, } } } pub fn escape(s: &str) -> String { let mut out = String::with_capacity(s.len()); for c in s.chars() { match c { '&' => out.push_str("&"), '<' => out.push_str("<"), '>' => out.push_str(">"), '"' => out.push_str("""), '\'' => out.push_str("'"), c if c.is_control() => {} c => out.push(c), } } out } const STYLE: &str = "\ *{box-sizing:border-box}\ body{margin:0;min-height:100vh;display:flex;flex-direction:column;align-items:center;justify-content:center;\ background:#0c0b0f;color:#f1edf3;font-family:system-ui,-apple-system,\"Segoe UI\",Roboto,sans-serif;padding:16px}\ .card{width:100%;max-width:420px;padding:32px;background:#18151d;border:1px solid #3c3445;border-radius:18px}\ @media (max-width:480px){.card{padding:24px}}\ .host{color:#cfc6d4;font-size:.95rem;font-weight:600;margin:0 0 16px;overflow-wrap:anywhere}\ svg{display:block;margin:0 0 12px}\ h1{font-size:1.5rem;font-weight:700;color:#f1edf3;margin:0 0 8px}\ p{font-size:1rem;line-height:1.5;color:#cfc6d4;margin:0 0 16px}\ label{display:block;color:#cfc6d4;font-size:.9rem;margin:0 0 6px}\ input[type=password]{width:100%;height:48px;background:#0c0b0f;border:1px solid #7d7189;border-radius:10px;\ color:#f1edf3;font-size:1rem;padding:0 12px}\ input[type=password]:focus{outline:none;border-color:#e0a3dc;box-shadow:0 0 0 4px rgba(224,163,220,.16)}\ .err{color:#e2683a;font-size:.92rem;margin:8px 0 0;display:flex;gap:6px;align-items:center}\ button{margin-top:16px;width:100%;height:48px;border-radius:12px;border:1px solid #c77cc1;color:#fff;font-weight:700;\ font-size:1rem;background:linear-gradient(#8d3b89,#76306f 55%,#5c2257)}\ button:focus{outline:2px solid #f2c9ee;outline-offset:2px}\ button:disabled{opacity:.5}\ .small{font-size:.92rem;margin:16px 0 0}\ a{color:#e0a3dc;display:inline-flex;align-items:center;min-height:44px}\ footer{color:#a99eb1;font-size:.72rem;letter-spacing:.2em;text-transform:uppercase;margin-top:16px}"; fn icon(kind: &str) -> String { let (colour, body) = match kind { // lock "lock" => ( "#e0a3dc", "", ), // clock "clock" => ( "#d99a35", "", ), // broken link _ => ( "#d99a35", "", ), }; format!( "{body}" ) } const RING: &str = ""; fn shell(host: &str, inner: &str, title: &str) -> String { format!( "\ \ \ {title}
\

{}

{inner}
Ianitor Domi
", escape(host) ) } pub fn render(p: &Page<'_>) -> String { match p { Page::Password { host, token, until, problem, } => { let h = escape(host); let (msg, disabled) = match problem { Some(Problem::WrongPassword) => ( format!( "

{RING}That password is not right. Check it with the person who sent the link.

" ), "", ), Some(Problem::TooMany) => ( format!( "

{RING}Too many tries. Wait a minute, then try again.

" ), " disabled", ), None => (String::new(), ""), }; let inner = format!( "{}

This link is protected

\

Someone shared {h} with you. Enter the password they gave you.

\
\ \ \ \ {msg}
\

The link works until {}.

", icon("lock"), escape(token), long_date(*until) ); shell(host, &inner, "This link is protected") } Page::Expired { host, until } => { let inner = format!( "{}

This link has expired

\

It worked until {}. Ask the person who sent it for a new one.

\ Have an account? Sign in", icon("clock"), long_date(*until) ); shell(host, &inner, "This link has expired") } Page::Invalid { host } => { let inner = format!( "{}

This link no longer works

\

It may have been stopped, or not copied in full. Ask the person who sent it for a new one.

\ Have an account? Sign in", icon("broken") ); shell(host, &inner, "This link no longer works") } } } #[cfg(test)] mod tests { use super::*; #[test] fn pages_are_small_and_escaped() { for p in [ Page::Password { host: "x", token: "\">