Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ Changelog

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

2.1.2
-----

**Unreleased**

- Avoid ``verify`` to raise an exception when the ``code`` argument is non-ascii.

2.1.1
-----

Expand Down
5 changes: 4 additions & 1 deletion src/otpauth/_rfc4226.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ def verify(self, code: int, counter: int) -> bool:
"""
if len(str(code)) > self.digit:
return False
return hmac.compare_digest(self.string_code(self.generate(counter)), self.string_code(code))
try:
return hmac.compare_digest(self.string_code(self.generate(counter)), self.string_code(code))
except (TypeError, ValueError):
return False

def to_uri(self, label: str, issuer: str, counter: int) -> str:
"""Generate the otpauth protocal string for HOTP.
Expand Down
6 changes: 5 additions & 1 deletion src/otpauth/_rfc6238.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,11 @@ def verify(self, code: int, timestamp: t.Optional[int] = None) -> bool:
"""
if len(str(code)) > self.digit:
return False
return hmac.compare_digest(self.string_code(self.generate(timestamp)), self.string_code(code))

try:
return hmac.compare_digest(self.string_code(self.generate(timestamp)), self.string_code(code))
except (TypeError, ValueError):
return False

def to_uri(self, label: str, issuer: str) -> str:
"""Generate the otpauth protocal string for TOTP.
Expand Down
3 changes: 3 additions & 0 deletions tests/test_hotp.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@ def test_verify(self):
# due to not match
self.assertFalse(self.hotp.verify(12345, 0))

self.assertFalse(self.hotp.verify("●●●●●●", 0))

self.assertTrue(self.hotp.verify(170566, 0))


def test_to_uri(self):
uri = self.hotp.to_uri("Typlog:lepture.com", "Authlib", 0)
expected = (
Expand Down
1 change: 1 addition & 0 deletions tests/test_totp.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ def test_verify(self):

# due to not match
self.assertFalse(self.totp.verify(12345, FIXED_TIME))
self.assertFalse(self.totp.verify("●●●●●●", FIXED_TIME))

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

Expand Down
Loading