@@ -154,6 +154,8 @@ pub struct OTPElement {
154154 pub pin : Option < String > ,
155155}
156156
157+ static ALLOWED_DIGITS_RANGE : std:: ops:: RangeInclusive < u64 > = 1 ..=10 ;
158+
157159impl OTPElement {
158160 pub fn get_otpauth_uri ( & self ) -> String {
159161 let otp_type = self . type_ . to_string ( ) . to_lowercase ( ) ;
@@ -183,6 +185,10 @@ impl OTPElement {
183185 }
184186
185187 pub fn get_otp_code ( & self ) -> Result < String , OtpError > {
188+ if !ALLOWED_DIGITS_RANGE . contains ( & self . digits ) {
189+ return Err ( OtpError :: InvalidDigits ) ;
190+ }
191+
186192 match self . type_ {
187193 OTPType :: Totp => {
188194 let code = totp ( & self . secret , self . algorithm ) ?;
@@ -361,7 +367,7 @@ mod test {
361367 #[ test]
362368 fn test_invalid_digits_should_not_overflow ( ) {
363369 // Arrange
364- let invalid_digits_value = 10 ;
370+ let invalid_digits_value = 11 ;
365371
366372 let element = OTPElement {
367373 secret : "xr5gh44x7bprcqgrdtulafeevt5rxqlbh5wvked22re43dh2d4mapv5g" . to_uppercase ( ) ,
@@ -382,6 +388,30 @@ mod test {
382388 assert_eq ! ( Err ( OtpError :: InvalidDigits ) , result) ;
383389 }
384390
391+ #[ test]
392+ fn test_10_digits_should_be_allowed ( ) {
393+ // Arrange
394+ let invalid_digits_value = 10 ;
395+
396+ let element = OTPElement {
397+ secret : "xr5gh44x7bprcqgrdtulafeevt5rxqlbh5wvked22re43dh2d4mapv5g" . to_uppercase ( ) ,
398+ issuer : String :: from ( "IssuerText" ) ,
399+ label : String :: from ( "LabelText" ) ,
400+ digits : invalid_digits_value,
401+ type_ : Totp ,
402+ algorithm : Sha1 ,
403+ period : 30 ,
404+ counter : None ,
405+ pin : None ,
406+ } ;
407+
408+ // Act
409+ let result = element. get_otp_code ( ) ;
410+
411+ // Assert
412+ assert ! ( result. is_ok( ) ) ;
413+ }
414+
385415 #[ test]
386416 fn test_lowercase_secret ( ) {
387417 // Arrange / Act
0 commit comments