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

View File

@ -4,7 +4,7 @@ import (
gc "github.com/rthornton128/goncurses"
)
const URLLogin = "http://127.0.0.1:8081/login"
const URLLogin = "http://172.23.0.1:8081/login"
func goLogin(stdscr *gc.Window, in chan gc.Key) (string, bool) {
username, password := login(stdscr, in)
@ -38,6 +38,7 @@ func main() {
gc.InitPair(2, gc.C_GREEN, gc.C_BLACK)
gc.InitPair(3, gc.C_CYAN, gc.C_BLACK)
gc.InitPair(4, gc.C_RED, gc.C_BLACK)
gc.InitPair(5, gc.C_BLACK, gc.C_WHITE)
// Register pressed key through channel
in := make(chan gc.Key)

View File

@ -14,12 +14,12 @@ func login(stdscr *gc.Window, in chan gc.Key) (username , password string) {
fields := make([]*gc.Field, 3)
fields[0], _ = gc.NewField(1, 15, 2, 18, 0, 0)
defer fields[0].Free()
fields[0].SetBackground(gc.A_UNDERLINE)
fields[0].SetBackground(gc.ColorPair(5) | gc.A_UNDERLINE)
fields[0].SetOptionsOff(gc.FO_AUTOSKIP)
fields[1], _ = gc.NewField(1, 15, 4, 18, 0, 0)
defer fields[1].Free()
fields[1].SetBackground(gc.A_UNDERLINE)
fields[1].SetBackground(gc.ColorPair(5) | gc.A_UNDERLINE)
fields[1].SetOptionsOff(gc.FO_PUBLIC)
fields[1].SetOptionsOff(gc.FO_AUTOSKIP)

View File

@ -71,8 +71,8 @@ func Index(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html")
if indexTmpl, err := template.New("index").Parse(indextpl); err != nil {
http.Error(w, "Cannot create template: " + err.Error(), http.StatusInternalServerError)
http.Error(w, "Cannot create template: "+err.Error(), http.StatusInternalServerError)
} else if err := indexTmpl.Execute(w, nil); err != nil {
http.Error(w, "An error occurs during template execution: " + err.Error(), http.StatusInternalServerError)
http.Error(w, "An error occurs during template execution: "+err.Error(), http.StatusInternalServerError)
}
}

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)
}