2022-01-22 19:54:41 +00:00
|
|
|
package sessionstore
|
2021-07-14 18:43:19 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"log"
|
2022-03-02 12:12:31 +00:00
|
|
|
"strings"
|
2021-07-14 18:43:19 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type RedisStore struct {
|
|
|
|
ctx context.Context
|
2022-02-17 14:24:15 +00:00
|
|
|
store RedisSessionClient
|
2021-07-14 18:43:19 +00:00
|
|
|
}
|
|
|
|
|
2022-01-17 06:02:13 +00:00
|
|
|
// ClearStore clears the redis store for authorizer related tokens
|
2021-07-14 18:43:19 +00:00
|
|
|
func (c *RedisStore) ClearStore() {
|
2021-07-28 06:23:37 +00:00
|
|
|
err := c.store.Del(c.ctx, "authorizer_*").Err()
|
2021-07-14 18:43:19 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalln("Error clearing redis store:", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-22 19:54:41 +00:00
|
|
|
// 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()
|
2022-01-22 19:54:41 +00:00
|
|
|
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]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-22 19:54:41 +00:00
|
|
|
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) {
|
|
|
|
err := c.store.Set(c.ctx, key, value, 0).Err()
|
2021-10-27 17:45:38 +00:00
|
|
|
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 {
|
2021-10-27 17:45:38 +00:00
|
|
|
state := ""
|
|
|
|
state, err := c.store.Get(c.ctx, key).Result()
|
|
|
|
if err != nil {
|
2021-12-17 15:55:07 +00:00
|
|
|
log.Println("error getting token from redis store:", err)
|
2021-10-27 17:45:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return state
|
|
|
|
}
|
|
|
|
|
2022-02-28 15:56:49 +00:00
|
|
|
// RemoveState removes the state from redis store.
|
|
|
|
func (c *RedisStore) RemoveState(key string) {
|
2021-10-27 17:45:38 +00:00
|
|
|
err := c.store.Del(c.ctx, key).Err()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalln("Error deleting redis token:", err)
|
|
|
|
}
|
|
|
|
}
|