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

221 lines
6.8 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"
2023-04-08 07:36:15 +00:00
"time"
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
)
const mfaSessionPrefix = "mfa_sess_"
// SetUserSession sets the user session for given user identifier in form recipe:user_id
2023-04-08 07:36:15 +00:00
func (c *provider) SetUserSession(userId, key, token string, expiration int64) error {
currentTime := time.Now()
expireTime := time.Unix(expiration, 0)
duration := expireTime.Sub(currentTime)
err := c.store.Set(c.ctx, fmt.Sprintf("%s:%s", userId, key), token, duration).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)
2023-04-08 07:36:15 +00:00
// continue
2022-06-11 18:57:21 +00:00
}
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)
2023-04-08 07:36:15 +00:00
// continue
2022-06-11 18:57:21 +00:00
}
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)
2023-04-08 07:36:15 +00:00
// continue
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 {
2023-04-08 07:36:15 +00:00
res := c.store.Keys(c.ctx, fmt.Sprintf("*%s*", userID))
if res.Err() != nil {
log.Debug("Error getting all user sessions from redis: ", res.Err())
return res.Err()
}
2023-04-08 07:36:15 +00:00
keys := res.Val()
for _, key := range keys {
err := c.store.Del(c.ctx, key).Err()
if err != nil {
log.Debug("Error deleting all user sessions from redis: ", err)
2023-04-08 07:36:15 +00:00
continue
}
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 {
2023-04-08 07:36:15 +00:00
res := c.store.Keys(c.ctx, fmt.Sprintf("%s:*", namespace))
if res.Err() != nil {
log.Debug("Error getting all user sessions from redis: ", res.Err())
return res.Err()
}
keys := res.Val()
for _, key := range keys {
err := c.store.Del(c.ctx, key).Err()
2022-07-01 16:32:34 +00:00
if err != nil {
2023-04-08 07:36:15 +00:00
log.Debug("Error deleting all user sessions from redis: ", err)
continue
2022-07-01 16:32:34 +00:00
}
}
return nil
}
2023-07-23 05:02:14 +00:00
// SetMfaSession sets the mfa session with key and value of email
func (c *provider) SetMfaSession(email, key string, expiration int64) error {
currentTime := time.Now()
expireTime := time.Unix(expiration, 0)
duration := expireTime.Sub(currentTime)
err := c.store.Set(c.ctx, fmt.Sprintf("%s%s:%s", mfaSessionPrefix, email, key), email, duration).Err()
if err != nil {
log.Debug("Error saving user session to redis: ", err)
return err
}
return nil
}
2023-07-23 05:02:14 +00:00
// GetMfaSession returns value of given mfa session
func (c *provider) GetMfaSession(email, key string) (string, error) {
data, err := c.store.Get(c.ctx, fmt.Sprintf("%s%s:%s", mfaSessionPrefix, email, key)).Result()
if err != nil {
return "", err
}
return data, nil
}
2023-07-23 05:02:14 +00:00
// DeleteMfaSession deletes given mfa session from in-memory store.
func (c *provider) DeleteMfaSession(email, key string) error {
if err := c.store.Del(c.ctx, fmt.Sprintf("%s%s:%s", mfaSessionPrefix, email, key)).Err(); err != nil {
log.Debug("Error deleting user session from redis: ", err)
// continue
}
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
}