authorizer/server/sessionstore/redis_store.go

79 lines
1.8 KiB
Go
Raw Normal View History

package sessionstore
import (
"context"
"log"
2022-03-02 12:12:31 +00:00
"strings"
)
type RedisStore struct {
ctx context.Context
store RedisSessionClient
}
2022-01-17 06:02:13 +00:00
// ClearStore clears the redis store for authorizer related tokens
func (c *RedisStore) ClearStore() {
2021-07-28 06:23:37 +00:00
err := c.store.Del(c.ctx, "authorizer_*").Err()
if err != nil {
log.Fatalln("Error clearing redis store:", err)
}
}
// GetUserSessions returns all the user session token from the redis store.
func (c *RedisStore) GetUserSessions(userID string) map[string]string {
2022-03-02 12:12:31 +00:00
data, err := c.store.HGetAll(c.ctx, "*").Result()
if err != nil {
log.Println("error getting token from redis store:", err)
}
2022-03-02 12:12:31 +00:00
res := map[string]string{}
for k, v := range data {
split := strings.Split(v, "@")
if split[1] == userID {
res[k] = split[0]
}
}
return res
}
2022-03-02 12:12:31 +00:00
// DeleteAllUserSession deletes all the user session from redis
func (c *RedisStore) DeleteAllUserSession(userId string) {
sessions := GetUserSessions(userId)
for k, v := range sessions {
if k == "token" {
err := c.store.Del(c.ctx, v)
if err != nil {
log.Println("Error deleting redis token:", err)
}
}
}
}
2022-02-28 15:56:49 +00:00
// SetState sets the state in redis store.
2022-03-02 12:12:31 +00:00
func (c *RedisStore) SetState(key, value string) {
2022-03-03 19:06:27 +00:00
err := c.store.Set(c.ctx, "authorizer_"+key, value, 0).Err()
if err != nil {
log.Fatalln("Error saving redis token:", err)
}
}
2022-02-28 15:56:49 +00:00
// GetState gets the state from redis store.
func (c *RedisStore) GetState(key string) string {
state := ""
2022-03-03 19:06:27 +00:00
state, err := c.store.Get(c.ctx, "authorizer_"+key).Result()
if err != nil {
log.Println("error getting token from redis store:", err)
}
return state
}
2022-02-28 15:56:49 +00:00
// RemoveState removes the state from redis store.
func (c *RedisStore) RemoveState(key string) {
2022-03-03 19:06:27 +00:00
err := c.store.Del(c.ctx, "authorizer_"+key).Err()
if err != nil {
log.Fatalln("Error deleting redis token:", err)
}
}