Skip to content

Commit

Permalink
Adds more type checking for latest mypy
Browse files Browse the repository at this point in the history
We need to assert the type of the variable for any Optional
type.
  • Loading branch information
kushaldas committed Aug 17, 2021
1 parent 96c16d4 commit a8f57b5
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions securedrop/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@

from db import db

from typing import Callable, Optional, Union, Dict, List, Any
from typing import Callable, Optional, Union, Dict, List, Any, AnyStr
from logging import Logger
from pyotp import OTP
from pyotp import TOTP, HOTP


LOGIN_HARDENING = True
Expand Down Expand Up @@ -531,6 +531,9 @@ def valid_password(self, passphrase: 'Optional[str]') -> bool:
if not passphrase:
return False

# For type checking
assert isinstance(self.pw_hash, bytes)

# Avoid hashing passwords that are over the maximum length
if len(passphrase) > self.MAX_PASSWORD_LEN:
raise InvalidPasswordLength(passphrase)
Expand Down Expand Up @@ -576,14 +579,14 @@ def set_hotp_secret(self, otp_secret: str) -> None:
self.hotp_counter = 0

@property
def totp(self) -> 'OTP':
def totp(self) -> 'TOTP':
if self.is_totp:
return pyotp.TOTP(self.otp_secret)
else:
raise ValueError('{} is not using TOTP'.format(self))

@property
def hotp(self) -> 'OTP':
def hotp(self) -> 'HOTP':
if not self.is_totp:
return pyotp.HOTP(self.otp_secret)
else:
Expand Down Expand Up @@ -690,6 +693,8 @@ def login(cls,

# Prevent TOTP token reuse
if user.last_token is not None:
# For type checking
assert isinstance(token, str)
if pyotp.utils.compare_digest(token, user.last_token):
raise BadTokenException("previously used two-factor code "
"{}".format(token))
Expand Down

0 comments on commit a8f57b5

Please sign in to comment.