Made WithUser middleware private
This commit is contained in:
+22
-22
@@ -94,7 +94,7 @@ func New(dbc *db.DB, scannr *scanner.Scanner, musicPaths []MusicPath, podcastsPa
|
|||||||
chain := handlerutil.Chain(
|
chain := handlerutil.Chain(
|
||||||
withParams,
|
withParams,
|
||||||
withRequiredParams,
|
withRequiredParams,
|
||||||
c.WithUser,
|
c.withUser,
|
||||||
)
|
)
|
||||||
|
|
||||||
c.Handle("/getLicense", chain(resp(c.ServeGetLicence)))
|
c.Handle("/getLicense", chain(resp(c.ServeGetLicence)))
|
||||||
@@ -220,7 +220,7 @@ func withRequiredParams(next http.Handler) http.Handler {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Controller) WithUser(next http.Handler) http.Handler {
|
func (c *Controller) withUser(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) {
|
||||||
params := r.Context().Value(CtxParams).(params.Params)
|
params := r.Context().Value(CtxParams).(params.Params)
|
||||||
// ignoring errors here, a middleware has already ensured they exist
|
// ignoring errors here, a middleware has already ensured they exist
|
||||||
@@ -238,16 +238,16 @@ func (c *Controller) WithUser(next http.Handler) http.Handler {
|
|||||||
}
|
}
|
||||||
user := c.dbc.GetUserByName(username)
|
user := c.dbc.GetUserByName(username)
|
||||||
|
|
||||||
newLDAPUser := false
|
newLDAPUser := false
|
||||||
if user == nil {
|
if user == nil {
|
||||||
newLDAPUser = true
|
newLDAPUser = true
|
||||||
|
|
||||||
// Because the user wasn't found we can now try
|
// Because the user wasn't found we can now try
|
||||||
// to use LDAP. ldapFQDN, err := c.dbc.GetSetting("ldap_fqdn")
|
// to use LDAP.
|
||||||
ldapFQDN, err := c.dbc.GetSetting("ldap_fqdn")
|
ldapFQDN, err := c.dbc.GetSetting("ldap_fqdn")
|
||||||
|
|
||||||
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.dbc.GetSetting("ldap_bind_user")
|
bindUID, _ := c.dbc.GetSetting("ldap_bind_user")
|
||||||
bindPWD, _ := c.dbc.GetSetting("ldap_bind_user_password")
|
bindPWD, _ := c.dbc.GetSetting("ldap_bind_user_password")
|
||||||
@@ -264,7 +264,7 @@ func (c *Controller) WithUser(next http.Handler) http.Handler {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer l.Close()
|
defer l.Close()
|
||||||
|
|
||||||
// After we have a connection, let's try binding
|
// After we have a connection, let's try binding
|
||||||
_, err = l.SimpleBind(&ldap.SimpleBindRequest{
|
_, err = l.SimpleBind(&ldap.SimpleBindRequest{
|
||||||
Username: fmt.Sprintf("uid=%s,%s", bindUID, baseDN),
|
Username: fmt.Sprintf("uid=%s,%s", bindUID, baseDN),
|
||||||
@@ -286,24 +286,24 @@ func (c *Controller) WithUser(next http.Handler) http.Handler {
|
|||||||
|
|
||||||
result, err := l.Search(searchReq)
|
result, err := l.Search(searchReq)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("failed to query LDAP:", err)
|
log.Println("failed to query LDAP:", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(result.Entries) > 1 {
|
if len(result.Entries) > 1 {
|
||||||
_ = writeResp(w, r, spec.NewError(40, "Ambiguous user: `%s`", username))
|
_ = writeResp(w, r, spec.NewError(40, "Ambiguous user: `%s`", username))
|
||||||
} else if len(result.Entries) == 1 {
|
} else if len(result.Entries) == 1 {
|
||||||
user := db.User{
|
user := db.User{
|
||||||
Name: username,
|
Name: username,
|
||||||
Password: "", // no password because we want auth to fail.
|
Password: "", // no password because we want auth to fail.
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := c.dbc.Create(&user).Error; err != nil {
|
if err := c.dbc.Create(&user).Error; err != nil {
|
||||||
fmt.Println("User created via LDAP:", username)
|
fmt.Println("User created via LDAP:", username)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
_ = writeResp(w, r, spec.NewError(40, "invalid username `%s`", username))
|
_ = writeResp(w, r, spec.NewError(40, "invalid username `%s`", username))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
_ = writeResp(w, r, spec.NewError(40,
|
_ = writeResp(w, r, spec.NewError(40,
|
||||||
"invalid username `%s`", username))
|
"invalid username `%s`", username))
|
||||||
@@ -317,17 +317,17 @@ func (c *Controller) WithUser(next http.Handler) http.Handler {
|
|||||||
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")
|
ldapFQDN, err := c.dbc.GetSetting("ldap_fqdn")
|
||||||
|
|
||||||
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).
|
||||||
ldapPort, _ := c.dbc.GetSetting("ldap_port")
|
ldapPort, _ := c.dbc.GetSetting("ldap_port")
|
||||||
baseDN, _ := c.dbc.GetSetting("ldap_base_dn")
|
baseDN, _ := c.dbc.GetSetting("ldap_base_dn")
|
||||||
tls, _ := c.dbc.GetSetting("ldap_tls")
|
tls, _ := c.dbc.GetSetting("ldap_tls")
|
||||||
|
|
||||||
// Now, we can try to connect to the LDAP server.
|
// Now, we can try to connect to the LDAP server.
|
||||||
l, err := createLDAPconnection(tls, ldapFQDN, ldapPort)
|
l, err := createLDAPconnection(tls, ldapFQDN, ldapPort)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -336,7 +336,7 @@ func (c *Controller) WithUser(next http.Handler) http.Handler {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer l.Close()
|
defer l.Close()
|
||||||
|
|
||||||
// After we have a connection, let's try binding
|
// After we have a connection, let's try binding
|
||||||
_, 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),
|
||||||
@@ -346,10 +346,10 @@ func (c *Controller) WithUser(next http.Handler) http.Handler {
|
|||||||
if err == nil {
|
if err == nil {
|
||||||
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))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_ = writeResp(w, r, spec.NewError(40, "invalid password"))
|
_ = writeResp(w, r, spec.NewError(40, "invalid password"))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user