Skip to main content

LDAP Auth

The ldap-auth plugin verifies HTTP Basic credentials with an LDAP directory and maps the authenticated directory user to a consumer. The consumer identity lets consumer-scoped plugins, rate limits, and analytics apply after authentication.

The plugin is best suited to directories where user distinguished names follow a predictable pattern. For nested directory structures or names that cannot be constructed from the user name, use LDAP Auth Advanced.

Examples​

The examples show how to authenticate and map a directory user to a consumer, then prevent the reusable directory password from reaching the upstream.

Set Up OpenLDAP and a Test Upstream​

Choose the environment for the isolated OpenLDAP directory and the upstream that reports whether it received the credential header and consumer identity.

caution

This fixture uses plaintext LDAP only for local evaluation. For production, use LDAPS with use_tls: true, enable tls_verify, and configure every gateway instance to trust the directory certificate's issuing CA.

Create and enter a working directory:

mkdir ldap-auth-example
cd ldap-auth-example

Create the directory bootstrap data:

ldap-users.ldif
dn: ou=users,dc=example,dc=org
objectClass: organizationalUnit
ou: users

dn: uid=johndoe,ou=users,dc=example,dc=org
objectClass: inetOrgPerson
cn: John Doe
sn: Doe
uid: johndoe
mail: johndoe@example.org
userPassword: john-secret

Create the test upstream:

ldap-upstream.py
import json
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer


class Handler(BaseHTTPRequestHandler):
protocol_version = "HTTP/1.1"

def do_GET(self):
if self.path != "/ldap-check":
self.send_error(404)
return

body = json.dumps({
"authorization": "present" if self.headers.get("Authorization") else "absent",
"consumer": self.headers.get("X-Consumer-Username", ""),
}).encode() + b"\n"

self.send_response(200)
self.send_header("Content-Type", "application/json")
self.send_header("Content-Length", str(len(body)))
self.end_headers()
self.wfile.write(body)

def log_message(self, *_):
pass


ThreadingHTTPServer(("0.0.0.0", 8000), Handler).serve_forever()

Set GATEWAY_CONTAINER to the running APISIX or API7 Gateway container. Create a dedicated network and connect the gateway:

export GATEWAY_CONTAINER=replace-with-gateway-container-name

docker network create gateway-ldap-auth-net
docker network connect gateway-ldap-auth-net "$GATEWAY_CONTAINER"

Start OpenLDAP with the test user:

docker run -d \
--name ldap-auth-openldap \
--hostname ldap-auth-openldap \
--network gateway-ldap-auth-net \
-e LDAP_DOMAIN=example.org \
-e LDAP_BASE_DN=dc=example,dc=org \
-e 'LDAP_ORGANISATION=Example Organization' \
-e LDAP_ADMIN_PASSWORD=admin-secret \
-e LDAP_CREATE_PEOPLE_OU=false \
-e LDAP_CREATE_GROUPS_OU=false \
-v "$PWD/ldap-users.ldif:/docker-entrypoint-initdb.d/20-users.ldif:ro" \
chrroessner/openldap:2.6.15-debian13.7

Wait until the container is healthy, then verify the test entry:

for attempt in $(seq 1 30); do
if [ "$(docker inspect --format '{{.State.Health.Status}}' ldap-auth-openldap)" = "healthy" ]; then
break
fi
sleep 1
done

test "$(docker inspect --format '{{.State.Health.Status}}' ldap-auth-openldap)" = "healthy"
docker inspect --format '{{.State.Health.Status}}' ldap-auth-openldap

docker exec ldap-auth-openldap ldapsearch -LLL -x \
-H ldap://127.0.0.1:389 \
-D 'cn=admin,dc=example,dc=org' \
-w admin-secret \
-b 'ou=users,dc=example,dc=org' \
'(uid=johndoe)' dn uid

The directory is ready and returns the test user's DN:

healthy
dn: uid=johndoe,ou=users,dc=example,dc=org
uid: johndoe

Start the test upstream:

docker run -d \
--name ldap-auth-upstream \
--network gateway-ldap-auth-net \
-v "$PWD/ldap-upstream.py:/ldap-upstream.py:ro" \
python:3.13.15-alpine3.24 \
python -u /ldap-upstream.py

Authenticate and Map a Directory User​

Create a consumer for the LDAP entry and protect the upstream route.

Create the consumer:

curl "http://127.0.0.1:9180/apisix/admin/consumers/ldap-john" -X PUT \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-H "Content-Type: application/json" \
-d '{
"username": "ldap-john",
"plugins": {
"ldap-auth": {
"user_dn": "uid=johndoe,ou=users,dc=example,dc=org"
}
}
}'

Create the route:

curl "http://127.0.0.1:9180/apisix/admin/routes/ldap-auth-route" -X PUT \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-H "Content-Type: application/json" \
-d '{
"uri": "/ldap-check",
"plugins": {
"ldap-auth": {
"ldap_uri": "ldap-auth-openldap:389",
"base_dn": "ou=users,dc=example,dc=org",
"uid": "uid"
}
},
"upstream": {
"type": "roundrobin",
"nodes": {
"ldap-auth-upstream:8000": 1
}
}
}'

Send a request without credentials:

curl --include "http://127.0.0.1:9080/ldap-check"

The plugin challenges the client and rejects the request before it reaches the upstream:

HTTP/1.1 401 Unauthorized
WWW-Authenticate: Basic realm="ldap"
...
{"message":"Missing authorization in request"}

Send a request with the correct user name and an incorrect password:

curl --include "http://127.0.0.1:9080/ldap-check" \
--user johndoe:wrong-secret

The directory rejects the bind:

HTTP/1.1 401 Unauthorized
WWW-Authenticate: Basic realm="ldap"
...
{"message":"Invalid user authorization"}

Send the valid directory credentials:

curl "http://127.0.0.1:9080/ldap-check" \
--user johndoe:john-secret

The request reaches the upstream with the matched consumer identity. The Admin API and ADC workflows use the configured consumer name, and the default configuration also forwards the credential header:

{"authorization": "present", "consumer": "ldap-john"}

Ingress Controller derives the consumer username from the resource namespace and name. For the aic namespace and ldap-john resource used above, the response contains:

{"authorization": "present", "consumer": "aic_ldap-john"}

Stop Forwarding Directory Credentials​

The HTTP Basic header contains the directory password, which is commonly reusable outside this API. Credential removal was introduced in API7 Gateway 3.10.6 and APISIX 3.18.0. API7 Gateway 3.9 does not support this option.

Replace the plugin configuration on the existing route:

curl "http://127.0.0.1:9180/apisix/admin/routes/ldap-auth-route/plugins/ldap-auth" -X PATCH \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-H "Content-Type: application/json" \
-d '{
"ldap_uri": "ldap-auth-openldap:389",
"base_dn": "ou=users,dc=example,dc=org",
"uid": "uid",
"hide_credentials": true
}'

Send the valid credentials again:

curl "http://127.0.0.1:9080/ldap-check" \
--user johndoe:john-secret

The consumer remains attached, but the upstream no longer receives the credential header. The Admin API and ADC workflows return:

{"authorization": "absent", "consumer": "ldap-john"}

The Ingress Controller workflow returns the derived consumer username:

{"authorization": "absent", "consumer": "aic_ldap-john"}

When to Use LDAP Auth Advanced​

LDAP Auth constructs the user's distinguished name directly from the configured user attribute, client-supplied user name, and base DN. LDAP Auth Advanced instead searches the directory to resolve the user's distinguished name and can authenticate without a consumer mapping. It is available in API7 Gateway 3.10.5 and later and in APISIX 3.18.0 and later.

Limitations​

Missing or malformed credentials, directory connection failures, rejected binds, and missing consumer mappings return 401 Unauthorized.

The plugin removes whitespace from the decoded user name and password, and a password containing a colon is truncated after its first segment.

DN escaping was introduced in API7 Gateway 3.10.6 and APISIX 3.18.0. API7 Gateway 3.9 does not escape structural characters in the user name. On that version, use directory user names without those characters.