about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorEmile <hanemile@protonmail.com>2019-10-09 11:15:21 +0200
committerEmile <hanemile@protonmail.com>2019-10-09 11:16:49 +0200
commitf331ae701b2dc7f78a14545b49db91e9436c6208 (patch)
treeef6279e8e2e76ff9304f5bed6f3677855ea48861 /src
parent1aa747711c73dc3f43f1b14b60e4c015c4caeee1 (diff)
generated a random access code
xkcd 221 doesn't seem great in production
Diffstat (limited to 'src')
-rw-r--r--src/access.go25
-rw-r--r--src/http.go2
2 files changed, 23 insertions, 4 deletions
diff --git a/src/access.go b/src/access.go
index e43d5aa..6de2c05 100644
--- a/src/access.go
+++ b/src/access.go
@@ -1,7 +1,26 @@
 package main
 
-import "fmt"
+import (
+	"math/rand"
+	"strings"
+	"time"
+)
 
-func newAccessCode() string {
-	return fmt.Sprintf("%d", 4)
+func newAccessCode(length int) string {
+	// seed the random numbergenerator
+	rand.Seed(time.Now().UnixNano())
+
+	// define the alphabet
+	chars := []rune("ABCDEFGHIJKLMNOPQRSTUVWXYZ" +
+		"abcdefghijklmnopqrstuvwxyz" +
+		"0123456789")
+
+	// build the accesscode
+	var b strings.Builder
+	for i := 0; i < length; i++ {
+		b.WriteRune(chars[rand.Intn(len(chars))])
+	}
+	str := b.String()
+
+	return str
 }
diff --git a/src/http.go b/src/http.go
index 2c15fc6..5263062 100644
--- a/src/http.go
+++ b/src/http.go
@@ -69,7 +69,7 @@ func registerPostHandler(w http.ResponseWriter, r *http.Request) {
 	usernames = append(usernames, username)
 
 	// generate a new accesscode
-	accesscode := newAccessCode()
+	accesscode := newAccessCode(16)
 	log.Printf("Generated a new AccessCode for user \"%s\": \"%s\"", username, accesscode)
 
 	// generate a new companion