feat: add recovery codes to user repository (#11702)

# Which Problems Are Solved

Adds a repository implementation to add/remove recovery codes to the
users relational table.

# How the Problems Are Solved

This is achieved by:
* adding the following columns to the users table: `recovery_codes`,
`recovery_code_last_successful_check`, `recovery_code_failed_attempts`
* adding the `RecoveryCodes` field to the `HumanUser` domain with fields
to set recovery `codes`, `lastSuccessfullyCheckedAt` timestamp, and
`failedAttempts`
* setting `recoveryCodes` in the `Get` user query statement to return a
json object with details related to the recovery codes
* adding the repository-layer implementation to add/remove recovery
codes and set fields related to recovery code checks.
* adding projection reducers to handle the following events:
`HumanRecoveryCodesAddedEvent`, `HumanRecoveryCodesRemovedEvent`,
`HumanRecoveryCodeCheckSucceededEvent`, and
`HumanRecoveryCodeCheckFailedEvent`
* adding unit tests

# Additional Changes
* fix the error message when the recovery is empty in
`internal/command/user_human_recovery_codes.go`
* add a new error message for empty recovery code during checks in
`en.yaml`

# Additional Context
- Closes https://github.com/zitadel/zitadel/issues/11212
- Follow-up: integration tests for the reducers will be added in a
different PR after this
[PR](https://github.com/zitadel/zitadel/pull/11478) is merged
This commit is contained in:
Gayathri Vijayan
2026-02-26 19:22:37 +01:00
committed by GitHub
parent 850adc0da4
commit fb35fdd3c7
12 changed files with 502 additions and 16 deletions
+16 -5
View File
@@ -43,11 +43,12 @@ type HumanUser struct {
MultifactorInitializationSkippedAt time.Time `json:"multifactorInitializationSkippedAt,omitzero" db:"-"`
Email HumanEmail `json:"email,omitzero" db:"-"`
Phone *HumanPhone `json:"phone,omitempty" db:"-"`
Passkeys []*Passkey `json:"passkeys,omitempty" db:"-"`
Password HumanPassword `json:"password,omitzero" db:"-"`
TOTP *HumanTOTP `json:"totp,omitempty" db:"-"`
Email HumanEmail `json:"email,omitzero" db:"-"`
Phone *HumanPhone `json:"phone,omitempty" db:"-"`
Passkeys []*Passkey `json:"passkeys,omitempty" db:"-"`
Password HumanPassword `json:"password,omitzero" db:"-"`
TOTP *HumanTOTP `json:"totp,omitempty" db:"-"`
RecoveryCodes *HumanRecoveryCodes `json:"recoveryCodes,omitempty" db:"-"`
IdentityProviderLinks []*IdentityProviderLink `json:"identityProviderLinks,omitempty" db:"-"`
@@ -236,3 +237,13 @@ type HumanInvite struct {
// FailedAttempts is the number of consecutive failed invite attempts
FailedAttempts uint8 `json:"failedAttempts,omitempty" db:"-"`
}
type HumanRecoveryCodes struct {
// Codes are the hashed recovery codes for the user
Codes []string `json:"codes" db:"-"`
// LastSuccessfullyCheckedAt is the time when a recovery code was last successfully checked
LastSuccessfullyCheckedAt *time.Time `json:"lastSuccessfullyCheckedAt,omitzero" db:"-"`
// FailedAttempts is the number of consecutive failed recovery code attempts
// It is reset to 0 on successful verification
FailedAttempts uint8 `json:"failedAttempts,omitempty" db:"-"`
}
+21 -2
View File
@@ -82,6 +82,7 @@ type HumanUserRepository interface {
// Update updates the user
// It ensures that updates are only applied to user
Update(ctx context.Context, client database.QueryExecutor, condition database.Condition, changes ...database.Change) (int64, error)
humanConditions
humanChanges
humanColumns
@@ -152,13 +153,14 @@ type humanChanges interface {
identityProviderLinkChanges
humanPasskeyChanges
inviteChanges
recoveryCodeChanges
}
type humanPasswordChanges interface {
// SetPassword overwrites the password field.
// It resets the password verification and the last successful password check time.
SetPassword(hash string) database.Change
// SetResetPassword handles the password reset process based on the verification type:
// SetResetPasswordVerification handles the password reset process based on the verification type:
// * [VerificationTypeInit]: to initialize password reset, a verification will be created. The current password remains unchanged
// * [VerificationTypeSucceeded]: to mark the password as reset and remove the verification. The current password remains unchanged use [humanPasswordChanges.SetPassword] to set the new password.
// * [VerificationTypeUpdate]: to update the existing password reset verification. If a new code is requested, use [VerificationTypeInit].
@@ -236,7 +238,7 @@ type humanPhoneChanges interface {
}
type humanTOTPChanges interface {
// SetTOTP changes the TOTP secret and verification timestamp
// SetTOTPSecret changes the TOTP secret and verification timestamp
SetTOTPSecret(secret *crypto.CryptoValue) database.Change
// SetTOTPVerifiedAt sets the TOTP verified at time
// If verifiedAt is zero, it will be set to NOW()
@@ -275,6 +277,23 @@ type inviteChanges interface {
SetInviteVerification(verification VerificationType) database.Change
}
type recoveryCodeChanges interface {
// AddRecoveryCodes adds new recovery codes / appends to the existing recovery codes for the user
AddRecoveryCodes(codes []string) database.Change
// RemoveRecoveryCode removes a recovery code for the user
RemoveRecoveryCode(code string) database.Change
// RemoveAllRecoveryCodes removes all recovery codes for the user
RemoveAllRecoveryCodes() database.Change
// SetLastSuccessfulRecoveryCodeCheck sets the last successful recovery code check time
// If checkedAt is zero, it will be set to NOW()
// It resets the failed attempts
SetLastSuccessfulRecoveryCodeCheck(checkedAt time.Time) database.Change
// IncrementRecoveryCodeFailedAttempts increments the recovery code verification failed attempts
IncrementRecoveryCodeFailedAttempts() database.Change
// ResetRecoveryCodeFailedAttempts resets the recovery code verification failed attempts
ResetRecoveryCodeFailedAttempts() database.Change
}
type HumanIdentityProviderLinkConditions interface {
ProviderIDCondition(providerID string) database.Condition
ProvidedUserIDCondition(providedUserID string) database.Condition