mirror of
https://github.com/mealie-recipes/mealie.git
synced 2025-05-24 01:12:54 -04:00
* Use Base DN for LDAP and fetch user attrs Requires that a Base DN be set for LDAP Set `full_name` and `email` based on LDAP attributes when creating user * Add support for secure LDAP Allow insecure LDAP connection (disabled by default) Use CA when connecting to secure LDAP server * Added missing quotes to example * Update security.py * Update security.py formatting * Update security.py Switched to f-String formatting * formatting * Update test_security.py Added at attributes for testing * Update test_security.py Modified tests for base DN * Update test_security.py Set proper base DN for testing * Update test_security.py Corrected testing for LDAP * Update test_security.py Defined base_dn * Authenticated user not in base DN Add check for when user can authenticate but is not in base DN * Update test_security.py LDAP user cannot exist as it is searched before it is created and the list returns False Co-authored-by: Hayden <64056131+hay-kot@users.noreply.github.com>
52 lines
1.7 KiB
Python
52 lines
1.7 KiB
Python
from pathlib import Path
|
|
|
|
from pytest import MonkeyPatch
|
|
|
|
from mealie.core import security
|
|
from mealie.core.config import get_app_settings
|
|
from mealie.core.dependencies import validate_file_token
|
|
from mealie.db.db_setup import create_session
|
|
from tests.utils.factories import random_string
|
|
|
|
|
|
def test_create_file_token():
|
|
file_path = Path(__file__).parent
|
|
file_token = security.create_file_token(file_path)
|
|
|
|
assert file_path == validate_file_token(file_token)
|
|
|
|
|
|
def test_ldap_authentication_mocked(monkeypatch: MonkeyPatch):
|
|
import ldap
|
|
|
|
user = random_string(10)
|
|
password = random_string(10)
|
|
bind_template = "cn={},dc=example,dc=com"
|
|
base_dn = "(dc=example,dc=com)"
|
|
monkeypatch.setenv("LDAP_AUTH_ENABLED", "true")
|
|
monkeypatch.setenv("LDAP_SERVER_URL", "") # Not needed due to mocking
|
|
monkeypatch.setenv("LDAP_BIND_TEMPLATE", bind_template)
|
|
monkeypatch.setenv("LDAP_BASE_DN", base_dn)
|
|
|
|
class LdapConnMock:
|
|
def simple_bind_s(self, dn, bind_pw):
|
|
assert dn == bind_template.format(user)
|
|
return bind_pw == password
|
|
|
|
def search_s(self, dn, scope, filter, attrlist):
|
|
assert attrlist == ["name", "mail"]
|
|
assert filter == f"(&(objectClass=user)(|(cn={user})(sAMAccountName={user})(mail={user})))"
|
|
assert dn == base_dn
|
|
assert scope == ldap.SCOPE_SUBTREE
|
|
return [()]
|
|
|
|
def ldap_initialize_mock(url):
|
|
assert url == ""
|
|
return LdapConnMock()
|
|
|
|
monkeypatch.setattr(ldap, "initialize", ldap_initialize_mock)
|
|
|
|
get_app_settings.cache_clear()
|
|
result = security.authenticate_user(create_session(), user, password)
|
|
assert result is False
|