Move LDAP into a seperate file
This commit is contained in:
No files matched your search
+150
@@ -0,0 +1,150 @@
|
|||||||
|
package ldap
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
|
||||||
|
"go.senan.xyz/gonic/db"
|
||||||
|
|
||||||
|
"github.com/go-ldap/ldap/v3"
|
||||||
|
)
|
||||||
|
|
||||||
|
func CheckLDAPcreds(username string, password string, dbc *db.DB) (bool, error) {
|
||||||
|
err := createUserFromLDAP(username, password, dbc)
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
|
||||||
|
ldapFQDN, err := dbc.GetSetting("ldap_fqdn")
|
||||||
|
|
||||||
|
log.Println(err)
|
||||||
|
log.Println(ldapFQDN)
|
||||||
|
|
||||||
|
// This checks if LDAP is setup or not.
|
||||||
|
if ldapFQDN == "" || err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// The configuration page wouldn't allow these setting to not be set
|
||||||
|
// while LDAP is enabled (a FQDN/IP is set).
|
||||||
|
ldapPort, _ := dbc.GetSetting("ldap_port")
|
||||||
|
baseDN, _ := dbc.GetSetting("ldap_base_dn")
|
||||||
|
tls, _ := dbc.GetSetting("ldap_tls")
|
||||||
|
|
||||||
|
// Now, we can try to connect to the LDAP server.
|
||||||
|
l, err := createLDAPconnection(tls, ldapFQDN, ldapPort)
|
||||||
|
if err != nil {
|
||||||
|
// Return a generic error.
|
||||||
|
log.Println("Failed to connect to LDAP server:", err)
|
||||||
|
return false, errors.New("Failed to connect to LDAP server.")
|
||||||
|
}
|
||||||
|
defer l.Close()
|
||||||
|
|
||||||
|
// After we have a connection, let's try binding
|
||||||
|
_, err = l.SimpleBind(&ldap.SimpleBindRequest{
|
||||||
|
Username: fmt.Sprintf("uid=%s,%s", username, baseDN),
|
||||||
|
Password: password,
|
||||||
|
})
|
||||||
|
|
||||||
|
if err == nil {
|
||||||
|
return true, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Println("Failed to bind to LDAP server:", err)
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Creates a user from creds
|
||||||
|
func createUserFromLDAP(username string, password string, dbc *db.DB) error {
|
||||||
|
user := dbc.GetUserByName(username)
|
||||||
|
if user != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
ldapFQDN, err := dbc.GetSetting("ldap_fqdn")
|
||||||
|
|
||||||
|
// This is basically checking if LDAP is setup, if ldapFQDN isn't set we can
|
||||||
|
// assume that the user hasn't configured LDAP.
|
||||||
|
if ldapFQDN == "" {
|
||||||
|
return nil
|
||||||
|
} else if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// The configuration page wouldn't allow these setting to not be set
|
||||||
|
// while LDAP is enabled (a FQDN/IP is set).
|
||||||
|
bindUID, _ := dbc.GetSetting("ldap_bind_user")
|
||||||
|
bindPWD, _ := dbc.GetSetting("ldap_bind_user_password")
|
||||||
|
ldapPort, _ := dbc.GetSetting("ldap_port")
|
||||||
|
baseDN, _ := dbc.GetSetting("ldap_base_dn")
|
||||||
|
filter, _ := dbc.GetSetting("ldap_filter")
|
||||||
|
tls, _ := dbc.GetSetting("ldap_tls")
|
||||||
|
|
||||||
|
// Now, we can try to connect to the LDAP server.
|
||||||
|
l, err := createLDAPconnection(tls, ldapFQDN, ldapPort)
|
||||||
|
if err != nil {
|
||||||
|
// Return a generic error.
|
||||||
|
return errors.New("Failed to connect to LDAP server.")
|
||||||
|
}
|
||||||
|
defer l.Close()
|
||||||
|
|
||||||
|
// After we have a connection, let's try binding
|
||||||
|
_, err = l.SimpleBind(&ldap.SimpleBindRequest{
|
||||||
|
Username: fmt.Sprintf("uid=%s,%s", bindUID, baseDN),
|
||||||
|
Password: bindPWD,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
log.Println("Failed to bind to LDAP:", err)
|
||||||
|
return errors.New("Wrong username or password.")
|
||||||
|
}
|
||||||
|
|
||||||
|
searchReq := ldap.NewSearchRequest(
|
||||||
|
baseDN,
|
||||||
|
ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, 0, false,
|
||||||
|
fmt.Sprintf("(&%s(uid=%s))", filter, ldap.EscapeFilter(username)),
|
||||||
|
[]string{"dn"},
|
||||||
|
nil,
|
||||||
|
)
|
||||||
|
|
||||||
|
result, err := l.Search(searchReq)
|
||||||
|
if err != nil {
|
||||||
|
log.Println("failed to query LDAP server:", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(result.Entries) > 1 {
|
||||||
|
return errors.New("Ambiguous user")
|
||||||
|
} else if len(result.Entries) == 1 {
|
||||||
|
user := db.User{
|
||||||
|
Name: username,
|
||||||
|
Password: "", // no password because we want auth to fail.
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := dbc.Create(&user).Error; err != nil {
|
||||||
|
log.Println("User created via LDAP:", username)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
} else {
|
||||||
|
return errors.New("Invalid username")
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// Creates a connection to an LDAP server.
|
||||||
|
func createLDAPconnection(tls string, fqdn string, port string) (*ldap.Conn, error) {
|
||||||
|
protocol := "ldap"
|
||||||
|
if tls == "true" {
|
||||||
|
protocol = "ldaps"
|
||||||
|
}
|
||||||
|
|
||||||
|
// Now, we can try to connect to the LDAP server.
|
||||||
|
l, err := ldap.DialURL(fmt.Sprintf("%s://%s:%s", protocol, fqdn, port))
|
||||||
|
if err != nil {
|
||||||
|
// Warn the server and return a generic error.
|
||||||
|
log.Println("Failed to connect to LDAP server", err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return l, nil
|
||||||
|
}
|
||||||
+11
-124
@@ -18,6 +18,7 @@ import (
|
|||||||
"go.senan.xyz/gonic/infocache/artistinfocache"
|
"go.senan.xyz/gonic/infocache/artistinfocache"
|
||||||
"go.senan.xyz/gonic/jukebox"
|
"go.senan.xyz/gonic/jukebox"
|
||||||
"go.senan.xyz/gonic/lastfm"
|
"go.senan.xyz/gonic/lastfm"
|
||||||
|
"go.senan.xyz/gonic/ldap"
|
||||||
"go.senan.xyz/gonic/playlist"
|
"go.senan.xyz/gonic/playlist"
|
||||||
"go.senan.xyz/gonic/podcast"
|
"go.senan.xyz/gonic/podcast"
|
||||||
"go.senan.xyz/gonic/scanner"
|
"go.senan.xyz/gonic/scanner"
|
||||||
@@ -25,8 +26,6 @@ import (
|
|||||||
"go.senan.xyz/gonic/server/ctrlsubsonic/params"
|
"go.senan.xyz/gonic/server/ctrlsubsonic/params"
|
||||||
"go.senan.xyz/gonic/server/ctrlsubsonic/spec"
|
"go.senan.xyz/gonic/server/ctrlsubsonic/spec"
|
||||||
"go.senan.xyz/gonic/transcode"
|
"go.senan.xyz/gonic/transcode"
|
||||||
|
|
||||||
"github.com/go-ldap/ldap/v3"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type CtxKey int
|
type CtxKey int
|
||||||
@@ -243,143 +242,31 @@ func (c *Controller) withUser(next http.Handler) http.Handler {
|
|||||||
}
|
}
|
||||||
user := c.dbc.GetUserByName(username)
|
user := c.dbc.GetUserByName(username)
|
||||||
|
|
||||||
newLDAPUser := false
|
|
||||||
if user == nil {
|
|
||||||
newLDAPUser = true
|
|
||||||
|
|
||||||
// Because the user wasn't found we can now try
|
|
||||||
// to use LDAP.
|
|
||||||
ldapFQDN, err := c.dbc.GetSetting("ldap_fqdn")
|
|
||||||
|
|
||||||
if ldapFQDN != "" && err == nil {
|
|
||||||
// The configuration page wouldn't allow these setting to not be set
|
|
||||||
// while LDAP is enabled (a FQDN/IP is set).
|
|
||||||
bindUID, _ := c.dbc.GetSetting("ldap_bind_user")
|
|
||||||
bindPWD, _ := c.dbc.GetSetting("ldap_bind_user_password")
|
|
||||||
ldapPort, _ := c.dbc.GetSetting("ldap_port")
|
|
||||||
baseDN, _ := c.dbc.GetSetting("ldap_base_dn")
|
|
||||||
filter, _ := c.dbc.GetSetting("ldap_filter")
|
|
||||||
tls, _ := c.dbc.GetSetting("ldap_tls")
|
|
||||||
|
|
||||||
// Now, we can try to connect to the LDAP server.
|
|
||||||
l, err := createLDAPconnection(tls, ldapFQDN, ldapPort)
|
|
||||||
if err != nil {
|
|
||||||
// Return a generic error.
|
|
||||||
_ = writeResp(w, r, spec.NewError(0, "Failed to connect to LDAP server."))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
defer l.Close()
|
|
||||||
|
|
||||||
// After we have a connection, let's try binding
|
|
||||||
_, err = l.SimpleBind(&ldap.SimpleBindRequest{
|
|
||||||
Username: fmt.Sprintf("uid=%s,%s", bindUID, baseDN),
|
|
||||||
Password: bindPWD,
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
log.Println("Failed to bind to LDAP:", err)
|
|
||||||
_ = writeResp(w, r, spec.NewError(40, "Wrong username or password."))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
searchReq := ldap.NewSearchRequest(
|
|
||||||
baseDN,
|
|
||||||
ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, 0, false,
|
|
||||||
fmt.Sprintf("(&%s(uid=%s))", filter, ldap.EscapeFilter(username)),
|
|
||||||
[]string{"dn"},
|
|
||||||
nil,
|
|
||||||
)
|
|
||||||
|
|
||||||
result, err := l.Search(searchReq)
|
|
||||||
if err != nil {
|
|
||||||
log.Println("failed to query LDAP server:", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(result.Entries) > 1 {
|
|
||||||
_ = writeResp(w, r, spec.NewError(40, "Ambiguous user: `%s`", username))
|
|
||||||
} else if len(result.Entries) == 1 {
|
|
||||||
user := db.User{
|
|
||||||
Name: username,
|
|
||||||
Password: "", // no password because we want auth to fail.
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := c.dbc.Create(&user).Error; err != nil {
|
|
||||||
log.Println("User created via LDAP:", username)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
_ = writeResp(w, r, spec.NewError(40, "invalid username `%s`", username))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
_ = writeResp(w, r, spec.NewError(40,
|
|
||||||
"invalid username `%s`", username))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
var credsOk bool
|
var credsOk bool
|
||||||
if tokenAuth && !newLDAPUser && user != nil {
|
if tokenAuth && user != nil {
|
||||||
credsOk = checkCredsToken(user.Password, token, salt)
|
credsOk = checkCredsToken(user.Password, token, salt)
|
||||||
} else if !newLDAPUser && user != nil {
|
} else if user != nil {
|
||||||
credsOk = checkCredsBasic(user.Password, password)
|
credsOk = checkCredsBasic(user.Password, password)
|
||||||
}
|
}
|
||||||
if !credsOk {
|
if !credsOk {
|
||||||
// Because internal authentication failed, we can now try to use LDAP, if
|
// Because internal authentication failed, we can now try to use LDAP, if
|
||||||
// it was enabled by the user.
|
// it was enabled by the user.
|
||||||
ldapFQDN, err := c.dbc.GetSetting("ldap_fqdn")
|
ok, err := ldap.CheckLDAPcreds(username, password, c.dbc)
|
||||||
|
if err != nil {
|
||||||
if ldapFQDN != "" && err == nil {
|
_ = writeResp(w, r, spec.NewError(40, err.Error()))
|
||||||
// The configuration page wouldn't allow these setting to not be set
|
return
|
||||||
// while LDAP is enabled (a FQDN/IP is set).
|
|
||||||
ldapPort, _ := c.dbc.GetSetting("ldap_port")
|
|
||||||
baseDN, _ := c.dbc.GetSetting("ldap_base_dn")
|
|
||||||
tls, _ := c.dbc.GetSetting("ldap_tls")
|
|
||||||
|
|
||||||
// Now, we can try to connect to the LDAP server.
|
|
||||||
l, err := createLDAPconnection(tls, ldapFQDN, ldapPort)
|
|
||||||
if err != nil {
|
|
||||||
// Return a generic error.
|
|
||||||
_ = writeResp(w, r, spec.NewError(0, "Failed to connect to LDAP server."))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
defer l.Close()
|
|
||||||
|
|
||||||
// After we have a connection, let's try binding
|
|
||||||
_, err = l.SimpleBind(&ldap.SimpleBindRequest{
|
|
||||||
Username: fmt.Sprintf("uid=%s,%s", username, baseDN),
|
|
||||||
Password: password,
|
|
||||||
})
|
|
||||||
|
|
||||||
if err == nil {
|
|
||||||
withUser := context.WithValue(r.Context(), CtxUser, user)
|
|
||||||
next.ServeHTTP(w, r.WithContext(withUser))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
_ = writeResp(w, r, spec.NewError(40, "invalid password"))
|
if !ok {
|
||||||
return
|
_ = writeResp(w, r, spec.NewError(40, "invalid password"))
|
||||||
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
withUser := context.WithValue(r.Context(), CtxUser, user)
|
withUser := context.WithValue(r.Context(), CtxUser, user)
|
||||||
next.ServeHTTP(w, r.WithContext(withUser))
|
next.ServeHTTP(w, r.WithContext(withUser))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func createLDAPconnection(tls string, fqdn string, port string) (*ldap.Conn, error) {
|
|
||||||
protocol := "ldap"
|
|
||||||
if tls == "true" {
|
|
||||||
protocol = "ldaps"
|
|
||||||
}
|
|
||||||
|
|
||||||
// Now, we can try to connect to the LDAP server.
|
|
||||||
l, err := ldap.DialURL(fmt.Sprintf("%s://%s:%s", protocol, fqdn, port))
|
|
||||||
if err != nil {
|
|
||||||
// Warn the server and return a generic error.
|
|
||||||
log.Println("Failed to connect to LDAP server", err)
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return l, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func slow(next http.Handler) http.Handler {
|
func slow(next http.Handler) http.Handler {
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
rc := http.NewResponseController(w) //nolint:bodyclose
|
rc := http.NewResponseController(w) //nolint:bodyclose
|
||||||
|
|||||||
Reference in New Issue
Block a user