mirror of
				https://gitlab.crans.org/nounous/ghostream.git
				synced 2025-11-04 15:42:26 +01:00 
			
		
		
		
	LDAP authentification backend
This commit is contained in:
		
							
								
								
									
										24
									
								
								auth/auth.go
									
									
									
									
									
								
							
							
						
						
									
										24
									
								
								auth/auth.go
									
									
									
									
									
								
							@@ -1,11 +1,33 @@
 | 
			
		||||
package auth
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"errors"
 | 
			
		||||
 | 
			
		||||
	"gitlab.crans.org/nounous/ghostream/auth/ldap"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// Options holds web package configuration
 | 
			
		||||
// Options holds package configuration
 | 
			
		||||
type Options struct {
 | 
			
		||||
	Backend string
 | 
			
		||||
	LDAP    ldap.Options
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Backend to log user in
 | 
			
		||||
type Backend interface {
 | 
			
		||||
	Login(string, string) (bool, error)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// New initialize authentification backend
 | 
			
		||||
func New(cfg *Options) (Backend, error) {
 | 
			
		||||
	var backend Backend
 | 
			
		||||
 | 
			
		||||
	if cfg.Backend == "LDAP" {
 | 
			
		||||
		backend = ldap.LDAP{Cfg: cfg.LDAP}
 | 
			
		||||
	} else {
 | 
			
		||||
		// Package is misconfigured
 | 
			
		||||
		return nil, errors.New("Authentification backend not found")
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// Init and return backend
 | 
			
		||||
	return backend, nil
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -1,7 +1,36 @@
 | 
			
		||||
package ldap
 | 
			
		||||
 | 
			
		||||
// Options holds web package configuration
 | 
			
		||||
import (
 | 
			
		||||
	"github.com/go-ldap/ldap/v3"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// Options holds package configuration
 | 
			
		||||
type Options struct {
 | 
			
		||||
	URI    string
 | 
			
		||||
	UserDn string
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// LDAP authentification backend
 | 
			
		||||
type LDAP struct {
 | 
			
		||||
	Cfg Options
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// 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)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return false, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// Login succeeded
 | 
			
		||||
	return true, nil
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user