Allow one to define Dovecot's schemes in settings

This commit is contained in:
Jérôme Lebleu
2021-02-08 16:47:41 +01:00
parent abd3ba537e
commit 5cbbd4655b
2 changed files with 24 additions and 13 deletions
+17 -12
View File
@@ -2,6 +2,7 @@
Password hashers for Modoboa.
"""
from django.conf import settings
from django.utils.encoding import smart_text
from modoboa.core.password_hashers.advanced import ( # NOQA:F401
@@ -32,17 +33,21 @@ def get_password_hasher(scheme):
def get_dovecot_schemes():
"""Return schemes supported by dovecot"""
supported_schemes = None
try:
_, schemes = doveadm_cmd("pw -l")
except OSError:
pass
else:
supported_schemes = ["{{{}}}".format(smart_text(scheme))
for scheme in schemes.split()]
"""Return schemes supported by Dovecot.
if not supported_schemes:
supported_schemes = ['{MD5-CRYPT}', '{PLAIN}']
It first try to get supported schemes fron the settings, then from the
doveadm output, and fallback to {MD5-CRYPT} and {PLAIN} if the command
is not found.
return supported_schemes
:return: A list of supported '{SCHEME}'
"""
schemes = getattr(settings, "DOVECOT_SUPPORTED_SCHEMES", None)
if not schemes:
try:
_, schemes = doveadm_cmd("pw -l")
except OSError:
schemes = "MD5-CRYPT PLAIN"
return ["{{{}}}".format(smart_text(scheme))
for scheme in schemes.split()]
+7 -1
View File
@@ -173,7 +173,7 @@ class AuthenticationTestCase(ModoTestCase):
@override_settings(DOVEADM_LOOKUP_PATH=[DOVEADM_TEST_PATH])
def test_dovecot_supported_schemes(self):
"""Validate dovecot supported schemes with fake output."""
"""Validate Dovecot supported schemes with fake command output."""
supported_schemes = get_dovecot_schemes()
self.assertEqual(supported_schemes,
["{MD5}",
@@ -207,6 +207,12 @@ class AuthenticationTestCase(ModoTestCase):
"{SHA256-CRYPT}",
"{SHA512-CRYPT}"])
@override_settings(DOVECOT_SUPPORTED_SCHEMES="SHA1 SHA512-CRYPT")
def test_dovecot_supported_schemes_from_settings(self):
"""Validate dovecot supported schemes from the settings."""
supported_schemes = get_dovecot_schemes()
self.assertEqual(supported_schemes, ["{SHA1}", "{SHA512-CRYPT}"])
def test_dovecot_default_schemes(self):
"""Check default scheme if doveadm is not found."""
supported_schemes = get_dovecot_schemes()