1+ use std:: fmt;
2+
13use serde:: { Deserialize , Serialize } ;
24
35/// Authentication options.
@@ -10,7 +12,7 @@ pub struct Auth {
1012 /// The maximum password length.
1113 pub max_password_length : usize ,
1214 /// The secret key used to sign JWT tokens.
13- pub secret_key : String ,
15+ pub secret_key : SecretKey ,
1416}
1517
1618impl Default for Auth {
@@ -19,14 +21,14 @@ impl Default for Auth {
1921 email_on_signup : EmailOnSignup :: default ( ) ,
2022 min_password_length : 6 ,
2123 max_password_length : 64 ,
22- secret_key : "MaxVerstappenWC2021" . to_string ( ) ,
24+ secret_key : SecretKey :: new ( "MaxVerstappenWC2021" ) ,
2325 }
2426 }
2527}
2628
2729impl Auth {
2830 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 ) ;
3032 }
3133}
3234
@@ -46,3 +48,40 @@ impl Default for EmailOnSignup {
4648 Self :: Optional
4749 }
4850}
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