2021-01-07 15:06:45 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
2021-02-18 13:48:27 +00:00
|
|
|
"time"
|
|
|
|
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
2021-07-06 11:55:57 +00:00
|
|
|
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/crypto"
|
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/user"
|
2021-01-07 15:06:45 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type HumanPasswordWriteModel struct {
|
|
|
|
eventstore.WriteModel
|
|
|
|
|
2023-07-14 06:49:57 +00:00
|
|
|
EncodedHash string
|
2021-01-07 15:06:45 +00:00
|
|
|
SecretChangeRequired bool
|
|
|
|
|
2021-08-11 06:36:32 +00:00
|
|
|
Code *crypto.CryptoValue
|
|
|
|
CodeCreationDate time.Time
|
|
|
|
CodeExpiry time.Duration
|
|
|
|
PasswordCheckFailedCount uint64
|
2021-02-08 10:30:30 +00:00
|
|
|
|
2021-01-07 15:06:45 +00:00
|
|
|
UserState domain.UserState
|
|
|
|
}
|
|
|
|
|
2021-01-12 11:59:51 +00:00
|
|
|
func NewHumanPasswordWriteModel(userID, resourceOwner string) *HumanPasswordWriteModel {
|
2021-01-07 15:06:45 +00:00
|
|
|
return &HumanPasswordWriteModel{
|
|
|
|
WriteModel: eventstore.WriteModel{
|
2021-01-12 11:59:51 +00:00
|
|
|
AggregateID: userID,
|
|
|
|
ResourceOwner: resourceOwner,
|
2021-01-07 15:06:45 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *HumanPasswordWriteModel) Reduce() error {
|
|
|
|
for _, event := range wm.Events {
|
|
|
|
switch e := event.(type) {
|
|
|
|
case *user.HumanAddedEvent:
|
2023-07-14 06:49:57 +00:00
|
|
|
wm.EncodedHash = user.SecretOrEncodedHash(e.Secret, e.EncodedHash)
|
2021-01-07 15:06:45 +00:00
|
|
|
wm.SecretChangeRequired = e.ChangeRequired
|
2021-03-22 13:40:25 +00:00
|
|
|
wm.UserState = domain.UserStateActive
|
2021-01-07 15:06:45 +00:00
|
|
|
case *user.HumanRegisteredEvent:
|
2023-07-14 06:49:57 +00:00
|
|
|
wm.EncodedHash = user.SecretOrEncodedHash(e.Secret, e.EncodedHash)
|
2021-01-07 15:06:45 +00:00
|
|
|
wm.SecretChangeRequired = e.ChangeRequired
|
|
|
|
wm.UserState = domain.UserStateActive
|
2021-03-22 13:40:25 +00:00
|
|
|
case *user.HumanInitialCodeAddedEvent:
|
|
|
|
wm.UserState = domain.UserStateInitial
|
|
|
|
case *user.HumanInitializedCheckSucceededEvent:
|
|
|
|
wm.UserState = domain.UserStateActive
|
2021-01-07 15:06:45 +00:00
|
|
|
case *user.HumanPasswordChangedEvent:
|
2023-07-14 06:49:57 +00:00
|
|
|
wm.EncodedHash = user.SecretOrEncodedHash(e.Secret, e.EncodedHash)
|
2021-01-07 15:06:45 +00:00
|
|
|
wm.SecretChangeRequired = e.ChangeRequired
|
2021-02-08 10:30:30 +00:00
|
|
|
wm.Code = nil
|
2021-08-11 06:36:32 +00:00
|
|
|
wm.PasswordCheckFailedCount = 0
|
2021-02-08 10:30:30 +00:00
|
|
|
case *user.HumanPasswordCodeAddedEvent:
|
|
|
|
wm.Code = e.Code
|
|
|
|
wm.CodeCreationDate = e.CreationDate()
|
|
|
|
wm.CodeExpiry = e.Expiry
|
2021-01-07 15:06:45 +00:00
|
|
|
case *user.HumanEmailVerifiedEvent:
|
|
|
|
if wm.UserState == domain.UserStateInitial {
|
|
|
|
wm.UserState = domain.UserStateActive
|
|
|
|
}
|
2021-08-11 06:36:32 +00:00
|
|
|
case *user.HumanPasswordCheckFailedEvent:
|
|
|
|
wm.PasswordCheckFailedCount += 1
|
|
|
|
case *user.HumanPasswordCheckSucceededEvent:
|
|
|
|
wm.PasswordCheckFailedCount = 0
|
|
|
|
case *user.UserUnlockedEvent:
|
|
|
|
wm.PasswordCheckFailedCount = 0
|
2021-01-07 15:06:45 +00:00
|
|
|
case *user.UserRemovedEvent:
|
|
|
|
wm.UserState = domain.UserStateDeleted
|
2023-07-14 06:49:57 +00:00
|
|
|
case *user.HumanPasswordHashUpdatedEvent:
|
|
|
|
wm.EncodedHash = e.EncodedHash
|
2021-01-07 15:06:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return wm.WriteModel.Reduce()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *HumanPasswordWriteModel) Query() *eventstore.SearchQueryBuilder {
|
2021-07-06 11:55:57 +00:00
|
|
|
query := eventstore.NewSearchQueryBuilder(eventstore.ColumnsEvent).
|
|
|
|
AddQuery().
|
|
|
|
AggregateTypes(user.AggregateType).
|
2021-02-18 13:48:27 +00:00
|
|
|
AggregateIDs(wm.AggregateID).
|
|
|
|
EventTypes(user.HumanAddedType,
|
|
|
|
user.HumanRegisteredType,
|
2021-03-22 16:15:24 +00:00
|
|
|
user.HumanInitialCodeAddedType,
|
|
|
|
user.HumanInitializedCheckSucceededType,
|
2021-02-18 13:48:27 +00:00
|
|
|
user.HumanPasswordChangedType,
|
|
|
|
user.HumanPasswordCodeAddedType,
|
|
|
|
user.HumanEmailVerifiedType,
|
2021-08-11 06:36:32 +00:00
|
|
|
user.HumanPasswordCheckFailedType,
|
|
|
|
user.HumanPasswordCheckSucceededType,
|
2023-07-28 07:09:15 +00:00
|
|
|
user.HumanPasswordHashUpdatedType,
|
2021-04-06 07:57:58 +00:00
|
|
|
user.UserRemovedType,
|
2021-08-11 06:36:32 +00:00
|
|
|
user.UserUnlockedType,
|
2021-04-06 07:57:58 +00:00
|
|
|
user.UserV1AddedType,
|
|
|
|
user.UserV1RegisteredType,
|
|
|
|
user.UserV1InitialCodeAddedType,
|
|
|
|
user.UserV1InitializedCheckSucceededType,
|
|
|
|
user.UserV1PasswordChangedType,
|
|
|
|
user.UserV1PasswordCodeAddedType,
|
2021-08-11 06:36:32 +00:00
|
|
|
user.UserV1EmailVerifiedType,
|
|
|
|
user.UserV1PasswordCheckFailedType,
|
2023-07-14 06:49:57 +00:00
|
|
|
user.UserV1PasswordCheckSucceededType,
|
2023-07-28 07:09:15 +00:00
|
|
|
).
|
2021-07-06 11:55:57 +00:00
|
|
|
Builder()
|
|
|
|
|
2021-02-08 10:30:30 +00:00
|
|
|
if wm.ResourceOwner != "" {
|
|
|
|
query.ResourceOwner(wm.ResourceOwner)
|
|
|
|
}
|
|
|
|
return query
|
2021-01-07 15:06:45 +00:00
|
|
|
}
|