🐛 Only enable argon2 if backend is available

We not only import it but also do a quick hash
and verify run to make sure the backend actually
works, otherwise we only support pbkdf2_sha256.

Additionally support multiple hash algorithms
and detect which one to use based on the password
hash.
This commit is contained in:
Gina Häußge committed 2022-09-26 17:56:18 +02:00
1 parent daa5dbc4d6
commit f334decb33
1 file changed
+25 -8
+25 -8
View File
@@ -10,6 +10,7 @@ import uuid
import wrapt
from flask_login import AnonymousUserMixin, UserMixin
from passlib.hash import pbkdf2_sha256
from werkzeug.local import LocalProxy
from octoprint.access.groups import Group, GroupChangeListener
@@ -19,10 +20,22 @@ from octoprint.util import atomic_write, deprecated, generate_api_key
from octoprint.util import get_fully_qualified_classname as fqcn
from octoprint.util import to_bytes, yaml
password_hashers = []
try:
from passlib.hash import argon2 as passwordhash
except ImportError:
from passlib.hash import pbkdf2_sha256 as passwordhash
from passlib.hash import argon2
# test if we can actually hash and verify, if not we won't use this backend
hash = argon2.hash("test")
argon2.verify("test", hash)
password_hashers.append(argon2)
except Exception:
logging.getLogger(__name__).warning(
"Argon2 passlib backend is not available, not using it for password hashing"
)
password_hashers.append(pbkdf2_sha256)
class UserManager(GroupChangeListener):
@@ -141,7 +154,7 @@ class UserManager(GroupChangeListener):
@staticmethod
def create_password_hash(password, *args, **kwargs):
return passwordhash.hash(password)
return password_hashers[0].hash(password)
@staticmethod
def create_legacy_password_hash(password, salt):
@@ -1173,10 +1186,14 @@ class User(UserMixin):
if legacy:
return self._passwordHash == password
try:
return passwordhash.verify(password, self._passwordHash)
except ValueError:
return False
for password_hash in password_hashers:
if password_hash.identify(self._passwordHash):
try:
return password_hash.verify(password, self._passwordHash)
except ValueError:
pass
return False
def get_id(self):
return self.get_name()