Implement login endpoint

This commit is contained in:
nemunaire 2018-02-10 14:37:23 +01:00
parent 2058e3c8e2
commit bf9707dc5a
4 changed files with 68 additions and 5 deletions

62
validator/login.go Normal file
View file

@ -0,0 +1,62 @@
package main
import (
"encoding/json"
"log"
"net/http"
)
type loginChecker struct{
students []Student
}
type loginUpload struct {
Username string
Password string
}
func (l loginChecker) ServeHTTP(w http.ResponseWriter, r *http.Request) {
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", "text/plain")
// Check request type and size
if r.Method != "POST" {
http.Error(w,
"Invalid request",
http.StatusBadRequest)
return
} else if r.ContentLength < 0 || r.ContentLength > 1023 {
http.Error(w,
"Request entity too large",
http.StatusRequestEntityTooLarge)
return
}
dec := json.NewDecoder(r.Body)
var lu loginUpload
if err := dec.Decode(&lu); err != nil {
http.Error(w,
err.Error(),
http.StatusBadRequest)
return
}
// Perform login check
canContinue := false
for _, std := range l.students {
if std.Login == lu.Username {
canContinue = true
}
}
if !canContinue {
http.Error(w, "Login not found in whitelist.", http.StatusUnauthorized)
return
}
http.Error(w, "Success", http.StatusOK)
}