token-validator: done many challenges
This commit is contained in:
parent
91edced44d
commit
133244897b
4 changed files with 313 additions and 8 deletions
|
@ -1,6 +1,8 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"crypto/hmac"
|
||||
"crypto/sha512"
|
||||
"encoding/json"
|
||||
"strings"
|
||||
"time"
|
||||
|
@ -10,19 +12,21 @@ import (
|
|||
|
||||
func init() {
|
||||
router.GET("/api/students/", apiHandler(
|
||||
func(httprouter.Params,[]byte) (interface{}, error) {
|
||||
return getStudents() }))
|
||||
func(httprouter.Params, []byte) (interface{}, error) {
|
||||
return getStudents()
|
||||
}))
|
||||
router.POST("/api/students/", remoteValidatorHandler(apiHandler(createStudent)))
|
||||
router.GET("/api/students/:sid/", apiHandler(studentHandler(
|
||||
func(std Student, _ []byte) (interface{}, error) {
|
||||
return std, nil })))
|
||||
return std, nil
|
||||
})))
|
||||
router.PUT("/api/students/:sid/", remoteValidatorHandler(apiHandler(studentHandler(updateStudent))))
|
||||
router.DELETE("/api/students/:sid/", remoteValidatorHandler(apiHandler(studentHandler(
|
||||
func(std Student, _ []byte) (interface{}, error) {
|
||||
return std.Delete() }))))
|
||||
return std.Delete()
|
||||
}))))
|
||||
}
|
||||
|
||||
|
||||
type Student struct {
|
||||
Id int64 `json:"id"`
|
||||
Login string `json:"login"`
|
||||
|
@ -70,6 +74,10 @@ func NewStudent(login string) (Student, error) {
|
|||
}
|
||||
}
|
||||
|
||||
func (s Student) GetPKey() []byte {
|
||||
return hmac.New(sha512.New512_224, []byte(sharedSecret)).Sum([]byte(s.Login))
|
||||
}
|
||||
|
||||
func (s Student) Update() (int64, error) {
|
||||
if res, err := DBExec("UPDATE students SET login = ?, time = ? WHERE id_student = ?", s.Login, s.Time, s.Id); err != nil {
|
||||
return 0, err
|
||||
|
@ -100,7 +108,6 @@ func ClearStudents() (int64, error) {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
func createStudent(_ httprouter.Params, body []byte) (interface{}, error) {
|
||||
var std Student
|
||||
if err := json.Unmarshal(body, &std); err != nil {
|
||||
|
@ -120,3 +127,20 @@ func updateStudent(current Student, body []byte) (interface{}, error) {
|
|||
current.Time = new.Time
|
||||
return current.Update()
|
||||
}
|
||||
|
||||
type UnlockedChallenge struct {
|
||||
Id int64 `json:"id"`
|
||||
IdStudent int64 `json:"id_student"`
|
||||
Challenge int `json:"challenge"`
|
||||
Time time.Time `json:"time"`
|
||||
}
|
||||
|
||||
func (s Student) UnlockNewChallenge(challenge int, value string) (UnlockedChallenge, error) {
|
||||
if res, err := DBExec("INSERT INTO student_challenges (id_student, challenge, time, value) VALUES (?, ?, ?, ?)", s.Id, challenge, time.Now(), value); err != nil {
|
||||
return UnlockedChallenge{}, err
|
||||
} else if utid, err := res.LastInsertId(); err != nil {
|
||||
return UnlockedChallenge{}, err
|
||||
} else {
|
||||
return UnlockedChallenge{utid, s.Id, challenge, time.Now()}, err
|
||||
}
|
||||
}
|
||||
|
|
Reference in a new issue