authorizer/server/memorystore/providers/redis/store.go

197 lines
5.9 KiB
Go
Raw Normal View History

2022-05-29 11:52:46 +00:00
package redis
import (
2023-04-03 04:56:27 +00:00
"fmt"
2022-05-31 07:41:54 +00:00
"strconv"
2022-05-29 11:52:46 +00:00
2022-05-31 07:41:54 +00:00
"github.com/authorizerdev/authorizer/server/constants"
2022-05-29 11:52:46 +00:00
log "github.com/sirupsen/logrus"
)
var (
2022-06-11 13:40:39 +00:00
// state store prefix
stateStorePrefix = "authorizer_state:"
2022-05-29 11:52:46 +00:00
// env store prefix
2022-05-31 07:41:54 +00:00
envStorePrefix = "authorizer_env"
2022-05-29 11:52:46 +00:00
)
// SetUserSession sets the user session for given user identifier in form recipe:user_id
2022-06-11 13:40:39 +00:00
func (c *provider) SetUserSession(userId, key, token string) error {
2023-04-03 04:56:27 +00:00
err := c.store.Set(c.ctx, fmt.Sprintf("%s:%s", userId, key), token, 0).Err()
2022-05-29 11:52:46 +00:00
if err != nil {
2023-04-03 04:56:27 +00:00
log.Debug("Error saving user session to redis: ", err)
2022-05-29 11:52:46 +00:00
return err
}
return nil
}
2022-06-11 13:40:39 +00:00
// GetUserSession returns the user session from redis store.
func (c *provider) GetUserSession(userId, key string) (string, error) {
2023-04-03 04:56:27 +00:00
data, err := c.store.Get(c.ctx, fmt.Sprintf("%s:%s", userId, key)).Result()
2022-06-11 13:40:39 +00:00
if err != nil {
return "", err
}
2022-06-11 18:57:21 +00:00
return data, nil
2022-05-29 11:52:46 +00:00
}
2022-06-11 13:40:39 +00:00
// DeleteUserSession deletes the user session from redis store.
func (c *provider) DeleteUserSession(userId, key string) error {
2023-04-03 04:56:27 +00:00
if err := c.store.Del(c.ctx, fmt.Sprintf("%s:%s", userId, constants.TokenTypeSessionToken+"_"+key)).Err(); err != nil {
2022-06-11 18:57:21 +00:00
log.Debug("Error deleting user session from redis: ", err)
return err
}
2023-04-03 04:56:27 +00:00
if err := c.store.Del(c.ctx, fmt.Sprintf("%s:%s", userId, constants.TokenTypeAccessToken+"_"+key)).Err(); err != nil {
2022-06-11 18:57:21 +00:00
log.Debug("Error deleting user session from redis: ", err)
return err
}
2023-04-03 04:56:27 +00:00
if err := c.store.Del(c.ctx, fmt.Sprintf("%s:%s", userId, constants.TokenTypeRefreshToken+"_"+key)).Err(); err != nil {
2022-06-11 13:40:39 +00:00
log.Debug("Error deleting user session from redis: ", err)
return err
2022-05-29 11:52:46 +00:00
}
2022-06-11 13:40:39 +00:00
return nil
}
2022-05-29 11:52:46 +00:00
2022-06-11 13:40:39 +00:00
// DeleteAllUserSessions deletes all the user session from redis
func (c *provider) DeleteAllUserSessions(userID string) error {
namespaces := []string{
constants.AuthRecipeMethodBasicAuth,
constants.AuthRecipeMethodMagicLinkLogin,
constants.AuthRecipeMethodApple,
constants.AuthRecipeMethodFacebook,
constants.AuthRecipeMethodGithub,
constants.AuthRecipeMethodGoogle,
constants.AuthRecipeMethodLinkedIn,
2022-08-13 07:05:00 +00:00
constants.AuthRecipeMethodTwitter,
2023-02-25 23:53:02 +00:00
constants.AuthRecipeMethodMicrosoft,
}
for _, namespace := range namespaces {
err := c.store.Del(c.ctx, namespace+":"+userID).Err()
if err != nil {
log.Debug("Error deleting all user sessions from redis: ", err)
return err
}
2022-06-11 13:40:39 +00:00
}
2022-05-29 11:52:46 +00:00
return nil
}
2022-07-01 16:32:34 +00:00
// DeleteSessionForNamespace to delete session for a given namespace example google,github
func (c *provider) DeleteSessionForNamespace(namespace string) error {
var cursor uint64
for {
keys := []string{}
keys, cursor, err := c.store.Scan(c.ctx, cursor, namespace+":*", 0).Result()
if err != nil {
log.Debugf("Error scanning keys for %s namespace: %s", namespace, err.Error())
return err
}
for _, key := range keys {
err := c.store.Del(c.ctx, key).Err()
if err != nil {
log.Debugf("Error deleting sessions for %s namespace: %s", namespace, err.Error())
return err
}
}
if cursor == 0 { // no more keys
break
}
}
return nil
}
2022-05-29 11:52:46 +00:00
// SetState sets the state in redis store.
func (c *provider) SetState(key, value string) error {
2022-06-11 13:40:39 +00:00
err := c.store.Set(c.ctx, stateStorePrefix+key, value, 0).Err()
2022-05-29 11:52:46 +00:00
if err != nil {
log.Debug("Error saving redis token: ", err)
return err
}
return nil
}
// GetState gets the state from redis store.
func (c *provider) GetState(key string) (string, error) {
2022-06-11 18:57:21 +00:00
data, err := c.store.Get(c.ctx, stateStorePrefix+key).Result()
2022-05-29 11:52:46 +00:00
if err != nil {
log.Debug("error getting token from redis store: ", err)
2022-06-11 18:57:21 +00:00
return "", err
2022-05-29 11:52:46 +00:00
}
2022-06-11 18:57:21 +00:00
return data, err
2022-05-29 11:52:46 +00:00
}
// RemoveState removes the state from redis store.
func (c *provider) RemoveState(key string) error {
2022-06-11 13:40:39 +00:00
err := c.store.Del(c.ctx, stateStorePrefix+key).Err()
2022-05-29 11:52:46 +00:00
if err != nil {
log.Fatalln("Error deleting redis token: ", err)
return err
}
return nil
}
// UpdateEnvStore to update the whole env store object
func (c *provider) UpdateEnvStore(store map[string]interface{}) error {
for key, value := range store {
2022-05-31 07:41:54 +00:00
err := c.store.HSet(c.ctx, envStorePrefix, key, value).Err()
2022-05-29 11:52:46 +00:00
if err != nil {
return err
}
}
return nil
}
// GetEnvStore returns the whole env store object
func (c *provider) GetEnvStore() (map[string]interface{}, error) {
2022-05-31 07:41:54 +00:00
res := make(map[string]interface{})
data, err := c.store.HGetAll(c.ctx, envStorePrefix).Result()
2022-05-29 11:52:46 +00:00
if err != nil {
return nil, err
}
2022-05-31 07:41:54 +00:00
for key, value := range data {
2022-12-21 17:44:24 +00:00
if key == constants.EnvKeyDisableBasicAuthentication || key == constants.EnvKeyDisableMobileBasicAuthentication || key == constants.EnvKeyDisableEmailVerification || key == constants.EnvKeyDisableLoginPage || key == constants.EnvKeyDisableMagicLinkLogin || key == constants.EnvKeyDisableRedisForEnv || key == constants.EnvKeyDisableSignUp || key == constants.EnvKeyDisableStrongPassword || key == constants.EnvKeyIsEmailServiceEnabled || key == constants.EnvKeyEnforceMultiFactorAuthentication || key == constants.EnvKeyDisableMultiFactorAuthentication || key == constants.EnvKeyAppCookieSecure || key == constants.EnvKeyAdminCookieSecure {
2022-05-31 07:41:54 +00:00
boolValue, err := strconv.ParseBool(value)
if err != nil {
return res, err
}
res[key] = boolValue
} else {
res[key] = value
}
}
2022-05-29 11:52:46 +00:00
return res, nil
}
// UpdateEnvVariable to update the particular env variable
func (c *provider) UpdateEnvVariable(key string, value interface{}) error {
2022-05-31 07:41:54 +00:00
err := c.store.HSet(c.ctx, envStorePrefix, key, value).Err()
2022-05-29 11:52:46 +00:00
if err != nil {
log.Debug("Error saving redis token: ", err)
return err
}
return nil
}
// GetStringStoreEnvVariable to get the string env variable from env store
func (c *provider) GetStringStoreEnvVariable(key string) (string, error) {
2022-06-11 18:57:21 +00:00
data, err := c.store.HGet(c.ctx, envStorePrefix, key).Result()
2022-05-29 11:52:46 +00:00
if err != nil {
2022-05-31 07:41:54 +00:00
return "", nil
2022-05-29 11:52:46 +00:00
}
2022-06-11 18:57:21 +00:00
return data, nil
2022-05-29 11:52:46 +00:00
}
// GetBoolStoreEnvVariable to get the bool env variable from env store
func (c *provider) GetBoolStoreEnvVariable(key string) (bool, error) {
2022-06-11 18:57:21 +00:00
data, err := c.store.HGet(c.ctx, envStorePrefix, key).Result()
2022-05-29 11:52:46 +00:00
if err != nil {
2022-05-31 07:41:54 +00:00
return false, nil
2022-05-29 11:52:46 +00:00
}
2022-06-11 18:57:21 +00:00
return data == "1", nil
2022-05-29 11:52:46 +00:00
}