2022-01-23 01:24:41 +05:30
|
|
|
package sessionstore
|
2021-07-15 00:13:19 +05:30
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"log"
|
2022-03-02 17:42:31 +05:30
|
|
|
"strings"
|
2021-07-15 00:13:19 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
type RedisStore struct {
|
|
|
|
ctx context.Context
|
2022-02-17 19:54:15 +05:30
|
|
|
store RedisSessionClient
|
2021-07-15 00:13:19 +05:30
|
|
|
}
|
|
|
|
|
2022-01-17 11:32:13 +05:30
|
|
|
// ClearStore clears the redis store for authorizer related tokens
|
2021-07-15 00:13:19 +05:30
|
|
|
func (c *RedisStore) ClearStore() {
|
2021-07-28 11:53:37 +05:30
|
|
|
err := c.store.Del(c.ctx, "authorizer_*").Err()
|
2021-07-15 00:13:19 +05:30
|
|
|
if err != nil {
|
|
|
|
log.Fatalln("Error clearing redis store:", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-23 01:24:41 +05:30
|
|
|
// GetUserSessions returns all the user session token from the redis store.
|
|
|
|
func (c *RedisStore) GetUserSessions(userID string) map[string]string {
|
2022-03-02 17:42:31 +05:30
|
|
|
data, err := c.store.HGetAll(c.ctx, "*").Result()
|
2022-01-23 01:24:41 +05:30
|
|
|
if err != nil {
|
|
|
|
log.Println("error getting token from redis store:", err)
|
|
|
|
}
|
|
|
|
|
2022-03-02 17:42:31 +05:30
|
|
|
res := map[string]string{}
|
|
|
|
for k, v := range data {
|
|
|
|
split := strings.Split(v, "@")
|
|
|
|
if split[1] == userID {
|
|
|
|
res[k] = split[0]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-23 01:24:41 +05:30
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2022-03-02 17:42:31 +05:30
|
|
|
// 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 21:26:49 +05:30
|
|
|
// SetState sets the state in redis store.
|
2022-03-02 17:42:31 +05:30
|
|
|
func (c *RedisStore) SetState(key, value string) {
|
2022-03-04 00:36:27 +05:30
|
|
|
err := c.store.Set(c.ctx, "authorizer_"+key, value, 0).Err()
|
2021-10-27 23:15:38 +05:30
|
|
|
if err != nil {
|
|
|
|
log.Fatalln("Error saving redis token:", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-28 21:26:49 +05:30
|
|
|
// GetState gets the state from redis store.
|
|
|
|
func (c *RedisStore) GetState(key string) string {
|
2021-10-27 23:15:38 +05:30
|
|
|
state := ""
|
2022-03-04 00:36:27 +05:30
|
|
|
state, err := c.store.Get(c.ctx, "authorizer_"+key).Result()
|
2021-10-27 23:15:38 +05:30
|
|
|
if err != nil {
|
2021-12-17 21:25:07 +05:30
|
|
|
log.Println("error getting token from redis store:", err)
|
2021-10-27 23:15:38 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
return state
|
|
|
|
}
|
|
|
|
|
2022-02-28 21:26:49 +05:30
|
|
|
// RemoveState removes the state from redis store.
|
|
|
|
func (c *RedisStore) RemoveState(key string) {
|
2022-03-04 00:36:27 +05:30
|
|
|
err := c.store.Del(c.ctx, "authorizer_"+key).Err()
|
2021-10-27 23:15:38 +05:30
|
|
|
if err != nil {
|
|
|
|
log.Fatalln("Error deleting redis token:", err)
|
|
|
|
}
|
|
|
|
}
|