authorizer/server/sessionstore/session.go

152 lines
4.1 KiB
Go
Raw Normal View History

package sessionstore
import (
"context"
"strings"
2022-05-24 07:20:33 +00:00
log "github.com/sirupsen/logrus"
2021-07-23 16:27:44 +00:00
"github.com/authorizerdev/authorizer/server/constants"
2022-01-17 06:02:13 +00:00
"github.com/authorizerdev/authorizer/server/envstore"
"github.com/go-redis/redis/v8"
)
2022-01-17 06:02:13 +00:00
// SessionStore is a struct that defines available session stores
// If redis store is available, higher preference is given to that store.
// Else in memory store is used.
type SessionStore struct {
InMemoryStoreObj *InMemoryStore
RedisMemoryStoreObj *RedisStore
}
2022-01-17 06:02:13 +00:00
// SessionStoreObj is a global variable that holds the
// reference to various session store instances
var SessionStoreObj SessionStore
2022-01-17 06:02:13 +00:00
// DeleteAllSessions deletes all the sessions from the session store
func DeleteAllUserSession(userId string) {
if SessionStoreObj.RedisMemoryStoreObj != nil {
2022-01-17 06:02:13 +00:00
SessionStoreObj.RedisMemoryStoreObj.DeleteAllUserSession(userId)
}
if SessionStoreObj.InMemoryStoreObj != nil {
2022-01-17 06:02:13 +00:00
SessionStoreObj.InMemoryStoreObj.DeleteAllUserSession(userId)
}
}
// GetUserSessions returns all the user sessions from the session store
func GetUserSessions(userId string) map[string]string {
if SessionStoreObj.RedisMemoryStoreObj != nil {
return SessionStoreObj.RedisMemoryStoreObj.GetUserSessions(userId)
}
if SessionStoreObj.InMemoryStoreObj != nil {
return SessionStoreObj.InMemoryStoreObj.GetUserSessions(userId)
}
return nil
}
2022-01-17 06:02:13 +00:00
// ClearStore clears the session store for authorizer tokens
func ClearStore() {
if SessionStoreObj.RedisMemoryStoreObj != nil {
SessionStoreObj.RedisMemoryStoreObj.ClearStore()
}
if SessionStoreObj.InMemoryStoreObj != nil {
SessionStoreObj.InMemoryStoreObj.ClearStore()
}
}
2022-02-28 15:56:49 +00:00
// SetState sets the login state (key, value form) in the session store
func SetState(key, state string) {
if SessionStoreObj.RedisMemoryStoreObj != nil {
2022-02-28 15:56:49 +00:00
SessionStoreObj.RedisMemoryStoreObj.SetState(key, state)
}
if SessionStoreObj.InMemoryStoreObj != nil {
2022-02-28 15:56:49 +00:00
SessionStoreObj.InMemoryStoreObj.SetState(key, state)
}
}
2022-02-28 15:56:49 +00:00
// GetState returns the state from the session store
func GetState(key string) string {
if SessionStoreObj.RedisMemoryStoreObj != nil {
2022-02-28 15:56:49 +00:00
return SessionStoreObj.RedisMemoryStoreObj.GetState(key)
}
if SessionStoreObj.InMemoryStoreObj != nil {
2022-02-28 15:56:49 +00:00
return SessionStoreObj.InMemoryStoreObj.GetState(key)
}
return ""
}
2022-02-28 15:56:49 +00:00
// RemoveState removes the social login state from the session store
func RemoveState(key string) {
if SessionStoreObj.RedisMemoryStoreObj != nil {
2022-02-28 15:56:49 +00:00
SessionStoreObj.RedisMemoryStoreObj.RemoveState(key)
}
if SessionStoreObj.InMemoryStoreObj != nil {
2022-02-28 15:56:49 +00:00
SessionStoreObj.InMemoryStoreObj.RemoveState(key)
}
}
2022-01-17 06:02:13 +00:00
// InitializeSessionStore initializes the SessionStoreObj based on environment variables
2022-02-26 04:36:26 +00:00
func InitSession() error {
2022-02-28 02:25:01 +00:00
if envstore.EnvStoreObj.GetStringStoreEnvVariable(constants.EnvKeyRedisURL) != "" {
2022-05-24 07:20:33 +00:00
log.Info("using redis store to save sessions")
2022-02-26 04:36:26 +00:00
2022-02-28 02:25:01 +00:00
redisURL := envstore.EnvStoreObj.GetStringStoreEnvVariable(constants.EnvKeyRedisURL)
2022-02-26 04:36:26 +00:00
redisURLHostPortsList := strings.Split(redisURL, ",")
if len(redisURLHostPortsList) > 1 {
opt, err := redis.ParseURL(redisURLHostPortsList[0])
if err != nil {
2022-02-26 04:36:26 +00:00
return err
}
2022-02-26 04:36:26 +00:00
urls := []string{opt.Addr}
urlList := redisURLHostPortsList[1:]
urls = append(urls, urlList...)
clusterOpt := &redis.ClusterOptions{Addrs: urls}
rdb := redis.NewClusterClient(clusterOpt)
ctx := context.Background()
_, err = rdb.Ping(ctx).Result()
if err != nil {
2022-02-26 04:36:26 +00:00
return err
}
SessionStoreObj.RedisMemoryStoreObj = &RedisStore{
ctx: ctx,
store: rdb,
}
2022-02-26 04:36:26 +00:00
// return on successful initialization
return nil
}
2022-02-26 04:36:26 +00:00
2022-02-28 02:25:01 +00:00
opt, err := redis.ParseURL(envstore.EnvStoreObj.GetStringStoreEnvVariable(constants.EnvKeyRedisURL))
if err != nil {
2022-02-26 04:36:26 +00:00
return err
}
2022-02-26 04:36:26 +00:00
rdb := redis.NewClient(opt)
ctx := context.Background()
_, err = rdb.Ping(ctx).Result()
if err != nil {
2022-02-26 04:36:26 +00:00
return err
}
2022-02-26 04:36:26 +00:00
SessionStoreObj.RedisMemoryStoreObj = &RedisStore{
ctx: ctx,
store: rdb,
}
2022-02-26 04:36:26 +00:00
// return on successful initialization
return nil
}
2022-02-26 04:36:26 +00:00
// if redis url is not set use in memory store
SessionStoreObj.InMemoryStoreObj = &InMemoryStore{
2022-02-28 15:56:49 +00:00
sessionStore: map[string]map[string]string{},
stateStore: map[string]string{},
}
2022-02-26 04:36:26 +00:00
return nil
}