Files
authorizer/server/sessionstore/session.go

151 lines
4.1 KiB
Go
Raw Normal View History

package sessionstore
import (
"context"
"log"
"strings"
2021-07-23 21:57:44 +05:30
"github.com/authorizerdev/authorizer/server/constants"
2022-01-17 11:32:13 +05:30
"github.com/authorizerdev/authorizer/server/envstore"
"github.com/go-redis/redis/v8"
)
2022-01-17 11:32:13 +05:30
// 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 11:32:13 +05:30
// SessionStoreObj is a global variable that holds the
// reference to various session store instances
var SessionStoreObj SessionStore
2022-01-17 11:32:13 +05:30
// DeleteAllSessions deletes all the sessions from the session store
func DeleteAllUserSession(userId string) {
if SessionStoreObj.RedisMemoryStoreObj != nil {
2022-01-17 11:32:13 +05:30
SessionStoreObj.RedisMemoryStoreObj.DeleteAllUserSession(userId)
}
if SessionStoreObj.InMemoryStoreObj != nil {
2022-01-17 11:32:13 +05:30
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 11:32:13 +05:30
// 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 21:26:49 +05:30
// SetState sets the login state (key, value form) in the session store
func SetState(key, state string) {
if SessionStoreObj.RedisMemoryStoreObj != nil {
2022-02-28 21:26:49 +05:30
SessionStoreObj.RedisMemoryStoreObj.SetState(key, state)
}
if SessionStoreObj.InMemoryStoreObj != nil {
2022-02-28 21:26:49 +05:30
SessionStoreObj.InMemoryStoreObj.SetState(key, state)
}
}
2022-02-28 21:26:49 +05:30
// GetState returns the state from the session store
func GetState(key string) string {
if SessionStoreObj.RedisMemoryStoreObj != nil {
2022-02-28 21:26:49 +05:30
return SessionStoreObj.RedisMemoryStoreObj.GetState(key)
}
if SessionStoreObj.InMemoryStoreObj != nil {
2022-02-28 21:26:49 +05:30
return SessionStoreObj.InMemoryStoreObj.GetState(key)
}
return ""
}
2022-02-28 21:26:49 +05:30
// RemoveState removes the social login state from the session store
func RemoveState(key string) {
if SessionStoreObj.RedisMemoryStoreObj != nil {
2022-02-28 21:26:49 +05:30
SessionStoreObj.RedisMemoryStoreObj.RemoveState(key)
}
if SessionStoreObj.InMemoryStoreObj != nil {
2022-02-28 21:26:49 +05:30
SessionStoreObj.InMemoryStoreObj.RemoveState(key)
}
}
2022-01-17 11:32:13 +05:30
// InitializeSessionStore initializes the SessionStoreObj based on environment variables
2022-02-26 10:06:26 +05:30
func InitSession() error {
2022-02-28 07:55:01 +05:30
if envstore.EnvStoreObj.GetStringStoreEnvVariable(constants.EnvKeyRedisURL) != "" {
log.Println("using redis store to save sessions")
2022-02-26 10:06:26 +05:30
2022-02-28 07:55:01 +05:30
redisURL := envstore.EnvStoreObj.GetStringStoreEnvVariable(constants.EnvKeyRedisURL)
2022-02-26 10:06:26 +05:30
redisURLHostPortsList := strings.Split(redisURL, ",")
if len(redisURLHostPortsList) > 1 {
opt, err := redis.ParseURL(redisURLHostPortsList[0])
if err != nil {
2022-02-26 10:06:26 +05:30
return err
}
2022-02-26 10:06:26 +05:30
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 10:06:26 +05:30
return err
}
SessionStoreObj.RedisMemoryStoreObj = &RedisStore{
ctx: ctx,
store: rdb,
}
2022-02-26 10:06:26 +05:30
// return on successful initialization
return nil
}
2022-02-26 10:06:26 +05:30
2022-02-28 07:55:01 +05:30
opt, err := redis.ParseURL(envstore.EnvStoreObj.GetStringStoreEnvVariable(constants.EnvKeyRedisURL))
if err != nil {
2022-02-26 10:06:26 +05:30
return err
}
2022-02-26 10:06:26 +05:30
rdb := redis.NewClient(opt)
ctx := context.Background()
_, err = rdb.Ping(ctx).Result()
if err != nil {
2022-02-26 10:06:26 +05:30
return err
}
2022-02-26 10:06:26 +05:30
SessionStoreObj.RedisMemoryStoreObj = &RedisStore{
ctx: ctx,
store: rdb,
}
2022-02-26 10:06:26 +05:30
// return on successful initialization
return nil
}
2022-02-26 10:06:26 +05:30
// if redis url is not set use in memory store
SessionStoreObj.InMemoryStoreObj = &InMemoryStore{
2022-02-28 21:26:49 +05:30
sessionStore: map[string]map[string]string{},
stateStore: map[string]string{},
}
2022-02-26 10:06:26 +05:30
return nil
}