token-validator: Try to have better error msg
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
52b218f2ea
commit
6dd9097351
@ -4,6 +4,8 @@ import (
|
|||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
@ -53,10 +55,12 @@ func completeAuth(w http.ResponseWriter, username string, session *adlin.Session
|
|||||||
var std *adlin.Student
|
var std *adlin.Student
|
||||||
if !adlin.StudentExists(username) {
|
if !adlin.StudentExists(username) {
|
||||||
if std, err = adlin.NewStudent(username); err != nil {
|
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 {
|
} 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 {
|
if session == nil {
|
||||||
@ -66,7 +70,8 @@ func completeAuth(w http.ResponseWriter, username string, session *adlin.Session
|
|||||||
}
|
}
|
||||||
|
|
||||||
if err != nil {
|
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{
|
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) {
|
func dummyAuth(w http.ResponseWriter, _ httprouter.Params, body []byte) (interface{}, error) {
|
||||||
var lf loginForm
|
var lf loginForm
|
||||||
if err := json.Unmarshal(body, &lf); err != nil {
|
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)
|
return map[string]string{"status": "OK"}, completeAuth(w, lf.Username, nil)
|
||||||
|
@ -59,14 +59,17 @@ func rawHandler(f func(http.ResponseWriter, *http.Request, httprouter.Params, []
|
|||||||
var student *adlin.Student = nil
|
var student *adlin.Student = nil
|
||||||
if cookie, err := r.Cookie("auth"); err == nil {
|
if cookie, err := r.Cookie("auth"); err == nil {
|
||||||
if sessionid, err := base64.StdEncoding.DecodeString(cookie.Value); 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)
|
http.Error(w, fmt.Sprintf(`{"errmsg": %q}`, err), http.StatusNotAcceptable)
|
||||||
return
|
return
|
||||||
} else if session, err := adlin.GetSession(sessionid); err != nil {
|
} 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)
|
http.Error(w, fmt.Sprintf(`{"errmsg": %q}`, err), http.StatusUnauthorized)
|
||||||
return
|
return
|
||||||
} else if session.IdStudent == nil {
|
} else if session.IdStudent == nil {
|
||||||
student = nil
|
student = nil
|
||||||
} else if std, err := adlin.GetStudent(int(*session.IdStudent)); err != 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)
|
http.Error(w, fmt.Sprintf(`{"errmsg": %q}`, err), http.StatusUnauthorized)
|
||||||
return
|
return
|
||||||
} else {
|
} else {
|
||||||
|
Reference in New Issue
Block a user