Skip to content

Commit f7d9fb7

Browse files
authored
Merge pull request #86 from adarnimrod/ldapi-support
ldapi support
2 parents 135a4dd + f1876a0 commit f7d9fb7

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

docs/index.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ directives:
6060
``LDAP_PORT`` The port number of your LDAP server. Default: 389.
6161
``LDAP_SCHEMA`` The LDAP schema to use between 'ldap' and 'ldaps'.
6262
Default: 'ldap'.
63+
``LDAP_SOCKET_PATH`` If ``LDAP_SCHEMA`` is set to `ldapi`, the
64+
path to the Unix socket path. Default: `/`.
6365
``LDAP_USERNAME`` **Required**: The user name used to bind.
6466
``LDAP_PASSWORD`` **Required**: The password used to bind.
6567
``LDAP_TIMEOUT`` How long (seconds) a connection can take to be opened

flask_simpleldap/__init__.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)