fix verification cache for redis-backed challenges

This commit is contained in:
richjohnrambo
2026-09-23 09:02:56 +08:00
parent e37b52c3c0
commit 137d6fb6da
4 changed files with 134 additions and 6 deletions
+76 -3
View File
@@ -4286,6 +4286,79 @@ type VerificationPersistenceInterface interface {
type verificationMapper struct{}
type verificationCodeCache struct {
ID int64 `json:"id,omitempty"`
TenantID types.TenantID `json:"tenant_id"`
ChallengeID string `json:"challenge_id"`
Scene string `json:"scene"`
Action string `json:"action,omitempty"`
PhoneHash string `json:"phone_hash"`
PhoneMask string `json:"phone_mask,omitempty"`
TargetUser types.Uid `json:"target_user,omitempty"`
CodeHash string `json:"code_hash"`
VerificationTokenHash *string `json:"verification_token_hash,omitempty"`
State types.VerificationState `json:"state"`
Attempts int `json:"attempts"`
MaxAttempts int `json:"max_attempts"`
ExpiresAt time.Time `json:"expires_at"`
ResendAfter time.Time `json:"resend_after"`
VerifiedAt *time.Time `json:"verified_at,omitempty"`
ConsumedAt *time.Time `json:"consumed_at,omitempty"`
CreatedAt time.Time `json:"created_at,omitempty"`
UpdatedAt time.Time `json:"updated_at,omitempty"`
}
func verificationCodeCacheFromType(vc *types.VerificationCode) verificationCodeCache {
if vc == nil {
return verificationCodeCache{}
}
return verificationCodeCache{
ID: vc.ID,
TenantID: vc.TenantID,
ChallengeID: vc.ChallengeID,
Scene: vc.Scene,
Action: vc.Action,
PhoneHash: vc.PhoneHash,
PhoneMask: vc.PhoneMask,
TargetUser: vc.TargetUser,
CodeHash: vc.CodeHash,
VerificationTokenHash: vc.VerificationTokenHash,
State: vc.State,
Attempts: vc.Attempts,
MaxAttempts: vc.MaxAttempts,
ExpiresAt: vc.ExpiresAt,
ResendAfter: vc.ResendAfter,
VerifiedAt: vc.VerifiedAt,
ConsumedAt: vc.ConsumedAt,
CreatedAt: vc.CreatedAt,
UpdatedAt: vc.UpdatedAt,
}
}
func (vc verificationCodeCache) toType() *types.VerificationCode {
return &types.VerificationCode{
ID: vc.ID,
TenantID: vc.TenantID,
ChallengeID: vc.ChallengeID,
Scene: vc.Scene,
Action: vc.Action,
PhoneHash: vc.PhoneHash,
PhoneMask: vc.PhoneMask,
TargetUser: vc.TargetUser,
CodeHash: vc.CodeHash,
VerificationTokenHash: vc.VerificationTokenHash,
State: vc.State,
Attempts: vc.Attempts,
MaxAttempts: vc.MaxAttempts,
ExpiresAt: vc.ExpiresAt,
ResendAfter: vc.ResendAfter,
VerifiedAt: vc.VerifiedAt,
ConsumedAt: vc.ConsumedAt,
CreatedAt: vc.CreatedAt,
UpdatedAt: vc.UpdatedAt,
}
}
// Verifications is a singleton for product verification challenges.
var Verifications VerificationPersistenceInterface
@@ -4537,7 +4610,7 @@ func (verificationMapper) Create(tenantID types.TenantID, vc *types.Verification
}
func (verificationMapper) Get(tenantID types.TenantID, challengeID string) (*types.VerificationCode, error) {
var vc types.VerificationCode
var vc verificationCodeCache
ok, err := GetCachedObject(tenantID, cacheNamespaceVerification, challengeID, &vc)
if err != nil {
return nil, err
@@ -4545,7 +4618,7 @@ func (verificationMapper) Get(tenantID types.TenantID, challengeID string) (*typ
if !ok {
return nil, types.ErrNotFound
}
return &vc, nil
return vc.toType(), nil
}
func (verificationMapper) GetActive(tenantID types.TenantID, scene, phoneHash string, now time.Time) (*types.VerificationCode, error) {
@@ -4611,7 +4684,7 @@ func cacheVerificationCode(tenantID types.TenantID, vc *types.VerificationCode)
if vc == nil {
return nil
}
if err := CacheObject(tenantID, cacheNamespaceVerification, vc.ChallengeID, vc); err != nil {
if err := CacheObject(tenantID, cacheNamespaceVerification, vc.ChallengeID, verificationCodeCacheFromType(vc)); err != nil {
return err
}
if vc.State == types.VerificationStatePending {