about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--src/http.go68
1 files changed, 65 insertions, 3 deletions
diff --git a/src/http.go b/src/http.go
index 1b5db1a..0f81f37 100644
--- a/src/http.go
+++ b/src/http.go
@@ -1,8 +1,10 @@
 package main
 
 import (
+	"encoding/base64"
 	"flag"
 	"fmt"
+	"html/template"
 	"io/ioutil"
 	"log"
 	"net/http"
@@ -19,6 +21,12 @@ var (
 	companion map[string]string
 )
 
+// Credentials stores user credentials
+type Credentials struct {
+	Username   string
+	Accesscode string
+}
+
 func registerHTTPFlags() {
 	port = flag.Int("port", 8081, "The port the http server should listen on")
 }
@@ -29,6 +37,7 @@ func setupHTTPServer() http.Server {
 	r.HandleFunc("/", indexHandler)
 	r.HandleFunc("/register", registerGetHandler).Methods("GET")
 	r.HandleFunc("/register", registerPostHandler).Methods("POST")
+	r.HandleFunc("/credentials", credentialsGetHandler).Methods("GET")
 
 	return http.Server{
 		Addr:    fmt.Sprintf("0.0.0.0:%d", *port),
@@ -64,13 +73,28 @@ func registerPostHandler(w http.ResponseWriter, r *http.Request) {
 
 	// generate a new accesscode
 	accesscode := newAccessCode()
-	log.Printf("Generated a new AccessCode for user %s: %s", username, accesscode)
+	log.Printf("Generated a new AccessCode for user \"%s\": \"%s\"", username, accesscode)
 
 	// generate a new companion
 	spawnCompanion(username, accesscode)
 
-	// redirect the user to the front page
-	http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
+	log.Println("---")
+	log.Println("Done generating the containers, filling the credentials page using a template")
+
+	usernameBytes := []byte(username)
+	usernameBase64 := base64.StdEncoding.EncodeToString(usernameBytes)
+
+	accesscodeBytes := []byte(accesscode)
+	accesscodeBase64 := base64.StdEncoding.EncodeToString(accesscodeBytes)
+
+	// insert the username and the accesscode into the http request parameters
+	r.Form["username"] = []string{usernameBase64}
+	r.Form["accesscode"] = []string{accesscodeBase64}
+
+	credentialsGetHandler(w, r)
+	//log.Println("redirecting to /credentials")
+	//endpoint := fmt.Sprintf("/credentials?username=%s&accesscode=%s", usernameBase64, accesscodeBase64)
+	//http.Redirect(w, r, endpoint, http.StatusSeeOther)
 }
 
 func usernameTakenGetHandler(w http.ResponseWriter, r *http.Request) {
@@ -98,3 +122,41 @@ func readFileToReponse(w http.ResponseWriter, path string) {
 		w.Write([]byte(contents))
 	}
 }
+
+func credentialsGetHandler(w http.ResponseWriter, r *http.Request) {
+	r.ParseForm()
+	usernameBase64 := r.Form.Get("username")
+	username, err := base64.StdEncoding.DecodeString(usernameBase64)
+	if err != nil {
+		fmt.Println("error decoding username base64:", err)
+	}
+
+	accesscodeBase64 := r.Form.Get("accesscode")
+	accesscode, err := base64.StdEncoding.DecodeString(accesscodeBase64)
+	if err != nil {
+		fmt.Println("error decoding accesscode base64:", err)
+	}
+
+	log.Println("[ ] Credentials GET Handler")
+	log.Println("%#v", r.Form)
+	log.Printf("username: %s", username)
+	log.Printf("accesscode: %s", accesscode)
+
+	// create a new template reading the credentials template file
+	// the template then gets executed inserting the username and the accesscode
+	log.Println("creating new template")
+	t := template.New("")
+	log.Println("parsing template file")
+	t, err = t.ParseFiles("./hosted/credentials.html")
+	if err != nil {
+		log.Println(err)
+	}
+	log.Println("creating a credentials struct")
+	creds := Credentials{
+		Username:   string(username),
+		Accesscode: string(accesscode),
+	}
+	log.Println("executing the template")
+	t.ExecuteTemplate(w, "credentials", creds)
+	log.Println("done executing the template")
+}