From 1138fd0eea807914876d6c75dd9728fdbcbf31ed Mon Sep 17 00:00:00 2001 From: Wim Brand Date: Thu, 28 Mar 2019 11:25:35 +0100 Subject: [PATCH] Fix user authentication when backend is down --- webcontroller/template_data.go | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/webcontroller/template_data.go b/webcontroller/template_data.go index 8749db7..2f2313c 100644 --- a/webcontroller/template_data.go +++ b/webcontroller/template_data.go @@ -40,14 +40,20 @@ func (wc *WebController) newTemplateData(w http.ResponseWriter, r *http.Request) t.PixelAPI = pixelapi.New(wc.conf.APIURLInternal, key) uinf, err := t.PixelAPI.UserInfo() if err != nil { - // This session key doesn't work, delete it + // This session key doesn't work, or the backend is down, user + // cannot be authenticated log.Debug("Session check for key '%s' failed: %s", key, err) - http.SetCookie(w, &http.Cookie{ - Name: "pd_auth_key", - Value: "", - Path: "/", - Expires: time.Unix(0, 0), - }) + + if aerr, ok := err.(pixelapi.Error); ok && aerr.Value == "authentication_required" { + // This key is invalid, delete it + log.Debug("Deleting invalid API key") + http.SetCookie(w, &http.Cookie{ + Name: "pd_auth_key", + Value: "", + Path: "/", + Expires: time.Unix(0, 0), + }) + } return t }