Skip to content

Commit 9a284c0

Browse files
committed
fix: redis session
1 parent c8fe05e commit 9a284c0

File tree

9 files changed

+34
-31
lines changed

9 files changed

+34
-31
lines changed

server/go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ require (
2121
github.com/joho/godotenv v1.3.0
2222
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
2323
github.com/pelletier/go-toml/v2 v2.0.5 // indirect
24+
github.com/redis/go-redis/v9 v9.0.3 // indirect
2425
github.com/robertkrimen/otto v0.0.0-20211024170158-b87d35c0b86f
2526
github.com/sirupsen/logrus v1.8.1
2627
github.com/stretchr/testify v1.8.0

server/go.sum

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,15 @@ github.com/bitly/go-hostpool v0.0.0-20171023180738-a3a6125de932 h1:mXoPYz/Ul5HYE
5858
github.com/bitly/go-hostpool v0.0.0-20171023180738-a3a6125de932/go.mod h1:NOuUCSz6Q9T7+igc/hlvDOUdtWKryOrtFyIVABv/p7k=
5959
github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869 h1:DDGfHa7BWjL4YnC6+E63dPcxHo2sUxDIu8g3QgEJdRY=
6060
github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869/go.mod h1:Ekp36dRnpXw/yCqJaO+ZrUyxD+3VXMFFr56k5XYrpB4=
61+
github.com/bsm/ginkgo/v2 v2.7.0/go.mod h1:AiKlXPm7ItEHNc/2+OkrNG4E0ITzojb9/xWzvQ9XZ9w=
62+
github.com/bsm/gomega v1.26.0/go.mod h1:JyEr/xRbxbtgWNi8tIEVPUYZ5Dzef52k01W3YH0H+O0=
6163
github.com/cenkalti/backoff/v4 v4.1.2 h1:6Yo7N8UP2K6LWZnW94DLVSSrbobcWdVzAYOisuDPIFo=
6264
github.com/cenkalti/backoff/v4 v4.1.2/go.mod h1:scbssz8iZGpm3xbr14ovlUdkxfGXNInqkPWOWmG2CLw=
6365
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
6466
github.com/cespare/xxhash/v2 v2.1.1 h1:6MnRN8NT7+YBpUIWxHtefFZOKTAPgGjpQSxqLNn0+qY=
6567
github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
68+
github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44=
69+
github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
6670
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
6771
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
6872
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
@@ -295,6 +299,8 @@ github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE
295299
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
296300
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
297301
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
302+
github.com/redis/go-redis/v9 v9.0.3 h1:+7mmR26M0IvyLxGZUHxu4GiBkJkVDid0Un+j4ScYu4k=
303+
github.com/redis/go-redis/v9 v9.0.3/go.mod h1:WqMKv5vnQbRuZstUwxQI195wHy+t4PuXDOjzMvcuQHk=
298304
github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0 h1:OdAsTTz6OkFY5QxjkYwrChwuRruF69c169dPK26NUlk=
299305
github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
300306
github.com/robertkrimen/otto v0.0.0-20211024170158-b87d35c0b86f h1:a7clxaGmmqtdNTXyvrp/lVO/Gnkzlhc/+dLs5v965GM=

server/memorystore/providers/inmemory/store.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,6 @@ func (c *provider) SetUserSession(userId, key, token string) error {
1313
return nil
1414
}
1515

16-
// GetAllUserSessions returns all the user sessions token from the in-memory store.
17-
func (c *provider) GetAllUserSessions(userId string) (map[string]string, error) {
18-
data := c.sessionStore.GetAll(userId)
19-
return data, nil
20-
}
21-
2216
// GetUserSession returns value for given session token
2317
func (c *provider) GetUserSession(userId, sessionToken string) (string, error) {
2418
return c.sessionStore.Get(userId, sessionToken), nil

server/memorystore/providers/inmemory/stores/env_store.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,15 @@ func (e *EnvStore) UpdateStore(store map[string]interface{}) {
3131

3232
// GetStore returns the env store
3333
func (e *EnvStore) GetStore() map[string]interface{} {
34+
e.mutex.Lock()
35+
defer e.mutex.Unlock()
3436
return e.store
3537
}
3638

3739
// Get returns the value of the key in evn store
3840
func (e *EnvStore) Get(key string) interface{} {
41+
e.mutex.Lock()
42+
defer e.mutex.Unlock()
3943
return e.store[key]
4044
}
4145

server/memorystore/providers/inmemory/stores/session_store.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,31 @@ import (
55
"sync"
66
)
77

8+
// SessionEntry is the struct for entry stored in store
9+
type SessionEntry struct {
10+
Value string
11+
ExpiresAt int64
12+
}
13+
814
// SessionStore struct to store the env variables
915
type SessionStore struct {
1016
mutex sync.Mutex
11-
store map[string]map[string]string
17+
store map[string]map[string]*SessionEntry
1218
}
1319

1420
// NewSessionStore create a new session store
1521
func NewSessionStore() *SessionStore {
1622
return &SessionStore{
1723
mutex: sync.Mutex{},
18-
store: make(map[string]map[string]string),
24+
store: make(map[string]map[string]*SessionEntry),
1925
}
2026
}
2127

2228
// Get returns the value of the key in state store
2329
func (s *SessionStore) Get(key, subKey string) string {
24-
return s.store[key][subKey]
30+
s.mutex.Lock()
31+
defer s.mutex.Unlock()
32+
return s.store[key][subKey].Value
2533
}
2634

2735
// Set sets the value of the key in state store

server/memorystore/providers/inmemory/stores/state_store.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ func NewStateStore() *StateStore {
2020

2121
// Get returns the value of the key in state store
2222
func (s *StateStore) Get(key string) string {
23+
s.mutex.Lock()
24+
defer s.mutex.Unlock()
2325
return s.store[key]
2426
}
2527

server/memorystore/providers/providers.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ package providers
44
type Provider interface {
55
// SetUserSession sets the user session for given user identifier in form recipe:user_id
66
SetUserSession(userId, key, token string) error
7-
// GetAllUserSessions returns all the user sessions from the session store
8-
GetAllUserSessions(userId string) (map[string]string, error)
97
// GetUserSession returns the session token for given token
108
GetUserSession(userId, key string) (string, error)
119
// DeleteUserSession deletes the user session

server/memorystore/providers/redis/provider.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"strings"
66
"time"
77

8-
"github.com/go-redis/redis/v8"
8+
"github.com/redis/go-redis/v9"
99
log "github.com/sirupsen/logrus"
1010
)
1111

@@ -17,10 +17,11 @@ type RedisClient interface {
1717
HMGet(ctx context.Context, key string, fields ...string) *redis.SliceCmd
1818
HSet(ctx context.Context, key string, values ...interface{}) *redis.IntCmd
1919
HGet(ctx context.Context, key, field string) *redis.StringCmd
20-
HGetAll(ctx context.Context, key string) *redis.StringStringMapCmd
20+
HGetAll(ctx context.Context, key string) *redis.MapStringStringCmd
2121
Set(ctx context.Context, key string, value interface{}, expiration time.Duration) *redis.StatusCmd
2222
Get(ctx context.Context, key string) *redis.StringCmd
2323
Scan(ctx context.Context, cursor uint64, match string, count int64) *redis.ScanCmd
24+
Keys(ctx context.Context, pattern string) *redis.StringSliceCmd
2425
}
2526

2627
type provider struct {
@@ -70,7 +71,6 @@ func NewRedisProvider(redisURL string) (*provider, error) {
7071
log.Debug("error connecting to redis: ", err)
7172
return nil, err
7273
}
73-
7474
return &provider{
7575
ctx: ctx,
7676
store: rdb,

server/memorystore/providers/redis/store.go

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package redis
22

33
import (
4+
"fmt"
45
"strconv"
56

67
"github.com/authorizerdev/authorizer/server/constants"
@@ -16,28 +17,17 @@ var (
1617

1718
// SetUserSession sets the user session for given user identifier in form recipe:user_id
1819
func (c *provider) SetUserSession(userId, key, token string) error {
19-
err := c.store.HSet(c.ctx, userId, key, token).Err()
20+
err := c.store.Set(c.ctx, fmt.Sprintf("%s:%s", userId, key), token, 0).Err()
2021
if err != nil {
21-
log.Debug("Error saving to redis: ", err)
22+
log.Debug("Error saving user session to redis: ", err)
2223
return err
2324
}
2425
return nil
2526
}
2627

27-
// GetAllUserSessions returns all the user session token from the redis store.
28-
func (c *provider) GetAllUserSessions(userID string) (map[string]string, error) {
29-
data, err := c.store.HGetAll(c.ctx, userID).Result()
30-
if err != nil {
31-
log.Debug("error getting all user sessions from redis store: ", err)
32-
return nil, err
33-
}
34-
35-
return data, nil
36-
}
37-
3828
// GetUserSession returns the user session from redis store.
3929
func (c *provider) GetUserSession(userId, key string) (string, error) {
40-
data, err := c.store.HGet(c.ctx, userId, key).Result()
30+
data, err := c.store.Get(c.ctx, fmt.Sprintf("%s:%s", userId, key)).Result()
4131
if err != nil {
4232
return "", err
4333
}
@@ -46,15 +36,15 @@ func (c *provider) GetUserSession(userId, key string) (string, error) {
4636

4737
// DeleteUserSession deletes the user session from redis store.
4838
func (c *provider) DeleteUserSession(userId, key string) error {
49-
if err := c.store.HDel(c.ctx, userId, constants.TokenTypeSessionToken+"_"+key).Err(); err != nil {
39+
if err := c.store.Del(c.ctx, fmt.Sprintf("%s:%s", userId, constants.TokenTypeSessionToken+"_"+key)).Err(); err != nil {
5040
log.Debug("Error deleting user session from redis: ", err)
5141
return err
5242
}
53-
if err := c.store.HDel(c.ctx, userId, constants.TokenTypeAccessToken+"_"+key).Err(); err != nil {
43+
if err := c.store.Del(c.ctx, fmt.Sprintf("%s:%s", userId, constants.TokenTypeAccessToken+"_"+key)).Err(); err != nil {
5444
log.Debug("Error deleting user session from redis: ", err)
5545
return err
5646
}
57-
if err := c.store.HDel(c.ctx, userId, constants.TokenTypeRefreshToken+"_"+key).Err(); err != nil {
47+
if err := c.store.Del(c.ctx, fmt.Sprintf("%s:%s", userId, constants.TokenTypeRefreshToken+"_"+key)).Err(); err != nil {
5848
log.Debug("Error deleting user session from redis: ", err)
5949
return err
6050
}

0 commit comments

Comments
 (0)