about summary refs log tree commit diff
path: root/src/session.go
diff options
context:
space:
mode:
authormaride <maride@darknebu.la>2018-08-14 13:57:13 +0200
committermaride <maride@darknebu.la>2018-08-14 13:57:13 +0200
commitcfb006c2be8717ebd10d39b9dd64211ff7ce1eb0 (patch)
tree3fe376f90c6caf932a7cc32cb3f9b60c15385f69 /src/session.go
parent61c8c9d29824a7ec1ebfb96413ce1713260dfd57 (diff)
Add Login/Logout/Sessions using given access codes
Diffstat (limited to 'src/session.go')
-rw-r--r--src/session.go60
1 files changed, 60 insertions, 0 deletions
diff --git a/src/session.go b/src/session.go
new file mode 100644
index 0000000..bfaf685
--- /dev/null
+++ b/src/session.go
@@ -0,0 +1,60 @@
+package main
+
+import (
+	"crypto/sha512"
+	"math/rand"
+	"encoding/base64"
+	"flag"
+)
+
+var (
+	tokenSalt *string
+	sessions = []string{}
+)
+
+func registerSessionFlags() {
+	tokenSalt = flag.String("sessionSalt", "", "Salt for the session token")
+}
+
+// Generates a random token
+func randToken() string {
+	// Generating the random part
+	randBuff := make([]byte, 128)
+	rand.Read(randBuff)
+
+	// Hashing the random part
+	hasher := sha512.New()
+	hasher.Write(randBuff)
+	hasher.Write([]byte(*tokenSalt))
+	hashBuff := hasher.Sum(nil)
+
+	// Returning that
+	return base64.URLEncoding.EncodeToString(hashBuff)
+}
+
+// Verifies a given session token against the internal session array
+func isValidSession(sessionToken string) (bool){
+	for _, token := range sessions {
+		if token == sessionToken {
+			return true
+		}
+	}
+	return false
+}
+
+// Generates a new session token and adds it to the internal session array
+func createSession() (string) {
+	newToken := randToken()
+	sessions = append(sessions, newToken)
+	return newToken
+}
+
+// Removes a session from the internal session array
+func destroySession(sessionToken string) {
+	for index, token := range sessions {
+		if token == sessionToken {
+			// delete session from slice
+			sessions = append(sessions[:index], sessions[:index + 1] ...)
+		}
+	}
+}