2020-12-12 17:11:22 +00:00
|
|
|
use std::{
|
|
|
|
convert::TryInto,
|
2020-12-12 17:50:40 +00:00
|
|
|
fmt::{self, Debug, Formatter},
|
2020-12-12 17:11:22 +00:00
|
|
|
ops::Deref,
|
|
|
|
};
|
|
|
|
|
|
|
|
use chrono::{DateTime, Duration, Utc};
|
|
|
|
use serde::{
|
|
|
|
de::{
|
|
|
|
self,
|
|
|
|
Visitor,
|
|
|
|
},
|
|
|
|
Deserialize,
|
|
|
|
Deserializer,
|
2021-04-27 19:31:58 +00:00
|
|
|
Serialize,
|
2020-12-12 17:11:22 +00:00
|
|
|
};
|
|
|
|
|
2020-12-12 17:50:40 +00:00
|
|
|
#[derive (Copy, Clone, PartialEq, Eq)]
|
2020-12-12 17:11:22 +00:00
|
|
|
pub struct BlakeHashWrapper (blake3::Hash);
|
|
|
|
|
2020-12-12 17:50:40 +00:00
|
|
|
impl Debug for BlakeHashWrapper {
|
|
|
|
fn fmt (&self, f: &mut Formatter <'_>) -> Result <(), fmt::Error> {
|
|
|
|
write! (f, "{}", self.encode_base64 ())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-12 17:11:22 +00:00
|
|
|
impl BlakeHashWrapper {
|
2021-03-21 03:34:47 +00:00
|
|
|
#[must_use]
|
2020-12-12 17:11:22 +00:00
|
|
|
pub fn from_key (bytes: &[u8]) -> Self {
|
|
|
|
Self (blake3::hash (bytes))
|
|
|
|
}
|
|
|
|
|
2021-03-21 03:34:47 +00:00
|
|
|
#[must_use]
|
2020-12-12 17:11:22 +00:00
|
|
|
pub fn encode_base64 (&self) -> String {
|
|
|
|
base64::encode (self.as_bytes ())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Deref for BlakeHashWrapper {
|
|
|
|
type Target = blake3::Hash;
|
|
|
|
|
|
|
|
fn deref (&self) -> &<Self as Deref>::Target {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct BlakeHashVisitor;
|
|
|
|
|
|
|
|
impl <'de> Visitor <'de> for BlakeHashVisitor {
|
|
|
|
type Value = blake3::Hash;
|
|
|
|
|
|
|
|
fn expecting (&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
formatter.write_str ("a 32-byte blake3 hash, encoded as base64")
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_str <E: de::Error> (self, value: &str)
|
|
|
|
-> Result <Self::Value, E>
|
|
|
|
{
|
|
|
|
let bytes: Vec <u8> = base64::decode (value).map_err (|_| E::custom (format! ("str is not base64: {}", value)))?;
|
|
|
|
let bytes: [u8; 32] = (&bytes [..]).try_into ().map_err (|_| E::custom (format! ("decode base64 is not 32 bytes long: {}", value)))?;
|
|
|
|
|
|
|
|
let tripcode = blake3::Hash::from (bytes);
|
|
|
|
|
|
|
|
Ok (tripcode)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl <'de> Deserialize <'de> for BlakeHashWrapper {
|
|
|
|
fn deserialize <D: Deserializer <'de>> (deserializer: D) -> Result <Self, D::Error> {
|
|
|
|
Ok (BlakeHashWrapper (deserializer.deserialize_str (BlakeHashVisitor)?))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-27 19:31:58 +00:00
|
|
|
impl Serialize for BlakeHashWrapper {
|
|
|
|
fn serialize <S: serde::Serializer> (&self, serializer: S) -> Result <S::Ok, S::Error>
|
|
|
|
{
|
|
|
|
serializer.serialize_str (&self.encode_base64 ())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-12 17:11:22 +00:00
|
|
|
pub trait MaxValidDuration {
|
|
|
|
fn dur () -> Duration;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive (Deserialize)]
|
2021-08-27 23:34:38 +00:00
|
|
|
pub struct ScraperKey {
|
2021-08-28 01:28:34 +00:00
|
|
|
pub name: String,
|
2020-12-16 14:46:03 +00:00
|
|
|
|
2020-12-13 01:54:54 +00:00
|
|
|
not_before: DateTime <Utc>,
|
|
|
|
not_after: DateTime <Utc>,
|
2020-12-16 14:46:03 +00:00
|
|
|
pub hash: BlakeHashWrapper,
|
2020-12-12 17:11:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive (Copy, Clone, Debug, PartialEq)]
|
|
|
|
pub enum KeyValidity {
|
|
|
|
Valid,
|
|
|
|
|
2020-12-12 17:50:40 +00:00
|
|
|
WrongKey (BlakeHashWrapper),
|
2020-12-12 17:11:22 +00:00
|
|
|
ClockIsBehind,
|
|
|
|
Expired,
|
|
|
|
DurationTooLong (Duration),
|
|
|
|
DurationNegative,
|
|
|
|
}
|
|
|
|
|
2021-08-27 23:34:38 +00:00
|
|
|
impl ScraperKey {
|
2020-12-16 14:46:03 +00:00
|
|
|
pub fn new_30_day <S: Into <String>> (name: S, input: &[u8]) -> Self {
|
2020-12-13 01:54:54 +00:00
|
|
|
let now = Utc::now ();
|
|
|
|
|
|
|
|
Self {
|
2020-12-16 14:46:03 +00:00
|
|
|
name: name.into (),
|
2020-12-13 01:54:54 +00:00
|
|
|
not_before: now,
|
2021-08-27 23:34:38 +00:00
|
|
|
not_after: now + Duration::days (30),
|
2020-12-13 01:54:54 +00:00
|
|
|
hash: BlakeHashWrapper::from_key (input),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-27 23:34:38 +00:00
|
|
|
impl ScraperKey {
|
2021-03-21 03:34:47 +00:00
|
|
|
#[must_use]
|
2020-12-12 17:11:22 +00:00
|
|
|
pub fn is_valid (&self, now: DateTime <Utc>, input: &[u8]) -> KeyValidity {
|
|
|
|
use KeyValidity::*;
|
|
|
|
|
|
|
|
// I put this first because I think the constant-time check should run
|
|
|
|
// before anything else. But I'm not a crypto expert, so it's just
|
|
|
|
// guesswork.
|
2020-12-12 17:50:40 +00:00
|
|
|
let input_hash = BlakeHashWrapper::from_key (input);
|
|
|
|
if input_hash != self.hash {
|
|
|
|
return WrongKey (input_hash);
|
2020-12-12 17:11:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if self.not_after < self.not_before {
|
|
|
|
return DurationNegative;
|
|
|
|
}
|
|
|
|
|
|
|
|
if now >= self.not_after {
|
|
|
|
return Expired;
|
|
|
|
}
|
|
|
|
|
|
|
|
if now < self.not_before {
|
|
|
|
return ClockIsBehind;
|
|
|
|
}
|
|
|
|
|
2021-03-21 03:34:47 +00:00
|
|
|
Valid
|
2020-12-12 17:11:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg (test)]
|
|
|
|
mod tests {
|
|
|
|
use chrono::{Utc};
|
2021-04-27 19:31:58 +00:00
|
|
|
use serde_json::json;
|
2020-12-12 17:11:22 +00:00
|
|
|
use super::*;
|
|
|
|
use KeyValidity::*;
|
|
|
|
|
2021-04-27 19:31:58 +00:00
|
|
|
#[test]
|
|
|
|
fn roundtrip_tripcode () {
|
|
|
|
let tripcode = "m8vG/sQnsn/87CQ5Ob6wMJeAnMKtXYfCBLNZ1SrSkvI=";
|
|
|
|
|
|
|
|
let j = json! ({
|
|
|
|
"tripcode": tripcode,
|
|
|
|
});
|
2021-04-27 19:36:05 +00:00
|
|
|
|
|
|
|
let s = serde_json::to_string (&j).unwrap ();
|
|
|
|
|
|
|
|
let j = serde_json::from_str::<serde_json::Value> (&s).unwrap ();
|
|
|
|
assert_eq! (j, json! ({"tripcode": tripcode}));
|
2021-04-27 19:31:58 +00:00
|
|
|
}
|
|
|
|
|
2020-12-12 17:11:22 +00:00
|
|
|
#[test]
|
|
|
|
fn duration_negative () {
|
|
|
|
let zero_time = Utc::now ();
|
|
|
|
|
2021-08-27 23:34:38 +00:00
|
|
|
let key = ScraperKey {
|
2020-12-16 14:46:03 +00:00
|
|
|
name: "automated testing".to_string (),
|
2020-12-12 17:11:22 +00:00
|
|
|
not_before: zero_time + Duration::days (1 + 2),
|
|
|
|
not_after: zero_time + Duration::days (1),
|
|
|
|
hash: BlakeHashWrapper::from_key ("bad_password".as_bytes ()),
|
|
|
|
};
|
|
|
|
|
|
|
|
let err = DurationNegative;
|
|
|
|
|
|
|
|
for (input, expected) in &[
|
|
|
|
(zero_time + Duration::days (0), err),
|
|
|
|
(zero_time + Duration::days (2), err),
|
|
|
|
(zero_time + Duration::days (100), err),
|
|
|
|
] {
|
|
|
|
assert_eq! (key.is_valid (*input, "bad_password".as_bytes ()), *expected);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn normal_key () {
|
|
|
|
let zero_time = Utc::now ();
|
|
|
|
|
2021-08-27 23:34:38 +00:00
|
|
|
let key = ScraperKey {
|
2020-12-16 14:46:03 +00:00
|
|
|
name: "automated testing".to_string (),
|
2020-12-12 17:11:22 +00:00
|
|
|
not_before: zero_time + Duration::days (1),
|
2021-08-27 23:34:38 +00:00
|
|
|
not_after: zero_time + Duration::days (1 + 60),
|
2020-12-12 17:11:22 +00:00
|
|
|
hash: BlakeHashWrapper::from_key ("bad_password".as_bytes ()),
|
|
|
|
};
|
|
|
|
|
|
|
|
for (input, expected) in &[
|
|
|
|
(zero_time + Duration::days (0), ClockIsBehind),
|
|
|
|
(zero_time + Duration::days (2), Valid),
|
2021-08-27 23:34:38 +00:00
|
|
|
(zero_time + Duration::days (60 - 1), Valid),
|
|
|
|
(zero_time + Duration::days (60 + 1), Expired),
|
2020-12-12 17:11:22 +00:00
|
|
|
(zero_time + Duration::days (100), Expired),
|
|
|
|
] {
|
|
|
|
assert_eq! (key.is_valid (*input, "bad_password".as_bytes ()), *expected);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn wrong_key () {
|
|
|
|
let zero_time = Utc::now ();
|
|
|
|
|
2021-08-27 23:34:38 +00:00
|
|
|
let key = ScraperKey {
|
2020-12-16 14:46:03 +00:00
|
|
|
name: "automated testing".to_string (),
|
2020-12-12 17:11:22 +00:00
|
|
|
not_before: zero_time + Duration::days (1),
|
2020-12-16 14:46:03 +00:00
|
|
|
not_after: zero_time + Duration::days (1 + 30),
|
2020-12-12 17:11:22 +00:00
|
|
|
hash: BlakeHashWrapper::from_key ("bad_password".as_bytes ()),
|
|
|
|
};
|
|
|
|
|
2020-12-12 17:50:40 +00:00
|
|
|
for input in &[
|
|
|
|
zero_time + Duration::days (0),
|
|
|
|
zero_time + Duration::days (2),
|
2020-12-16 14:46:03 +00:00
|
|
|
zero_time + Duration::days (1 + 30),
|
2020-12-12 17:50:40 +00:00
|
|
|
zero_time + Duration::days (100),
|
2020-12-12 17:11:22 +00:00
|
|
|
] {
|
2020-12-12 17:50:40 +00:00
|
|
|
let validity = key.is_valid (*input, "badder_password".as_bytes ());
|
|
|
|
|
|
|
|
match validity {
|
|
|
|
WrongKey (_) => (),
|
|
|
|
_ => panic! ("Expected WrongKey here"),
|
|
|
|
}
|
2020-12-12 17:11:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|