fixes suggested by tox

This commit is contained in:
Peter Bieringer 2024-12-31 18:26:43 +01:00
parent c00ab76c83
commit c1be04abd1
2 changed files with 11 additions and 10 deletions

View file

@ -30,8 +30,8 @@ Take a look at the class ``BaseAuth`` if you want to implement your own.
""" """
import hashlib import hashlib
import time
import threading import threading
import time
from typing import Sequence, Set, Tuple, Union, final from typing import Sequence, Set, Tuple, Union, final
from radicale import config, types, utils from radicale import config, types, utils
@ -89,7 +89,7 @@ class BaseAuth:
# cache_successful_logins # cache_successful_logins
self._cache_logins = configuration.get("auth", "cache_logins") self._cache_logins = configuration.get("auth", "cache_logins")
self._type = configuration.get("auth", "type") self._type = configuration.get("auth", "type")
if (self._type in [ "dovecot", "ldap", "htpasswd" ]) or (self._cache_logins is False): if (self._type in ["dovecot", "ldap", "htpasswd"]) or (self._cache_logins is False):
logger.info("auth.cache_logins: %s", self._cache_logins) logger.info("auth.cache_logins: %s", self._cache_logins)
else: else:
logger.info("auth.cache_logins: %s (but not required for type '%s' and disabled therefore)", self._cache_logins, self._type) logger.info("auth.cache_logins: %s (but not required for type '%s' and disabled therefore)", self._cache_logins, self._type)

View file

@ -48,12 +48,12 @@ When bcrypt is installed:
""" """
import os
import time
import functools import functools
import hmac import hmac
import os
import threading import threading
from typing import Any import time
from typing import Any, Tuple
from passlib.hash import apr_md5_crypt, sha256_crypt, sha512_crypt from passlib.hash import apr_md5_crypt, sha256_crypt, sha512_crypt
@ -66,9 +66,9 @@ class Auth(auth.BaseAuth):
_encoding: str _encoding: str
_htpasswd: dict # login -> digest _htpasswd: dict # login -> digest
_htpasswd_mtime_ns: int _htpasswd_mtime_ns: int
_htpasswd_size: bytes _htpasswd_size: int
_htpasswd_ok: bool _htpasswd_ok: bool
_htpasswd_not_ok_seconds: int _htpasswd_not_ok_time: float
_htpasswd_not_ok_reminder_seconds: int _htpasswd_not_ok_reminder_seconds: int
_htpasswd_bcrypt_use: int _htpasswd_bcrypt_use: int
_has_bcrypt: bool _has_bcrypt: bool
@ -154,7 +154,7 @@ class Auth(auth.BaseAuth):
# assumed plaintext # assumed plaintext
return self._plain(hash_value, password) return self._plain(hash_value, password)
def _read_htpasswd(self, init: bool) -> (bool, int): def _read_htpasswd(self, init: bool) -> Tuple[bool, int]:
"""Read htpasswd file """Read htpasswd file
init == True: stop on error init == True: stop on error
@ -168,6 +168,7 @@ class Auth(auth.BaseAuth):
else: else:
info = "Re-read" info = "Re-read"
logger.info("%s content of htpasswd file start: %r", info, self._filename) logger.info("%s content of htpasswd file start: %r", info, self._filename)
htpasswd: dict[str, str]
htpasswd = dict() htpasswd = dict()
try: try:
with open(self._filename, encoding=self._encoding) as f: with open(self._filename, encoding=self._encoding) as f:
@ -179,7 +180,7 @@ class Auth(auth.BaseAuth):
line = line.rstrip("\n") line = line.rstrip("\n")
if line.lstrip() and not line.lstrip().startswith("#"): if line.lstrip() and not line.lstrip().startswith("#"):
try: try:
login, digest = line.split( ":", maxsplit=1) login, digest = line.split(":", maxsplit=1)
skip = False skip = False
if login == "" or digest == "": if login == "" or digest == "":
if init is True: if init is True:
@ -216,7 +217,7 @@ class Auth(auth.BaseAuth):
if init is True: if init is True:
raise RuntimeError("Failed to load htpasswd file %r: %s" % (self._filename, e)) from e raise RuntimeError("Failed to load htpasswd file %r: %s" % (self._filename, e)) from e
else: else:
logger.warning("Failed to load htpasswd file on re-read: %r" % (self._filename, e)) logger.warning("Failed to load htpasswd file on re-read: %r" % self._filename)
htpasswd_ok = False htpasswd_ok = False
else: else:
self._htpasswd_size = os.stat(self._filename).st_size self._htpasswd_size = os.stat(self._filename).st_size