From f49b42f70f902d4da1e0fa4080e99164b331b716 Mon Sep 17 00:00:00 2001 From: Frédéric Guillot Date: Sun, 29 Apr 2018 16:35:04 -0700 Subject: Use vanilla HTTP handlers (refactoring) --- middleware/basic_auth.go | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) (limited to 'middleware/basic_auth.go') 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 } -- cgit v1.2.3