@@ -6,6 +6,29 @@ use serde::Serialize;
6
6
use log:: debug;
7
7
use derive_more:: { Display , Error } ;
8
8
9
+ pub fn generate_auth_key ( seconds_valid : u64 ) -> AuthKey {
10
+ let key: String = thread_rng ( )
11
+ . sample_iter ( & Alphanumeric )
12
+ . take ( AUTH_KEY_LENGTH )
13
+ . map ( char:: from)
14
+ . collect ( ) ;
15
+
16
+ debug ! ( "Generated key: {}, valid for: {} seconds" , key, seconds_valid) ;
17
+
18
+ AuthKey {
19
+ key,
20
+ valid_until : Some ( current_time ( ) + seconds_valid) ,
21
+ }
22
+ }
23
+
24
+ pub fn verify_auth_key ( auth_key : & AuthKey ) -> Result < ( ) , Error > {
25
+ let current_time = current_time ( ) ;
26
+ if auth_key. valid_until . is_none ( ) { return Err ( Error :: KeyInvalid ) }
27
+ if auth_key. valid_until . unwrap ( ) < current_time { return Err ( Error :: KeyExpired ) }
28
+
29
+ Ok ( ( ) )
30
+ }
31
+
9
32
#[ derive( Serialize , Debug , Eq , PartialEq , Clone ) ]
10
33
pub struct AuthKey {
11
34
pub key : String ,
@@ -14,27 +37,35 @@ pub struct AuthKey {
14
37
15
38
impl AuthKey {
16
39
pub fn from_buffer ( key_buffer : [ u8 ; AUTH_KEY_LENGTH ] ) -> Option < AuthKey > {
17
- Some ( AuthKey {
18
- key : String :: from_utf8 ( Vec :: from ( key_buffer) ) . unwrap ( ) ,
19
- valid_until : None ,
20
- } )
40
+ if let Ok ( key) = String :: from_utf8 ( Vec :: from ( key_buffer) ) {
41
+ Some ( AuthKey {
42
+ key,
43
+ valid_until : None ,
44
+ } )
45
+ } else {
46
+ None
47
+ }
21
48
}
22
49
23
50
pub fn from_string ( key : & str ) -> Option < AuthKey > {
24
- if key. len ( ) != AUTH_KEY_LENGTH { return None }
25
-
26
- Some ( AuthKey {
27
- key : key. to_string ( ) ,
28
- valid_until : None ,
29
- } )
51
+ if key. len ( ) != AUTH_KEY_LENGTH {
52
+ None
53
+ } else {
54
+ Some ( AuthKey {
55
+ key : key. to_string ( ) ,
56
+ valid_until : None ,
57
+ } )
58
+ }
30
59
}
31
60
}
32
61
33
62
#[ derive( Debug , Display , PartialEq , Error ) ]
34
63
#[ allow( dead_code) ]
35
64
pub enum Error {
36
- #[ display( fmt = "Key is invalid ." ) ]
65
+ #[ display( fmt = "Key could not be verified ." ) ]
37
66
KeyVerificationError ,
67
+ #[ display( fmt = "Key is invalid." ) ]
68
+ KeyInvalid ,
38
69
#[ display( fmt = "Key has expired." ) ]
39
70
KeyExpired
40
71
}
@@ -46,29 +77,49 @@ impl From<r2d2_sqlite::rusqlite::Error> for Error {
46
77
}
47
78
}
48
79
49
- pub struct KeyManager ;
80
+ #[ cfg( test) ]
81
+ mod tests {
82
+ use crate :: key_manager;
50
83
51
- impl KeyManager {
52
- pub fn generate_auth_key ( & self , seconds_valid : u64 ) -> AuthKey {
53
- let key: String = thread_rng ( )
54
- . sample_iter ( & Alphanumeric )
55
- . take ( AUTH_KEY_LENGTH )
56
- . map ( char:: from)
57
- . collect ( ) ;
84
+ #[ test]
85
+ fn auth_key_from_buffer ( ) {
86
+ let auth_key = key_manager:: AuthKey :: from_buffer (
87
+ [
88
+ 89 , 90 , 83 , 108 ,
89
+ 52 , 108 , 77 , 90 ,
90
+ 117 , 112 , 82 , 117 ,
91
+ 79 , 112 , 83 , 82 ,
92
+ 67 , 51 , 107 , 114 ,
93
+ 73 , 75 , 82 , 53 ,
94
+ 66 , 80 , 66 , 49 ,
95
+ 52 , 110 , 114 , 74 ]
96
+ ) ;
58
97
59
- debug ! ( "Generated key: {}, valid for: {} seconds" , key, seconds_valid) ;
98
+ assert ! ( auth_key. is_some( ) ) ;
99
+ assert_eq ! ( auth_key. unwrap( ) . key, "YZSl4lMZupRuOpSRC3krIKR5BPB14nrJ" ) ;
100
+ }
60
101
61
- AuthKey {
62
- key,
63
- valid_until : Some ( current_time ( ) + seconds_valid) ,
64
- }
102
+ #[ test]
103
+ fn auth_key_from_string ( ) {
104
+ let key_string = "YZSl4lMZupRuOpSRC3krIKR5BPB14nrJ" ;
105
+ let auth_key = key_manager:: AuthKey :: from_string ( key_string) ;
106
+
107
+ assert ! ( auth_key. is_some( ) ) ;
108
+ assert_eq ! ( auth_key. unwrap( ) . key, key_string) ;
109
+ }
110
+
111
+ #[ test]
112
+ fn generate_valid_auth_key ( ) {
113
+ let auth_key = key_manager:: generate_auth_key ( 9999 ) ;
114
+
115
+ assert ! ( key_manager:: verify_auth_key( & auth_key) . is_ok( ) ) ;
65
116
}
66
117
67
- pub async fn verify_auth_key ( & self , auth_key : & AuthKey ) -> Result < ( ) , Error > {
68
- let current_time = current_time ( ) ;
69
- if auth_key. valid_until . is_none ( ) { return Err ( Error :: KeyVerificationError ) }
70
- if & auth_key. valid_until . unwrap ( ) < & current_time { return Err ( Error :: KeyExpired ) }
118
+ # [ test ]
119
+ fn generate_expired_auth_key ( ) {
120
+ let mut auth_key = key_manager :: generate_auth_key ( 0 ) ;
121
+ auth_key. valid_until = Some ( 0 ) ;
71
122
72
- Ok ( ( ) )
123
+ assert ! ( key_manager :: verify_auth_key ( & auth_key ) . is_err ( ) ) ;
73
124
}
74
125
}
0 commit comments