aboutsummaryrefslogtreecommitdiff
path: root/middleware/basic_auth.go
diff options
context:
space:
mode:
authorGravatar Frédéric Guillot <fred@miniflux.net> 2018-04-29 16:35:04 -0700
committerGravatar Frédéric Guillot <fred@miniflux.net> 2018-04-29 16:35:04 -0700
commitf49b42f70f902d4da1e0fa4080e99164b331b716 (patch)
treec6bdd19f11d100c44b0d30344ec37038f649e988 /middleware/basic_auth.go
parent1eba1730d1af50ed545f4fde78b22d6fb62ca11e (diff)
downloadv2-f49b42f70f902d4da1e0fa4080e99164b331b716.tar.gz
v2-f49b42f70f902d4da1e0fa4080e99164b331b716.tar.zst
v2-f49b42f70f902d4da1e0fa4080e99164b331b716.zip
Use vanilla HTTP handlers (refactoring)
Diffstat (limited to 'middleware/basic_auth.go')
-rw-r--r--middleware/basic_auth.go14
1 files changed, 5 insertions, 9 deletions
diff --git a/middleware/basic_auth.go b/middleware/basic_auth.go
index 9d7a4b2d..edea3338 100644
--- a/middleware/basic_auth.go
+++ b/middleware/basic_auth.go
@@ -8,6 +8,7 @@ import (
"context"
"net/http"
+ "github.com/miniflux/miniflux/http/response/json"
"github.com/miniflux/miniflux/logger"
)
@@ -15,35 +16,30 @@ import (
func (m *Middleware) BasicAuth(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("WWW-Authenticate", `Basic realm="Restricted"`)
- errorResponse := `{"error_message": "Not Authorized"}`
username, password, authOK := r.BasicAuth()
if !authOK {
logger.Debug("[Middleware:BasicAuth] No authentication headers sent")
- w.WriteHeader(http.StatusUnauthorized)
- w.Write([]byte(errorResponse))
+ json.Unauthorized(w)
return
}
if err := m.store.CheckPassword(username, password); err != nil {
logger.Info("[Middleware:BasicAuth] Invalid username or password: %s", username)
- w.WriteHeader(http.StatusUnauthorized)
- w.Write([]byte(errorResponse))
+ json.Unauthorized(w)
return
}
user, err := m.store.UserByUsername(username)
if err != nil {
logger.Error("[Middleware:BasicAuth] %v", err)
- w.WriteHeader(http.StatusInternalServerError)
- w.Write([]byte(errorResponse))
+ json.ServerError(w, err)
return
}
if user == nil {
logger.Info("[Middleware:BasicAuth] User not found: %s", username)
- w.WriteHeader(http.StatusUnauthorized)
- w.Write([]byte(errorResponse))
+ json.Unauthorized(w)
return
}