use alloy_primitives::address;
use super::*;
use crate::error::VerificationKind;
const ORIGIN: &str = "http://localhost:5173";
const ADDRESS: Address = address!("5aaeb6053f3e94c9b9a09f33669435e7ef1beaed");
const MESSAGE: &str = "localhost:5173 wants you to sign in with your Ethereum account:\n\
0x5aAeb6053F3E94C9b9A09f33669435E7Ef1BeAed\n\n\
Sign in to LaMarge.\n\n\
URI: http://localhost:5173\n\
Version: 1\n\
Chain ID: 1\n\
Nonce: 0123456789abcdef\n\
Issued At: 2026-09-27T10:00:00Z\n\
Expiration Time: 2026-09-27T10:10:00Z";
const NONCE: &str = "0123456789abcdef";
fn at(text: &str) -> DateTime<Utc> {
text.parse().unwrap()
}
fn check(
message: &str,
nonce: &str,
origin: &str,
now: DateTime<Utc>,
) -> Result<Verified, VerificationError> {
check_login_challenge(message, nonce, at("2026-09-27T10:10:00Z"), ADDRESS, origin, now)
}
#[test]
fn the_servers_message_is_accepted_within_two_minutes_of_skew() {
let now = at("2026-09-27T10:00:00Z");
for skew in [-120, 0, 120] {
let verified = check(MESSAGE, NONCE, ORIGIN, now + Duration::seconds(skew)).unwrap();
assert_eq!(verified.digest(), alloy_primitives::eip191_hash_message(MESSAGE));
}
let verified = check(MESSAGE, NONCE, ORIGIN, now).unwrap();
assert_eq!(verified.not_after(), Some(at("2026-09-27T10:11:59Z").timestamp()));
let lower = MESSAGE.replace(
"0x5aAeb6053F3E94C9b9A09f33669435E7Ef1BeAed",
"0x5aaeb6053f3e94c9b9a09f33669435e7ef1beaed",
);
assert!(check(&lower, NONCE, ORIGIN, now).is_ok());
}
#[test]
fn origin_address_nonce_and_text_are_pinned() {
let now = at("2026-09-27T10:00:00Z");
let differs = |message: &str, nonce: &str, origin: &str| {
let error = check(message, nonce, origin, now).unwrap_err();
(error.kind, error.detail) == (VerificationKind::Differs, details::CHALLENGE)
};
for (from, to) in [
(
"0x5aAeb6053F3E94C9b9A09f33669435E7Ef1BeAed",
"0x1111111111111111111111111111111111111111",
),
("Chain ID: 1", "Chain ID: 137"),
("Sign in to LaMarge.", "Sign something else"),
("URI: http://localhost:5173", "URI: https://localhost:5173"),
("localhost:5173 wants", "evil.example wants"),
("Version: 1", "Version: 2"),
("Expiration Time: 2026-09-27T10:10:00Z", "Expiration Time: 2026-09-27T10:09:00Z"),
("Issued At: 2026-09-27T10:00:00Z", "Issued At: yesterday"),
] {
assert!(differs(&MESSAGE.replace(from, to), NONCE, ORIGIN), "{to}");
}
assert!(differs(MESSAGE, "other", ORIGIN));
assert!(differs(MESSAGE, NONCE, "https://other.example"));
assert!(differs(&format!("{MESSAGE}\nResources: x"), NONCE, ORIGIN));
assert!(differs(&MESSAGE.replacen("\n\n", "\n", 1), NONCE, ORIGIN));
}
#[test]
fn windows_are_bounded() {
let stale = |message: &str, now: &str| {
let error = check(message, NONCE, ORIGIN, at(now)).unwrap_err();
(error.kind, error.detail) == (VerificationKind::Stale, details::CHALLENGE_WINDOW)
};
assert!(stale(MESSAGE, "2026-09-27T10:12:00Z"));
assert!(check(MESSAGE, NONCE, ORIGIN, at("2026-09-27T10:11:59Z")).is_ok());
assert!(stale(MESSAGE, "2026-09-27T09:57:59Z"));
assert!(stale(
&MESSAGE.replace("Issued At: 2026-09-27T10:00:00Z", "Issued At: 2026-09-27T09:59:59Z"),
"2026-09-27T10:00:00Z"
));
let backwards =
MESSAGE.replace("Issued At: 2026-09-27T10:00:00Z", "Issued At: 2026-09-27T10:10:01Z");
assert!(stale(&backwards, "2026-09-27T10:09:00Z"));
}