mirror of
				https://gitlab.crans.org/nounous/ghostream.git
				synced 2025-11-04 15:42:26 +01:00 
			
		
		
		
	Use NewLDAP to instanciate LDAP backend
This commit is contained in:
		@@ -15,14 +15,19 @@ type Options struct {
 | 
			
		||||
// Backend to log user in
 | 
			
		||||
type Backend interface {
 | 
			
		||||
	Login(string, string) (bool, error)
 | 
			
		||||
	Close()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// New initialize authentification backend
 | 
			
		||||
func New(cfg *Options) (Backend, error) {
 | 
			
		||||
	var backend Backend
 | 
			
		||||
	var err error
 | 
			
		||||
 | 
			
		||||
	if cfg.Backend == "LDAP" {
 | 
			
		||||
		backend = ldap.LDAP{Cfg: cfg.LDAP}
 | 
			
		||||
		backend, err = ldap.NewLDAP(&cfg.LDAP)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return nil, err
 | 
			
		||||
		}
 | 
			
		||||
	} else {
 | 
			
		||||
		// Package is misconfigured
 | 
			
		||||
		return nil, errors.New("Authentification backend not found")
 | 
			
		||||
 
 | 
			
		||||
@@ -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
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user