-
Notifications
You must be signed in to change notification settings - Fork 13
/
redis.go
84 lines (75 loc) · 1.75 KB
/
redis.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
package session
import (
"encoding/json"
"fmt"
"net/http"
"github.com/airware/vili/redis"
)
// RedisConfig is the redis session configuration
type RedisConfig struct {
Secure bool
}
type redisService struct {
config *RedisConfig
}
// InitRedisService initializes the redis session service
func InitRedisService(c *RedisConfig) {
service = &redisService{
config: c,
}
}
func (s *redisService) Login(r *http.Request, w http.ResponseWriter, u *User) error {
userBytes, err := json.Marshal(u)
if err != nil {
return err
}
for i := 0; i < 10; i++ {
sessionID := newSessionID()
success, err := redis.GetClient().SetNX(sessionRedisKey(sessionID), string(userBytes), 0).Result()
if err != nil {
return err
}
if success {
http.SetCookie(w, &http.Cookie{
Name: sessionCookie,
Value: sessionID,
MaxAge: 60 * 60 * 24, // 1 day
Path: "/",
Secure: s.config.Secure,
})
return nil
}
}
return fmt.Errorf("failed to find a unique session ID")
}
func (s *redisService) Logout(r *http.Request, w http.ResponseWriter) error {
sessionID := getSessionCookie(r)
err := redis.GetClient().Del(sessionRedisKey(sessionID)).Err()
if err != nil {
return err
}
http.SetCookie(w, &http.Cookie{
Name: sessionCookie,
MaxAge: -1, // delete cookie
})
return nil
}
func (s *redisService) GetUser(r *http.Request) (*User, error) {
sessionID := getSessionCookie(r)
if sessionID == "" {
return nil, nil
}
userBytes, err := redis.GetClient().Get(sessionRedisKey(sessionID)).Bytes()
if err != nil {
if err == redis.Nil {
return nil, nil
}
return nil, err
}
user := &User{}
json.Unmarshal(userBytes, user)
return user, nil
}
func sessionRedisKey(sessionID string) string {
return fmt.Sprintf("session:%s", sessionID)
}