-
Notifications
You must be signed in to change notification settings - Fork 2
/
sessiongate.go
128 lines (102 loc) · 3.21 KB
/
sessiongate.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package sessiongate
import (
"errors"
"time"
"github.com/garyburd/redigo/redis"
)
// A Sessiongate represents a connection to the SessionGate module loaded in the
// Redis server.
type Sessiongate struct {
redisPool *redis.Pool
signKey []byte
}
// NewSessiongate initializes a new Sessiongate.
func NewSessiongate(config *Config) (*Sessiongate, error) {
// Returns an error if SignKey is not set
if config.SignKey == nil {
return nil, errors.New("SignKey is required for the Sessiongate config")
}
// Sets addr to a default value if it is an empty string.
var addr string
if config.Addr == "" {
addr = ":6379"
} else {
addr = config.Addr
}
// Sets maxIdle to a default value if it is 0.
var maxIdle int
if config.MaxIdle == 0 {
maxIdle = 3
} else {
maxIdle = config.MaxIdle
}
// Sets idleTimeout to a default value if it is 0.
var idleTimeout time.Duration
if config.IdleTimeout == 0 {
idleTimeout = 240 * time.Second
} else {
idleTimeout = config.IdleTimeout
}
sessiongate := new(Sessiongate)
// Initialize the Redis connection pool.
sessiongate.redisPool = &redis.Pool{
MaxIdle: maxIdle,
IdleTimeout: idleTimeout,
Dial: func() (redis.Conn, error) { return redis.Dial("tcp", addr) },
}
// Sets the SignKey passed to config.
sessiongate.signKey = config.SignKey
return sessiongate, nil
}
// Start starts a new session in the SessionGate module and returns the
// generated token.
func (sessiongate *Sessiongate) Start(ttl int) ([]byte, error) {
conn := sessiongate.redisPool.Get()
defer conn.Close()
r, err := conn.Do("SESSIONGATE.START", sessiongate.signKey, ttl)
if err != nil {
return nil, err
}
return r.([]byte), nil
}
// Expire sets the TTL for a session in the SessionGate module.
func (sessiongate *Sessiongate) Expire(token []byte, ttl int) error {
conn := sessiongate.redisPool.Get()
defer conn.Close()
_, err := conn.Do("SESSIONGATE.EXPIRE", sessiongate.signKey, token, ttl)
return err
}
// PSet sets a payload for a session in the SessionGate module
// name is the payload name to be used to get the payload later
func (sessiongate *Sessiongate) PSet(token, name, payload []byte) error {
conn := sessiongate.redisPool.Get()
defer conn.Close()
_, err := conn.Do("SESSIONGATE.PSET", sessiongate.signKey, token, name, payload)
return err
}
// PGet gets a payload for a session in the SessionGate module
// name is the payload name.
func (sessiongate *Sessiongate) PGet(token, name []byte) ([]byte, error) {
conn := sessiongate.redisPool.Get()
defer conn.Close()
r, err := conn.Do("SESSIONGATE.PGET", sessiongate.signKey, token, name)
if err != nil {
return nil, err
}
return r.([]byte), nil
}
// PDel deletes a payload for a session in the SessionGate module
// name is the payload name to be used to delete.
func (sessiongate *Sessiongate) PDel(token, name []byte) error {
conn := sessiongate.redisPool.Get()
defer conn.Close()
_, err := conn.Do("SESSIONGATE.PDEL", sessiongate.signKey, token, name)
return err
}
// End ends a session in the SessionGate module.
func (sessiongate *Sessiongate) End(token []byte) error {
conn := sessiongate.redisPool.Get()
defer conn.Close()
_, err := conn.Do("SESSIONGATE.END", sessiongate.signKey, token)
return err
}