mirror of
				https://github.com/h44z/wg-portal.git
				synced 2025-11-04 08:06:18 +00:00 
			
		
		
		
	Feat/ldap certificate connexion (#92)
* Give the way to connect against LDAP server with certificate and key * fix(ldap) Update cert variable name In order to be more explicit Co-authored-by: Alexis Aurin <alexis@so6.pw>
This commit is contained in:
		@@ -4,7 +4,6 @@ import (
 | 
			
		||||
	gldap "github.com/go-ldap/ldap/v3"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
type Type string
 | 
			
		||||
 | 
			
		||||
const (
 | 
			
		||||
@@ -26,8 +25,11 @@ type Config struct {
 | 
			
		||||
	PhoneAttribute       string `yaml:"attrPhone" envconfig:"LDAP_ATTR_PHONE"`
 | 
			
		||||
	GroupMemberAttribute string `yaml:"attrGroups" envconfig:"LDAP_ATTR_GROUPS"`
 | 
			
		||||
 | 
			
		||||
	LoginFilter    string `yaml:"loginFilter" envconfig:"LDAP_LOGIN_FILTER"` // {{login_identifier}} gets replaced with the login email address
 | 
			
		||||
	SyncFilter     string `yaml:"syncFilter" envconfig:"LDAP_SYNC_FILTER"`
 | 
			
		||||
	AdminLdapGroup string `yaml:"adminGroup" envconfig:"LDAP_ADMIN_GROUP"` // Members of this group receive admin rights in WG-Portal
 | 
			
		||||
	LoginFilter     string    `yaml:"loginFilter" envconfig:"LDAP_LOGIN_FILTER"` // {{login_identifier}} gets replaced with the login email address
 | 
			
		||||
	SyncFilter      string    `yaml:"syncFilter" envconfig:"LDAP_SYNC_FILTER"`
 | 
			
		||||
	AdminLdapGroup  string    `yaml:"adminGroup" envconfig:"LDAP_ADMIN_GROUP"` // Members of this group receive admin rights in WG-Portal
 | 
			
		||||
	AdminLdapGroup_ *gldap.DN `yaml:"-"`
 | 
			
		||||
	LdapCertConn    bool      `yaml:"ldapCertConn" envconfig:"LDAP_CERT_CONN"`
 | 
			
		||||
	LdapTlsCert     string    `yaml:"ldapTlsCert" envconfig:"LDAPTLS_CERT"`
 | 
			
		||||
	LdapTlsKey      string    `yaml:"ldapTlsKey" envconfig:"LDAPTLS_KEY"`
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -2,6 +2,7 @@ package ldap
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"crypto/tls"
 | 
			
		||||
	"io/ioutil"
 | 
			
		||||
 | 
			
		||||
	"github.com/go-ldap/ldap/v3"
 | 
			
		||||
	"github.com/pkg/errors"
 | 
			
		||||
@@ -14,7 +15,33 @@ type RawLdapData struct {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func Open(cfg *Config) (*ldap.Conn, error) {
 | 
			
		||||
	tlsConfig := &tls.Config{InsecureSkipVerify: !cfg.CertValidation}
 | 
			
		||||
	var tlsConfig *tls.Config
 | 
			
		||||
 | 
			
		||||
	if cfg.LdapCertConn {
 | 
			
		||||
 | 
			
		||||
		cert_plain, err := ioutil.ReadFile(cfg.LdapTlsCert)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return nil, errors.WithMessage(err, "failed to load the certificate")
 | 
			
		||||
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		key, err := ioutil.ReadFile(cfg.LdapTlsKey)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return nil, errors.WithMessage(err, "failed to load the key")
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		cert_x509, err := tls.X509KeyPair(cert_plain, key)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return nil, errors.WithMessage(err, "failed X509")
 | 
			
		||||
 | 
			
		||||
		}
 | 
			
		||||
		tlsConfig = &tls.Config{Certificates: []tls.Certificate{cert_x509}}
 | 
			
		||||
 | 
			
		||||
	} else {
 | 
			
		||||
 | 
			
		||||
		tlsConfig = &tls.Config{InsecureSkipVerify: !cfg.CertValidation}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	conn, err := ldap.DialURL(cfg.URL, ldap.DialWithTLSConfig(tlsConfig))
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, errors.Wrap(err, "failed to connect to LDAP")
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user