2022-07-28 13:42:35 +00:00
|
|
|
package query
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-12-01 08:18:53 +00:00
|
|
|
"time"
|
|
|
|
|
2022-07-28 13:42:35 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/crypto"
|
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
2022-12-01 08:18:53 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/errors"
|
2022-07-28 13:42:35 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/user"
|
|
|
|
"github.com/zitadel/zitadel/internal/telemetry/tracing"
|
|
|
|
)
|
|
|
|
|
2023-02-27 21:36:43 +00:00
|
|
|
type HumanPasswordReadModel struct {
|
|
|
|
*eventstore.ReadModel
|
2022-07-28 13:42:35 +00:00
|
|
|
|
2023-07-14 06:49:57 +00:00
|
|
|
EncodedHash string
|
2022-07-28 13:42:35 +00:00
|
|
|
SecretChangeRequired bool
|
|
|
|
|
|
|
|
Code *crypto.CryptoValue
|
|
|
|
CodeCreationDate time.Time
|
|
|
|
CodeExpiry time.Duration
|
|
|
|
PasswordCheckFailedCount uint64
|
|
|
|
|
|
|
|
UserState domain.UserState
|
|
|
|
}
|
|
|
|
|
2023-07-14 06:49:57 +00:00
|
|
|
func (q *Queries) GetHumanPassword(ctx context.Context, orgID, userID string) (encodedHash string, err error) {
|
2022-12-01 08:18:53 +00:00
|
|
|
ctx, span := tracing.NewSpan(ctx)
|
|
|
|
defer func() { span.EndWithError(err) }()
|
|
|
|
|
2022-07-28 13:42:35 +00:00
|
|
|
if userID == "" {
|
2023-07-14 06:49:57 +00:00
|
|
|
return "", errors.ThrowInvalidArgument(nil, "QUERY-4Mfsf", "Errors.User.UserIDMissing")
|
2022-07-28 13:42:35 +00:00
|
|
|
}
|
2023-02-27 21:36:43 +00:00
|
|
|
existingPassword, err := q.passwordReadModel(ctx, userID, orgID)
|
2022-07-28 13:42:35 +00:00
|
|
|
if err != nil {
|
2023-07-14 06:49:57 +00:00
|
|
|
return "", errors.ThrowInternal(nil, "QUERY-p1k1n2i", "Errors.User.NotFound")
|
2022-07-28 13:42:35 +00:00
|
|
|
}
|
|
|
|
if existingPassword.UserState == domain.UserStateUnspecified || existingPassword.UserState == domain.UserStateDeleted {
|
2023-07-14 06:49:57 +00:00
|
|
|
return "", errors.ThrowPreconditionFailed(nil, "QUERY-3n77z", "Errors.User.NotFound")
|
2022-07-28 13:42:35 +00:00
|
|
|
}
|
2023-07-14 06:49:57 +00:00
|
|
|
return existingPassword.EncodedHash, nil
|
2022-07-28 13:42:35 +00:00
|
|
|
}
|
|
|
|
|
2023-02-27 21:36:43 +00:00
|
|
|
func (q *Queries) passwordReadModel(ctx context.Context, userID, resourceOwner string) (readModel *HumanPasswordReadModel, err error) {
|
2022-07-28 13:42:35 +00:00
|
|
|
ctx, span := tracing.NewSpan(ctx)
|
|
|
|
defer func() { span.EndWithError(err) }()
|
|
|
|
|
2023-02-27 21:36:43 +00:00
|
|
|
readModel = NewHumanPasswordReadModel(userID, resourceOwner)
|
|
|
|
err = q.eventstore.FilterToQueryReducer(ctx, readModel)
|
2022-07-28 13:42:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-02-27 21:36:43 +00:00
|
|
|
return readModel, nil
|
2022-07-28 13:42:35 +00:00
|
|
|
}
|
|
|
|
|
2023-02-27 21:36:43 +00:00
|
|
|
func NewHumanPasswordReadModel(userID, resourceOwner string) *HumanPasswordReadModel {
|
|
|
|
return &HumanPasswordReadModel{
|
|
|
|
ReadModel: &eventstore.ReadModel{
|
2022-07-28 13:42:35 +00:00
|
|
|
AggregateID: userID,
|
|
|
|
ResourceOwner: resourceOwner,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-27 21:36:43 +00:00
|
|
|
func (rm *HumanPasswordReadModel) AppendEvents(events ...eventstore.Event) {
|
|
|
|
rm.ReadModel.AppendEvents(events...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *HumanPasswordReadModel) Reduce() error {
|
2022-07-28 13:42:35 +00:00
|
|
|
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)
|
2022-07-28 13:42:35 +00:00
|
|
|
wm.SecretChangeRequired = e.ChangeRequired
|
|
|
|
wm.UserState = domain.UserStateActive
|
|
|
|
case *user.HumanRegisteredEvent:
|
2023-07-14 06:49:57 +00:00
|
|
|
wm.EncodedHash = user.SecretOrEncodedHash(e.Secret, e.EncodedHash)
|
2022-07-28 13:42:35 +00:00
|
|
|
wm.SecretChangeRequired = e.ChangeRequired
|
|
|
|
wm.UserState = domain.UserStateActive
|
|
|
|
case *user.HumanInitialCodeAddedEvent:
|
|
|
|
wm.UserState = domain.UserStateInitial
|
|
|
|
case *user.HumanInitializedCheckSucceededEvent:
|
|
|
|
wm.UserState = domain.UserStateActive
|
|
|
|
case *user.HumanPasswordChangedEvent:
|
2023-07-14 06:49:57 +00:00
|
|
|
wm.EncodedHash = user.SecretOrEncodedHash(e.Secret, e.EncodedHash)
|
2022-07-28 13:42:35 +00:00
|
|
|
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.UserUnlockedEvent:
|
|
|
|
wm.PasswordCheckFailedCount = 0
|
|
|
|
case *user.UserRemovedEvent:
|
|
|
|
wm.UserState = domain.UserStateDeleted
|
2023-07-14 06:49:57 +00:00
|
|
|
case *user.HumanPasswordHashUpdatedEvent:
|
|
|
|
wm.EncodedHash = e.EncodedHash
|
2022-07-28 13:42:35 +00:00
|
|
|
}
|
|
|
|
}
|
2023-02-27 21:36:43 +00:00
|
|
|
return wm.ReadModel.Reduce()
|
2022-07-28 13:42:35 +00:00
|
|
|
}
|
|
|
|
|
2023-02-27 21:36:43 +00:00
|
|
|
func (wm *HumanPasswordReadModel) Query() *eventstore.SearchQueryBuilder {
|
2022-07-28 13:42:35 +00:00
|
|
|
query := eventstore.NewSearchQueryBuilder(eventstore.ColumnsEvent).
|
2023-10-19 10:19:10 +00:00
|
|
|
AwaitOpenTransactions().
|
2023-02-27 21:36:43 +00:00
|
|
|
AllowTimeTravel().
|
2022-07-28 13:42:35 +00:00
|
|
|
AddQuery().
|
|
|
|
AggregateTypes(user.AggregateType).
|
|
|
|
AggregateIDs(wm.AggregateID).
|
|
|
|
EventTypes(user.HumanAddedType,
|
|
|
|
user.HumanRegisteredType,
|
|
|
|
user.HumanInitialCodeAddedType,
|
|
|
|
user.HumanInitializedCheckSucceededType,
|
|
|
|
user.HumanPasswordChangedType,
|
|
|
|
user.HumanPasswordCodeAddedType,
|
|
|
|
user.HumanEmailVerifiedType,
|
|
|
|
user.HumanPasswordCheckFailedType,
|
|
|
|
user.HumanPasswordCheckSucceededType,
|
2023-07-28 07:09:15 +00:00
|
|
|
user.HumanPasswordHashUpdatedType,
|
2022-07-28 13:42:35 +00:00
|
|
|
user.UserRemovedType,
|
|
|
|
user.UserUnlockedType,
|
|
|
|
user.UserV1AddedType,
|
|
|
|
user.UserV1RegisteredType,
|
|
|
|
user.UserV1InitialCodeAddedType,
|
|
|
|
user.UserV1InitializedCheckSucceededType,
|
|
|
|
user.UserV1PasswordChangedType,
|
|
|
|
user.UserV1PasswordCodeAddedType,
|
|
|
|
user.UserV1EmailVerifiedType,
|
|
|
|
user.UserV1PasswordCheckFailedType,
|
2023-07-14 06:49:57 +00:00
|
|
|
user.UserV1PasswordCheckSucceededType,
|
2023-07-28 07:09:15 +00:00
|
|
|
).
|
2022-07-28 13:42:35 +00:00
|
|
|
Builder()
|
|
|
|
|
|
|
|
if wm.ResourceOwner != "" {
|
|
|
|
query.ResourceOwner(wm.ResourceOwner)
|
|
|
|
}
|
|
|
|
return query
|
|
|
|
}
|