func (c *Client) dial() (*ldap.Conn, error) {
	address := fmt.Sprintf("%s:%d", c.LdapServer, c.LdapPort)
	if c.TLSConfig != nil {
		return ldap.DialTLS("tcp", address, c.TLSConfig)
	}
	if c.TLSConfig == nil && c.AllowInsecure {
		return ldap.Dial("tcp", address)
	}
	return nil, errors.New("The LDAP TLS Configuration was not set.")
}
Create a new TCP connection to the LDAP server
package ldap
import (
	"crypto/tls"
	"errors"
	"fmt"
	"github.com/go-ldap/ldap"
)
type Authenticator interface {
	Authenticate(username, password string) (*ldap.Entry, error)
}
type Client struct {
	BaseDN             string
	LdapServer         string
	LdapPort           uint
	AllowInsecure      bool
	UserLoginAttribute string
	SearchUserDN       string
	SearchUserPassword string
	TLSConfig          *tls.Config
}
func (c *Client) Authenticate(username, password string) (*ldap.Entry, error) {
	conn, err := c.dial()
	if err != nil {
		return nil, fmt.Errorf("Error opening LDAP connection: %v", err)
	}
	defer conn.Close()
	if c.SearchUserDN != "" && c.SearchUserPassword != "" {
		err = conn.Bind(c.SearchUserDN, c.SearchUserPassword)
	} else {
		err = conn.Bind(username, password)
	}
	if err != nil {
		return nil, fmt.Errorf("Error binding user to LDAP server: %v", err)
	}
	req := c.newUserSearchRequest(username)
	res, err := conn.Search(req)
	if err != nil {
		return nil, fmt.Errorf("Error searching for user %s: %v", username, err)
	}
	switch {
	case len(res.Entries) == 0:
		return nil, fmt.Errorf("No result for the search filter '%s'", req.Filter)
	case len(res.Entries) > 1:
		return nil, fmt.Errorf("Multiple entries found for the search filter '%s': %+v", req.Filter, res.Entries)
	}
	if c.SearchUserDN != "" && c.SearchUserPassword != "" {
		err = conn.Bind(res.Entries[0].DN, password)
		if err != nil {
			return nil, fmt.Errorf("Error binding user %s, invalid credentials: %v", username, err)
		}
	}
	return res.Entries[0], nil
}