Skip to content

Commit 7713958

Browse files
authored
Merge pull request #13 from azmeuk/non-ascii
avoid verify to raise an exception when the code argument is non-ascii
2 parents 9a29d6d + 09fce7c commit 7713958

File tree

5 files changed

+20
-2
lines changed

5 files changed

+20
-2
lines changed

docs/changelog.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@ Changelog
99

1010
For **v1**, please head over to https://pythonhosted.org/otpauth/
1111

12+
2.1.2
13+
-----
14+
15+
**Unreleased**
16+
17+
- Avoid ``verify`` to raise an exception when the ``code`` argument is non-ascii.
18+
1219
2.1.1
1320
-----
1421

src/otpauth/_rfc4226.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,10 @@ def verify(self, code: int, counter: int) -> bool:
3737
"""
3838
if len(str(code)) > self.digit:
3939
return False
40-
return hmac.compare_digest(self.string_code(self.generate(counter)), self.string_code(code))
40+
try:
41+
return hmac.compare_digest(self.string_code(self.generate(counter)), self.string_code(code))
42+
except (TypeError, ValueError):
43+
return False
4144

4245
def to_uri(self, label: str, issuer: str, counter: int) -> str:
4346
"""Generate the otpauth protocal string for HOTP.

src/otpauth/_rfc6238.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,11 @@ def verify(self, code: int, timestamp: t.Optional[int] = None) -> bool:
4242
"""
4343
if len(str(code)) > self.digit:
4444
return False
45-
return hmac.compare_digest(self.string_code(self.generate(timestamp)), self.string_code(code))
45+
46+
try:
47+
return hmac.compare_digest(self.string_code(self.generate(timestamp)), self.string_code(code))
48+
except (TypeError, ValueError):
49+
return False
4650

4751
def to_uri(self, label: str, issuer: str) -> str:
4852
"""Generate the otpauth protocal string for TOTP.

tests/test_hotp.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,11 @@ def test_verify(self):
1818
# due to not match
1919
self.assertFalse(self.hotp.verify(12345, 0))
2020

21+
self.assertFalse(self.hotp.verify("●●●●●●", 0))
22+
2123
self.assertTrue(self.hotp.verify(170566, 0))
2224

25+
2326
def test_to_uri(self):
2427
uri = self.hotp.to_uri("Typlog:lepture.com", "Authlib", 0)
2528
expected = (

tests/test_totp.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ def test_verify(self):
1919

2020
# due to not match
2121
self.assertFalse(self.totp.verify(12345, FIXED_TIME))
22+
self.assertFalse(self.totp.verify("●●●●●●", FIXED_TIME))
2223

2324
self.assertTrue(self.totp.verify(129815, FIXED_TIME))
2425

0 commit comments

Comments
 (0)