From 6a66ed60af5ba83d3e9c064d41dbd1b7a0f23468 Mon Sep 17 00:00:00 2001 From: maride Date: Tue, 14 Aug 2018 17:28:47 +0200 Subject: Add challenges --- src/http.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'src/http.go') diff --git a/src/http.go b/src/http.go index 9f97352..1ddebe5 100644 --- a/src/http.go +++ b/src/http.go @@ -8,6 +8,7 @@ import ( "strings" "io/ioutil" "time" + "log" ) var ( @@ -26,6 +27,7 @@ func runHTTPServer() (error) { r.HandleFunc("/login", loginGetHandler).Methods("GET") r.HandleFunc("/login", loginPostHandler).Methods("POST") r.HandleFunc("/logout", logoutHandler).Methods("GET") + r.HandleFunc("/api/getChallenges", getChallengesHandler).Methods("GET") address := fmt.Sprintf(":%d", *port) return http.ListenAndServe(address, r) @@ -141,3 +143,22 @@ func logoutHandler(w http.ResponseWriter, r *http.Request) { http.Redirect(w, r, "/", http.StatusTemporaryRedirect) } + +func getChallengesHandler(w http.ResponseWriter, r *http.Request) { + session, cookieNotFoundError := r.Cookie("session") + + if cookieNotFoundError != nil || !isValidSession(session.Value) { + // either no session cookie found, or it contains an invalid session token. Redirect. + http.Redirect(w, r, "/login", http.StatusTemporaryRedirect) + } else { + // valid session token found, send out JSON array containing all challenges + json, jsonErr := generateJSONFromChallenges() + + if jsonErr == nil { + w.Write([]byte(json)) + } else { + log.Println(jsonErr) + w.WriteHeader(500) + } + } +} -- cgit 1.4.1