2019-12-06 15:15:54 +07:00
|
|
|
package authentication
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
|
|
|
|
2020-03-19 11:22:46 +07:00
|
|
|
"github.com/go-ldap/ldap/v3"
|
2019-12-06 15:15:54 +07:00
|
|
|
)
|
|
|
|
|
2020-05-02 12:06:39 +07:00
|
|
|
// ********************* CONNECTION *********************.
|
2019-12-06 15:15:54 +07: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
|
2021-07-06 16:13:17 +07:00
|
|
|
PasswordModify(pwdModifyRequest *ldap.PasswordModifyRequest) error
|
2020-12-03 12:23:52 +07:00
|
|
|
StartTLS(config *tls.Config) error
|
2019-12-06 15:15:54 +07:00
|
|
|
}
|
|
|
|
|
2020-04-21 04:03:38 +07:00
|
|
|
// LDAPConnectionImpl the production implementation of an ldap connection.
|
2019-12-06 15:15:54 +07:00
|
|
|
type LDAPConnectionImpl struct {
|
|
|
|
conn *ldap.Conn
|
|
|
|
}
|
|
|
|
|
2020-04-21 04:03:38 +07:00
|
|
|
// NewLDAPConnectionImpl create a new ldap connection.
|
2019-12-06 15:15:54 +07:00
|
|
|
func NewLDAPConnectionImpl(conn *ldap.Conn) *LDAPConnectionImpl {
|
|
|
|
return &LDAPConnectionImpl{conn}
|
|
|
|
}
|
|
|
|
|
2020-04-21 04:03:38 +07:00
|
|
|
// Bind binds ldap connection to a username/password.
|
2019-12-06 15:15:54 +07:00
|
|
|
func (lc *LDAPConnectionImpl) Bind(username, password string) error {
|
|
|
|
return lc.conn.Bind(username, password)
|
|
|
|
}
|
|
|
|
|
2020-04-21 04:03:38 +07:00
|
|
|
// Close closes a ldap connection.
|
2019-12-06 15:15:54 +07:00
|
|
|
func (lc *LDAPConnectionImpl) Close() {
|
|
|
|
lc.conn.Close()
|
|
|
|
}
|
|
|
|
|
2020-04-21 04:03:38 +07:00
|
|
|
// Search searches a ldap server.
|
2019-12-06 15:15:54 +07:00
|
|
|
func (lc *LDAPConnectionImpl) Search(searchRequest *ldap.SearchRequest) (*ldap.SearchResult, error) {
|
|
|
|
return lc.conn.Search(searchRequest)
|
|
|
|
}
|
|
|
|
|
2020-04-21 04:03:38 +07:00
|
|
|
// Modify modifies an ldap object.
|
2019-12-06 15:15:54 +07:00
|
|
|
func (lc *LDAPConnectionImpl) Modify(modifyRequest *ldap.ModifyRequest) error {
|
|
|
|
return lc.conn.Modify(modifyRequest)
|
|
|
|
}
|
|
|
|
|
2021-07-06 16:13:17 +07:00
|
|
|
// PasswordModify modifies an ldap objects password.
|
|
|
|
func (lc *LDAPConnectionImpl) PasswordModify(pwdModifyRequest *ldap.PasswordModifyRequest) error {
|
|
|
|
_, err := lc.conn.PasswordModify(pwdModifyRequest)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-12-03 12:23:52 +07:00
|
|
|
// StartTLS requests the LDAP server upgrades to TLS encryption.
|
|
|
|
func (lc *LDAPConnectionImpl) StartTLS(config *tls.Config) error {
|
|
|
|
return lc.conn.StartTLS(config)
|
|
|
|
}
|
|
|
|
|
2020-05-02 12:06:39 +07:00
|
|
|
// ********************* FACTORY ***********************.
|
2019-12-06 15:15:54 +07:00
|
|
|
|
2020-04-21 04:03:38 +07:00
|
|
|
// LDAPConnectionFactory an interface of factory of ldap connections.
|
2019-12-06 15:15:54 +07:00
|
|
|
type LDAPConnectionFactory interface {
|
2021-08-05 11:30:00 +07:00
|
|
|
DialURL(addr string, opts ...ldap.DialOpt) (LDAPConnection, error)
|
2019-12-06 15:15:54 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
// LDAPConnectionFactoryImpl the production implementation of an ldap connection factory.
|
|
|
|
type LDAPConnectionFactoryImpl struct{}
|
|
|
|
|
2020-04-21 04:03:38 +07:00
|
|
|
// NewLDAPConnectionFactoryImpl create a concrete ldap connection factory.
|
2019-12-06 15:15:54 +07:00
|
|
|
func NewLDAPConnectionFactoryImpl() *LDAPConnectionFactoryImpl {
|
|
|
|
return &LDAPConnectionFactoryImpl{}
|
|
|
|
}
|
|
|
|
|
2021-01-04 17:28:55 +07:00
|
|
|
// DialURL creates a connection from an LDAP URL when successful.
|
2021-08-05 11:30:00 +07:00
|
|
|
func (lcf *LDAPConnectionFactoryImpl) DialURL(addr string, opts ...ldap.DialOpt) (LDAPConnection, error) {
|
|
|
|
conn, err := ldap.DialURL(addr, opts...)
|
2019-12-06 15:15:54 +07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-06 02:35:32 +07:00
|
|
|
|
2019-12-06 15:15:54 +07:00
|
|
|
return NewLDAPConnectionImpl(conn), nil
|
|
|
|
}
|