This repository has been archived on 2024-03-03. You can view files and clone it, but cannot push or open issues or pull requests.
adlin/token-validator/ping.go

42 lines
1007 B
Go
Raw Normal View History

2019-02-26 23:19:17 +00:00
package main
import (
2019-02-27 04:49:08 +00:00
"encoding/json"
"errors"
2020-03-27 13:57:14 +00:00
2021-10-31 15:43:43 +00:00
"git.nemunai.re/srs/adlin/libadlin"
2019-02-26 23:19:17 +00:00
)
2019-02-27 04:49:08 +00:00
var PongSecret = "felixfixit"
2019-02-26 23:19:17 +00:00
func init() {
router.GET("/api/students/:sid/ping", apiHandler(studentHandler(lastPing)))
2021-03-07 11:39:38 +00:00
router.GET("/api/students/:sid/pong", apiHandler(studentHandler(func(student *adlin.Student, body []byte) (interface{}, error) {
2020-03-27 13:57:14 +00:00
return student.LastPongs()
2019-02-27 04:49:08 +00:00
})))
2019-02-26 23:19:17 +00:00
router.POST("/api/students/:sid/pong", apiHandler(studentHandler(stdPong), sslOnly))
}
2021-03-07 11:39:38 +00:00
func lastPing(student *adlin.Student, body []byte) (interface{}, error) {
2020-03-27 13:57:14 +00:00
if pongs, err := student.LastPongs(); err != nil {
2019-02-27 04:49:08 +00:00
return nil, err
} else if len(pongs) <= 0 {
return false, nil
} else {
return pongs[0], nil
2019-02-27 04:49:08 +00:00
}
}
2021-03-07 11:39:38 +00:00
func stdPong(student *adlin.Student, body []byte) (interface{}, error) {
2019-02-27 04:49:08 +00:00
var gt givenToken
if err := json.Unmarshal(body, &gt); err != nil {
return nil, err
}
if gt.Token != PongSecret {
return nil, errors.New("This is not the expected token.")
}
2020-03-27 13:57:14 +00:00
return true, student.OnPong(gt.Challenge == 0)
2019-02-26 23:19:17 +00:00
}