From e7f4eb1e3645160099e4212cbcb29503bcb8a0d3 Mon Sep 17 00:00:00 2001 From: Samir Patel <48686912+54mir@users.noreply.github.com> Date: Mon, 20 Dec 2021 12:28:17 -0600 Subject: [PATCH] response codes --- authn/authenticate.go | 11 +++++------ http/handler.go | 28 +++++++++++++++++++++++----- server/server.go | 1 - 3 files changed, 28 insertions(+), 12 deletions(-) diff --git a/authn/authenticate.go b/authn/authenticate.go index 214b5a0c6..bb207560e 100644 --- a/authn/authenticate.go +++ b/authn/authenticate.go @@ -131,13 +131,14 @@ func (a *Auth) Redirect(w http.ResponseWriter, r *http.Request) { code := r.FormValue("code") token, err := a.getToken(code) if err != nil { - errors.Wrap(err, "getting token") - http.Redirect(w, r, "/login", http.StatusUnauthorized) + http.Error(w, "Bad Request: 400", http.StatusBadRequest) + return } cv, err := a.newCookieValue(token) - if err != nil { - http.Error(w, "authenticating", http.StatusBadRequest) + if err != nil || cv == nil { + http.Error(w, "Bad Request: 400", http.StatusBadRequest) + return } a.setCookie(w, cv) @@ -181,8 +182,6 @@ func (a *Auth) newCookieValue(token *oauth2.Token) (*CookieValue, error) { } // not needed at this point in the logic and makes the encoded cookie too large token.AccessToken = "" - // mannually setting expiry for testing ... REMOVE - token.Expiry = time.Now().Add(time.Second * time.Duration(30)) return &CookieValue{ UserID: claims["oid"].(string), UserName: claims["name"].(string), diff --git a/http/handler.go b/http/handler.go index 654f37e75..79c45b734 100644 --- a/http/handler.go +++ b/http/handler.go @@ -3369,7 +3369,9 @@ func (h *Handler) handlePostRestore(w http.ResponseWriter, r *http.Request) { func (h *Handler) handleLogin(w http.ResponseWriter, r *http.Request) { if h.auth == nil { - http.Error(w, "Trying to login but authentication is off.", http.StatusBadRequest) + w.Header().Add("Content-Type", "text/plain") + w.WriteHeader(http.StatusNoContent) + w.Write([]byte("Auth Off")) //nolint:errcheck return } @@ -3378,15 +3380,23 @@ func (h *Handler) handleLogin(w http.ResponseWriter, r *http.Request) { func (h *Handler) handleRedirect(w http.ResponseWriter, r *http.Request) { if h.auth == nil { - http.Error(w, "Authentication is off.", http.StatusBadRequest) + w.Header().Add("Content-Type", "text/plain") + w.WriteHeader(http.StatusNoContent) + w.Write([]byte("Auth Off")) //nolint:errcheck return } h.auth.Redirect(w, r) } func (h *Handler) handleCheckAuthentication(w http.ResponseWriter, r *http.Request) { + if !validHeaderAcceptJSON(r.Header) { + http.Error(w, "JSON only acceptable response", http.StatusNotAcceptable) + return + } if h.auth == nil { - http.Error(w, "Trying to authenticate but authentication is off.", http.StatusBadRequest) + w.Header().Add("Content-Type", "text/plain") + w.WriteHeader(http.StatusNoContent) + w.Write([]byte("Auth Off")) //nolint:errcheck return } groups, err := h.auth.Authenticate(w, r) @@ -3402,8 +3412,14 @@ func (h *Handler) handleCheckAuthentication(w http.ResponseWriter, r *http.Reque } func (h *Handler) handleUserInfo(w http.ResponseWriter, r *http.Request) { + if !validHeaderAcceptJSON(r.Header) { + http.Error(w, "JSON only acceptable response", http.StatusNotAcceptable) + return + } if h.auth == nil { - http.Error(w, "Authentication is off.", http.StatusBadRequest) + w.Header().Add("Content-Type", "text/plain") + w.WriteHeader(http.StatusNoContent) + w.Write([]byte("Auth Off")) //nolint:errcheck return } if err := json.NewEncoder(w).Encode(h.auth.GetUserInfo(r)); err != nil { @@ -3413,7 +3429,9 @@ func (h *Handler) handleUserInfo(w http.ResponseWriter, r *http.Request) { func (h *Handler) handleLogout(w http.ResponseWriter, r *http.Request) { if h.auth == nil { - http.Error(w, "Trying to log out but authentication is off.", http.StatusBadRequest) + w.Header().Add("Content-Type", "text/plain") + w.WriteHeader(http.StatusNoContent) + w.Write([]byte("Auth Off")) //nolint:errcheck return } h.auth.Logout(w, r) diff --git a/server/server.go b/server/server.go index 1946a3442..d5fefc2db 100644 --- a/server/server.go +++ b/server/server.go @@ -532,7 +532,6 @@ func (m *Command) SetupServer() error { } - m.logger.Infof("Before Handler %+v", m.auth) m.Handler, err = http.NewHandler( http.OptHandlerAllowedOrigins(m.Config.Handler.AllowedOrigins), http.OptHandlerAPI(m.API),