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

1#!/usr/bin/env python 

2# -*- coding: utf-8 -*- 

3import ldap 

4 

5from marshmallow import fields, Schema, EXCLUDE 

6 

7from daiquiri.core.authenticator import AuthenticatorInterface 

8 

9import logging 

10 

11logger = logging.getLogger(__name__) 

12 

13 

14class LDAPConfigSchema(Schema): 

15 ldap_dn = fields.Str() 

16 

17 

18class LdapAuthenticator(AuthenticatorInterface): 

19 def __init__(self, *args, **kwargs): 

20 super().__init__(*args, **kwargs) 

21 logger.debug("Loaded: {c}".format(c=self.__class__.__name__)) 

22 

23 self._ldap_options = LDAPConfigSchema().load( 

24 args[0].get("auth_options", [{}])[0], unknown=EXCLUDE 

25 ) 

26 

27 self._ldap_dn = self._ldap_options.get("ldap_dn") 

28 

29 def authenticate(self, username, password): 

30 res = False 

31 

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 

42 

43 return res