2018-02-20 12:49:03 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2018-02-20 13:55:32 +00:00
|
|
|
"crypto/hmac"
|
|
|
|
"crypto/sha512"
|
|
|
|
"encoding/base64"
|
2018-02-20 12:49:03 +00:00
|
|
|
"encoding/json"
|
2019-03-13 12:47:01 +00:00
|
|
|
"errors"
|
2018-02-20 12:49:03 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
2018-02-20 13:55:32 +00:00
|
|
|
"strconv"
|
|
|
|
"time"
|
2018-02-20 12:49:03 +00:00
|
|
|
|
|
|
|
"github.com/julienschmidt/httprouter"
|
2020-03-27 13:57:14 +00:00
|
|
|
|
2021-10-31 15:43:43 +00:00
|
|
|
"git.nemunai.re/srs/adlin/libadlin"
|
2018-02-20 12:49:03 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var router = httprouter.New()
|
|
|
|
|
|
|
|
func Router() *httprouter.Router {
|
|
|
|
return router
|
|
|
|
}
|
|
|
|
|
|
|
|
type DispatchFunction func(httprouter.Params, []byte) (interface{}, error)
|
|
|
|
|
2018-02-20 13:55:32 +00:00
|
|
|
func remoteValidatorHandler(f func(http.ResponseWriter, *http.Request, httprouter.Params)) func(http.ResponseWriter, *http.Request, httprouter.Params) {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
2020-03-27 13:57:14 +00:00
|
|
|
expectedMAC := hmac.New(sha512.New, []byte(adlin.SharedSecret)).Sum([]byte(fmt.Sprintf("%d", time.Now().Unix()/10)))
|
|
|
|
previousMAC := hmac.New(sha512.New, []byte(adlin.SharedSecret)).Sum([]byte(fmt.Sprintf("%d", time.Now().Unix()/10-1)))
|
2018-02-20 13:55:32 +00:00
|
|
|
|
|
|
|
if aauth, err := base64.StdEncoding.DecodeString(r.Header.Get("X-ADLIN-Authentication")); err != nil {
|
2019-03-04 08:00:22 +00:00
|
|
|
http.Error(w, fmt.Sprintf("{\"errmsg\":%q}\n", err), http.StatusUnauthorized)
|
2018-02-20 13:55:32 +00:00
|
|
|
} else if !hmac.Equal(expectedMAC, aauth) && !hmac.Equal(previousMAC, aauth) {
|
2019-03-04 08:00:22 +00:00
|
|
|
http.Error(w, fmt.Sprintf("{\"errmsg\":%q}\n", "Bad authentication header"), http.StatusUnauthorized)
|
2018-02-20 13:55:32 +00:00
|
|
|
} else {
|
|
|
|
f(w, r, ps)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-27 13:57:14 +00:00
|
|
|
func rawHandler(f func(http.ResponseWriter, *http.Request, httprouter.Params, []byte), access ...func(*adlin.Student, *http.Request) error) func(http.ResponseWriter, *http.Request, httprouter.Params) {
|
2018-02-20 12:49:03 +00:00
|
|
|
return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
|
|
|
if addr := r.Header.Get("X-Forwarded-For"); addr != "" {
|
|
|
|
r.RemoteAddr = addr
|
|
|
|
}
|
|
|
|
log.Printf("%s \"%s %s\" [%s]\n", r.RemoteAddr, r.Method, r.URL.Path, r.UserAgent())
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
|
2019-03-13 10:19:51 +00:00
|
|
|
// Read Authorization header
|
2020-03-27 13:57:14 +00:00
|
|
|
var student *adlin.Student = nil
|
2020-03-01 17:15:19 +00:00
|
|
|
if cookie, err := r.Cookie("auth"); err == nil {
|
|
|
|
if sessionid, err := base64.StdEncoding.DecodeString(cookie.Value); err != nil {
|
2019-03-13 10:19:51 +00:00
|
|
|
http.Error(w, fmt.Sprintf(`{"errmsg": %q}`, err), http.StatusNotAcceptable)
|
2020-03-01 17:15:19 +00:00
|
|
|
return
|
2020-03-27 13:57:14 +00:00
|
|
|
} else if session, err := adlin.GetSession(sessionid); err != nil {
|
2019-03-13 10:19:51 +00:00
|
|
|
http.Error(w, fmt.Sprintf(`{"errmsg": %q}`, err), http.StatusUnauthorized)
|
|
|
|
return
|
2020-03-01 17:15:19 +00:00
|
|
|
} else if session.IdStudent == nil {
|
|
|
|
student = nil
|
2020-03-27 13:57:14 +00:00
|
|
|
} else if std, err := adlin.GetStudent(int(*session.IdStudent)); err != nil {
|
2019-03-13 10:19:51 +00:00
|
|
|
http.Error(w, fmt.Sprintf(`{"errmsg": %q}`, err), http.StatusUnauthorized)
|
|
|
|
return
|
|
|
|
} else {
|
2021-03-07 11:39:38 +00:00
|
|
|
student = std
|
2019-03-13 10:19:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-26 18:23:28 +00:00
|
|
|
// Check access limitation
|
|
|
|
for _, a := range access {
|
2019-03-13 10:19:51 +00:00
|
|
|
if err := a(student, r); err != nil {
|
|
|
|
http.Error(w, fmt.Sprintf(`{"errmsg":%q}`, err), http.StatusForbidden)
|
2019-02-26 18:23:28 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-20 12:49:03 +00:00
|
|
|
// Read the body
|
|
|
|
if r.ContentLength < 0 || r.ContentLength > 6553600 {
|
2019-03-14 05:46:09 +00:00
|
|
|
http.Error(w, "{errmsg:\"Request too large or request size unknown\"}", http.StatusRequestEntityTooLarge)
|
2018-02-20 12:49:03 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
var body []byte
|
|
|
|
if r.ContentLength > 0 {
|
|
|
|
tmp := make([]byte, 1024)
|
|
|
|
for {
|
|
|
|
n, err := r.Body.Read(tmp)
|
|
|
|
for j := 0; j < n; j++ {
|
|
|
|
body = append(body, tmp[j])
|
|
|
|
}
|
|
|
|
if err != nil || n <= 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-01 17:09:20 +00:00
|
|
|
f(w, r, ps, body)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func responseHandler(f func(*http.Request, httprouter.Params, []byte) (interface{}, error)) func(http.ResponseWriter, *http.Request, httprouter.Params, []byte) {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params, body []byte) {
|
|
|
|
ret, err := f(r, ps, body)
|
2018-02-20 12:49:03 +00:00
|
|
|
|
|
|
|
// Format response
|
|
|
|
resStatus := http.StatusOK
|
|
|
|
if err != nil {
|
|
|
|
ret = map[string]string{"errmsg": err.Error()}
|
|
|
|
resStatus = http.StatusBadRequest
|
|
|
|
log.Println(r.RemoteAddr, resStatus, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
if ret == nil {
|
|
|
|
ret = map[string]string{"errmsg": "Page not found"}
|
|
|
|
resStatus = http.StatusNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
if str, found := ret.(string); found {
|
|
|
|
w.WriteHeader(resStatus)
|
|
|
|
io.WriteString(w, str)
|
2019-03-04 08:00:22 +00:00
|
|
|
w.Write([]byte("\n"))
|
2018-02-20 12:49:03 +00:00
|
|
|
} else if bts, found := ret.([]byte); found {
|
|
|
|
w.WriteHeader(resStatus)
|
|
|
|
w.Write(bts)
|
|
|
|
} else if j, err := json.Marshal(ret); err != nil {
|
2019-03-04 08:00:22 +00:00
|
|
|
http.Error(w, fmt.Sprintf("{\"errmsg\":%q}\n", err), http.StatusInternalServerError)
|
2018-02-20 12:49:03 +00:00
|
|
|
} else {
|
|
|
|
w.WriteHeader(resStatus)
|
|
|
|
w.Write(j)
|
2019-03-04 08:00:22 +00:00
|
|
|
w.Write([]byte("\n"))
|
2018-02-20 12:49:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-02-20 13:55:32 +00:00
|
|
|
|
2020-03-27 13:57:14 +00:00
|
|
|
func challengeHandler(f func(*http.Request, []byte, int) (interface{}, error)) func(*http.Request, httprouter.Params, []byte) (interface{}, error) {
|
2019-02-27 04:49:08 +00:00
|
|
|
return func(r *http.Request, ps httprouter.Params, body []byte) (interface{}, error) {
|
|
|
|
return f(r, body, 0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-27 13:57:14 +00:00
|
|
|
func definedChallengeHandler(f func(*http.Request, []byte, int) (interface{}, error), chid int) func(*http.Request, httprouter.Params, []byte) (interface{}, error) {
|
2019-02-27 04:49:08 +00:00
|
|
|
return func(r *http.Request, ps httprouter.Params, body []byte) (interface{}, error) {
|
|
|
|
return f(r, body, chid)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-27 13:57:14 +00:00
|
|
|
func apiRawHandler(f func(http.ResponseWriter, httprouter.Params, []byte) (interface{}, error), access ...func(*adlin.Student, *http.Request) error) func(http.ResponseWriter, *http.Request, httprouter.Params) {
|
|
|
|
return rawHandler(func(w http.ResponseWriter, r *http.Request, ps httprouter.Params, b []byte) {
|
|
|
|
responseHandler(func(_ *http.Request, ps httprouter.Params, b []byte) (interface{}, error) {
|
2020-03-01 17:09:20 +00:00
|
|
|
return f(w, ps, b)
|
|
|
|
})(w, r, ps, b)
|
|
|
|
}, access...)
|
|
|
|
}
|
|
|
|
|
2020-03-27 13:57:14 +00:00
|
|
|
func apiHandler(f DispatchFunction, access ...func(*adlin.Student, *http.Request) error) func(http.ResponseWriter, *http.Request, httprouter.Params) {
|
|
|
|
return rawHandler(responseHandler(func(_ *http.Request, ps httprouter.Params, b []byte) (interface{}, error) { return f(ps, b) }), access...)
|
2018-02-20 17:20:07 +00:00
|
|
|
}
|
|
|
|
|
2021-03-07 11:39:38 +00:00
|
|
|
func apiAuthHandler(f func(*adlin.Student, httprouter.Params, []byte) (interface{}, error), access ...func(*adlin.Student, *http.Request) error) func(http.ResponseWriter, *http.Request, httprouter.Params) {
|
2020-03-27 13:57:14 +00:00
|
|
|
return rawHandler(responseHandler(func(r *http.Request, ps httprouter.Params, b []byte) (interface{}, error) {
|
2020-03-01 17:15:19 +00:00
|
|
|
if cookie, err := r.Cookie("auth"); err != nil {
|
2019-03-13 12:47:01 +00:00
|
|
|
return nil, errors.New("Authorization required")
|
2020-03-01 17:15:19 +00:00
|
|
|
} else if sessionid, err := base64.StdEncoding.DecodeString(cookie.Value); err != nil {
|
2019-03-13 12:47:01 +00:00
|
|
|
return nil, err
|
2020-03-27 13:57:14 +00:00
|
|
|
} else if session, err := adlin.GetSession(sessionid); err != nil {
|
2019-03-13 12:47:01 +00:00
|
|
|
return nil, err
|
2020-03-01 17:15:19 +00:00
|
|
|
} else if session.IdStudent == nil {
|
|
|
|
return nil, errors.New("Authorization required")
|
2020-03-27 13:57:14 +00:00
|
|
|
} else if std, err := adlin.GetStudent(int(*session.IdStudent)); err != nil {
|
2019-03-13 12:47:01 +00:00
|
|
|
return nil, err
|
|
|
|
} else {
|
|
|
|
return f(std, ps, b)
|
|
|
|
}
|
2020-03-01 17:09:20 +00:00
|
|
|
}), access...)
|
2019-03-13 12:47:01 +00:00
|
|
|
}
|
|
|
|
|
2021-03-07 11:39:38 +00:00
|
|
|
func studentHandler(f func(*adlin.Student, []byte) (interface{}, error)) func(httprouter.Params, []byte) (interface{}, error) {
|
2018-02-20 13:55:32 +00:00
|
|
|
return func(ps httprouter.Params, body []byte) (interface{}, error) {
|
|
|
|
if sid, err := strconv.Atoi(string(ps.ByName("sid"))); err != nil {
|
2020-03-27 13:57:14 +00:00
|
|
|
if student, err := adlin.GetStudentByLogin(ps.ByName("sid")); err != nil {
|
2018-02-20 13:55:32 +00:00
|
|
|
return nil, err
|
|
|
|
} else {
|
|
|
|
return f(student, body)
|
|
|
|
}
|
2020-03-27 13:57:14 +00:00
|
|
|
} else if student, err := adlin.GetStudent(sid); err != nil {
|
2018-02-20 13:55:32 +00:00
|
|
|
return nil, err
|
|
|
|
} else {
|
|
|
|
return f(student, body)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|