2021-01-07 15:06:45 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-02-08 10:30:30 +00:00
|
|
|
"github.com/caos/logging"
|
2021-01-07 15:06:45 +00:00
|
|
|
"github.com/caos/zitadel/internal/crypto"
|
2021-02-23 14:13:04 +00:00
|
|
|
"github.com/caos/zitadel/internal/domain"
|
2021-01-07 15:06:45 +00:00
|
|
|
caos_errs "github.com/caos/zitadel/internal/errors"
|
2021-02-23 14:13:04 +00:00
|
|
|
"github.com/caos/zitadel/internal/eventstore"
|
|
|
|
"github.com/caos/zitadel/internal/repository/user"
|
2021-01-07 15:06:45 +00:00
|
|
|
"github.com/caos/zitadel/internal/telemetry/tracing"
|
|
|
|
)
|
|
|
|
|
2021-02-24 10:17:39 +00:00
|
|
|
func (c *Commands) SetOneTimePassword(ctx context.Context, orgID, userID, passwordString string) (err error) {
|
2021-01-07 15:06:45 +00:00
|
|
|
ctx, span := tracing.NewSpan(ctx)
|
|
|
|
defer func() { span.EndWithError(err) }()
|
|
|
|
|
2021-02-24 10:17:39 +00:00
|
|
|
existingPassword, err := c.passwordWriteModel(ctx, userID, orgID)
|
2021-01-07 15:06:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
password := &domain.Password{
|
|
|
|
SecretString: passwordString,
|
|
|
|
ChangeRequired: true,
|
|
|
|
}
|
2021-02-08 10:30:30 +00:00
|
|
|
userAgg := UserAggregateFromWriteModel(&existingPassword.WriteModel)
|
2021-02-24 10:17:39 +00:00
|
|
|
passwordEvent, err := c.changePassword(ctx, "", password, userAgg, existingPassword)
|
2021-02-18 13:48:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-02-24 10:17:39 +00:00
|
|
|
_, err = c.eventstore.PushEvents(ctx, passwordEvent)
|
2021-02-18 13:48:27 +00:00
|
|
|
return err
|
2021-02-08 10:30:30 +00:00
|
|
|
}
|
|
|
|
|
2021-02-24 10:17:39 +00:00
|
|
|
func (c *Commands) SetPassword(ctx context.Context, orgID, userID, code, passwordString, userAgentID string) (err error) {
|
2021-02-08 10:30:30 +00:00
|
|
|
ctx, span := tracing.NewSpan(ctx)
|
|
|
|
defer func() { span.EndWithError(err) }()
|
|
|
|
|
2021-02-24 10:17:39 +00:00
|
|
|
existingCode, err := c.passwordWriteModel(ctx, userID, orgID)
|
2021-02-08 10:30:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if existingCode.Code == nil || existingCode.UserState == domain.UserStateUnspecified || existingCode.UserState == domain.UserStateDeleted {
|
|
|
|
return caos_errs.ThrowNotFound(nil, "COMMAND-2M9fs", "Errors.User.Code.NotFound")
|
|
|
|
}
|
|
|
|
|
2021-02-24 10:17:39 +00:00
|
|
|
err = crypto.VerifyCode(existingCode.CodeCreationDate, existingCode.CodeExpiry, existingCode.Code, code, c.emailVerificationCode)
|
2021-02-08 10:30:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
password := &domain.Password{
|
|
|
|
SecretString: passwordString,
|
|
|
|
ChangeRequired: false,
|
|
|
|
}
|
|
|
|
userAgg := UserAggregateFromWriteModel(&existingCode.WriteModel)
|
2021-02-24 10:17:39 +00:00
|
|
|
passwordEvent, err := c.changePassword(ctx, userAgentID, password, userAgg, existingCode)
|
2021-02-18 13:48:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-02-24 10:17:39 +00:00
|
|
|
_, err = c.eventstore.PushEvents(ctx, passwordEvent)
|
2021-02-18 13:48:27 +00:00
|
|
|
return err
|
2021-01-07 15:06:45 +00:00
|
|
|
}
|
|
|
|
|
2021-02-24 10:17:39 +00:00
|
|
|
func (c *Commands) ChangePassword(ctx context.Context, orgID, userID, oldPassword, newPassword, userAgentID string) (err error) {
|
2021-01-07 15:06:45 +00:00
|
|
|
ctx, span := tracing.NewSpan(ctx)
|
|
|
|
defer func() { span.EndWithError(err) }()
|
|
|
|
|
2021-02-24 10:17:39 +00:00
|
|
|
existingPassword, err := c.passwordWriteModel(ctx, userID, orgID)
|
2021-01-07 15:06:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-02-08 10:30:30 +00:00
|
|
|
if existingPassword.Secret == nil {
|
2021-01-07 15:06:45 +00:00
|
|
|
return caos_errs.ThrowPreconditionFailed(nil, "COMMAND-Fds3s", "Errors.User.Password.Empty")
|
|
|
|
}
|
|
|
|
ctx, spanPasswordComparison := tracing.NewNamedSpan(ctx, "crypto.CompareHash")
|
2021-02-24 10:17:39 +00:00
|
|
|
err = crypto.CompareHash(existingPassword.Secret, []byte(oldPassword), c.userPasswordAlg)
|
2021-01-07 15:06:45 +00:00
|
|
|
spanPasswordComparison.EndWithError(err)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return caos_errs.ThrowInvalidArgument(nil, "COMMAND-3M0fs", "Errors.User.Password.Invalid")
|
|
|
|
}
|
|
|
|
password := &domain.Password{
|
|
|
|
SecretString: newPassword,
|
2021-02-08 10:30:30 +00:00
|
|
|
ChangeRequired: false,
|
2021-01-07 15:06:45 +00:00
|
|
|
}
|
2021-02-08 10:30:30 +00:00
|
|
|
|
|
|
|
userAgg := UserAggregateFromWriteModel(&existingPassword.WriteModel)
|
2021-02-24 10:17:39 +00:00
|
|
|
eventPusher, err := c.changePassword(ctx, userAgentID, password, userAgg, existingPassword)
|
2021-02-18 13:48:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-02-24 10:17:39 +00:00
|
|
|
_, err = c.eventstore.PushEvents(ctx, eventPusher)
|
2021-02-18 13:48:27 +00:00
|
|
|
return err
|
2021-01-07 15:06:45 +00:00
|
|
|
}
|
|
|
|
|
2021-02-24 10:17:39 +00:00
|
|
|
func (c *Commands) changePassword(ctx context.Context, userAgentID string, password *domain.Password, userAgg *eventstore.Aggregate, existingPassword *HumanPasswordWriteModel) (event eventstore.EventPusher, err error) {
|
2021-01-07 15:06:45 +00:00
|
|
|
ctx, span := tracing.NewSpan(ctx)
|
|
|
|
defer func() { span.EndWithError(err) }()
|
|
|
|
|
|
|
|
if existingPassword.UserState == domain.UserStateUnspecified || existingPassword.UserState == domain.UserStateDeleted {
|
2021-02-18 13:48:27 +00:00
|
|
|
return nil, caos_errs.ThrowNotFound(nil, "COMMAND-G8dh3", "Errors.User.Email.NotFound")
|
2021-01-07 15:06:45 +00:00
|
|
|
}
|
|
|
|
if existingPassword.UserState == domain.UserStateInitial {
|
2021-02-18 13:48:27 +00:00
|
|
|
return nil, caos_errs.ThrowPreconditionFailed(nil, "COMMAND-M9dse", "Errors.User.NotInitialised")
|
2021-01-07 15:06:45 +00:00
|
|
|
}
|
2021-02-24 10:17:39 +00:00
|
|
|
pwPolicy, err := c.getOrgPasswordComplexityPolicy(ctx, userAgg.ResourceOwner)
|
2021-01-07 15:06:45 +00:00
|
|
|
if err != nil {
|
2021-02-18 13:48:27 +00:00
|
|
|
return nil, err
|
2021-01-07 15:06:45 +00:00
|
|
|
}
|
2021-02-24 10:17:39 +00:00
|
|
|
if err := password.HashPasswordIfExisting(pwPolicy, c.userPasswordAlg); err != nil {
|
2021-02-18 13:48:27 +00:00
|
|
|
return nil, err
|
2021-01-07 15:06:45 +00:00
|
|
|
}
|
2021-02-18 13:48:27 +00:00
|
|
|
return user.NewHumanPasswordChangedEvent(ctx, userAgg, password.SecretCrypto, password.ChangeRequired, userAgentID), nil
|
2021-01-07 15:06:45 +00:00
|
|
|
}
|
|
|
|
|
2021-02-24 10:17:39 +00:00
|
|
|
func (c *Commands) RequestSetPassword(ctx context.Context, userID, resourceOwner string, notifyType domain.NotificationType) (err error) {
|
|
|
|
existingHuman, err := c.userWriteModelByID(ctx, userID, resourceOwner)
|
2021-01-07 15:06:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if existingHuman.UserState == domain.UserStateUnspecified || existingHuman.UserState == domain.UserStateDeleted {
|
|
|
|
return caos_errs.ThrowNotFound(nil, "COMMAND-Hj9ds", "Errors.User.NotFound")
|
|
|
|
}
|
|
|
|
if existingHuman.UserState == domain.UserStateInitial {
|
|
|
|
return caos_errs.ThrowPreconditionFailed(nil, "COMMAND-2M9sd", "Errors.User.NotInitialised")
|
|
|
|
}
|
|
|
|
userAgg := UserAggregateFromWriteModel(&existingHuman.WriteModel)
|
2021-02-24 10:17:39 +00:00
|
|
|
passwordCode, err := domain.NewPasswordCode(c.passwordVerificationCode)
|
2021-01-07 15:06:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-02-24 10:17:39 +00:00
|
|
|
_, err = c.eventstore.PushEvents(ctx, user.NewHumanPasswordCodeAddedEvent(ctx, userAgg, passwordCode.Code, passwordCode.Expiry, notifyType))
|
2021-02-18 13:48:27 +00:00
|
|
|
return err
|
2021-01-07 15:06:45 +00:00
|
|
|
}
|
|
|
|
|
2021-02-24 10:17:39 +00:00
|
|
|
func (c *Commands) PasswordCodeSent(ctx context.Context, orgID, userID string) (err error) {
|
|
|
|
existingPassword, err := c.passwordWriteModel(ctx, userID, orgID)
|
2021-02-08 10:30:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if existingPassword.UserState == domain.UserStateUnspecified || existingPassword.UserState == domain.UserStateDeleted {
|
|
|
|
return caos_errs.ThrowNotFound(nil, "COMMAND-3n77z", "Errors.User.NotFound")
|
|
|
|
}
|
|
|
|
userAgg := UserAggregateFromWriteModel(&existingPassword.WriteModel)
|
2021-02-24 10:17:39 +00:00
|
|
|
_, err = c.eventstore.PushEvents(ctx, user.NewHumanPasswordCodeSentEvent(ctx, userAgg))
|
2021-02-18 13:48:27 +00:00
|
|
|
return err
|
2021-02-08 10:30:30 +00:00
|
|
|
}
|
|
|
|
|
2021-02-24 10:17:39 +00:00
|
|
|
func (c *Commands) HumanCheckPassword(ctx context.Context, orgID, userID, password string, authRequest *domain.AuthRequest) (err error) {
|
2021-02-08 10:30:30 +00:00
|
|
|
ctx, span := tracing.NewSpan(ctx)
|
|
|
|
defer func() { span.EndWithError(err) }()
|
|
|
|
|
|
|
|
if password == "" {
|
|
|
|
return caos_errs.ThrowNotFound(nil, "COMMAND-3n8fs", "Errors.User.Password.Empty")
|
|
|
|
}
|
|
|
|
|
2021-02-24 10:17:39 +00:00
|
|
|
existingPassword, err := c.passwordWriteModel(ctx, userID, orgID)
|
2021-02-08 10:30:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if existingPassword.UserState == domain.UserStateUnspecified || existingPassword.UserState == domain.UserStateDeleted {
|
|
|
|
return caos_errs.ThrowNotFound(nil, "COMMAND-3n77z", "Errors.User.NotFound")
|
|
|
|
}
|
|
|
|
|
|
|
|
if existingPassword.Secret == nil {
|
|
|
|
return caos_errs.ThrowNotFound(nil, "COMMAND-3n77z", "Errors.User.Password.NotSet")
|
|
|
|
}
|
|
|
|
|
|
|
|
userAgg := UserAggregateFromWriteModel(&existingPassword.WriteModel)
|
|
|
|
ctx, spanPasswordComparison := tracing.NewNamedSpan(ctx, "crypto.CompareHash")
|
2021-02-24 10:17:39 +00:00
|
|
|
err = crypto.CompareHash(existingPassword.Secret, []byte(password), c.userPasswordAlg)
|
2021-02-08 10:30:30 +00:00
|
|
|
spanPasswordComparison.EndWithError(err)
|
|
|
|
if err == nil {
|
2021-02-24 10:17:39 +00:00
|
|
|
_, err = c.eventstore.PushEvents(ctx, user.NewHumanPasswordCheckSucceededEvent(ctx, userAgg, authRequestDomainToAuthRequestInfo(authRequest)))
|
2021-02-18 13:48:27 +00:00
|
|
|
return err
|
2021-02-08 10:30:30 +00:00
|
|
|
}
|
2021-02-24 10:17:39 +00:00
|
|
|
_, err = c.eventstore.PushEvents(ctx, user.NewHumanPasswordCheckFailedEvent(ctx, userAgg, authRequestDomainToAuthRequestInfo(authRequest)))
|
2021-02-08 10:30:30 +00:00
|
|
|
logging.Log("COMMAND-9fj7s").OnError(err).Error("error create password check failed event")
|
|
|
|
return caos_errs.ThrowInvalidArgument(nil, "COMMAND-452ad", "Errors.User.Password.Invalid")
|
|
|
|
}
|
|
|
|
|
2021-02-24 10:17:39 +00:00
|
|
|
func (c *Commands) passwordWriteModel(ctx context.Context, userID, resourceOwner string) (writeModel *HumanPasswordWriteModel, err error) {
|
2021-01-07 15:06:45 +00:00
|
|
|
ctx, span := tracing.NewSpan(ctx)
|
|
|
|
defer func() { span.EndWithError(err) }()
|
|
|
|
|
2021-01-12 11:59:51 +00:00
|
|
|
writeModel = NewHumanPasswordWriteModel(userID, resourceOwner)
|
2021-02-24 10:17:39 +00:00
|
|
|
err = c.eventstore.FilterToQueryReducer(ctx, writeModel)
|
2021-01-07 15:06:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return writeModel, nil
|
|
|
|
}
|