mirror of
https://github.com/nabbar/golib.git
synced 2025-09-26 20:01:15 +08:00
ldap
Package Documentation
Note:
This package uses an older design and would benefit from a refactor to modern Go idioms and best practices.
Overview
The ldap
package provides helpers for connecting to, authenticating with, and querying LDAP servers in Go. It supports both plain and TLS/StartTLS connections, user and group lookups, and flexible configuration.
Features
- Connect to LDAP servers with or without TLS/StartTLS
- Bind and authenticate users
- Retrieve user and group information
- Check group membership and list group members
- Customizable search filters and attributes
- Integrated error handling with custom codes
- Logging support for debugging and tracing
Main Types
Config
Represents the LDAP server configuration.
Uri
: Server hostname (FQDN, required)PortLdap
: LDAP port (required, integer)Portldaps
: LDAPS port (optional, integer)Basedn
: Base DN for searchesFilterGroup
: Pattern for group search (e.g.,(&(objectClass=groupOfNames)(%s=%s))
)FilterUser
: Pattern for user search (e.g.,(%s=%s)
)
Validation:
Use Validate()
to check config correctness.
TLSMode
Enum for connection mode:
TLSModeNone
: No TLSTLSModeTLS
: Strict TLSTLSModeStarttls
: StartTLS_TLSModeInit
: Not defined
HelperLDAP
Main struct for managing LDAP connections and queries.
NewLDAP(ctx, config, attributes)
: Create a new helperSetLogger(fct)
: Set a logger functionSetCredentials(user, pass)
: Set bind DN and passwordForceTLSMode(mode, tlsConfig)
: Force a specific TLS mode and config
Main Methods
Check()
: Test connection (no bind)Connect()
: Connect and bind using credentialsAuthUser(username, password)
: Test user bindUserInfo(username)
: Get user attributes as a mapUserInfoByField(username, field)
: Get user info by a specific fieldGroupInfo(groupname)
: Get group attributes as a mapGroupInfoByField(groupname, field)
: Get group info by a specific fieldUserMemberOf(username)
: List groups a user belongs toUserIsInGroup(username, groupnames)
: Check if user is in any of the given groupsUsersOfGroup(groupname)
: List users in a groupParseEntries(entry)
: Parse DN or attribute string into a map
Error Handling
All errors are wrapped with custom codes for diagnostics, such as:
ErrorParamEmpty
ErrorLDAPContext
ErrorLDAPServerConfig
ErrorLDAPServerConnection
ErrorLDAPBind
ErrorLDAPSearch
ErrorLDAPUserNotFound
ErrorLDAPGroupNotFound
- ...and more
Use err.Error()
for user-friendly messages and check error codes for diagnostics.
Example Usage
import (
"context"
"github.com/nabbar/golib/ldap"
)
cfg := ldap.Config{
Uri: "ldap.example.com",
PortLdap: 389,
Portldaps: 636,
Basedn: "dc=example,dc=com",
FilterUser: "(uid=%s)",
FilterGroup: "(&(objectClass=groupOfNames)(cn=%s))",
}
if err := cfg.Validate(); err != nil {
// handle config error
}
helper, err := ldap.NewLDAP(context.Background(), &cfg, ldap.GetDefaultAttributes())
if err != nil {
// handle error
}
helper.SetCredentials("cn=admin,dc=example,dc=com", "password")
if err := helper.Connect(); err != nil {
// handle connection/bind error
}
userInfo, err := helper.UserInfo("jdoe")
if err != nil {
// handle user lookup error
}
// ... use userInfo map
helper.Close()
Notes
- The package is thread-safe for most operations.
- Designed for Go 1.18+.
- Logging is optional but recommended for debugging.
- The API and code structure are legacy and may not follow modern Go conventions.