use crate::{EventBuilder, Kind, RelayUrl, Tag};
impl EventBuilder {
pub fn auth(relay: &RelayUrl, challenge: &str) -> Self {
Self::new(Kind::CLIENT_AUTH, "").tags([
Tag::custom("relay", [relay.as_str()]),
Tag::custom("challenge", [challenge]),
])
}
}
#[must_use]
pub fn is_auth_required(reason: &str) -> bool {
reason.starts_with("auth-required:")
}
#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::panic, reason = "tests fail by panicking")]
mod tests {
use alloc::string::String;
use alloc::vec::Vec;
use crate::{PublicKey, Timestamp};
use super::*;
#[test]
fn auth_event_shape() {
let relay = RelayUrl::parse("wss://relay.example").unwrap();
let event = EventBuilder::auth(&relay, "abc123").build_at(
PublicKey::from_hex("79c2cae114ea28a981e7559b4fe7854a473521a8d22a66bbab9fa248eb820ff6")
.unwrap(),
Timestamp::from_secs(1_700_000_000),
);
assert_eq!(event.kind(), Kind::CLIENT_AUTH);
assert_eq!(event.content(), "");
assert_eq!(event.created_at(), Timestamp::from_secs(1_700_000_000));
let tags: Vec<_> = event.tags().iter().map(Tag::as_slice).collect();
assert_eq!(
tags,
[
["relay", relay.as_str()].as_slice(),
["challenge", "abc123"].as_slice(),
]
);
}
#[test]
fn relay_url_is_normalized() {
let relay = RelayUrl::parse("WSS://RELAY.EXAMPLE:443").unwrap();
assert_eq!(relay.as_str(), "wss://relay.example/");
let event = EventBuilder::auth(&relay, "").build_at(
PublicKey::from_hex("79c2cae114ea28a981e7559b4fe7854a473521a8d22a66bbab9fa248eb820ff6")
.unwrap(),
Timestamp::from_secs(0),
);
assert_eq!(
event
.tags()
.iter()
.next()
.unwrap()
.as_slice()
.get(1)
.map(String::as_str),
Some("wss://relay.example/")
);
}
#[test]
fn auth_required_prefix() {
assert!(is_auth_required("auth-required: take a ticket"));
assert!(is_auth_required("auth-required:"));
assert!(!is_auth_required("auth-required"));
assert!(!is_auth_required(" auth-required: leading space"));
assert!(!is_auth_required("restricted: nope"));
assert!(!is_auth_required(""));
}
}