Documentation
¶
Overview ¶
Package ldap provides a simple LDAP client to authenticate users, retrieve basic information and groups.
Index ¶
- type Client
- func (lc *Client) Authenticate(username, password string) (ok bool, user map[string][]string, err error)
- func (lc *Client) AuthenticateContext(ctx context.Context, username, password string) (ok bool, user map[string][]string, err error)
- func (lc *Client) Close()
- func (lc *Client) Connect() error
- func (lc *Client) ConnectContext(ctx context.Context) error
- func (lc *Client) GetGroupsOfUser(username string) ([]string, error)
- func (lc *Client) GetGroupsOfUserContext(ctx context.Context, username string) ([]string, error)
- type Conn
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Client ¶
type Client struct {
// Attributes is a list of attributes to retrieve for the user.
Attributes []string
// Base is the base DN to search for users.
Base string
// BindDN is the DN of the user to use for the initial search.
BindDN string
// BindPassword is the password of the BindDN user.
BindPassword string
// GroupFilter is the LDAP filter to use for retrieving groups (e.g. "(memberUid=%s)").
GroupFilter string
// Host is the LDAP host to connect to (legacy single host).
Host string
// Hosts is a list of LDAP hosts to connect to for failover support.
Hosts []string
// ServerName is the server name to use for TLS verification.
ServerName string
// UserFilter is the LDAP filter to use for searching for users (e.g. "(uid=%s)").
UserFilter string
// Conn is the underlying LDAP connection.
Conn Conn
// Port is the LDAP port to connect to.
Port int
// InsecureSkipVerify allows skipping TLS verification (not recommended for production).
InsecureSkipVerify bool
// UseSSL enables LDAPS connection.
UseSSL bool
// SkipTLS disables StartTLS when using non-SSL connection.
SkipTLS bool
// ClientCertificates provides client-side certificates for MTLS.
ClientCertificates []tls.Certificate
// TLSConfig provides a custom TLS configuration. If set, it overrides other TLS settings.
TLSConfig *tls.Config
}
Client represents the configuration for the LDAP client.
func (*Client) Authenticate ¶
func (lc *Client) Authenticate(username, password string) (ok bool, user map[string][]string, err error)
Authenticate authenticates the user against the ldap backend.
Example ¶
ExampleClient_Authenticate shows how a typical application can verify a login attempt
client := &ldap.Client{
Base: "dc=example,dc=com",
Host: "ldap.example.com",
Port: 389,
UseSSL: false,
BindDN: "uid=readonlysuer,ou=People,dc=example,dc=com",
BindPassword: "readonlypassword",
UserFilter: "(uid=%s)",
GroupFilter: "(memberUid=%s)",
Attributes: []string{"givenName", "sn", "mail", "uid"},
}
defer client.Close()
ok, user, err := client.Authenticate("username", "password")
if err != nil {
log.Printf("Error authenticating user %s: %+v", "username", err)
return
}
if !ok {
log.Printf("Authenticating failed for user %s", "username")
return
}
log.Printf("User: %+v", user)
func (*Client) AuthenticateContext ¶
func (lc *Client) AuthenticateContext(ctx context.Context, username, password string) (ok bool, user map[string][]string, err error)
AuthenticateContext authenticates the user against the ldap backend with context.
Example ¶
ExampleClient_AuthenticateContext shows how to use the context-aware Authenticate method
client := &ldap.Client{
Base: "dc=example,dc=com",
Host: "ldap.example.com",
Port: 389,
UseSSL: false,
BindDN: "uid=readonlysuer,ou=People,dc=example,dc=com",
BindPassword: "readonlypassword",
UserFilter: "(uid=%s)",
GroupFilter: "(memberUid=%s)",
Attributes: []string{"givenName", "sn", "mail", "uid"},
}
defer client.Close()
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
ok, user, err := client.AuthenticateContext(ctx, "username", "password")
if err != nil {
log.Printf("Error authenticating user %s: %+v", "username", err)
return
}
if !ok {
log.Printf("Authenticating failed for user %s", "username")
return
}
log.Printf("User: %+v", user)
func (*Client) ConnectContext ¶
ConnectContext connects to the ldap backend with context.
func (*Client) GetGroupsOfUser ¶
GetGroupsOfUser returns the group for a user.
Example ¶
ExampleClient_GetGroupsOfUser shows how to retrieve user groups
client := &ldap.Client{
Base: "dc=example,dc=com",
Host: "ldap.example.com",
Port: 389,
GroupFilter: "(memberUid=%s)",
}
defer client.Close()
groups, err := client.GetGroupsOfUser("username")
if err != nil {
log.Printf("Error getting groups for user %s: %+v", "username", err)
return
}
log.Printf("Groups: %+v", groups)