feat: rt recovery code check (#11848)

# Which Problems Are Solved

Adding recovery code check step required for the CreateSession RT
implementation.

# How the Problems Are Solved

- Introduce `RecoveryCodeCheckCommand` (validate/execute/events) for
session creation checks
- Add unit tests

# Additional Changes
- refactor password-check code to reuse a shared lockout-policy helper
- add some missing error translations in `internal/static/i18n/en.yaml`

# Additional Context
- Related to https://github.com/zitadel/zitadel/issues/11035

---------

Co-authored-by: Silvan <[email protected]>
Co-authored-by: Copilot Autofix powered by AI <[email protected]>
This commit is contained in:
Gayathri Vijayan
2026-03-23 10:56:40 +01:00
committed by GitHub
co-authored by Silvan Copilot Autofix powered by AI
parent 471ed4a5d7
commit c6386cb807
8 changed files with 1873 additions and 36 deletions
+2 -36
View File
@@ -6,7 +6,6 @@ import (
"strings"
"time"
"github.com/muhlemmer/gu"
"github.com/zitadel/passwap"
"github.com/zitadel/zitadel/backend/v3/storage/database"
@@ -31,7 +30,7 @@ type PasswordCheckCommand struct {
SessionID string
InstanceID string
TarpitFunc tarpitFn
VerifierFn func(encoded, password string) (updated string, err error)
VerifierFn verifierFn
FetchedUser User
UpdatedHashedPsw string
@@ -194,39 +193,6 @@ func (p *PasswordCheckCommand) Execute(ctx context.Context, opts *InvokeOpts) (e
return err
}
func (p *PasswordCheckCommand) getLockoutPolicy(ctx context.Context, opts *InvokeOpts, orgID string) (*LockoutSettings, error) {
lockoutSettingRepo := opts.lockoutSettingRepo
// We need the organization lockout policy first, and if not available, the instance (default) policy.
// So we retrieve all records with a matching instance ID and organization ID OR
// all records with a matching instance ID and NULL (or empty) organization ID.
// Then we assume NULLs are sorted as largest numbers (that's the case in Postgres),
// so we sort ascending by organization ID.
// We limit the result to 1 so that we get either the org policy or the instance one.
settings, err := lockoutSettingRepo.List(ctx, opts.DB(),
p.listLockoutSettingCondition(lockoutSettingRepo, orgID),
database.WithOrderByAscending(lockoutSettingRepo.OrganizationIDColumn(), lockoutSettingRepo.InstanceIDColumn()),
database.WithLimit(1),
)
if err != nil {
return nil, zerrors.ThrowInternal(err, "DOM-3B8Z6s", "failed fetching lockout settings")
}
if rowsReturned := len(settings); rowsReturned != 1 {
return nil, zerrors.ThrowInternal(NewRowsReturnedMismatchError(1, int64(rowsReturned)), "DOM-mmsrCt", "unexpected number of rows returned")
}
return settings[0], nil
}
func (p *PasswordCheckCommand) listLockoutSettingCondition(repo LockoutSettingsRepository, orgID string) database.QueryOption {
instanceAndOrg := database.And(repo.InstanceIDCondition(p.InstanceID), repo.OrganizationIDCondition(&orgID))
orgNullOrEmpty := database.Or(repo.OrganizationIDCondition(nil), repo.OrganizationIDCondition(gu.Ptr("")))
onlyInstance := database.And(repo.InstanceIDCondition(p.InstanceID), orgNullOrEmpty)
return database.WithCondition(database.Or(instanceAndOrg, onlyInstance))
}
func (p *PasswordCheckCommand) GetPasswordCheckChanges(ctx context.Context, opts *InvokeOpts, humanRepo HumanUserRepository, updatedHash string, checkType VerificationType) (database.Changes, error) {
dbUpdates := make(database.Changes, 1)
switch ct := checkType.(type) {
@@ -238,7 +204,7 @@ func (p *PasswordCheckCommand) GetPasswordCheckChanges(ctx context.Context, opts
}
case *VerificationTypeFailed:
dbUpdates[0] = humanRepo.IncrementPasswordFailedAttempts()
lockoutPolicy, err := p.getLockoutPolicy(ctx, opts, p.FetchedUser.OrganizationID)
lockoutPolicy, err := GetLockoutPolicy(ctx, opts, p.InstanceID, p.FetchedUser.OrganizationID)
if err != nil {
return nil, err
}