2020-09-22 09:42:57 +00:00
|
|
|
package ldap
|
|
|
|
|
2020-09-22 10:54:12 +00:00
|
|
|
import (
|
|
|
|
"github.com/go-ldap/ldap/v3"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Options holds package configuration
|
2020-09-22 09:42:57 +00:00
|
|
|
type Options struct {
|
|
|
|
URI string
|
|
|
|
UserDn string
|
|
|
|
}
|
2020-09-22 10:54:12 +00:00
|
|
|
|
|
|
|
// LDAP authentification backend
|
|
|
|
type LDAP struct {
|
2020-09-22 12:16:52 +00:00
|
|
|
Cfg *Options
|
|
|
|
Conn *ldap.Conn
|
2020-09-22 10:54:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Login tries to bind to LDAP
|
|
|
|
// Returns (true, nil) if success
|
|
|
|
func (a LDAP) Login(username string, password string) (bool, error) {
|
|
|
|
// Try to bind as user
|
2020-09-22 12:16:52 +00:00
|
|
|
bindDn := "cn=" + username + "," + a.Cfg.UserDn
|
|
|
|
err := a.Conn.Bind(bindDn, password)
|
2020-09-22 10:54:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Login succeeded
|
|
|
|
return true, nil
|
|
|
|
}
|
2020-09-22 12:16:52 +00:00
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|