fix: env saving

This commit is contained in:
Lakhan Samani
2022-05-31 13:11:54 +05:30
parent cf8762b7a0
commit eeff88c853
27 changed files with 532 additions and 493 deletions

View File

@@ -1,16 +1,18 @@
package redis
import (
"strconv"
"strings"
"github.com/authorizerdev/authorizer/server/constants"
log "github.com/sirupsen/logrus"
)
var (
// session store prefix
sessionStorePrefix = "authorizer_session_"
sessionStorePrefix = "authorizer_session:"
// env store prefix
envStorePrefix = "authorizer_env_"
envStorePrefix = "authorizer_env"
)
// ClearStore clears the redis store for authorizer related tokens
@@ -94,9 +96,8 @@ func (c *provider) RemoveState(key string) error {
// UpdateEnvStore to update the whole env store object
func (c *provider) UpdateEnvStore(store map[string]interface{}) error {
for key, value := range store {
err := c.store.Set(c.ctx, envStorePrefix+key, value, 0).Err()
err := c.store.HSet(c.ctx, envStorePrefix, key, value).Err()
if err != nil {
log.Debug("Error saving redis token: ", err)
return err
}
}
@@ -105,19 +106,28 @@ func (c *provider) UpdateEnvStore(store map[string]interface{}) error {
// GetEnvStore returns the whole env store object
func (c *provider) GetEnvStore() (map[string]interface{}, error) {
var res map[string]interface{}
err := c.store.HGetAll(c.ctx, envStorePrefix+"*").Scan(res)
res := make(map[string]interface{})
data, err := c.store.HGetAll(c.ctx, envStorePrefix).Result()
if err != nil {
log.Debug("error getting token from redis store: ", err)
return nil, err
}
for key, value := range data {
if key == constants.EnvKeyDisableBasicAuthentication || key == constants.EnvKeyDisableEmailVerification || key == constants.EnvKeyDisableLoginPage || key == constants.EnvKeyDisableMagicLinkLogin || key == constants.EnvKeyDisableRedisForEnv || key == constants.EnvKeyDisableSignUp {
boolValue, err := strconv.ParseBool(value)
if err != nil {
return res, err
}
res[key] = boolValue
} else {
res[key] = value
}
}
return res, nil
}
// UpdateEnvVariable to update the particular env variable
func (c *provider) UpdateEnvVariable(key string, value interface{}) error {
err := c.store.Set(c.ctx, envStorePrefix+key, value, 0).Err()
err := c.store.HSet(c.ctx, envStorePrefix, key, value).Err()
if err != nil {
log.Debug("Error saving redis token: ", err)
return err
@@ -128,10 +138,9 @@ func (c *provider) UpdateEnvVariable(key string, value interface{}) error {
// GetStringStoreEnvVariable to get the string env variable from env store
func (c *provider) GetStringStoreEnvVariable(key string) (string, error) {
var res string
err := c.store.Get(c.ctx, envStorePrefix+key).Scan(&res)
err := c.store.HGet(c.ctx, envStorePrefix, key).Scan(&res)
if err != nil {
log.Debug("error getting token from redis store: ", err)
return "", err
return "", nil
}
return res, nil
@@ -140,10 +149,9 @@ func (c *provider) GetStringStoreEnvVariable(key string) (string, error) {
// GetBoolStoreEnvVariable to get the bool env variable from env store
func (c *provider) GetBoolStoreEnvVariable(key string) (bool, error) {
var res bool
err := c.store.Get(c.ctx, envStorePrefix+key).Scan(res)
err := c.store.HGet(c.ctx, envStorePrefix, key).Scan(res)
if err != nil {
log.Debug("error getting token from redis store: ", err)
return false, err
return false, nil
}
return res, nil