@@ -154,6 +154,8 @@ pub struct OTPElement {
154
154
pub pin : Option < String > ,
155
155
}
156
156
157
+ static ALLOWED_DIGITS_RANGE : std:: ops:: RangeInclusive < u64 > = 1 ..=10 ;
158
+
157
159
impl OTPElement {
158
160
pub fn get_otpauth_uri ( & self ) -> String {
159
161
let otp_type = self . type_ . to_string ( ) . to_lowercase ( ) ;
@@ -183,6 +185,10 @@ impl OTPElement {
183
185
}
184
186
185
187
pub fn get_otp_code ( & self ) -> Result < String , OtpError > {
188
+ if !ALLOWED_DIGITS_RANGE . contains ( & self . digits ) {
189
+ return Err ( OtpError :: InvalidDigits ) ;
190
+ }
191
+
186
192
match self . type_ {
187
193
OTPType :: Totp => {
188
194
let code = totp ( & self . secret , self . algorithm ) ?;
@@ -361,7 +367,7 @@ mod test {
361
367
#[ test]
362
368
fn test_invalid_digits_should_not_overflow ( ) {
363
369
// Arrange
364
- let invalid_digits_value = 10 ;
370
+ let invalid_digits_value = 11 ;
365
371
366
372
let element = OTPElement {
367
373
secret : "xr5gh44x7bprcqgrdtulafeevt5rxqlbh5wvked22re43dh2d4mapv5g" . to_uppercase ( ) ,
@@ -382,6 +388,30 @@ mod test {
382
388
assert_eq ! ( Err ( OtpError :: InvalidDigits ) , result) ;
383
389
}
384
390
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
+
385
415
#[ test]
386
416
fn test_lowercase_secret ( ) {
387
417
// Arrange / Act
0 commit comments