2022-09-27 17:59:05 +08:00
|
|
|
package auth
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
2023-07-28 16:03:30 +08:00
|
|
|
"github.com/answerdev/answer/internal/service/auth"
|
2022-09-27 17:59:05 +08:00
|
|
|
|
2022-10-24 16:51:05 +08:00
|
|
|
"github.com/answerdev/answer/internal/base/constant"
|
|
|
|
"github.com/answerdev/answer/internal/base/data"
|
|
|
|
"github.com/answerdev/answer/internal/base/reason"
|
|
|
|
"github.com/answerdev/answer/internal/entity"
|
2022-09-27 17:59:05 +08:00
|
|
|
"github.com/segmentfault/pacman/errors"
|
2022-12-05 17:20:30 +08:00
|
|
|
"github.com/segmentfault/pacman/log"
|
2022-09-27 17:59:05 +08:00
|
|
|
)
|
|
|
|
|
2022-10-26 16:59:33 +08:00
|
|
|
// authRepo auth repository
|
2022-09-27 17:59:05 +08:00
|
|
|
type authRepo struct {
|
|
|
|
data *data.Data
|
|
|
|
}
|
|
|
|
|
2023-07-28 16:03:30 +08:00
|
|
|
// NewAuthRepo new repository
|
|
|
|
func NewAuthRepo(data *data.Data) auth.AuthRepo {
|
|
|
|
return &authRepo{
|
|
|
|
data: data,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-26 16:37:55 +08:00
|
|
|
// GetUserCacheInfo get user cache info
|
2022-09-27 17:59:05 +08:00
|
|
|
func (ar *authRepo) GetUserCacheInfo(ctx context.Context, accessToken string) (userInfo *entity.UserCacheInfo, err error) {
|
|
|
|
userInfoCache, err := ar.data.Cache.GetString(ctx, constant.UserTokenCacheKey+accessToken)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
|
|
|
}
|
|
|
|
userInfo = &entity.UserCacheInfo{}
|
|
|
|
err = json.Unmarshal([]byte(userInfoCache), userInfo)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
|
|
|
}
|
|
|
|
return userInfo, nil
|
|
|
|
}
|
|
|
|
|
2022-10-26 16:59:33 +08:00
|
|
|
// SetUserCacheInfo set user cache info
|
|
|
|
func (ar *authRepo) SetUserCacheInfo(ctx context.Context, accessToken string, userInfo *entity.UserCacheInfo) (err error) {
|
|
|
|
userInfoCache, err := json.Marshal(userInfo)
|
2022-09-27 17:59:05 +08:00
|
|
|
if err != nil {
|
2022-10-26 16:59:33 +08:00
|
|
|
return err
|
2022-09-27 17:59:05 +08:00
|
|
|
}
|
2022-10-26 16:59:33 +08:00
|
|
|
err = ar.data.Cache.SetString(ctx, constant.UserTokenCacheKey+accessToken,
|
|
|
|
string(userInfoCache), constant.UserTokenCacheTime)
|
2022-09-27 17:59:05 +08:00
|
|
|
if err != nil {
|
2022-10-26 16:59:33 +08:00
|
|
|
return errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
2022-09-27 17:59:05 +08:00
|
|
|
}
|
2022-12-05 17:20:30 +08:00
|
|
|
if err := ar.AddUserTokenMapping(ctx, userInfo.UserID, accessToken); err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
}
|
2022-10-26 16:59:33 +08:00
|
|
|
return nil
|
2022-09-27 17:59:05 +08:00
|
|
|
}
|
|
|
|
|
2022-10-26 16:59:33 +08:00
|
|
|
// RemoveUserCacheInfo remove user cache info
|
|
|
|
func (ar *authRepo) RemoveUserCacheInfo(ctx context.Context, accessToken string) (err error) {
|
|
|
|
err = ar.data.Cache.Del(ctx, constant.UserTokenCacheKey+accessToken)
|
2022-10-20 18:21:38 +08:00
|
|
|
if err != nil {
|
|
|
|
return errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-10-26 16:59:33 +08:00
|
|
|
// SetUserStatus set user status
|
|
|
|
func (ar *authRepo) SetUserStatus(ctx context.Context, userID string, userInfo *entity.UserCacheInfo) (err error) {
|
2022-09-27 17:59:05 +08:00
|
|
|
userInfoCache, err := json.Marshal(userInfo)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-10-26 16:59:33 +08:00
|
|
|
err = ar.data.Cache.SetString(ctx, constant.UserStatusChangedCacheKey+userID,
|
|
|
|
string(userInfoCache), constant.UserStatusChangedCacheTime)
|
2022-09-27 17:59:05 +08:00
|
|
|
if err != nil {
|
|
|
|
return errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-10-26 16:59:33 +08:00
|
|
|
// GetUserStatus get user status
|
|
|
|
func (ar *authRepo) GetUserStatus(ctx context.Context, userID string) (userInfo *entity.UserCacheInfo, err error) {
|
|
|
|
userInfoCache, err := ar.data.Cache.GetString(ctx, constant.UserStatusChangedCacheKey+userID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
|
|
|
}
|
|
|
|
userInfo = &entity.UserCacheInfo{}
|
|
|
|
err = json.Unmarshal([]byte(userInfoCache), userInfo)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
|
|
|
}
|
|
|
|
return userInfo, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveUserStatus remove user status
|
|
|
|
func (ar *authRepo) RemoveUserStatus(ctx context.Context, userID string) (err error) {
|
|
|
|
err = ar.data.Cache.Del(ctx, constant.UserStatusChangedCacheKey+userID)
|
2022-09-27 17:59:05 +08:00
|
|
|
if err != nil {
|
2022-10-26 16:37:55 +08:00
|
|
|
return errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
2022-09-27 17:59:05 +08:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-12-21 16:55:16 +08:00
|
|
|
// GetAdminUserCacheInfo get admin user cache info
|
|
|
|
func (ar *authRepo) GetAdminUserCacheInfo(ctx context.Context, accessToken string) (userInfo *entity.UserCacheInfo, err error) {
|
2022-09-27 17:59:05 +08:00
|
|
|
userInfoCache, err := ar.data.Cache.GetString(ctx, constant.AdminTokenCacheKey+accessToken)
|
|
|
|
if err != nil {
|
|
|
|
err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
userInfo = &entity.UserCacheInfo{}
|
|
|
|
err = json.Unmarshal([]byte(userInfoCache), userInfo)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return userInfo, nil
|
|
|
|
}
|
|
|
|
|
2022-12-21 16:55:16 +08:00
|
|
|
// SetAdminUserCacheInfo set admin user cache info
|
|
|
|
func (ar *authRepo) SetAdminUserCacheInfo(ctx context.Context, accessToken string, userInfo *entity.UserCacheInfo) (err error) {
|
2022-09-27 17:59:05 +08:00
|
|
|
userInfoCache, err := json.Marshal(userInfo)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = ar.data.Cache.SetString(ctx, constant.AdminTokenCacheKey+accessToken, string(userInfoCache),
|
|
|
|
constant.AdminTokenCacheTime)
|
|
|
|
if err != nil {
|
2022-10-26 16:37:55 +08:00
|
|
|
return errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
2022-09-27 17:59:05 +08:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-12-21 16:55:16 +08:00
|
|
|
// RemoveAdminUserCacheInfo remove admin user cache info
|
|
|
|
func (ar *authRepo) RemoveAdminUserCacheInfo(ctx context.Context, accessToken string) (err error) {
|
2022-09-27 17:59:05 +08:00
|
|
|
err = ar.data.Cache.Del(ctx, constant.AdminTokenCacheKey+accessToken)
|
|
|
|
if err != nil {
|
2022-10-26 16:37:55 +08:00
|
|
|
return errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
2022-09-27 17:59:05 +08:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-12-05 17:20:30 +08:00
|
|
|
// AddUserTokenMapping add user token mapping
|
|
|
|
func (ar *authRepo) AddUserTokenMapping(ctx context.Context, userID, accessToken string) (err error) {
|
|
|
|
key := constant.UserTokenMappingCacheKey + userID
|
|
|
|
resp, _ := ar.data.Cache.GetString(ctx, key)
|
|
|
|
mapping := make(map[string]bool, 0)
|
|
|
|
if len(resp) > 0 {
|
|
|
|
_ = json.Unmarshal([]byte(resp), &mapping)
|
|
|
|
}
|
|
|
|
mapping[accessToken] = true
|
|
|
|
content, _ := json.Marshal(mapping)
|
|
|
|
return ar.data.Cache.SetString(ctx, key, string(content), constant.UserTokenCacheTime)
|
|
|
|
}
|
|
|
|
|
2023-03-26 17:26:16 +08:00
|
|
|
// RemoveUserTokens Log out all users under this user id
|
2023-05-08 17:51:29 +08:00
|
|
|
func (ar *authRepo) RemoveUserTokens(ctx context.Context, userID string, remainToken string) {
|
2022-12-05 17:20:30 +08:00
|
|
|
key := constant.UserTokenMappingCacheKey + userID
|
|
|
|
resp, _ := ar.data.Cache.GetString(ctx, key)
|
|
|
|
mapping := make(map[string]bool, 0)
|
|
|
|
if len(resp) > 0 {
|
|
|
|
_ = json.Unmarshal([]byte(resp), &mapping)
|
|
|
|
log.Debugf("find %d user tokens by user id %s", len(mapping), userID)
|
|
|
|
}
|
|
|
|
|
|
|
|
for token := range mapping {
|
2023-05-08 17:51:29 +08:00
|
|
|
if token == remainToken {
|
|
|
|
continue
|
|
|
|
}
|
2022-12-05 17:20:30 +08:00
|
|
|
if err := ar.RemoveUserCacheInfo(ctx, token); err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
} else {
|
|
|
|
log.Debugf("del user %s token success")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err := ar.RemoveUserStatus(ctx, userID); err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
}
|
|
|
|
if err := ar.data.Cache.Del(ctx, key); err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
}
|
|
|
|
}
|