Make bridge tokens optional

This commit is contained in:
Vecna 2023-12-05 18:05:44 -05:00
parent 086e1a6a70
commit 1f21eeeb53
1 changed files with 14 additions and 5 deletions

View File

@ -8,6 +8,9 @@ use sha3::Sha3_256;
// for generating ed25519 keys during initial development // for generating ed25519 keys during initial development
use rand::rngs::OsRng; use rand::rngs::OsRng;
// TODO: These should be loaded from config file
pub const REQUIRE_BRIDGE_TOKEN: bool = true;
/// Get Julian date /// Get Julian date
pub fn today() -> u32 { pub fn today() -> u32 {
time::OffsetDateTime::now_utc() time::OffsetDateTime::now_utc()
@ -127,7 +130,7 @@ pub struct PositiveUserReport {
/// hashed fingerprint (SHA-1 hash of 20-byte bridge ID) /// hashed fingerprint (SHA-1 hash of 20-byte bridge ID)
pub fingerprint: [u8; 20], pub fingerprint: [u8; 20],
/// token from the bridge indicating it was reached /// token from the bridge indicating it was reached
pub bridge_token: BridgeToken, pub bridge_token: Option<BridgeToken>,
// TODO: proof of level, something involving credential show // TODO: proof of level, something involving credential show
/// user's country code, may be an empty string /// user's country code, may be an empty string
pub country: String, pub country: String,
@ -136,7 +139,7 @@ pub struct PositiveUserReport {
} }
impl PositiveUserReport { impl PositiveUserReport {
pub fn new(bridge_id: [u8; 20], bridge_token: BridgeToken, country: String) -> Self { pub fn new(bridge_id: [u8; 20], bridge_token: Option<BridgeToken>, country: String) -> Self {
let mut hasher = Sha1::new(); let mut hasher = Sha1::new();
hasher.update(bridge_id); hasher.update(bridge_id);
let fingerprint: [u8; 20] = hasher.finalize().into(); let fingerprint: [u8; 20] = hasher.finalize().into();
@ -153,9 +156,15 @@ impl PositiveUserReport {
impl Report for PositiveUserReport { impl Report for PositiveUserReport {
fn verify(&self) -> bool { fn verify(&self) -> bool {
// possibly include check that self.today is recent as well // possibly include check that self.today is recent as well
self.today == self.bridge_token.unsigned_bridge_token.today self.today <= today()
&& self.today <= today() && (!REQUIRE_BRIDGE_TOKEN || {
&& self.bridge_token.verify() if self.bridge_token.is_none() {
false
} else {
let bt = self.bridge_token.as_ref().unwrap();
self.today == bt.unsigned_bridge_token.today && bt.verify()
}
})
} }
} }