Merge branch 'ldap_auth' into master
This commit is contained in:
No files matched your search
@@ -43,7 +43,9 @@
|
||||
{{ end }}
|
||||
{{ end }}
|
||||
{{ if .User.IsAdmin }}
|
||||
<div class="col-span-full">{{ component "link" (props . "To" (path "/admin/create_user")) }}create new{{ end }}</div>
|
||||
<div class="col-span-full">{{ component "link" (props . "To" (path
|
||||
"/admin/create_user")) }}create new{{ end }} {{component "link"
|
||||
(props . "To" (path "/admin/ldap")) }}LDAP{{ end }}</div>
|
||||
{{ end }}
|
||||
</div>
|
||||
{{ end }}
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
{{ component "layout" . }}
|
||||
{{ component "layout_user" . }}
|
||||
|
||||
{{ component "block" (props .
|
||||
"Icon" "user"
|
||||
"Name" "configuring LDAP"
|
||||
) }}
|
||||
<form class="flex flex-col gap-2 items-end" action="{{ path
|
||||
"/admin/ldap_do" }}" method="post">
|
||||
<p>Leave the server empty to disable LDAP.</p>
|
||||
<input
|
||||
type="username"
|
||||
id="bind_user"
|
||||
name="bind_user"
|
||||
placeholder="bind username"
|
||||
value={{ default "" .CurrentBindUID }}
|
||||
>
|
||||
<input
|
||||
type="password"
|
||||
id="bind_user_password"
|
||||
name="bind_user_password"
|
||||
placeholder="bind password"
|
||||
>
|
||||
|
||||
<input
|
||||
type="text"
|
||||
id="fqdn"
|
||||
name="fqdn"
|
||||
placeholder="LDAP server"
|
||||
value={{ default "" .CurrentFQDN }}
|
||||
>
|
||||
|
||||
<input
|
||||
type="number"
|
||||
id="port"
|
||||
name="port"
|
||||
placeholder="port"
|
||||
value={{ default "389" .CurrentPort }}
|
||||
>
|
||||
|
||||
<select name="tls" id="tls">
|
||||
<option value="true">TLS</option>
|
||||
<option value="false" {{ .CurrentNoTLS }}>None</option>
|
||||
</select>
|
||||
|
||||
<input
|
||||
type="text"
|
||||
id="base_dn"
|
||||
name="base_dn"
|
||||
placeholder="Base DN"
|
||||
value={{ default "" .CurrentBaseDN }}
|
||||
>
|
||||
|
||||
<input
|
||||
type="text"
|
||||
id="filter"
|
||||
name="filter"
|
||||
placeholder="filter"
|
||||
value={{ default "" .CurrentFilter }}
|
||||
>
|
||||
|
||||
|
||||
<input type="submit" value="update">
|
||||
</form>
|
||||
{{ end }}
|
||||
|
||||
{{ end }}
|
||||
{{ end }}
|
||||
@@ -85,6 +85,8 @@ func New(dbc *db.DB, sessDB *gormstore.Store, scanner *scanner.Scanner, podcasts
|
||||
// user routes (if session is valid)
|
||||
c.Handle("/logout", userChain(respRaw(c.ServeLogout)))
|
||||
c.Handle("/home", userChain(resp(c.ServeHome)))
|
||||
c.Handle("/ldap", userChain(resp(c.ServeLDAPConfig)))
|
||||
c.Handle("/ldap_do", userChain(resp(c.ServeLDAPConfigDo)))
|
||||
c.Handle("/change_username", userChain(resp(c.ServeChangeUsername)))
|
||||
c.Handle("/change_username_do", userChain(resp(c.ServeChangeUsernameDo)))
|
||||
c.Handle("/change_password", userChain(resp(c.ServeChangePassword)))
|
||||
@@ -290,6 +292,13 @@ type templateData struct {
|
||||
DefaultListenBrainzURL string
|
||||
SelectedUser *db.User
|
||||
|
||||
CurrentBindUID string
|
||||
CurrentFQDN string
|
||||
CurrentPort string
|
||||
CurrentNoTLS string
|
||||
CurrentBaseDN string
|
||||
CurrentFilter string
|
||||
|
||||
Podcasts []*db.Podcast
|
||||
InternetRadioStations []*db.InternetRadioStation
|
||||
|
||||
|
||||
@@ -138,6 +138,125 @@ func (c *Controller) ServeUnlinkListenBrainzDo(r *http.Request) *Response {
|
||||
return &Response{redirect: "/admin/home"}
|
||||
}
|
||||
|
||||
func (c *Controller) ServeLDAPConfig(_ *http.Request) *Response {
|
||||
data := &templateData{}
|
||||
|
||||
data.CurrentBindUID, _ = c.dbc.GetSetting("ldap_bind_user")
|
||||
data.CurrentFQDN, _ = c.dbc.GetSetting("ldap_fqdn")
|
||||
data.CurrentPort, _ = c.dbc.GetSetting("ldap_port")
|
||||
data.CurrentBaseDN, _ = c.dbc.GetSetting("ldap_base_dn")
|
||||
data.CurrentFilter, _ = c.dbc.GetSetting("ldap_filter")
|
||||
|
||||
// This is to fix the fact that select boxes are strange.
|
||||
currentTLS, _ := c.dbc.GetSetting("ldap_tls")
|
||||
data.CurrentNoTLS = "selected"
|
||||
|
||||
if currentTLS == "true" {
|
||||
data.CurrentNoTLS = ""
|
||||
}
|
||||
|
||||
return &Response{
|
||||
template: "ldap.tmpl",
|
||||
data: data,
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Controller) ServeLDAPConfigDo(r *http.Request) *Response {
|
||||
bindUID := r.FormValue("bind_user")
|
||||
bindPSWD := r.FormValue("bind_user_password") // TODO: Secure this better.
|
||||
fqdn := r.FormValue("fqdn")
|
||||
port := r.FormValue("port")
|
||||
tls := r.FormValue("tls")
|
||||
baseDN := r.FormValue("base_dn")
|
||||
filter := r.FormValue("filter")
|
||||
|
||||
if bindUID == "" || bindPSWD == "" || port == "" || baseDN == "" || filter == "" {
|
||||
return &Response{
|
||||
redirect: r.Referer(),
|
||||
flashW: []string{"please provide a bind username and password, port, base CN, and filter"},
|
||||
}
|
||||
}
|
||||
|
||||
err := c.dbc.SetSetting("ldap_bind_user", bindUID)
|
||||
if err != nil {
|
||||
log.Println("Failed to set bind user:", err)
|
||||
|
||||
return &Response{
|
||||
redirect: r.Referer(),
|
||||
flashW: []string{"Failed to set bind user."},
|
||||
}
|
||||
}
|
||||
|
||||
err = c.dbc.SetSetting("ldap_bind_user_password", bindPSWD)
|
||||
if err != nil {
|
||||
log.Println("Failed to set bind user password:", err)
|
||||
|
||||
return &Response{
|
||||
redirect: r.Referer(),
|
||||
flashW: []string{"Failed to set bind user password."},
|
||||
}
|
||||
}
|
||||
|
||||
err = c.dbc.SetSetting("ldap_fqdn", fqdn)
|
||||
if err != nil {
|
||||
log.Println("Failed to set server:", err)
|
||||
|
||||
return &Response{
|
||||
redirect: r.Referer(),
|
||||
flashW: []string{"Failed to set server."},
|
||||
}
|
||||
}
|
||||
|
||||
err = c.dbc.SetSetting("ldap_port", port)
|
||||
if err != nil {
|
||||
log.Println("Failed to set port:", err)
|
||||
|
||||
return &Response{
|
||||
redirect: r.Referer(),
|
||||
flashW: []string{"Failed to set port."},
|
||||
}
|
||||
}
|
||||
|
||||
err = c.dbc.SetSetting("ldap_tls", tls)
|
||||
if err != nil {
|
||||
log.Println("Failed to set TLS:", err)
|
||||
|
||||
return &Response{
|
||||
redirect: r.Referer(),
|
||||
flashW: []string{"Failed to set TLS."},
|
||||
}
|
||||
}
|
||||
|
||||
err = c.dbc.SetSetting("ldap_base_dn", baseDN)
|
||||
if err != nil {
|
||||
log.Println("Failed to set base DN:", err)
|
||||
|
||||
return &Response{
|
||||
redirect: r.Referer(),
|
||||
flashW: []string{"Failed to set base DN."},
|
||||
}
|
||||
}
|
||||
|
||||
err = c.dbc.SetSetting("ldap_filter", filter)
|
||||
if err != nil {
|
||||
log.Println("Failed to set filter:", err)
|
||||
|
||||
return &Response{
|
||||
redirect: r.Referer(),
|
||||
flashW: []string{"Failed to set filter."},
|
||||
}
|
||||
}
|
||||
|
||||
if fqdn == "" {
|
||||
return &Response{
|
||||
redirect: "/admin/home",
|
||||
flashW: []string{"LDAP server was left empty, LDAP authentication will be disabled."},
|
||||
}
|
||||
}
|
||||
|
||||
return &Response{redirect: "/admin/home"}
|
||||
}
|
||||
|
||||
func (c *Controller) ServeChangeUsername(r *http.Request) *Response {
|
||||
user, err := selectedUserIfAdmin(c, r)
|
||||
if err != nil {
|
||||
|
||||
+151
-33
@@ -25,6 +25,8 @@ import (
|
||||
"go.senan.xyz/gonic/server/ctrlsubsonic/params"
|
||||
"go.senan.xyz/gonic/server/ctrlsubsonic/spec"
|
||||
"go.senan.xyz/gonic/transcode"
|
||||
|
||||
"github.com/go-ldap/ldap/v3"
|
||||
)
|
||||
|
||||
type CtxKey int
|
||||
@@ -93,7 +95,7 @@ func New(dbc *db.DB, scannr *scanner.Scanner, musicPaths []MusicPath, podcastsPa
|
||||
chain := handlerutil.Chain(
|
||||
withParams,
|
||||
withRequiredParams,
|
||||
withUser(dbc),
|
||||
c.withUser,
|
||||
)
|
||||
chainRaw := handlerutil.Chain(
|
||||
chain,
|
||||
@@ -223,43 +225,159 @@ func withRequiredParams(next http.Handler) http.Handler {
|
||||
})
|
||||
}
|
||||
|
||||
func withUser(dbc *db.DB) handlerutil.Middleware {
|
||||
return func(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
params := r.Context().Value(CtxParams).(params.Params)
|
||||
// ignoring errors here, a middleware has already ensured they exist
|
||||
username, _ := params.Get("u")
|
||||
password, _ := params.Get("p")
|
||||
token, _ := params.Get("t")
|
||||
salt, _ := params.Get("s")
|
||||
func (c *Controller) withUser(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
params := r.Context().Value(CtxParams).(params.Params)
|
||||
// ignoring errors here, a middleware has already ensured they exist
|
||||
username, _ := params.Get("u")
|
||||
password, _ := params.Get("p")
|
||||
token, _ := params.Get("t")
|
||||
salt, _ := params.Get("s")
|
||||
|
||||
passwordAuth := token == "" && salt == ""
|
||||
tokenAuth := password == ""
|
||||
if tokenAuth == passwordAuth {
|
||||
_ = writeResp(w, r, spec.NewError(10,
|
||||
"please provide `t` and `s`, or just `p`"))
|
||||
return
|
||||
}
|
||||
user := dbc.GetUserByName(username)
|
||||
if user == nil {
|
||||
_ = writeResp(w, r, spec.NewError(40,
|
||||
"invalid username %q", username))
|
||||
return
|
||||
}
|
||||
var credsOk bool
|
||||
if tokenAuth {
|
||||
credsOk = checkCredsToken(user.Password, token, salt)
|
||||
passwordAuth := token == "" && salt == ""
|
||||
tokenAuth := password == ""
|
||||
if tokenAuth == passwordAuth {
|
||||
_ = writeResp(w, r, spec.NewError(10,
|
||||
"please provide `t` and `s`, or just `p`"))
|
||||
return
|
||||
}
|
||||
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 {
|
||||
credsOk = checkCredsBasic(user.Password, password)
|
||||
}
|
||||
if !credsOk {
|
||||
_ = writeResp(w, r, spec.NewError(40, "invalid password"))
|
||||
_ = writeResp(w, r, spec.NewError(40,
|
||||
"invalid username `%s`", username))
|
||||
return
|
||||
}
|
||||
withUser := context.WithValue(r.Context(), CtxUser, user)
|
||||
next.ServeHTTP(w, r.WithContext(withUser))
|
||||
})
|
||||
}
|
||||
var credsOk bool
|
||||
if tokenAuth && !newLDAPUser && user != nil {
|
||||
credsOk = checkCredsToken(user.Password, token, salt)
|
||||
} else if !newLDAPUser && user != nil {
|
||||
credsOk = checkCredsBasic(user.Password, password)
|
||||
}
|
||||
if !credsOk {
|
||||
// Because internal authentication failed, we can now try to use LDAP, if
|
||||
// it was enabled by the user.
|
||||
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).
|
||||
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"))
|
||||
return
|
||||
}
|
||||
withUser := context.WithValue(r.Context(), CtxUser, user)
|
||||
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 {
|
||||
|
||||
Reference in New Issue
Block a user