2019-12-06 08:15:54 +00:00
|
|
|
package authentication
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
|
|
|
|
2020-03-19 04:22:46 +00:00
|
|
|
"github.com/go-ldap/ldap/v3"
|
2019-12-06 08:15:54 +00:00
|
|
|
)
|
|
|
|
|
2020-05-02 05:06:39 +00:00
|
|
|
// ********************* CONNECTION *********************.
|
2019-12-06 08:15:54 +00:00
|
|
|
|
|
|
|
// LDAPConnection interface representing a connection to the ldap.
|
|
|
|
type LDAPConnection interface {
|
|
|
|
Bind(username, password string) error
|
|
|
|
Close()
|
|
|
|
|
|
|
|
Search(searchRequest *ldap.SearchRequest) (*ldap.SearchResult, error)
|
|
|
|
Modify(modifyRequest *ldap.ModifyRequest) error
|
|
|
|
}
|
|
|
|
|
2020-04-20 21:03:38 +00:00
|
|
|
// LDAPConnectionImpl the production implementation of an ldap connection.
|
2019-12-06 08:15:54 +00:00
|
|
|
type LDAPConnectionImpl struct {
|
|
|
|
conn *ldap.Conn
|
|
|
|
}
|
|
|
|
|
2020-04-20 21:03:38 +00:00
|
|
|
// NewLDAPConnectionImpl create a new ldap connection.
|
2019-12-06 08:15:54 +00:00
|
|
|
func NewLDAPConnectionImpl(conn *ldap.Conn) *LDAPConnectionImpl {
|
|
|
|
return &LDAPConnectionImpl{conn}
|
|
|
|
}
|
|
|
|
|
2020-04-20 21:03:38 +00:00
|
|
|
// Bind binds ldap connection to a username/password.
|
2019-12-06 08:15:54 +00:00
|
|
|
func (lc *LDAPConnectionImpl) Bind(username, password string) error {
|
|
|
|
return lc.conn.Bind(username, password)
|
|
|
|
}
|
|
|
|
|
2020-04-20 21:03:38 +00:00
|
|
|
// Close closes a ldap connection.
|
2019-12-06 08:15:54 +00:00
|
|
|
func (lc *LDAPConnectionImpl) Close() {
|
|
|
|
lc.conn.Close()
|
|
|
|
}
|
|
|
|
|
2020-04-20 21:03:38 +00:00
|
|
|
// Search searches a ldap server.
|
2019-12-06 08:15:54 +00:00
|
|
|
func (lc *LDAPConnectionImpl) Search(searchRequest *ldap.SearchRequest) (*ldap.SearchResult, error) {
|
|
|
|
return lc.conn.Search(searchRequest)
|
|
|
|
}
|
|
|
|
|
2020-04-20 21:03:38 +00:00
|
|
|
// Modify modifies an ldap object.
|
2019-12-06 08:15:54 +00:00
|
|
|
func (lc *LDAPConnectionImpl) Modify(modifyRequest *ldap.ModifyRequest) error {
|
|
|
|
return lc.conn.Modify(modifyRequest)
|
|
|
|
}
|
|
|
|
|
2020-05-02 05:06:39 +00:00
|
|
|
// ********************* FACTORY ***********************.
|
2019-12-06 08:15:54 +00:00
|
|
|
|
2020-04-20 21:03:38 +00:00
|
|
|
// LDAPConnectionFactory an interface of factory of ldap connections.
|
2019-12-06 08:15:54 +00:00
|
|
|
type LDAPConnectionFactory interface {
|
|
|
|
DialTLS(network, addr string, config *tls.Config) (LDAPConnection, error)
|
|
|
|
Dial(network, addr string) (LDAPConnection, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
// LDAPConnectionFactoryImpl the production implementation of an ldap connection factory.
|
|
|
|
type LDAPConnectionFactoryImpl struct{}
|
|
|
|
|
2020-04-20 21:03:38 +00:00
|
|
|
// NewLDAPConnectionFactoryImpl create a concrete ldap connection factory.
|
2019-12-06 08:15:54 +00:00
|
|
|
func NewLDAPConnectionFactoryImpl() *LDAPConnectionFactoryImpl {
|
|
|
|
return &LDAPConnectionFactoryImpl{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// DialTLS contact ldap server over TLS.
|
|
|
|
func (lcf *LDAPConnectionFactoryImpl) DialTLS(network, addr string, config *tls.Config) (LDAPConnection, error) {
|
|
|
|
conn, err := ldap.DialTLS(network, addr, config)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return NewLDAPConnectionImpl(conn), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Dial contact ldap server over raw tcp.
|
|
|
|
func (lcf *LDAPConnectionFactoryImpl) Dial(network, addr string) (LDAPConnection, error) {
|
|
|
|
conn, err := ldap.Dial(network, addr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return NewLDAPConnectionImpl(conn), nil
|
|
|
|
}
|