-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsession.go
100 lines (92 loc) · 2.31 KB
/
session.go
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package main
import (
"crypto/rand"
"database/sql"
"fmt"
_ "github.com/lib/pq"
)
func GenerateRandomString() (token string, err error) {
var b [16]byte
num, err := rand.Read(b[:])
if num != 16 || err != nil {
return "", err
}
uuid := fmt.Sprintf("%x%x%x%x%x", b[0:4], b[4:6], b[6:8], b[8:10], b[10:])
return uuid, err
}
// This function does its darndest to generate a session key if one doesn't exist
// and then return that session.
func GetSessionKey(userID int) (string, error) {
var key string
var isStillValid bool
// If the key is still valid
err := db.QueryRow(`
Select cookieInfo, (valid_til > TIMESTAMPTZ 'NOW')
from sessions where userID = $1;`, userID).Scan(&key, &isStillValid)
if isStillValid {
return key, nil
}
// If the user doesn't have any Sessions yet.
if err == sql.ErrNoRows {
key, _ = GenerateRandomString()
_, err = db.Exec(`
Insert into sessions (userID, cookieInfo, valid_til)
VALUES ($1, $2, DATE 'NOW' + INTERVAL '1 WEEK');
`, userID, key)
if err != nil {
fmt.Println(err)
return "", err
}
}
if err != nil {
fmt.Println(err)
return "", err
}
// Update an out of date session
key, _ = GenerateRandomString()
_, err = db.Exec(`
Update sessions
set
cookieInfo = $1,
valid_til = TIMESTAMPTZ 'NOW' + INTERVAL '1 WEEK'
where userID = $2`, key, userID)
if err != nil {
fmt.Println(err)
return "", err
}
return key, nil
}
var (
ErrUserNotValidatedYet = fmt.Errorf("User not validated")
)
func CheckSessionHasValidated(key string) (int, bool, error) {
var hasValidated bool
var userID int
err := db.QueryRow(`
Select
sessions.userID,
cookieInfo,
isValidated
from sessions
left join emailvalidation as evalid on evalid.userid = sessions.userid
where cookieinfo = $1
`, key).Scan(&userID, &key, &hasValidated)
if err != nil {
return -1, false, err
}
return userID, hasValidated, nil
}
// Return userID, if the session is still valid, and any errors we found.
func CheckSessionsKey(key string) (int, bool, error) {
var isStillValid bool
var userID int
err := db.QueryRow(`
Select userID, cookieInfo, (valid_til > TIMESTAMPTZ 'NOW')
from sessions where cookieInfo = $1
`, key).Scan(&userID, &key, &isStillValid)
if err != nil {
return -1, false, err
}
return userID, isStillValid, nil
// If the user doesn't have any Sessions yet.
}