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/validator/login.go

63 lines
1.2 KiB
Go
Raw Normal View History

2018-02-10 13:37:23 +00:00
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)
}