Skip to content

Commit 8b3203f

Browse files
committed
Handle secret length in two cases.
1 parent 3f03090 commit 8b3203f

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

authenticatorpy/authenticator.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,12 @@ def __check_secret(self, secret):
3636
raise TypeError('You must set an ascii str variable as secret!')
3737
secret_without_spaces = self.remove_spaces(secret)
3838
self._secret = self.to_upper_case(secret_without_spaces)
39-
if len(self._secret) % 8 != 0:
40-
raise ValueError('You must set a string multiple of 8!')
39+
secret_length = len(self._secret)
40+
if secret_length < 8:
41+
raise ValueError('You must set a secret of minimum 8 characters!')
42+
if secret_length > 8:
43+
index = secret_length % 8
44+
self._secret = self._secret[:-index]
4145
if self.__is_alpha(self._secret) == False:
4246
raise TypeError('All characters in the secret must be alphabetic!')
4347

tests/test_authenticator.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def test_wrong_initiation(self):
2121
with self.assertRaises(Exception) as context:
2222
Authenticator('abcd')
2323

24-
self.assertTrue('You must set a string multiple of 8!' in str(context.exception))
24+
self.assertTrue('You must set a secret of minimum 8 characters!' in str(context.exception))
2525

2626
with self.assertRaises(Exception) as context:
2727
Authenticator(lambda: None)
@@ -81,9 +81,12 @@ def test_create_hmac(self):
8181
def test_one_time_password(self):
8282
password = self._authenticator.one_time_password()
8383
self.assertIsNotNone(password)
84+
self.assertIsNotNone(Authenticator('abcd xyzw a').one_time_password())
85+
self.assertIsNotNone(Authenticator('abcd xyzw ab').one_time_password())
86+
self.assertIsNotNone(Authenticator('abcd xyzw abcd').one_time_password())
8487

8588
def test_one_time_password_with_empty_spaces(self):
86-
password = Authenticator('\t\t\t\t \t\t\t\t').one_time_password()
89+
password = Authenticator('\ta\bt\tc\td \te\tf\tg\th').one_time_password()
8790
self.assertIsNotNone(password)
88-
password = Authenticator('\r\r\r\r \r\r\r\r').one_time_password()
91+
password = Authenticator('\ra\rb\rc\rd \re\rf\rg\rh').one_time_password()
8992
self.assertIsNotNone(password)

0 commit comments

Comments
 (0)