token-validator: introduce dummyAuth to avoid auth check

This commit is contained in:
nemunaire 2019-07-15 17:31:13 +02:00
parent ad0a3eaf2b
commit ffcccce709
2 changed files with 39 additions and 22 deletions

View file

@ -8,9 +8,13 @@ import (
"github.com/julienschmidt/httprouter"
)
var AuthFunc = checkAuth
func init() {
router.GET("/api/auth", apiAuthHandler(validateAuthToken))
router.POST("/api/auth", apiHandler(checkAuth))
router.POST("/api/auth", apiHandler(func(ps httprouter.Params, body []byte) (interface{}, error) {
return AuthFunc(ps, body)
}))
}
func validateAuthToken(s Student, _ httprouter.Params, _ []byte) (interface{}, error) {
@ -22,6 +26,34 @@ type loginForm struct {
Password string
}
func dummyAuth(_ httprouter.Params, body []byte) (interface{}, error) {
var lf loginForm
if err := json.Unmarshal(body, &lf); err != nil {
return nil, err
}
var std Student
var err error
if !studentExists(lf.Username) {
if std, err = NewStudent(lf.Username); err != nil {
return nil, err
}
} else if std, err = getStudentByLogin(lf.Username); err != nil {
return nil, err
}
session, err := std.NewSession()
if err != nil {
return nil, err
}
res := map[string]interface{}{}
res["status"] = "OK"
res["id_session"] = session.Id
return res, nil
}
func checkAuth(_ httprouter.Params, body []byte) (interface{}, error) {
var lf loginForm
if err := json.Unmarshal(body, &lf); err != nil {
@ -39,30 +71,10 @@ func checkAuth(_ httprouter.Params, body []byte) (interface{}, error) {
defer resp.Body.Close()
if resp.StatusCode == http.StatusOK {
var std Student
if !studentExists(lf.Username) {
if std, err = NewStudent(lf.Username); err != nil {
return nil, err
}
} else if std, err = getStudentByLogin(lf.Username); err != nil {
return nil, err
}
session, err := std.NewSession()
if err != nil {
return nil, err
}
res := map[string]interface{}{}
res["status"] = "OK"
res["id_session"] = session.Id
return res, nil
return dummyAuth(nil, body)
} else {
return nil, errors.New(`{"status": "Invalid username or password"}`)
}
}
return nil, nil
}
}