token-validator: Try to have better error msg
continuous-integration/drone/push Build is passing Details

This commit is contained in:
nemunaire 2022-03-10 18:14:59 +01:00
parent 52b218f2ea
commit 6dd9097351
2 changed files with 13 additions and 4 deletions

View File

@ -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)

View File

@ -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 {