blob: bfaf685ead439492dc6550147c0a115c6f1e084d (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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] ...)
}
}
}
|