mirror of
https://github.com/andwati/zitadel.git
synced 2026-11-17 10:55:12 +00:00
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:
@@ -424,6 +424,22 @@ func (p *userRelationalProjection) Reducers() []handler.AggregateReducer {
|
||||
Event: user.HumanInviteCheckFailedType,
|
||||
Reduce: p.reduceInviteCheckFailed,
|
||||
},
|
||||
{
|
||||
Event: user.HumanRecoveryCodesAddedType,
|
||||
Reduce: p.reduceRecoveryCodesAdded,
|
||||
},
|
||||
{
|
||||
Event: user.HumanRecoveryCodesRemovedType,
|
||||
Reduce: p.reduceRecoveryCodesRemoved,
|
||||
},
|
||||
{
|
||||
Event: user.HumanRecoveryCodeCheckSucceededType,
|
||||
Reduce: p.reduceRecoveryCodeCheckSucceeded,
|
||||
},
|
||||
{
|
||||
Event: user.HumanRecoveryCodeCheckFailedType,
|
||||
Reduce: p.reduceRecoveryCodeCheckFailed,
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
@@ -2116,6 +2132,87 @@ func (p *userRelationalProjection) reduceInviteCheckFailed(event eventstore.Even
|
||||
}), nil
|
||||
}
|
||||
|
||||
func (p *userRelationalProjection) reduceRecoveryCodesAdded(event eventstore.Event) (*handler.Statement, error) {
|
||||
e, err := assertEvent[*user.HumanRecoveryCodesAddedEvent](event)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return handler.NewStatement(e, func(ctx context.Context, ex handler.Executer, projectionName string) error {
|
||||
tx, ok := ex.(*sql.Tx)
|
||||
if !ok {
|
||||
return zerrors.ThrowInvalidArgumentf(nil, "HANDL-oqwfuV", "reduce.wrong.db.pool %T", ex)
|
||||
}
|
||||
repo := repository.HumanUserRepository()
|
||||
_, err := repo.Update(ctx, v3_sql.SQLTx(tx),
|
||||
repo.PrimaryKeyCondition(e.Aggregate().InstanceID, e.Aggregate().ID),
|
||||
repo.AddRecoveryCodes(e.Codes),
|
||||
repo.SetUpdatedAt(e.CreatedAt()),
|
||||
)
|
||||
return err
|
||||
}), nil
|
||||
}
|
||||
|
||||
func (p *userRelationalProjection) reduceRecoveryCodesRemoved(event eventstore.Event) (*handler.Statement, error) {
|
||||
e, err := assertEvent[*user.HumanRecoveryCodesRemovedEvent](event)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return handler.NewStatement(e, func(ctx context.Context, ex handler.Executer, projectionName string) error {
|
||||
tx, ok := ex.(*sql.Tx)
|
||||
if !ok {
|
||||
return zerrors.ThrowInvalidArgumentf(nil, "HANDL-ZueNKs", "reduce.wrong.db.pool %T", ex)
|
||||
}
|
||||
repo := repository.HumanUserRepository()
|
||||
_, err := repo.Update(ctx, v3_sql.SQLTx(tx),
|
||||
repo.PrimaryKeyCondition(e.Aggregate().InstanceID, e.Aggregate().ID),
|
||||
repo.RemoveAllRecoveryCodes(),
|
||||
repo.SetUpdatedAt(e.CreatedAt()),
|
||||
)
|
||||
return err
|
||||
}), nil
|
||||
}
|
||||
|
||||
func (p *userRelationalProjection) reduceRecoveryCodeCheckSucceeded(event eventstore.Event) (*handler.Statement, error) {
|
||||
e, err := assertEvent[*user.HumanRecoveryCodeCheckSucceededEvent](event)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return handler.NewStatement(e, func(ctx context.Context, ex handler.Executer, projectionName string) error {
|
||||
tx, ok := ex.(*sql.Tx)
|
||||
if !ok {
|
||||
return zerrors.ThrowInvalidArgumentf(nil, "HANDL-wr8WuW", "reduce.wrong.db.pool %T", ex)
|
||||
}
|
||||
repo := repository.HumanUserRepository()
|
||||
_, err := repo.Update(ctx, v3_sql.SQLTx(tx),
|
||||
repo.PrimaryKeyCondition(e.Aggregate().InstanceID, e.Aggregate().ID),
|
||||
repo.RemoveRecoveryCode(e.CodeChecked),
|
||||
repo.SetLastSuccessfulRecoveryCodeCheck(e.CreatedAt()),
|
||||
repo.SetUpdatedAt(e.CreatedAt()),
|
||||
)
|
||||
return err
|
||||
}), nil
|
||||
}
|
||||
|
||||
func (p *userRelationalProjection) reduceRecoveryCodeCheckFailed(event eventstore.Event) (*handler.Statement, error) {
|
||||
e, err := assertEvent[*user.HumanRecoveryCodeCheckFailedEvent](event)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return handler.NewStatement(e, func(ctx context.Context, ex handler.Executer, projectionName string) error {
|
||||
tx, ok := ex.(*sql.Tx)
|
||||
if !ok {
|
||||
return zerrors.ThrowInvalidArgumentf(nil, "HANDL-mWuV7a", "reduce.wrong.db.pool %T", ex)
|
||||
}
|
||||
repo := repository.HumanUserRepository()
|
||||
_, err := repo.Update(ctx, v3_sql.SQLTx(tx),
|
||||
repo.PrimaryKeyCondition(e.Aggregate().InstanceID, e.Aggregate().ID),
|
||||
repo.IncrementRecoveryCodeFailedAttempts(),
|
||||
repo.SetUpdatedAt(e.CreatedAt()),
|
||||
)
|
||||
return err
|
||||
}), nil
|
||||
}
|
||||
|
||||
func mapHumanGender(gender old_domain.Gender) domain.HumanGender {
|
||||
switch gender {
|
||||
case old_domain.GenderFemale:
|
||||
|
||||
Reference in New Issue
Block a user