@@ -249,11 +249,11 @@ impl OTPElementBuilder {
249249 match self . type_ . unwrap_or_default ( ) {
250250 OTPType :: Motp => hex:: decode ( & self . secret . as_ref ( ) . unwrap ( ) )
251251 . map ( |_| { } )
252- . map_err ( ErrReport :: from ) ,
252+ . map_err ( |e| eyre ! ( "Invalid hex secret: {e}" ) ) ,
253253 _ => BASE32_NOPAD
254254 . decode ( self . secret . as_ref ( ) . unwrap ( ) . as_bytes ( ) )
255255 . map ( |_| { } )
256- . map_err ( ErrReport :: from ) ,
256+ . map_err ( |e| eyre ! ( "Invalid BASE32 secret: {e}" ) ) ,
257257 }
258258 }
259259}
@@ -272,6 +272,7 @@ mod test {
272272
273273 use crate :: otp:: from_otp_uri:: FromOtpUri ;
274274 use crate :: otp:: otp_error:: OtpError ;
275+ use crate :: otp:: otp_type:: OTPType ;
275276
276277 #[ test]
277278 fn test_serialization_otp_uri_full_element ( ) {
@@ -365,4 +366,45 @@ mod test {
365366 // Assert
366367 assert_eq ! ( "AA" , result. unwrap( ) . secret) ;
367368 }
369+
370+ #[ test]
371+ fn test_invalid_secret_base32 ( ) {
372+ let result = OTPElementBuilder :: default ( )
373+ . secret ( "aaa" )
374+ . label ( "label" )
375+ . issuer ( "" )
376+ . build ( ) ;
377+
378+ assert_eq ! (
379+ "Invalid BASE32 secret: invalid length at 2" ,
380+ result. unwrap_err( ) . to_string( )
381+ )
382+ }
383+
384+ #[ test]
385+ fn valid_hex_secret ( ) {
386+ let result = OTPElementBuilder :: default ( )
387+ . secret ( "aaaf" )
388+ . label ( "label" )
389+ . issuer ( "" )
390+ . type_ ( OTPType :: Motp )
391+ . build ( ) ;
392+
393+ assert_eq ! ( "aaaf" , result. unwrap( ) . secret)
394+ }
395+
396+ #[ test]
397+ fn invalid_secret_hex ( ) {
398+ let result = OTPElementBuilder :: default ( )
399+ . secret ( "aaa" )
400+ . label ( "label" )
401+ . issuer ( "" )
402+ . type_ ( OTPType :: Motp )
403+ . build ( ) ;
404+
405+ assert_eq ! (
406+ "Invalid hex secret: Odd number of digits" ,
407+ result. unwrap_err( ) . to_string( )
408+ )
409+ }
368410}
0 commit comments