self.totp_key = get_env("FLATNOTES_TOTP_KEY", mandatory=True)\r
self.totp_key = b32encode(self.totp_key.encode("utf-8"))\r
self.totp = TOTP(self.totp_key)\r
- self.last_used_totp = None\r
self._display_totp_enrolment()\r
\r
def login(self, data: Login) -> Token:\r
# Check Password & TOTP\r
expected_password = self.password\r
if self.is_totp_enabled:\r
- current_totp = self.totp.now()\r
- expected_password += current_totp\r
+ expected_password += self.totp.now()\r
password_correct = secrets.compare_digest(\r
expected_password, data.password\r
)\r
if not (\r
username_correct\r
and password_correct\r
- # Prevent TOTP from being reused\r
- and (\r
- self.is_totp_enabled is False\r
- or current_totp != self.last_used_totp\r
- )\r
):\r
raise ValueError("Incorrect login credentials.")\r
- if self.is_totp_enabled:\r
- self.last_used_totp = current_totp\r
\r
# Create Token\r
access_token = self._create_access_token(data={"sub": self.username})\r