Skip to content

Commit

Permalink
feat: support LDAPS protocol (casdoor#3390)
Browse files Browse the repository at this point in the history
* feat: support ldaps

* fix: unencrypted port 389 not work after enable SSL
fix: remove useless conf and set ldapsCertId to empty
fix: return and log getTLSconfig error

* fix: remove unused setting

* fix: check nil condition

* fix: not log fail when certId is empty
  • Loading branch information
dacongda authored Dec 7, 2024
1 parent 922b19c commit 58e1c28
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 5 deletions.
2 changes: 2 additions & 0 deletions conf/app.conf
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ enableErrorMask = false
enableGzip = true
inactiveTimeoutMinutes =
ldapServerPort = 389
ldapsCertId = ""
ldapsServerPort = 636
radiusServerPort = 1812
radiusSecret = "secret"
quota = {"organization": -1, "user": -1, "application": -1, "provider": -1}
Expand Down
58 changes: 53 additions & 5 deletions ldap/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package ldap

import (
"crypto/tls"
"fmt"
"hash/fnv"
"log"
Expand All @@ -27,21 +28,68 @@ import (

func StartLdapServer() {
ldapServerPort := conf.GetConfigString("ldapServerPort")
if ldapServerPort == "" || ldapServerPort == "0" {
return
}
ldapsServerPort := conf.GetConfigString("ldapsServerPort")

server := ldap.NewServer()
serverSsl := ldap.NewServer()
routes := ldap.NewRouteMux()

routes.Bind(handleBind)
routes.Search(handleSearch).Label(" SEARCH****")

server.Handle(routes)
err := server.ListenAndServe("0.0.0.0:" + ldapServerPort)
serverSsl.Handle(routes)
go func() {
if ldapServerPort == "" || ldapServerPort == "0" {
return
}
err := server.ListenAndServe("0.0.0.0:" + ldapServerPort)
if err != nil {
log.Printf("StartLdapServer() failed, err = %s", err.Error())
}
}()

go func() {
if ldapsServerPort == "" || ldapsServerPort == "0" {
return
}
ldapsCertId := conf.GetConfigString("ldapsCertId")
if ldapsCertId == "" {
return
}
config, err := getTLSconfig(ldapsCertId)
if err != nil {
log.Printf("StartLdapsServer() failed, err = %s", err.Error())
return
}
secureConn := func(s *ldap.Server) {
s.Listener = tls.NewListener(s.Listener, config)
}
err = serverSsl.ListenAndServe("0.0.0.0:"+ldapsServerPort, secureConn)
if err != nil {
log.Printf("StartLdapsServer() failed, err = %s", err.Error())
}
}()
}

func getTLSconfig(ldapsCertId string) (*tls.Config, error) {
rawCert, err := object.GetCert(ldapsCertId)
if err != nil {
log.Printf("StartLdapServer() failed, err = %s", err.Error())
return nil, err
}
if rawCert == nil {
return nil, fmt.Errorf("cert is empty")
}
cert, err := tls.X509KeyPair([]byte(rawCert.Certificate), []byte(rawCert.PrivateKey))
if err != nil {
return &tls.Config{}, err
}

return &tls.Config{
MinVersion: tls.VersionTLS10,
MaxVersion: tls.VersionTLS13,
Certificates: []tls.Certificate{cert},
}, nil
}

func handleBind(w ldap.ResponseWriter, m *ldap.Message) {
Expand Down

0 comments on commit 58e1c28

Please sign in to comment.