token-validator: get challenge id also via json

This commit is contained in:
nemunaire 2018-02-22 01:23:13 +01:00 committed by Pierre-Olivier Mercier
parent 87599668b9
commit 9a821276e0

View file

@ -216,10 +216,6 @@ func accessibleChallenge(r *http.Request, ps httprouter.Params, _ []byte) (inter
}
func receiveToken(r *http.Request, ps httprouter.Params, body []byte) (interface{}, error) {
if _, err := accessibleChallenge(r, ps, body); err != nil {
return nil, err
}
var gt givenToken
if err := json.Unmarshal(body, &gt); err != nil {
return nil, err
@ -230,11 +226,27 @@ func receiveToken(r *http.Request, ps httprouter.Params, body []byte) (interface
return nil, err
}
if chid, err := strconv.Atoi(string(ps.ByName("chid"))); err != nil {
return nil, err
} else if chid == 0 || chid > len(challenges) {
// Find challenge ID
var chid int
var err error
if chid, err = strconv.Atoi(string(ps.ByName("chid"))); err != nil {
if gt.Challenge > 0 {
chid = gt.Challenge
}
}
if chid == 0 || chid > len(challenges) {
return nil, errors.New("This challenge doesn't exist")
} else if std, err := getStudentByLogin(gt.Login); err != nil {
}
// Is the challenge accessible?
for _, a := range challenges[chid - 1].Accessible {
if err := a(nil, r); err != nil {
return nil, err
}
}
if std, err := getStudentByLogin(gt.Login); err != nil {
return nil, err
} else {
if err := challenges[chid - 1].Check(&std, &gt, chid); err != nil {