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"
|
2024-05-21 00:37:14 +00:00
|
|
|
|
|
2024-05-21 00:38:56 +00:00
|
|
|
|
"github.com/redis/go-redis/v9"
|
2022-05-29 11:52:46 +00:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
)
|
|
|
|
|
|
2023-07-20 07:11:39 +00:00
|
|
|
|
const mfaSessionPrefix = "mfa_sess_"
|
|
|
|
|
|
2022-08-29 02:48:20 +00:00
|
|
|
|
// 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()
|
2022-06-29 16:54:00 +00:00
|
|
|
|
}
|
2023-04-08 07:36:15 +00:00
|
|
|
|
keys := res.Val()
|
|
|
|
|
for _, key := range keys {
|
|
|
|
|
err := c.store.Del(c.ctx, key).Err()
|
2022-06-29 16:54:00 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
log.Debug("Error deleting all user sessions from redis: ", err)
|
2023-04-08 07:36:15 +00:00
|
|
|
|
continue
|
2022-06-29 16:54:00 +00:00
|
|
|
|
}
|
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 {
|
2023-07-20 07:11:39 +00:00
|
|
|
|
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()
|
2023-07-20 07:11:39 +00:00
|
|
|
|
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()
|
2023-07-20 07:11:39 +00:00
|
|
|
|
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 {
|
2023-07-20 07:11:39 +00:00
|
|
|
|
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 {
|
2023-11-16 13:00:54 +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.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-03-02 11:51:37 +00:00
|
|
|
|
|
2024-03-02 11:06:20 +00:00
|
|
|
|
type AuthorProfile struct {
|
|
|
|
|
ID int `json:"id"`
|
2024-03-02 06:56:48 +00:00
|
|
|
|
// 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-06-05 20:40:09 +00:00
|
|
|
|
// Получаем ID автора из Redis
|
2024-06-06 04:51:45 +00:00
|
|
|
|
rkey := fmt.Sprintf("author:user:%s", userId)
|
|
|
|
|
fmt.Println("get redis cached by key:", rkey)
|
|
|
|
|
authorIdString, err := c.store.Get(c.ctx, rkey).Result()
|
2024-06-06 06:46:38 +00:00
|
|
|
|
fmt.Println("redis found string value:", authorIdString)
|
2024-06-05 20:40:09 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Преобразуем ID автора из строки в int
|
2024-06-05 20:47:41 +00:00
|
|
|
|
var authorIdFloat float64
|
|
|
|
|
err = json.Unmarshal([]byte(authorIdString), &authorIdFloat)
|
2024-06-05 20:40:09 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
2024-06-05 20:47:41 +00:00
|
|
|
|
authorId := int(authorIdFloat)
|
2024-06-06 04:51:45 +00:00
|
|
|
|
fmt.Println("recognized author id: ", authorId)
|
2024-06-05 20:40:09 +00:00
|
|
|
|
|
2024-05-21 00:25:15 +00:00
|
|
|
|
// Получаем профиль автора из Redis
|
2024-06-05 20:40:09 +00:00
|
|
|
|
authorProfileString, err := c.store.Get(c.ctx, fmt.Sprintf("author:id:%d", authorId)).Result()
|
2024-03-02 07:12:05 +00:00
|
|
|
|
if err != nil {
|
2024-03-02 11:14:54 +00:00
|
|
|
|
return "", err
|
2024-03-02 07:12:05 +00:00
|
|
|
|
}
|
2024-03-02 06:56:48 +00:00
|
|
|
|
|
2024-05-21 00:25:15 +00:00
|
|
|
|
// Парсим профиль пользователя в map
|
2024-03-02 11:14:54 +00:00
|
|
|
|
var authorProfileMap map[string]interface{}
|
|
|
|
|
err = json.Unmarshal([]byte(authorProfileString), &authorProfileMap)
|
2024-03-02 06:56:48 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
2024-03-02 11:22:27 +00:00
|
|
|
|
|
2024-05-21 00:25:15 +00:00
|
|
|
|
// Начинаем сбор данных в общий JSON
|
|
|
|
|
combinedData := map[string]interface{}{
|
|
|
|
|
"profile": authorProfileMap,
|
|
|
|
|
}
|
2024-03-02 11:51:37 +00:00
|
|
|
|
|
2024-05-21 00:25:15 +00:00
|
|
|
|
// Получаем подписки автора на других авторов
|
|
|
|
|
if authorsObjectsString, err := c.getFollowedObjectsString(fmt.Sprintf("author:follows-authors:%d", authorId), "author:id:%d"); err == nil {
|
|
|
|
|
combinedData["authors"] = authorsObjectsString
|
2024-03-02 11:35:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-05-21 00:25:15 +00:00
|
|
|
|
// Получаем подписки автора на темы
|
|
|
|
|
if topicsObjectString, err := c.getFollowedObjectsString(fmt.Sprintf("author:follows-topics:%d", authorId), "topic:id:%d"); err == nil {
|
|
|
|
|
combinedData["topics"] = topicsObjectString
|
2024-03-02 11:35:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-05-21 00:25:15 +00:00
|
|
|
|
// Получаем подписчиков автора
|
|
|
|
|
if authorFollowersObjectsString, err := c.getFollowedObjectsString(fmt.Sprintf("author:followers:%d", authorId), "author:id:%d"); err == nil {
|
|
|
|
|
combinedData["followers"] = authorFollowersObjectsString
|
2024-03-02 11:35:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-05-21 00:25:15 +00:00
|
|
|
|
// Преобразуем собранные данные в JSON строку
|
|
|
|
|
combinedDataString, err := json.Marshal(combinedData)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
2024-03-02 11:35:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-05-21 00:25:15 +00:00
|
|
|
|
return string(combinedDataString), nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Универсальная функция для получения объектов по списку ID из Redis
|
|
|
|
|
func (c *provider) getFollowedObjectsString(followKey string, objectKeyPattern string) ([]map[string]interface{}, error) {
|
|
|
|
|
followsString, err := c.store.Get(c.ctx, followKey).Result()
|
|
|
|
|
if err != nil {
|
|
|
|
|
if err == redis.Nil {
|
|
|
|
|
return []map[string]interface{}{}, nil
|
|
|
|
|
}
|
|
|
|
|
return nil, err
|
2024-03-02 11:35:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-05-21 00:25:15 +00:00
|
|
|
|
var ids []int
|
|
|
|
|
err = json.Unmarshal([]byte(followsString), &ids)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
objects := make([]map[string]interface{}, 0, len(ids))
|
|
|
|
|
for _, id := range ids {
|
|
|
|
|
objectString, err := c.store.Get(c.ctx, fmt.Sprintf(objectKeyPattern, id)).Result()
|
|
|
|
|
if err == redis.Nil {
|
|
|
|
|
continue
|
|
|
|
|
} else if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var object map[string]interface{}
|
|
|
|
|
err = json.Unmarshal([]byte(objectString), &object)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
objects = append(objects, object)
|
|
|
|
|
}
|
2024-03-02 11:22:27 +00:00
|
|
|
|
|
2024-05-21 00:25:15 +00:00
|
|
|
|
return objects, nil
|
2024-02-22 07:40:39 +00:00
|
|
|
|
}
|