mirror of
https://gitlab.crans.org/nounous/ghostream.git
synced 2024-12-22 13:52:19 +00:00
Use NewLDAP to instanciate LDAP backend
This commit is contained in:
parent
07c8dc6ca1
commit
c1de814a2a
@ -15,14 +15,19 @@ type Options struct {
|
|||||||
// Backend to log user in
|
// Backend to log user in
|
||||||
type Backend interface {
|
type Backend interface {
|
||||||
Login(string, string) (bool, error)
|
Login(string, string) (bool, error)
|
||||||
|
Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
// New initialize authentification backend
|
// New initialize authentification backend
|
||||||
func New(cfg *Options) (Backend, error) {
|
func New(cfg *Options) (Backend, error) {
|
||||||
var backend Backend
|
var backend Backend
|
||||||
|
var err error
|
||||||
|
|
||||||
if cfg.Backend == "LDAP" {
|
if cfg.Backend == "LDAP" {
|
||||||
backend = ldap.LDAP{Cfg: cfg.LDAP}
|
backend, err = ldap.NewLDAP(&cfg.LDAP)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
// Package is misconfigured
|
// Package is misconfigured
|
||||||
return nil, errors.New("Authentification backend not found")
|
return nil, errors.New("Authentification backend not found")
|
||||||
|
@ -12,21 +12,16 @@ type Options struct {
|
|||||||
|
|
||||||
// LDAP authentification backend
|
// LDAP authentification backend
|
||||||
type LDAP struct {
|
type LDAP struct {
|
||||||
Cfg Options
|
Cfg *Options
|
||||||
|
Conn *ldap.Conn
|
||||||
}
|
}
|
||||||
|
|
||||||
// Login tries to bind to LDAP
|
// Login tries to bind to LDAP
|
||||||
// Returns (true, nil) if success
|
// Returns (true, nil) if success
|
||||||
func (a LDAP) Login(username string, password string) (bool, error) {
|
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
|
// 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 {
|
if err != nil {
|
||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
@ -34,3 +29,18 @@ func (a LDAP) Login(username string, password string) (bool, error) {
|
|||||||
// Login succeeded
|
// Login succeeded
|
||||||
return true, nil
|
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
|
||||||
|
}
|
||||||
|
6
main.go
6
main.go
@ -62,7 +62,11 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Init authentification
|
// Init authentification
|
||||||
//authBackend := auth.New(&cfg.Auth)
|
authBackend, err := auth.New(&cfg.Auth)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalln("Failed to load authentification backend:", err)
|
||||||
|
}
|
||||||
|
defer authBackend.Close()
|
||||||
|
|
||||||
// Start web server routine
|
// Start web server routine
|
||||||
go func() {
|
go func() {
|
||||||
|
Loading…
Reference in New Issue
Block a user