Added LDAP user creation
This commit is contained in:
No files matched your search
@@ -55,7 +55,7 @@
|
|||||||
type="text"
|
type="text"
|
||||||
id="filter"
|
id="filter"
|
||||||
name="filter"
|
name="filter"
|
||||||
placeholder="filter"
|
placeholder="group name (filter)"
|
||||||
value={{ default "" .CurrentFilter }}
|
value={{ default "" .CurrentFilter }}
|
||||||
>
|
>
|
||||||
|
|
||||||
|
|||||||
@@ -192,7 +192,7 @@ func (c *Controller) ServeLDAPConfigDo(r *http.Request) *Response {
|
|||||||
if bindUID == "" || bindPSWD == ""|| port == "" || baseDN == "" || filter == "" {
|
if bindUID == "" || bindPSWD == ""|| port == "" || baseDN == "" || filter == "" {
|
||||||
return &Response{
|
return &Response{
|
||||||
redirect: r.Referer(),
|
redirect: r.Referer(),
|
||||||
flashW: []string{"please provide a bind username and password, port, base CN, and filter"},
|
flashW: []string{"please provide a bind username and password, port, base CN, and filter (group name)"},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"log"
|
"log"
|
||||||
|
|
||||||
|
"go.senan.xyz/gonic/db"
|
||||||
"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"
|
||||||
|
|
||||||
@@ -71,16 +72,91 @@ func (c *Controller) WithUser(next http.Handler) http.Handler {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
user := c.DB.GetUserByName(username)
|
user := c.DB.GetUserByName(username)
|
||||||
|
|
||||||
|
newLDAPUser := false
|
||||||
if user == nil {
|
if user == nil {
|
||||||
_ = writeResp(w, r, spec.NewError(40,
|
// Because the user wasn't found we can now try
|
||||||
"invalid username `%s`", username))
|
// to use LDAP. ldapFQDN, err := c.DB.GetSetting("ldap_fqdn")
|
||||||
return
|
ldapFQDN, err := c.DB.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.DB.GetSetting("ldap_bind_user")
|
||||||
|
bindPWD, _ := c.DB.GetSetting("ldap_bind_user_password")
|
||||||
|
ldapPort, _ := c.DB.GetSetting("ldap_port")
|
||||||
|
baseDN, _ := c.DB.GetSetting("ldap_base_dn")
|
||||||
|
filter, _ := c.DB.GetSetting("ldap_filter")
|
||||||
|
tls, _ := c.DB.GetSetting("ldap_tls")
|
||||||
|
|
||||||
|
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, ldapFQDN, ldapPort))
|
||||||
|
defer l.Close()
|
||||||
|
if err != nil {
|
||||||
|
newLDAPUser = true
|
||||||
|
|
||||||
|
// Warn the server and return a generic error.
|
||||||
|
log.Println("Failed to connect to LDAP server", err)
|
||||||
|
|
||||||
|
_ = writeResp(w, r, spec.NewError(0, "Failed to connect to LDAP server."))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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, "invalid username `%s`", username))
|
||||||
|
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:", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(result.Entries) > 0 {
|
||||||
|
user := db.User{
|
||||||
|
Name: username,
|
||||||
|
Password: "", // no password because we want auth to fail.
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := c.DB.Create(&user).Error; err != nil {
|
||||||
|
fmt.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 {
|
if tokenAuth && newLDAPUser {
|
||||||
credsOk = checkCredsToken(user.Password, token, salt)
|
credsOk = checkCredsToken(user.Password, token, salt)
|
||||||
} else {
|
} else {
|
||||||
credsOk = checkCredsBasic(user.Password, password)
|
if newLDAPUser {
|
||||||
|
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
|
||||||
@@ -90,11 +166,8 @@ func (c *Controller) WithUser(next http.Handler) http.Handler {
|
|||||||
if ldapFQDN != "" && err == nil {
|
if ldapFQDN != "" && err == nil {
|
||||||
// The configuration page wouldn't allow these setting to not be set
|
// The configuration page wouldn't allow these setting to not be set
|
||||||
// while LDAP is enabled (a FQDN/IP is set).
|
// while LDAP is enabled (a FQDN/IP is set).
|
||||||
//bindUID, _ := c.DB.GetSetting("ldap_bind_user")
|
|
||||||
//bindPWD, _ := c.DB.GetSetting("ldap_bind_user_password")
|
|
||||||
ldapPort, _ := c.DB.GetSetting("ldap_port")
|
ldapPort, _ := c.DB.GetSetting("ldap_port")
|
||||||
baseDN, _ := c.DB.GetSetting("ldap_base_dn")
|
baseDN, _ := c.DB.GetSetting("ldap_base_dn")
|
||||||
//filter, _ := c.DB.GetSetting("ldap_filter")
|
|
||||||
tls, _ := c.DB.GetSetting("ldap_tls")
|
tls, _ := c.DB.GetSetting("ldap_tls")
|
||||||
|
|
||||||
protocol := "ldap"
|
protocol := "ldap"
|
||||||
@@ -114,7 +187,6 @@ func (c *Controller) WithUser(next http.Handler) http.Handler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// After we have a connection, let's try binding
|
// After we have a connection, let's try binding
|
||||||
err = l.Bind(username, password)
|
|
||||||
_, err = l.SimpleBind(&ldap.SimpleBindRequest{
|
_, err = l.SimpleBind(&ldap.SimpleBindRequest{
|
||||||
Username: fmt.Sprintf("uid=%s,%s", username, baseDN),
|
Username: fmt.Sprintf("uid=%s,%s", username, baseDN),
|
||||||
Password: password,
|
Password: password,
|
||||||
|
|||||||
Reference in New Issue
Block a user