1
0
mirror of https://gitlab.crans.org/nounous/ghostream.git synced 2025-06-27 21:32:15 +02:00

Use NewLDAP to instanciate LDAP backend

This commit is contained in:
Alexandre Iooss
2020-09-22 14:16:52 +02:00
parent 07c8dc6ca1
commit c1de814a2a
3 changed files with 30 additions and 11 deletions

View File

@ -12,21 +12,16 @@ type Options struct {
// LDAP authentification backend
type LDAP struct {
Cfg Options
Cfg *Options
Conn *ldap.Conn
}
// Login tries to bind to LDAP
// Returns (true, nil) if success
func (a LDAP) Login(username string, password string) (bool, error) {
// Connect to LDAP server
l, err := ldap.DialURL(a.Cfg.URI)
if err != nil {
return false, err
}
defer l.Close()
// Try to bind as user
err = l.Bind("cn=username,dc=example,dc=com", password)
bindDn := "cn=" + username + "," + a.Cfg.UserDn
err := a.Conn.Bind(bindDn, password)
if err != nil {
return false, err
}
@ -34,3 +29,18 @@ func (a LDAP) Login(username string, password string) (bool, error) {
// Login succeeded
return true, nil
}
// Close LDAP connection
func (a LDAP) Close() {
a.Conn.Close()
}
// NewLDAP instanciate a new LDAP connection
func NewLDAP(cfg *Options) (LDAP, error) {
backend := LDAP{Cfg: cfg}
// Connect to LDAP server
c, err := ldap.DialURL(backend.Cfg.URI)
backend.Conn = c
return backend, err
}