@@ -34,6 +34,7 @@ def init_app(app):
3434 app .config .setdefault ("LDAP_HOST" , "localhost" )
3535 app .config .setdefault ("LDAP_PORT" , 389 )
3636 app .config .setdefault ("LDAP_SCHEMA" , "ldap" )
37+ app .config .setdefault ("LDAP_SOCKET_PATH" , "/" )
3738 app .config .setdefault ("LDAP_USERNAME" , None )
3839 app .config .setdefault ("LDAP_PASSWORD" , None )
3940 app .config .setdefault ("LDAP_TIMEOUT" , 10 )
@@ -68,9 +69,13 @@ def init_app(app):
6869 ldap .set_option (ldap .OPT_X_TLS_REQUIRE_CERT , ldap .OPT_X_TLS_DEMAND )
6970 ldap .set_option (ldap .OPT_X_TLS_CACERTFILE , app .config ["LDAP_CERT_PATH" ])
7071
71- for option in ["USERNAME" , "PASSWORD" , "BASE_DN" ]:
72- if app .config ["LDAP_{0}" .format (option )] is None :
73- raise LDAPException ("LDAP_{0} cannot be None!" .format (option ))
72+ if app .config ["LDAP_BASE_DN" ] is None :
73+ raise LDAPException ("LDAP_BASE_DN cannot be None!" )
74+
75+ if app .config ["LDAP_SCHEMA" ] != "ldapi" :
76+ for option in ["USERNAME" , "PASSWORD" ]:
77+ if app .config ["LDAP_{0}" .format (option )] is None :
78+ raise LDAPException ("LDAP_{0} cannot be None!" .format (option ))
7479
7580 @staticmethod
7681 def _set_custom_options (conn ):
@@ -88,13 +93,18 @@ def initialize(self):
8893 """
8994
9095 try :
91- conn = ldap .initialize (
92- "{0}://{1}:{2}" .format (
96+ if current_app .config ["LDAP_SCHEMA" ] == "ldapi" :
97+ uri = "{0}://{1}" .format (
98+ current_app .config ["LDAP_SCHEMA" ],
99+ current_app .config ["LDAP_SOCKET_PATH" ],
100+ )
101+ else :
102+ uri = "{0}://{1}:{2}" .format (
93103 current_app .config ["LDAP_SCHEMA" ],
94104 current_app .config ["LDAP_HOST" ],
95105 current_app .config ["LDAP_PORT" ],
96106 )
97- )
107+ conn = ldap . initialize ( uri )
98108 conn .set_option (
99109 ldap .OPT_NETWORK_TIMEOUT , current_app .config ["LDAP_TIMEOUT" ]
100110 )
0 commit comments