Coverage for /opt/conda/envs/apienv/lib/python3.10/site-packages/daiquiri/core/authenticator/ldap.py: 0%
26 statements
« prev ^ index » next coverage.py v7.6.5, created at 2024-11-15 02:12 +0000
« prev ^ index » next coverage.py v7.6.5, created at 2024-11-15 02:12 +0000
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3import ldap
5from marshmallow import fields, Schema, EXCLUDE
7from daiquiri.core.authenticator import AuthenticatorInterface
9import logging
11logger = logging.getLogger(__name__)
14class LDAPConfigSchema(Schema):
15 ldap_dn = fields.Str()
18class LdapAuthenticator(AuthenticatorInterface):
19 def __init__(self, *args, **kwargs):
20 super().__init__(*args, **kwargs)
21 logger.debug("Loaded: {c}".format(c=self.__class__.__name__))
23 self._ldap_options = LDAPConfigSchema().load(
24 args[0].get("auth_options", [{}])[0], unknown=EXCLUDE
25 )
27 self._ldap_dn = self._ldap_options.get("ldap_dn")
29 def authenticate(self, username, password):
30 res = False
32 try:
33 ldap_obj = ldap.initialize("ldap://" + self._config["auth_server"])
34 ldap_obj.protocol_version = ldap.VERSION3
35 user_dn = "uid=" + username + ",ou=people," + self._ldap_dn
36 result = ldap_obj.bind_s(user_dn, password)
37 res = True if (len(result) == 2 and result[0] == 97) else False
38 ldap_obj.unbind_s()
39 except ldap.LDAPError as e:
40 logger.debug("LDAP Error: {e}".format(e=e))
41 res = False
43 return res