1
+ use std:: fmt;
2
+
1
3
use serde:: { Deserialize , Serialize } ;
2
4
3
5
/// Authentication options.
@@ -10,7 +12,7 @@ pub struct Auth {
10
12
/// The maximum password length.
11
13
pub max_password_length : usize ,
12
14
/// The secret key used to sign JWT tokens.
13
- pub secret_key : String ,
15
+ pub secret_key : SecretKey ,
14
16
}
15
17
16
18
impl Default for Auth {
@@ -19,14 +21,14 @@ impl Default for Auth {
19
21
email_on_signup : EmailOnSignup :: default ( ) ,
20
22
min_password_length : 6 ,
21
23
max_password_length : 64 ,
22
- secret_key : "MaxVerstappenWC2021" . to_string ( ) ,
24
+ secret_key : SecretKey :: new ( "MaxVerstappenWC2021" ) ,
23
25
}
24
26
}
25
27
}
26
28
27
29
impl Auth {
28
30
pub fn override_secret_key ( & mut self , secret_key : & str ) {
29
- self . secret_key = secret_key . to_string ( ) ;
31
+ self . secret_key = SecretKey :: new ( secret_key ) ;
30
32
}
31
33
}
32
34
@@ -46,3 +48,40 @@ impl Default for EmailOnSignup {
46
48
Self :: Optional
47
49
}
48
50
}
51
+
52
+ #[ derive( Debug , Clone , Serialize , Deserialize , PartialEq ) ]
53
+ pub struct SecretKey ( String ) ;
54
+
55
+ impl SecretKey {
56
+ /// # Panics
57
+ ///
58
+ /// Will panic if the key if empty.
59
+ #[ must_use]
60
+ pub fn new ( key : & str ) -> Self {
61
+ assert ! ( !key. is_empty( ) , "secret key cannot be empty" ) ;
62
+
63
+ Self ( key. to_owned ( ) )
64
+ }
65
+
66
+ #[ must_use]
67
+ pub fn as_bytes ( & self ) -> & [ u8 ] {
68
+ self . 0 . as_bytes ( )
69
+ }
70
+ }
71
+
72
+ impl fmt:: Display for SecretKey {
73
+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
74
+ write ! ( f, "{}" , self . 0 )
75
+ }
76
+ }
77
+
78
+ #[ cfg( test) ]
79
+ mod tests {
80
+ use super :: SecretKey ;
81
+
82
+ #[ test]
83
+ #[ should_panic( expected = "secret key cannot be empty" ) ]
84
+ fn secret_key_can_not_be_empty ( ) {
85
+ drop ( SecretKey :: new ( "" ) ) ;
86
+ }
87
+ }
0 commit comments