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

256 lines
8.2 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"
2024-03-02 07:08:38 +00:00
"encoding/json"
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-24 03:58:36 +00:00
// SetMfaSession sets the mfa session with key and value of userId
func (c *provider) SetMfaSession(userId, key string, expiration int64) error {
currentTime := time.Now()
expireTime := time.Unix(expiration, 0)
duration := expireTime.Sub(currentTime)
2023-07-24 03:58:36 +00:00
err := c.store.Set(c.ctx, fmt.Sprintf("%s%s:%s", mfaSessionPrefix, userId, key), userId, duration).Err()
if err != nil {
log.Debug("Error saving user session to redis: ", err)
return err
}
return nil
}
2023-07-24 03:58:36 +00:00
// GetMfaSession returns value of given mfa session
func (c *provider) GetMfaSession(userId, key string) (string, error) {
data, err := c.store.Get(c.ctx, fmt.Sprintf("%s%s:%s", mfaSessionPrefix, userId, 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.
2023-07-24 03:58:36 +00:00
func (c *provider) DeleteMfaSession(userId, key string) error {
if err := c.store.Del(c.ctx, fmt.Sprintf("%s%s:%s", mfaSessionPrefix, userId, 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 {
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.EnvKeyIsSMSServiceEnabled || key == constants.EnvKeyEnforceMultiFactorAuthentication || key == constants.EnvKeyDisableMultiFactorAuthentication || key == constants.EnvKeyAppCookieSecure || key == constants.EnvKeyAdminCookieSecure || key == constants.EnvKeyDisablePlayGround || key == constants.EnvKeyDisableTOTPLogin || key == constants.EnvKeyDisableMailOTPLogin {
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
}
2024-02-22 07:40:39 +00:00
2024-03-02 06:56:48 +00:00
type UserProfile struct {
ID string `json:"id"`
// Add other fields as necessary
}
2024-02-22 07:40:39 +00:00
// GetUserAppDataFromRedis retrieves user profile and follows from Redis, combines them into a JSON format,
// and assigns the JSON string to the provided user's ID.
func (c *provider) GetUserAppDataFromRedis(userId string) (string, error) {
2024-03-02 07:12:05 +00:00
userProfileString, err := c.store.Get(c.ctx, fmt.Sprintf(`user:%s:author`, userId)).Result()
if err != nil {
return "", err
}
2024-03-02 06:56:48 +00:00
// Parse userProfileString into a UserProfile struct
var userProfile UserProfile
2024-03-02 07:19:56 +00:00
err = json.Unmarshal([]byte(userProfileString), &userProfile)
2024-03-02 06:56:48 +00:00
if err != nil {
return "", err
}
// Use userProfile.ID here if necessary
authorId := userProfile.ID
userFollowsAuthors := c.store.Get(c.ctx, fmt.Sprintf(`author:%s:follows-authors`, authorId))
userFollowsTopics := c.store.Get(c.ctx, fmt.Sprintf(`author:%s:follows-topics`, authorId))
userFollowers := c.store.Get(c.ctx, fmt.Sprintf(`author:%s:followers`, authorId))
2024-02-22 07:40:39 +00:00
// Combine user data into a JSON string
2024-03-02 06:56:48 +00:00
combinedData := fmt.Sprintf(
`{"profile": %s, "authors": %s, "topics": %s, "followers": %s}`,
userProfile, userFollowsAuthors, userFollowsTopics, userFollowers)
2024-02-22 07:40:39 +00:00
return combinedData, nil
}