mirror of
https://github.com/nabbar/golib.git
synced 2025-09-26 20:01:15 +08:00

- Add some README file to give missing documentations or update existing documentation file Package Archive: - Add some comments to godoc information - Moving NopWriterCloser interface to ioutils package Package IOUtils: - New package NopWriterCloser to implement interfac like NopReader Package Database: - KVMap: fix missing function following update of kvdriver Package Duration: - Rename BDD testing Package Context/Gin: - Moving function New between model & interface file Package AWS: - rework Walk function to use more generic with standard walk caller function - func walk will now no more return and include error (can be catched into the given func) - func walk will now return a bool to continue or stop the loop - func walk with many input function will now stop when all given function return false - func walk will now return error only about main process and not given function Package errors: - Add interface error into interface Error Package IOUtils: - Moving IOWrapper as subPackage and optimize process + allow thread safe
3.6 KiB
3.6 KiB
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.