mirror of
https://github.com/andwati/zitadel.git
synced 2026-10-18 05:52:51 +00:00
# Which Problems Are Solved - The eventstore did not support intentionally overwriting the resource owner when creating events for aggregates that may be reused across owners. - Resource owner handling was implicit and could not be controlled per command/event type. - We needed a safe way to distinguish between: - keeping the existing aggregate owner, and - explicitly setting a new owner for specific create-like events. # How the Problems Are Solved - Introduced a new eventstore command type with an explicit enforce_owner flag. - Updated eventstore.commands_to_events and eventstore.push so owner assignment is now explicit: - if enforce_owner is true, the command owner is written - if enforce_owner is false, the existing aggregate owner is retained when present - Added EnforceResourceOwnerCommand and wiring so command types can opt in to enforced owner behavior. - Wired the new behavior through the v3 eventstore push path, including compatibility fallback for older command type mapping. - Added migration/setup changes to register and use the new command type and SQL functions. - Added and updated tests for owner overwrite and aggregate ID reuse scenarios. # Additional Changes - Added small migration/setup robustness improvements related to eventstore setup ordering and helper reuse. - Added focused test coverage for enforced owner behavior and sequencing. - Events that currently allow owner changes (implement EnforceResourceOwner) are: - AddedEvent (action) - GroupAddedEvent - StartedEvent (idp intent) - ProjectAddedEvent - HumanAddedEvent - HumanRegisteredEvent - MachineAddedEvent - CreatedEvent (schema user) # Additional Context - Follow-up for eventstore owner-handling correctness in create flows and aggregate ID reuse cases. - No additional issue link was attached for this change. --------- Co-authored-by: abhishek kumar gupta <[email protected]>
178 lines
5.5 KiB
Go
178 lines
5.5 KiB
Go
package query
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/zitadel/zitadel/internal/crypto"
|
|
"github.com/zitadel/zitadel/internal/domain"
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
|
"github.com/zitadel/zitadel/internal/repository/user"
|
|
"github.com/zitadel/zitadel/internal/telemetry/tracing"
|
|
"github.com/zitadel/zitadel/internal/zerrors"
|
|
)
|
|
|
|
type HumanPasswordReadModel struct {
|
|
*eventstore.ReadModel
|
|
|
|
EncodedHash string
|
|
SecretChangeRequired bool
|
|
|
|
Code *crypto.CryptoValue
|
|
CodeCreationDate time.Time
|
|
CodeExpiry time.Duration
|
|
PasswordCheckFailedCount uint64
|
|
|
|
UserState domain.UserState
|
|
}
|
|
|
|
func (q *Queries) GetHumanPassword(ctx context.Context, orgID, userID string) (encodedHash string, err error) {
|
|
ctx, span := tracing.NewSpan(ctx)
|
|
defer func() { span.EndWithError(err) }()
|
|
|
|
if userID == "" {
|
|
return "", zerrors.ThrowInvalidArgument(nil, "QUERY-4Mfsf", "Errors.User.UserIDMissing")
|
|
}
|
|
existingPassword, err := q.passwordReadModel(ctx, userID, orgID)
|
|
if err != nil {
|
|
return "", zerrors.ThrowInternal(nil, "QUERY-p1k1n2i", "Errors.User.NotFound")
|
|
}
|
|
if existingPassword.UserState == domain.UserStateUnspecified || existingPassword.UserState == domain.UserStateDeleted {
|
|
return "", zerrors.ThrowPreconditionFailed(nil, "QUERY-3n77z", "Errors.User.NotFound")
|
|
}
|
|
return existingPassword.EncodedHash, nil
|
|
}
|
|
|
|
func (q *Queries) passwordReadModel(ctx context.Context, userID, resourceOwner string) (readModel *HumanPasswordReadModel, err error) {
|
|
ctx, span := tracing.NewSpan(ctx)
|
|
defer func() { span.EndWithError(err) }()
|
|
|
|
readModel = NewHumanPasswordReadModel(userID, resourceOwner)
|
|
err = q.eventstore.FilterToQueryReducer(ctx, readModel)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return readModel, nil
|
|
}
|
|
|
|
func NewHumanPasswordReadModel(userID, resourceOwner string) *HumanPasswordReadModel {
|
|
return &HumanPasswordReadModel{
|
|
ReadModel: &eventstore.ReadModel{
|
|
AggregateID: userID,
|
|
ResourceOwner: resourceOwner,
|
|
},
|
|
}
|
|
}
|
|
|
|
func (rm *HumanPasswordReadModel) AppendEvents(events ...eventstore.Event) {
|
|
rm.ReadModel.AppendEvents(events...)
|
|
}
|
|
|
|
func (wm *HumanPasswordReadModel) Reduce() error {
|
|
for _, event := range wm.Events {
|
|
switch e := event.(type) {
|
|
case *user.HumanAddedEvent:
|
|
wm.Code = nil
|
|
wm.CodeCreationDate = time.Time{}
|
|
wm.CodeExpiry = 0
|
|
wm.PasswordCheckFailedCount = 0
|
|
wm.EncodedHash = crypto.SecretOrEncodedHash(e.Secret, e.EncodedHash)
|
|
wm.SecretChangeRequired = e.ChangeRequired
|
|
wm.UserState = domain.UserStateActive
|
|
case *user.HumanRegisteredEvent:
|
|
wm.Code = nil
|
|
wm.CodeCreationDate = time.Time{}
|
|
wm.CodeExpiry = 0
|
|
wm.PasswordCheckFailedCount = 0
|
|
wm.EncodedHash = crypto.SecretOrEncodedHash(e.Secret, e.EncodedHash)
|
|
wm.SecretChangeRequired = e.ChangeRequired
|
|
wm.UserState = domain.UserStateActive
|
|
case *user.MachineAddedEvent:
|
|
wm.Code = nil
|
|
wm.CodeCreationDate = time.Time{}
|
|
wm.CodeExpiry = 0
|
|
wm.PasswordCheckFailedCount = 0
|
|
wm.EncodedHash = ""
|
|
wm.SecretChangeRequired = false
|
|
wm.UserState = domain.UserStateUnspecified
|
|
case *user.HumanInitialCodeAddedEvent:
|
|
wm.UserState = domain.UserStateInitial
|
|
case *user.HumanInitializedCheckSucceededEvent:
|
|
wm.UserState = domain.UserStateActive
|
|
case *user.HumanPasswordChangedEvent:
|
|
wm.EncodedHash = crypto.SecretOrEncodedHash(e.Secret, e.EncodedHash)
|
|
wm.SecretChangeRequired = e.ChangeRequired
|
|
wm.Code = nil
|
|
wm.PasswordCheckFailedCount = 0
|
|
case *user.HumanPasswordCodeAddedEvent:
|
|
wm.Code = e.Code
|
|
wm.CodeCreationDate = e.CreationDate()
|
|
wm.CodeExpiry = e.Expiry
|
|
case *user.HumanEmailVerifiedEvent:
|
|
if wm.UserState == domain.UserStateInitial {
|
|
wm.UserState = domain.UserStateActive
|
|
}
|
|
case *user.HumanPasswordCheckFailedEvent:
|
|
wm.PasswordCheckFailedCount += 1
|
|
case *user.HumanPasswordCheckSucceededEvent:
|
|
wm.PasswordCheckFailedCount = 0
|
|
case *user.UserLockedEvent:
|
|
wm.UserState = domain.UserStateLocked
|
|
case *user.UserUnlockedEvent:
|
|
wm.PasswordCheckFailedCount = 0
|
|
if wm.UserState != domain.UserStateDeleted {
|
|
wm.UserState = domain.UserStateActive
|
|
}
|
|
case *user.UserRemovedEvent:
|
|
wm.EncodedHash = ""
|
|
wm.SecretChangeRequired = false
|
|
wm.Code = nil
|
|
wm.CodeCreationDate = time.Time{}
|
|
wm.CodeExpiry = 0
|
|
wm.PasswordCheckFailedCount = 0
|
|
wm.UserState = domain.UserStateDeleted
|
|
case *user.HumanPasswordHashUpdatedEvent:
|
|
wm.EncodedHash = e.EncodedHash
|
|
}
|
|
}
|
|
return wm.ReadModel.Reduce()
|
|
}
|
|
|
|
func (wm *HumanPasswordReadModel) Query() *eventstore.SearchQueryBuilder {
|
|
query := eventstore.NewSearchQueryBuilder(eventstore.ColumnsEvent).
|
|
AwaitOpenTransactions().
|
|
AddQuery().
|
|
AggregateTypes(user.AggregateType).
|
|
AggregateIDs(wm.AggregateID).
|
|
EventTypes(user.HumanAddedType,
|
|
user.HumanRegisteredType,
|
|
user.MachineAddedEventType,
|
|
user.HumanInitialCodeAddedType,
|
|
user.HumanInitializedCheckSucceededType,
|
|
user.HumanPasswordChangedType,
|
|
user.HumanPasswordCodeAddedType,
|
|
user.HumanEmailVerifiedType,
|
|
user.HumanPasswordCheckFailedType,
|
|
user.HumanPasswordCheckSucceededType,
|
|
user.HumanPasswordHashUpdatedType,
|
|
user.UserRemovedType,
|
|
user.UserLockedType,
|
|
user.UserUnlockedType,
|
|
user.UserV1AddedType,
|
|
user.UserV1RegisteredType,
|
|
user.UserV1InitialCodeAddedType,
|
|
user.UserV1InitializedCheckSucceededType,
|
|
user.UserV1PasswordChangedType,
|
|
user.UserV1PasswordCodeAddedType,
|
|
user.UserV1EmailVerifiedType,
|
|
user.UserV1PasswordCheckFailedType,
|
|
user.UserV1PasswordCheckSucceededType,
|
|
).
|
|
Builder()
|
|
|
|
if wm.ResourceOwner != "" {
|
|
query.ResourceOwner(wm.ResourceOwner)
|
|
}
|
|
return query
|
|
}
|