Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ldapi support #86

Merged
merged 1 commit into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ directives:
``LDAP_PORT`` The port number of your LDAP server. Default: 389.
``LDAP_SCHEMA`` The LDAP schema to use between 'ldap' and 'ldaps'.
Default: 'ldap'.
``LDAP_SOCKET_PATH`` If ``LDAP_SCHEMA`` is set to `ldapi`, the
path to the Unix socket path. Default: `/`.
``LDAP_USERNAME`` **Required**: The user name used to bind.
``LDAP_PASSWORD`` **Required**: The password used to bind.
``LDAP_TIMEOUT`` How long (seconds) a connection can take to be opened
Expand Down
22 changes: 16 additions & 6 deletions flask_simpleldap/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ def init_app(app):
app.config.setdefault("LDAP_HOST", "localhost")
app.config.setdefault("LDAP_PORT", 389)
app.config.setdefault("LDAP_SCHEMA", "ldap")
app.config.setdefault("LDAP_SOCKET_PATH", "/")
app.config.setdefault("LDAP_USERNAME", None)
app.config.setdefault("LDAP_PASSWORD", None)
app.config.setdefault("LDAP_TIMEOUT", 10)
Expand Down Expand Up @@ -68,9 +69,13 @@ def init_app(app):
ldap.set_option(ldap.OPT_X_TLS_REQUIRE_CERT, ldap.OPT_X_TLS_DEMAND)
ldap.set_option(ldap.OPT_X_TLS_CACERTFILE, app.config["LDAP_CERT_PATH"])

for option in ["USERNAME", "PASSWORD", "BASE_DN"]:
if app.config["LDAP_{0}".format(option)] is None:
raise LDAPException("LDAP_{0} cannot be None!".format(option))
if app.config["LDAP_BASE_DN"] is None:
raise LDAPException("LDAP_BASE_DN cannot be None!")

if app.config["LDAP_SCHEMA"] != "ldapi":
for option in ["USERNAME", "PASSWORD"]:
if app.config["LDAP_{0}".format(option)] is None:
raise LDAPException("LDAP_{0} cannot be None!".format(option))

@staticmethod
def _set_custom_options(conn):
Expand All @@ -88,13 +93,18 @@ def initialize(self):
"""

try:
conn = ldap.initialize(
"{0}://{1}:{2}".format(
if current_app.config["LDAP_SCHEMA"] == "ldapi":
uri = "{0}://{1}".format(
current_app.config["LDAP_SCHEMA"],
current_app.config["LDAP_SOCKET_PATH"],
)
else:
uri = "{0}://{1}:{2}".format(
current_app.config["LDAP_SCHEMA"],
current_app.config["LDAP_HOST"],
current_app.config["LDAP_PORT"],
)
)
conn = ldap.initialize(uri)
conn.set_option(
ldap.OPT_NETWORK_TIMEOUT, current_app.config["LDAP_TIMEOUT"]
)
Expand Down