From 6dd90973516b7440b5f6abdbec8f751a7a4003c6 Mon Sep 17 00:00:00 2001 From: Pierre-Olivier Mercier Date: Thu, 10 Mar 2022 18:14:59 +0100 Subject: [PATCH] token-validator: Try to have better error msg --- token-validator/auth.go | 14 ++++++++++---- token-validator/handler.go | 3 +++ 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/token-validator/auth.go b/token-validator/auth.go index e4cf15c..93cbaf2 100644 --- a/token-validator/auth.go +++ b/token-validator/auth.go @@ -4,6 +4,8 @@ import ( "encoding/base64" "encoding/json" "errors" + "fmt" + "log" "net/http" "strings" "time" @@ -53,10 +55,12 @@ func completeAuth(w http.ResponseWriter, username string, session *adlin.Session var std *adlin.Student if !adlin.StudentExists(username) { if std, err = adlin.NewStudent(username); err != nil { - return err + log.Printf("Unable to NewStudent(%s): %s", username, err) + return fmt.Errorf("Quelque chose s'est mal passé lors de la création de ton compte (première connexion ?).") } } else if std, err = adlin.GetStudentByLogin(username); err != nil { - return err + log.Printf("Unable to GetStudentByLogin(%s): %s", username, err) + return fmt.Errorf("Quelque chose s'est mal passé lors de l'accès à ton compte. Réessaye dans quelques instants.") } if session == nil { @@ -66,7 +70,8 @@ func completeAuth(w http.ResponseWriter, username string, session *adlin.Session } if err != nil { - return err + log.Println("Session creation/retrieval problem:", err) + return fmt.Errorf("Quelque chose s'est mal passé lors de l'attribution de ta session. Réessaye dans quelques instants.") } http.SetCookie(w, &http.Cookie{ @@ -84,7 +89,8 @@ func completeAuth(w http.ResponseWriter, username string, session *adlin.Session func dummyAuth(w http.ResponseWriter, _ httprouter.Params, body []byte) (interface{}, error) { var lf loginForm if err := json.Unmarshal(body, &lf); err != nil { - return nil, err + log.Println("Bad auth request:", err) + return nil, fmt.Errorf("Unable to unmarshal your request: %w", err) } return map[string]string{"status": "OK"}, completeAuth(w, lf.Username, nil) diff --git a/token-validator/handler.go b/token-validator/handler.go index 3648fce..f9305a2 100644 --- a/token-validator/handler.go +++ b/token-validator/handler.go @@ -59,14 +59,17 @@ func rawHandler(f func(http.ResponseWriter, *http.Request, httprouter.Params, [] var student *adlin.Student = nil if cookie, err := r.Cookie("auth"); err == nil { if sessionid, err := base64.StdEncoding.DecodeString(cookie.Value); err != nil { + http.SetCookie(w, &http.Cookie{Name: "auth", Value: "", Path: baseURL + "/", Expires: time.Time{}, HttpOnly: true}) http.Error(w, fmt.Sprintf(`{"errmsg": %q}`, err), http.StatusNotAcceptable) return } else if session, err := adlin.GetSession(sessionid); err != nil { + http.SetCookie(w, &http.Cookie{Name: "auth", Value: "", Path: baseURL + "/", Expires: time.Time{}, HttpOnly: true}) http.Error(w, fmt.Sprintf(`{"errmsg": %q}`, err), http.StatusUnauthorized) return } else if session.IdStudent == nil { student = nil } else if std, err := adlin.GetStudent(int(*session.IdStudent)); err != nil { + http.SetCookie(w, &http.Cookie{Name: "auth", Value: "", Path: baseURL + "/", Expires: time.Time{}, HttpOnly: true}) http.Error(w, fmt.Sprintf(`{"errmsg": %q}`, err), http.StatusUnauthorized) return } else {