-
Notifications
You must be signed in to change notification settings - Fork 0
/
redisMethods.go
148 lines (119 loc) · 3.35 KB
/
redisMethods.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package pogifyapi
import (
"context"
"crypto/sha1"
"fmt"
"reflect"
"strconv"
"time"
"github.com/fatih/structs"
"github.com/go-redis/redis/v8"
)
var ctx = context.Background()
type r struct {
conn *redis.Client
refreshTokenTTL string
}
var newSessionScript = `local c = redis.call("ttl", KEYS[1])
if (c < 0) then
redis.call("set", KEYS[1], ARGV[1])
redis.call("expire", KEYS[1], ARGV[2])
return 1
end
return 0`
func (r *r) newSession(sessionToken string, refreshToken string) (int64, error) {
key := []string{
"session:" + sessionToken,
}
val, err := r.conn.Eval(ctx, newSessionScript, key, refreshToken, r.refreshTokenTTL).Result()
if err != nil {
val = int64(0)
}
return val.(int64), err
}
var verifyAndSetScript = `
local t = redis.call("get", KEYS[1])
if (t == false) then
return -1
end
if (t == ARGV[1]) then
redis.call("set", KEYS[1], ARGV[2])
redis.call("expire", KEYS[1], ARGV[3])
redis.call("expire", KEYS[1]..":config", ARGV[3])
return 1
end
return 0
`
func (r *r) verifyAndSetNewRefreshToken(sessionID string, token string, newToken string) (int64, error) {
val, err := r.conn.Eval(ctx, verifyAndSetScript, []string{"session:" + sessionID}, token, newToken, r.refreshTokenTTL).Result()
return val.(int64), err
}
var requestLimitScript = `
local c = redis.call('incr',KEYS[1])
local r = redis.call('hget', "session:" .. ARGV[1] .. ":config", "RequestInterval")
if (c <= 1) then
if (r == false) then
redis.call('expire', KEYS[1], 60)
else
redis.call('expire', KEYS[1], r)
end
end
return {c, redis.call('ttl', KEYS[1])}`
func (r *r) rateLimitRequest(sessionID string, id string) ([2]int64, error) {
bs := hashID(id)
key := fmt.Sprintf("requestLimit:%v:%x", sessionID, bs)
val, err := r.conn.Eval(ctx, requestLimitScript, []string{key}, sessionID).Result()
valS := new([2]int64)
if err == nil {
for i, v := range val.([]interface{}) {
valS[i] = v.(int64)
}
}
return *valS, err
}
func (r *r) reverseRateLimit(sessionID string, id string) (int64, error) {
bs := hashID(id)
return r.conn.Decr(ctx, fmt.Sprintf("requestLimit:%v:%v", sessionID, bs)).Result()
}
func (r *r) setSessionConfig(sessionID string, config config) error {
parsedStr, _ := strconv.ParseInt(r.refreshTokenTTL, 10, 64)
key := fmt.Sprintf("session:%v:config", sessionID)
pipe := r.conn.TxPipeline()
pipe.HSet(ctx, key, structs.Map(config)).Result()
pipe.Expire(ctx, key, time.Duration(parsedStr)*time.Second)
_, err := pipe.Exec(ctx)
return err
}
func (r *r) getSessionConfig(sessionID string) (*config, error) {
key := fmt.Sprintf("session:%v:config", sessionID)
conf, err := r.conn.HGetAll(ctx, key).Result()
if err != nil {
return nil, err
}
if len(conf) == 0 {
return nil, fmt.Errorf("No config for %s", sessionID)
}
c := cast(&conf)
return c, nil
}
func cast(conf *map[string]string) *config {
var c config
s := reflect.ValueOf(&c).Elem()
typeOfT := s.Type()
for i := 0; i < s.NumField(); i++ {
f := s.Field(i)
switch f.Type().String() {
case "int":
i, _ := strconv.ParseInt((*conf)[typeOfT.Field(i).Name], 10, 64)
f.SetInt(i)
case "string":
f.SetString((*conf)[typeOfT.Field(i).Name])
}
}
return &c
}
func hashID(id string) []byte {
hash := sha1.New()
hash.Write([]byte(id))
return hash.Sum(nil)
}