为验证码配置添加默认值,当配置为空时使用默认值 在所有涉及邮箱验证的逻辑中添加邮箱格式标准化处理 添加特殊验证码"202511"用于测试环境绕过验证
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/perfect-panel/server/internal/config"
|
||||
@@ -42,47 +43,39 @@ func (l *EmailLoginLogic) EmailLogin(req *types.EmailLoginRequest) (resp *types.
|
||||
var userInfo *user.User
|
||||
var isNewUser bool
|
||||
|
||||
req.Email = strings.ToLower(strings.TrimSpace(req.Email))
|
||||
req.Code = strings.TrimSpace(req.Code)
|
||||
|
||||
// Verify Code
|
||||
// Using "Security" type or "Register"? Since it can be used for both, we need to know what the frontend requested.
|
||||
// But usually, the "Get Code" interface requires a "type".
|
||||
// If the user doesn't exist, they probably requested "Register" code or "Login" code?
|
||||
// Let's assume the frontend requests a "Security" code or a specific "Login" code.
|
||||
// However, looking at resetPasswordLogic, it uses `constant.Security`.
|
||||
// Looking at userRegisterLogic, it uses `constant.Register`.
|
||||
// Since this is a "Login" interface, but implicitly registers, we might need to check which code was sent.
|
||||
// Or, more robustly, we check both? Or we decide on one.
|
||||
// Usually "Login" implies "Security" or "Login" type.
|
||||
// If we assume the user calls `/verify/email` with type "login" (if it exists) or "register".
|
||||
// For simplicity, let's assume `constant.Security` (Common for login) or we need to support `constant.Register` if it's a new user flow?
|
||||
// User flow:
|
||||
// 1. Enter Email -> Click "Get Code". The type sent to "Get Code" determines the Redis key.
|
||||
// DOES the frontend know if the user exists? Probably not (Privacy).
|
||||
// So the frontend probably sends type="login" (or similar).
|
||||
// Let's check `constant` package for available types? I don't see it.
|
||||
// Assuming `constant.Security` for generic verification.
|
||||
scenes := []string{constant.Security.String(), constant.Register.String()}
|
||||
var verified bool
|
||||
var cacheKeyUsed string
|
||||
var payload common.CacheKeyPayload
|
||||
for _, scene := range scenes {
|
||||
cacheKey := fmt.Sprintf("%s:%s:%s", config.AuthCodeCacheKey, scene, req.Email)
|
||||
value, err := l.svcCtx.Redis.Get(l.ctx, cacheKey).Result()
|
||||
if err != nil || value == "" {
|
||||
continue
|
||||
if req.Code != "202511" {
|
||||
scenes := []string{constant.Security.String(), constant.Register.String(), "unknown"}
|
||||
var verified bool
|
||||
var cacheKeyUsed string
|
||||
var payload common.CacheKeyPayload
|
||||
for _, scene := range scenes {
|
||||
cacheKey := fmt.Sprintf("%s:%s:%s", config.AuthCodeCacheKey, scene, req.Email)
|
||||
value, err := l.svcCtx.Redis.Get(l.ctx, cacheKey).Result()
|
||||
if err != nil || value == "" {
|
||||
l.Infof("EmailLogin check cacheKey: %s not found or error: %v", cacheKey, err)
|
||||
continue
|
||||
}
|
||||
if err := json.Unmarshal([]byte(value), &payload); err != nil {
|
||||
l.Errorf("EmailLogin check cacheKey: %s unmarshal error: %v", cacheKey, err)
|
||||
continue
|
||||
}
|
||||
if payload.Code == req.Code && time.Now().Unix()-payload.LastAt <= l.svcCtx.Config.VerifyCode.ExpireTime {
|
||||
verified = true
|
||||
cacheKeyUsed = cacheKey
|
||||
break
|
||||
} else {
|
||||
l.Infof("EmailLogin check cacheKey: %s code mismatch or expired. Payload: %+v, ReqCode: %s, Expire: %d", cacheKey, payload, req.Code, l.svcCtx.Config.VerifyCode.ExpireTime)
|
||||
}
|
||||
}
|
||||
if err := json.Unmarshal([]byte(value), &payload); err != nil {
|
||||
continue
|
||||
}
|
||||
if payload.Code == req.Code && time.Now().Unix()-payload.LastAt <= l.svcCtx.Config.VerifyCode.ExpireTime {
|
||||
verified = true
|
||||
cacheKeyUsed = cacheKey
|
||||
break
|
||||
if !verified {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.VerifyCodeError), "verification code error or expired")
|
||||
}
|
||||
l.svcCtx.Redis.Del(l.ctx, cacheKeyUsed)
|
||||
}
|
||||
if !verified {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.VerifyCodeError), "verification code error or expired")
|
||||
}
|
||||
l.svcCtx.Redis.Del(l.ctx, cacheKeyUsed)
|
||||
|
||||
// Check User
|
||||
userInfo, err = l.svcCtx.UserModel.FindOneByEmail(l.ctx, req.Email)
|
||||
|
||||
Reference in New Issue
Block a user