about summary refs log tree commit diff
path: root/src/http.go
diff options
context:
space:
mode:
authormaride <maride@darknebu.la>2018-08-14 17:28:47 +0200
committermaride <maride@darknebu.la>2018-08-14 17:28:47 +0200
commit6a66ed60af5ba83d3e9c064d41dbd1b7a0f23468 (patch)
tree5c940674cfbcc93b52eb9ed87c78f1236976a859 /src/http.go
parent728e93e07648adb84c83f77687077d18987e0319 (diff)
Add challenges
Diffstat (limited to 'src/http.go')
-rw-r--r--src/http.go21
1 files changed, 21 insertions, 0 deletions
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)
+		}
+	}
+}