LDAP +

๐Ÿ” A comprehensive LDAP module for BoxLang that brings full-featured LDAP directory access to your applications!

This module provides powerful LDAP (Lightweight Directory Access Protocol) capabilities to the BoxLang language, making it easy to query, modify, and manage directory services with minimal code.

โœจ Features

  • ๐Ÿ” Query: Search LDAP directories with filters, scopes, and pagination

  • โž• Add: Create new directory entries with multi-valued attributes

  • โœ๏ธ Modify: Update existing entries (replace/add/delete attributes)

  • ๐Ÿ—‘๏ธ Delete: Remove directory entries

  • ๐Ÿ”„ ModifyDN: Rename entries or move within directory tree

  • ๐Ÿ”’ Secure: SSL/TLS support with certificate validation

  • ๐Ÿ”‘ Authentication: Simple bind and anonymous access

  • ๐ŸŽฏ Flexible Filtering: Complex LDAP filters with boolean logic

  • ๐Ÿ“„ Pagination: Handle large result sets efficiently

  • ๐Ÿ”Œ Connection Pooling: Automatic connection management

  • ๐Ÿข Enterprise Grade: Built and Supported by Ortus Solutions

๐Ÿ“ฆ Installation

Requirements

  • BoxLang 1.6+

  • Access to an LDAP server (Active Directory, OpenLDAP, etc.)

Install via CommandBox

If you are using CommandBox for your web applications, simply run:

Install via BoxLang OS Binary

If you are using the BoxLang OS Binary, simply run:

The module will automatically register and be available as bxldap in your BoxLang applications.

๐Ÿš€ Quick Start

Here's how to query an LDAP directory in just a few lines:

That's it! ๐ŸŽ‰ You now have LDAP query results in a BoxLang Query object.

๐Ÿ”ง LDAP Actions

The module supports seven core operations:

๐Ÿ” Query

Search the directory with filters, scopes, and attribute selection.

  • Use Case: Find users, groups, or any directory objects

  • Returns: BoxLang Query object with results (contains matched LDAP entries as rows with their attributes as columns)

  • Features: Filtering, sorting, pagination, scope control

  • Empty Results: Returns a Query with recordCount=0 if no entries match (no exception thrown)

โž• Add

Create new entries in the directory.

  • Use Case: Add new users, groups, organizational units

  • Supports: Multi-valued attributes, all attribute types

  • Returns: Boolean true on success

  • Error Handling: Throws exception if entry already exists or DN is invalid

โœ๏ธ Modify

Update existing directory entries.

  • Use Case: Update user information, group memberships

  • Operations: Replace, add, or delete attributes

  • Returns: Boolean true on success

  • Flexibility: Modify multiple attributes in one operation

  • Error Handling: Throws exception if entry does not exist or modification type is invalid

๐Ÿ—‘๏ธ Delete

Remove entries from the directory.

  • Use Case: Delete obsolete users, groups, or objects

  • Returns: Boolean true on success

  • Safety: Validates entry exists before deletion (throws exception if not found)

  • Note: Cannot delete entries with children (delete children first)

  • Error Handling: Throws exception if entry has subordinate entries or does not exist

๐Ÿ”„ ModifyDN

Rename entries or move them within the directory tree.

  • Use Case: Rename users, reorganize directory structure

  • Returns: Boolean true on success

  • Operations: Rename (change RDN) or move to different parent

  • Flexibility: Supports both rename and move operations

  • Error Handling: Throws exception if new DN is invalid or already exists

๐Ÿ”“ Open

Create and store a named connection for reuse across multiple operations.

  • Use Case: Establish a connection with credentials that can be reused without repetition

  • Required Attributes: connection (connection name), server, plus authentication details

  • Returns: Connection object if result attribute is specified, otherwise ignored

  • Benefits: Simplifies multi-operation workflows by eliminating credential repetition

  • Note: Connection remains open until explicitly closed or application terminates

๐Ÿ” Close

Close and release a named connection.

  • Use Case: Explicitly close a named connection to free resources

  • Required Attributes: connection (connection name to close)

  • Returns: Boolean true on success

  • Benefits: Allows proper resource management for long-running applications

  • Note: Once closed, connection can be reopened with same name if needed

๐Ÿ“š Component Reference

๐Ÿ” <bx:ldap> Component

The main component for all LDAP operations.

Core Attributes

Attribute
Type
Required
Default
Description

action

string

โœ… Yes

-

LDAP operation: "query", "add", "modify", "delete", "modifydn", "open", "close"

server

string

โš ๏ธ Conditional

-

LDAP server hostname or IP address. Required if connection is not provided.

connection

string

โš ๏ธ Conditional

-

Named connection to an LDAP server (previously created). Required if server is not provided. Allows reusing connection credentials across multiple operations.

port

number

-

389 (636 for SSL)

LDAP server port

result

string

โš ๏ธ Query

-

Variable name to store query results (primary attribute for result handling)

name

string

-

-

Deprecated: Use result instead. Maintained for backwards compatibility with query actions only

start

string

โœ… Yes

-

Starting DN (Distinguished Name) for search or operation

Authentication Attributes

Attribute
Type
Default
Description

username

string

"" (anonymous)

Bind DN for authentication (e.g., "cn=admin,dc=example,dc=org")

password

string

"" (anonymous)

Password for authentication

secure

boolean/string

false

Security mode: false (no SSL), true (SSL), "CFSSL_BASIC" (server auth only), "CFSSL_CLIENT_AUTH" (mutual TLS)

useTls

boolean

false

Whether to use StartTLS extension to initiate SSL/TLS over normal port

clientCert

string

-

Full path to keystore file containing client certificate

clientCertPassword

string

-

Password for client certificate keystore

timeout

integer

60000

Operation timeout in milliseconds

Query-Specific Attributes

Attribute
Type
Default
Description

filter

string

"(objectClass=*)"

LDAP search filter (e.g., "(uid=jdoe)")

scope

string

"onelevel"

Search scope: "base", "onelevel", "subtree"

attributes

string

"*"

Comma-separated list of attributes to return

sort

string

-

Attribute name to sort results by

sortDirection

string

"asc"

Sort direction: "asc" or "desc"

maxrows

number

-

Maximum number of results to return

startRow

number

1

Starting row for pagination

returnFormat

string

"query"

Result format: "query" (Query object) or "array" (Array of structs)

Modify-Specific Attributes

Attribute
Type
Required
Description

dn

string

โœ… Yes

Distinguished Name of entry to modify

attributes

struct

โœ… Yes

Struct of attributes to modify (key=attribute, value=new value)

modifyType

string

"replace"

Modification type: "replace", "add", "delete"

Add-Specific Attributes

Attribute
Type
Required
Description

dn

string

โœ… Yes

Distinguished Name for new entry

attributes

struct

โœ… Yes

Struct of attributes for new entry

Delete-Specific Attributes

Attribute
Type
Required
Description

dn

string

โœ… Yes

Distinguished Name of entry to delete

ModifyDN-Specific Attributes

Attribute
Type
Required
Description

dn

string

โœ… Yes

Current Distinguished Name

attributes

struct

โœ… Yes

Struct with "newRDN" and optionally "newParentDN"

๐Ÿ’ก Examples

Basic Examples

๐Ÿ” Simple Query

Find all users in a directory:

๐Ÿ’ก Use Case: Quick directory lookup to list all users.

Search for a specific user:

๐Ÿ’ก Use Case: User lookup with specific attributes for profile display.

โž• Add New User

Create a new directory entry:

๐Ÿ’ก Use Case: User registration or bulk user import.

โœ๏ธ Modify User

Update an existing entry:

๐Ÿ’ก Use Case: Profile updates, contact information changes.

๐Ÿ—‘๏ธ Delete User

Remove an entry from the directory:

๐Ÿ’ก Use Case: Account deactivation, cleanup of obsolete entries.

๐Ÿ”„ Rename User

Change an entry's RDN (Relative Distinguished Name):

๐Ÿ’ก Use Case: Username changes, standardizing naming conventions.

Connection Management Examples

๐Ÿ”Œ Define and Reuse Named Connections

Create a named connection once and reuse it across multiple operations:

๐Ÿ’ก Use Case: Simplifies code when performing multiple LDAP operations. Credentials are passed once, then the connection is reused by its name.

๐Ÿ”“ Explicitly Open a Named Connection

Establish a connection with the open action for later reuse:

๐Ÿ’ก Use Case: Use open when you need explicit connection lifecycle management. Useful in microservices or long-running applications.

๐Ÿ” Close a Named Connection

Explicitly close a connection to free resources:

๐Ÿ’ก Use Case: Important for resource management in applications that maintain many connections or run for extended periods.

Advanced Examples

๐Ÿ” Complex Filter Query

Use advanced LDAP filter syntax:

๐Ÿ’ก Use Case: Department reporting, audit queries, compliance checks.

Filter Operators:

  • & - AND (all conditions must match)

  • | - OR (any condition matches)

  • ! - NOT (negation)

  • = - Equals

  • >= - Greater than or equal

  • <= - Less than or equal

  • =* - Presence check (attribute exists)

  • =value* - Starts with

  • =*value - Ends with

  • =*value* - Contains

๐Ÿ“„ Paginated Query

Handle large result sets efficiently:

๐Ÿ’ก Use Case: User management interfaces, large directory browsing.

๏ฟฝ Query Results as Array

Return results as an array of structs instead of a Query object:

๐Ÿ’ก Use Case: JSON API responses, data transformation, modern data handling patterns.

Supported Formats:

  • "query" - Returns BoxLang Query object (default) - good for compatibility with traditional CFML patterns

  • "array" - Returns Array of Structs - better for JSON APIs and modern applications

๏ฟฝ๐Ÿ”’ SSL/TLS Secure Connection

Connect securely with SSL:

๐Ÿ’ก Use Case: Production environments, sensitive data access, compliance requirements.

๐Ÿ” Mutual TLS Authentication

Use client certificates for authentication:

๐Ÿ’ก Use Case: High-security environments, API integrations, service accounts.

โž• Add Entry with Multiple Values

Create an entry with multi-valued attributes:

๐Ÿ’ก Use Case: Group management, access control lists, distribution lists.

โœ๏ธ Add Attribute Values

Add values to existing multi-valued attributes:

๐Ÿ’ก Use Case: Group membership management, role assignments.

๐Ÿ—‘๏ธ Delete Attribute Values

Remove specific values from multi-valued attributes:

๐Ÿ’ก Use Case: Membership revocation, access control updates.

๐Ÿ”„ Move Entry to Different OU

Move an entry to a different organizational unit:

๐Ÿ’ก Use Case: Organizational restructuring, employee status changes.

โš ๏ธ Error Handling

Understanding Empty Results

Important: LDAP queries return empty Query objects (recordCount=0) instead of throwing exceptions when:

  • Entry does not exist

  • Filter matches no entries

  • Insufficient permissions (sometimes)

  • Entry was deleted

Exception Handling

Handle connection and operation errors:

Common Error Scenarios

Error
Cause
Solution

Connection timeout

Server unreachable

Check server hostname, port, firewall

Invalid credentials

Wrong username/password

Verify bind DN and password

Entry already exists

Duplicate DN in add

Use unique DN or modify existing entry

No such object

DN doesn't exist

Verify DN syntax and entry existence

Insufficient access

Permission denied

Check ACLs and bind user permissions

Invalid DN syntax

Malformed DN

Validate DN format: uid=user,ou=org,dc=example,dc=org

๐ŸŽฏ Best Practices

Security

  • โœ… Always use SSL/TLS in production (secure="true")

  • โœ… Never hardcode credentials - use environment variables or secure vaults

  • โœ… Use least privilege - bind with minimum required permissions

  • โœ… Validate user input - sanitize filter parameters to prevent LDAP injection

  • โœ… Implement connection timeouts - prevent hanging operations

  • โœ… Use service accounts - dedicated accounts for application access

  • โœ… Rotate passwords regularly - follow security policies

  • โœ… Log security events - audit all modify/add/delete operations

Performance

  • โœ… Use specific filters - narrow searches with precise LDAP filters

  • โœ… Limit attribute retrieval - only request needed attributes

  • โœ… Implement pagination - use maxrows and startRow for large result sets

  • โœ… Choose appropriate scope - use "base" or "onelevel" when possible

  • โœ… Cache results - cache frequently accessed data

  • โœ… Use indexed attributes - filter on indexed attributes for speed

  • โœ… Connection pooling - automatically handled by Apache Directory API

Filter Optimization

  • โœ… Put most restrictive filters first - in AND operations

  • โœ… Use equality over substring - (uid=jdoe) faster than (uid=*jdoe*)

  • โœ… Avoid leading wildcards - (cn=John*) faster than (cn=*John)

  • โœ… Combine filters efficiently - use (&(attr1=val1)(attr2=val2)) not multiple queries

Directory Structure

  • โœ… Follow DN conventions - consistent naming schemes

  • โœ… Use organizational units - logical grouping (ou=users, ou=groups)

  • โœ… Plan DN hierarchy - consider future growth and reorganization

  • โœ… Document schema - maintain documentation of custom attributes

๐Ÿ“ข Event Handling

The LDAP module announces events at key points in the connection lifecycle, allowing you to monitor, audit, and react to connection operations. These events are dispatched through BoxLang's interception system.

Available Events

onLDAPConnectionOpen

Announced when an LDAP connection is successfully opened (either new or from pool).

Event Payload:

Example Interceptor:

onLDAPConnectionClose

Announced when an LDAP connection is closed or removed from the pool.

Event Payload:

Example Interceptor:

Connection Monitoring Example

Create an interceptor to monitor all connection lifecycle events:

โ“ Troubleshooting

Connection Issues

Problem: Cannot connect to LDAP server.

Solutions:

  • โœ… Verify server hostname and port (389 standard, 636 SSL)

  • โœ… Check firewall rules allow LDAP traffic

  • โœ… Test connectivity with telnet ldap.example.com 389

  • โœ… Verify DNS resolution of hostname

  • โœ… Check LDAP server is running and accepting connections

  • โœ… Review LDAP server logs for connection attempts

Authentication Failures

Problem: Invalid credentials or bind failure.

Solutions:

  • โœ… Verify bind DN format: cn=admin,dc=example,dc=org

  • โœ… Confirm password is correct (no extra spaces)

  • โœ… Check if account is locked or disabled

  • โœ… Verify user has appropriate permissions

  • โœ… For Active Directory: use [email protected] or DOMAIN\user format

  • โœ… Test credentials with LDAP browser tool (Apache Directory Studio)

Query Returns No Results

Problem: Query completes but returns 0 results.

Solutions:

  • โœ… Verify start DN exists in directory

  • โœ… Check filter syntax is correct

  • โœ… Confirm scope is appropriate ("base" vs "onelevel" vs "subtree")

  • โœ… Verify bind user has read permissions on entries

  • โœ… Test filter with LDAP browser tool

  • โœ… Check for typos in attribute names

  • โœ… Confirm entries actually exist in search base

SSL/TLS Issues

Problem: SSL handshake failure or certificate errors.

Solutions:

  • โœ… Verify port 636 is used for LDAPS (not 389)

  • โœ… Check server certificate is valid and not expired

  • โœ… Import server certificate into Java truststore

  • โœ… For self-signed certs: add to trusted certificates

  • โœ… Verify certificate hostname matches server hostname

  • โœ… Check for certificate chain issues

Modify/Add/Delete Failures

Problem: Write operations fail or return errors.

Solutions:

  • โœ… Verify bind user has write permissions

  • โœ… Check DN exists (for modify/delete) or parent exists (for add)

  • โœ… Confirm attribute syntax matches schema requirements

  • โœ… For add: ensure all required attributes are provided

  • โœ… For modify: verify attributes exist before deleting

  • โœ… Check for constraints (unique attributes, referential integrity)

  • โœ… Cannot delete entries with children (delete children first)

Performance Problems

Problem: Queries are slow or timeout.

Solutions:

  • โœ… Add indexes to frequently filtered attributes on LDAP server

  • โœ… Use more specific filters to reduce result set size

  • โœ… Implement pagination for large result sets

  • โœ… Limit attributes retrieved with attributes parameter

  • โœ… Use appropriate scope (avoid "subtree" when possible)

  • โœ… Increase timeout value for complex queries

  • โœ… Check LDAP server performance and load

Last updated

Was this helpful?