-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsession.go
More file actions
44 lines (39 loc) · 981 Bytes
/
session.go
File metadata and controls
44 lines (39 loc) · 981 Bytes
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
package main
import (
"crypto/rand"
"fmt"
"math/big"
"strings"
)
var sessions = make(map[string]Session)
type Session struct {
id string
userID int
}
func newSession(userID int) (Session, error) {
for i := 0; i < 10; i++ {
id, err := generateRandomString(32)
if err != nil {
continue // Try again.
}
if _, ok := sessions[id]; !ok {
sessions[id] = Session{id: id, userID: userID}
return sessions[id], nil
}
}
return Session{}, fmt.Errorf("exhausted all attempts generating unique session ID")
}
func generateRandomString(n int) (string, error) {
const letters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
sb := strings.Builder{}
sb.Grow(n)
for i := 0; i < n; i++ {
num, err := rand.Int(rand.Reader, big.NewInt(int64(len(letters))))
if err != nil {
return "", fmt.Errorf("failed to generate random string of length %d", n)
}
letter := letters[num.Int64()]
sb.WriteByte(letter)
}
return sb.String(), nil
}