Skip to content

Commit

Permalink
Merge pull request #70 from appbaseio/fix/user-management
Browse files Browse the repository at this point in the history
feat: update error messages + add response header for 401 errors
  • Loading branch information
lakhansamani authored Feb 3, 2020
2 parents d74aa9c + ae73094 commit c9589ed
Show file tree
Hide file tree
Showing 10 changed files with 29 additions and 2 deletions.
1 change: 1 addition & 0 deletions middleware/ratelimiter/ratelimiter.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ func (rl *Ratelimiter) rateLimit(h http.HandlerFunc) http.HandlerFunc {
// limit on Categories per second
categoryLimit, err := reqPermission.GetLimitFor(*reqCategory)
if err != nil {
w.Header().Set("www-authenticate", "Basic realm=\"Authentication Required\"")
util.WriteBackError(w, err.Error(), http.StatusUnauthorized)
return
}
Expand Down
1 change: 1 addition & 0 deletions middleware/validate/acl.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ func validateACL(h http.HandlerFunc) http.HandlerFunc {

if !ok {
msg := fmt.Sprintf(`credentials cannot access "%s" acl`, reqACL.String())
w.Header().Set("www-authenticate", "Basic realm=\"Authentication Required\"")
util.WriteBackError(w, msg, http.StatusUnauthorized)
return
}
Expand Down
1 change: 1 addition & 0 deletions middleware/validate/category.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ func validateCategory(h http.HandlerFunc) http.HandlerFunc {

if !ok {
msg := fmt.Sprintf(`credential can't access "%s" category`, reqCategory.String())
w.Header().Set("www-authenticate", "Basic realm=\"Authentication Required\"")
util.WriteBackError(w, msg, http.StatusUnauthorized)
return
}
Expand Down
1 change: 1 addition & 0 deletions middleware/validate/expiry.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func validateExpiry(h http.HandlerFunc) http.HandlerFunc {

if expired {
msg := fmt.Sprintf("permission with username=%s is expired", reqPermission.Username)
w.Header().Set("www-authenticate", "Basic realm=\"Authentication Required\"")
util.WriteBackError(w, msg, http.StatusUnauthorized)
return
}
Expand Down
2 changes: 2 additions & 0 deletions middleware/validate/indices.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ func indices(h http.HandlerFunc) http.HandlerFunc {
return
}
if !ok {
w.Header().Set("www-authenticate", "Basic realm=\"Authentication Required\"")
util.WriteBackError(w, "credentials cannot access cluster level routes", http.StatusUnauthorized)
return
}
Expand All @@ -61,6 +62,7 @@ func indices(h http.HandlerFunc) http.HandlerFunc {
}
if !ok {
msg := fmt.Sprintf("credentials cannot access %v index/indices", reqIndices)
w.Header().Set("www-authenticate", "Basic realm=\"Authentication Required\"")
util.WriteBackError(w, msg, http.StatusUnauthorized)
return
}
Expand Down
1 change: 1 addition & 0 deletions middleware/validate/op.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ func operation(h http.HandlerFunc) http.HandlerFunc {

if !ok {
msg := fmt.Sprintf(`credential cannot perform "%v" operation`, reqOp.String())
w.Header().Set("www-authenticate", "Basic realm=\"Authentication Required\"")
util.WriteBackError(w, msg, http.StatusUnauthorized)
return
}
Expand Down
1 change: 1 addition & 0 deletions middleware/validate/referers.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ func referers(h http.HandlerFunc) http.HandlerFunc {
}

if !validated {
w.Header().Set("www-authenticate", "Basic realm=\"Authentication Required\"")
util.WriteBackError(w, "permission doesn't have required referers", http.StatusUnauthorized)
return
}
Expand Down
2 changes: 2 additions & 0 deletions middleware/validate/sources.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func sources(h http.HandlerFunc) http.HandlerFunc {
reqIP := iplookup.FromRequest(req)
if reqIP == "" {
msg := fmt.Sprintf(`failed to recognize request ip: "%s"`, reqIP)
w.Header().Set("www-authenticate", "Basic realm=\"Authentication Required\"")
util.WriteBackError(w, msg, http.StatusUnauthorized)
return
}
Expand Down Expand Up @@ -70,6 +71,7 @@ func sources(h http.HandlerFunc) http.HandlerFunc {
if !validated {
msg := fmt.Sprintf(`permission with username %s doesn't have required sources. reqIP = %s, sources = %s`,
reqPermission.Username, reqIP, allowedSources)
w.Header().Set("www-authenticate", "Basic realm=\"Authentication Required\"")
util.WriteBackError(w, msg, http.StatusUnauthorized)
return
}
Expand Down
17 changes: 16 additions & 1 deletion plugins/auth/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ func (a *Auth) basicAuth(h http.HandlerFunc) http.HandlerFunc {
} else {
msg = fmt.Sprintf("Unable to parse JWT: %v", err)
}
w.Header().Set("www-authenticate", "Basic realm=\"Authentication Required\"")
util.WriteBackError(w, msg, http.StatusUnauthorized)
return
}
Expand All @@ -118,10 +119,12 @@ func (a *Auth) basicAuth(h http.HandlerFunc) http.HandlerFunc {
} else if u, ok := claims["role"]; ok {
role = u.(string)
} else {
w.Header().Set("www-authenticate", "Basic realm=\"Authentication Required\"")
util.WriteBackError(w, fmt.Sprintf("Invalid JWT"), http.StatusUnauthorized)
return
}
} else {
w.Header().Set("www-authenticate", "Basic realm=\"Authentication Required\"")
util.WriteBackError(w, fmt.Sprintf("Invalid JWT"), http.StatusUnauthorized)
return
}
Expand All @@ -133,6 +136,7 @@ func (a *Auth) basicAuth(h http.HandlerFunc) http.HandlerFunc {
if err != nil || obj == nil {
msg := fmt.Sprintf("No API credentials match with provided role: %s", role)
log.Errorln(logTag, ":", err)
w.Header().Set("www-authenticate", "Basic realm=\"Authentication Required\"")
util.WriteBackError(w, msg, http.StatusUnauthorized)
return
}
Expand All @@ -141,12 +145,14 @@ func (a *Auth) basicAuth(h http.HandlerFunc) http.HandlerFunc {
if err != nil || obj == nil {
msg := fmt.Sprintf("No API credentials match with provided username: %s", username)
log.Errorln(logTag, ":", err)
w.Header().Set("www-authenticate", "Basic realm=\"Authentication Required\"")
util.WriteBackError(w, msg, http.StatusUnauthorized)
return
}
}

var authenticated bool
var errorMsg = "invalid credentials provided"

// since we are able to fetch a result with the given credentials, we
// do not need to validate the username and password.
Expand All @@ -156,6 +162,7 @@ func (a *Auth) basicAuth(h http.HandlerFunc) http.HandlerFunc {
// if the request is made to elasticsearch using user credentials, then the user has to be an admin
reqUser := obj.(*user.User)
if hasBasicAuth && bcrypt.CompareHashAndPassword([]byte(reqUser.Password), []byte(password)) != nil {
w.Header().Set("www-authenticate", "Basic realm=\"Authentication Required\"")
util.WriteBackError(w, "invalid password", http.StatusUnauthorized)
return
}
Expand All @@ -165,6 +172,10 @@ func (a *Auth) basicAuth(h http.HandlerFunc) http.HandlerFunc {
authenticated = true
}

if !authenticated {
errorMsg = "only admin users are allowed to access elasticsearch"
}

// cache the user
if _, ok := a.cachedCredential(username); !ok {
a.cacheCredential(username, reqUser)
Expand All @@ -179,12 +190,15 @@ func (a *Auth) basicAuth(h http.HandlerFunc) http.HandlerFunc {
{
reqPermission := obj.(*permission.Permission)
if hasBasicAuth && reqPermission.Password != password {
w.Header().Set("www-authenticate", "Basic realm=\"Authentication Required\"")
util.WriteBackError(w, "invalid password", http.StatusUnauthorized)
return
}

if reqCategory.IsFromES() {
authenticated = true
} else {
errorMsg = "credential is only allowed to access elasticsearch"
}

// cache the permission
Expand All @@ -202,7 +216,8 @@ func (a *Auth) basicAuth(h http.HandlerFunc) http.HandlerFunc {
}

if !authenticated {
util.WriteBackError(w, "invalid credentials provided", http.StatusUnauthorized)
w.Header().Set("www-authenticate", "Basic realm=\"Authentication Required\"")
util.WriteBackError(w, errorMsg, http.StatusUnauthorized)
return
}

Expand Down
4 changes: 3 additions & 1 deletion plugins/users/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package users

import (
"fmt"
log "github.com/sirupsen/logrus"
"net/http"

log "github.com/sirupsen/logrus"

"github.com/appbaseio/arc/middleware"
"github.com/appbaseio/arc/middleware/classify"
"github.com/appbaseio/arc/middleware/validate"
Expand Down Expand Up @@ -68,6 +69,7 @@ func isAdmin(h http.HandlerFunc) http.HandlerFunc {

if !*reqUser.IsAdmin {
msg := fmt.Sprintf(`user with "username"="%s" is not an admin`, reqUser.Username)
w.Header().Set("www-authenticate", "Basic realm=\"Authentication Required\"")
util.WriteBackError(w, msg, http.StatusUnauthorized)
return
}
Expand Down

0 comments on commit c9589ed

Please sign in to comment.