1
1
use std:: str:: FromStr ;
2
+ use std:: time:: Duration ;
2
3
3
4
use derive_more:: Display ;
4
5
use rand:: distr:: Alphanumeric ;
@@ -12,7 +13,7 @@ use super::AUTH_KEY_LENGTH;
12
13
13
14
/// An authentication key which can potentially have an expiration time.
14
15
/// After that time is will automatically become invalid.
15
- #[ derive( Serialize , Deserialize , Debug , Eq , PartialEq , Clone ) ]
16
+ #[ derive( Serialize , Deserialize , Debug , Clone ) ]
16
17
pub struct PeerKey {
17
18
/// Random 32-char string. For example: `YZSl4lMZupRuOpSRC3krIKR5BPB14nrJ`
18
19
pub key : Key ,
@@ -22,6 +23,21 @@ pub struct PeerKey {
22
23
pub valid_until : Option < DurationSinceUnixEpoch > ,
23
24
}
24
25
26
+ impl PartialEq for PeerKey {
27
+ fn eq ( & self , other : & Self ) -> bool {
28
+ // We ignore the fractions of seconds when comparing the timestamps
29
+ // because we only store the seconds in the database.
30
+ self . key == other. key
31
+ && match ( & self . valid_until , & other. valid_until ) {
32
+ ( Some ( a) , Some ( b) ) => a. as_secs ( ) == b. as_secs ( ) ,
33
+ ( None , None ) => true ,
34
+ _ => false ,
35
+ }
36
+ }
37
+ }
38
+
39
+ impl Eq for PeerKey { }
40
+
25
41
impl std:: fmt:: Display for PeerKey {
26
42
fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
27
43
match self . expiry_time ( ) {
@@ -47,7 +63,10 @@ impl PeerKey {
47
63
/// (this will naturally happen in 292.5 billion years)
48
64
#[ must_use]
49
65
pub fn expiry_time ( & self ) -> Option < chrono:: DateTime < chrono:: Utc > > {
50
- self . valid_until . map ( convert_from_timestamp_to_datetime_utc)
66
+ // We remove the fractions of seconds because we only store the seconds
67
+ // in the database.
68
+ self . valid_until
69
+ . map ( |valid_until| convert_from_timestamp_to_datetime_utc ( Duration :: from_secs ( valid_until. as_secs ( ) ) ) )
51
70
}
52
71
}
53
72
0 commit comments