2021-01-04 14:52:13 +01:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-01-06 10:47:55 +01:00
|
|
|
|
2021-01-04 14:52:13 +01:00
|
|
|
caos_errs "github.com/caos/zitadel/internal/errors"
|
|
|
|
"github.com/caos/zitadel/internal/telemetry/tracing"
|
|
|
|
"github.com/caos/zitadel/internal/v2/domain"
|
|
|
|
iam_repo "github.com/caos/zitadel/internal/v2/repository/iam"
|
|
|
|
)
|
|
|
|
|
2021-01-18 11:24:15 +01:00
|
|
|
func (r *CommandSide) getDefaultLoginPolicy(ctx context.Context) (*domain.LoginPolicy, error) {
|
2021-01-12 12:59:51 +01:00
|
|
|
policyWriteModel := NewIAMLoginPolicyWriteModel()
|
2021-01-06 10:47:55 +01:00
|
|
|
err := r.eventstore.FilterToQueryReducer(ctx, policyWriteModel)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-01-18 11:24:15 +01:00
|
|
|
policy := writeModelToLoginPolicy(&policyWriteModel.LoginPolicyWriteModel)
|
2021-01-06 10:47:55 +01:00
|
|
|
policy.Default = true
|
|
|
|
return policy, nil
|
|
|
|
}
|
|
|
|
|
2021-01-05 09:33:45 +01:00
|
|
|
func (r *CommandSide) AddDefaultLoginPolicy(ctx context.Context, policy *domain.LoginPolicy) (*domain.LoginPolicy, error) {
|
2021-01-12 12:59:51 +01:00
|
|
|
addedPolicy := NewIAMLoginPolicyWriteModel()
|
2021-01-06 10:47:55 +01:00
|
|
|
iamAgg := IAMAggregateFromWriteModel(&addedPolicy.WriteModel)
|
|
|
|
err := r.addDefaultLoginPolicy(ctx, nil, addedPolicy, policy)
|
2021-01-04 14:52:13 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = r.eventstore.PushAggregate(ctx, addedPolicy, iamAgg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-01-18 11:24:15 +01:00
|
|
|
return writeModelToLoginPolicy(&addedPolicy.LoginPolicyWriteModel), nil
|
2021-01-04 14:52:13 +01:00
|
|
|
}
|
|
|
|
|
2021-01-06 10:47:55 +01:00
|
|
|
func (r *CommandSide) addDefaultLoginPolicy(ctx context.Context, iamAgg *iam_repo.Aggregate, addedPolicy *IAMLoginPolicyWriteModel, policy *domain.LoginPolicy) error {
|
2021-01-04 14:52:13 +01:00
|
|
|
err := r.eventstore.FilterToQueryReducer(ctx, addedPolicy)
|
|
|
|
if err != nil {
|
2021-01-06 10:47:55 +01:00
|
|
|
return err
|
2021-01-04 14:52:13 +01:00
|
|
|
}
|
2021-01-07 16:06:45 +01:00
|
|
|
if addedPolicy.State == domain.PolicyStateActive {
|
2021-01-06 10:47:55 +01:00
|
|
|
return caos_errs.ThrowAlreadyExists(nil, "IAM-2B0ps", "Errors.IAM.LoginPolicy.AlreadyExists")
|
2021-01-04 14:52:13 +01:00
|
|
|
}
|
|
|
|
|
2021-02-08 11:30:30 +01:00
|
|
|
iamAgg.PushEvents(iam_repo.NewLoginPolicyAddedEvent(ctx, policy.AllowUsernamePassword, policy.AllowRegister, policy.AllowExternalIDP, policy.ForceMFA, policy.PasswordlessType))
|
2021-01-04 14:52:13 +01:00
|
|
|
|
2021-01-06 10:47:55 +01:00
|
|
|
return nil
|
2021-01-04 14:52:13 +01:00
|
|
|
}
|
|
|
|
|
2021-01-05 09:33:45 +01:00
|
|
|
func (r *CommandSide) ChangeDefaultLoginPolicy(ctx context.Context, policy *domain.LoginPolicy) (*domain.LoginPolicy, error) {
|
2021-01-12 12:59:51 +01:00
|
|
|
existingPolicy := NewIAMLoginPolicyWriteModel()
|
2021-01-06 10:47:55 +01:00
|
|
|
iamAgg := IAMAggregateFromWriteModel(&existingPolicy.LoginPolicyWriteModel.WriteModel)
|
|
|
|
err := r.changeDefaultLoginPolicy(ctx, iamAgg, existingPolicy, policy)
|
2021-01-04 14:52:13 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-01-06 10:47:55 +01:00
|
|
|
err = r.eventstore.PushAggregate(ctx, existingPolicy, iamAgg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-01-18 11:24:15 +01:00
|
|
|
return writeModelToLoginPolicy(&existingPolicy.LoginPolicyWriteModel), nil
|
2021-01-06 10:47:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *CommandSide) changeDefaultLoginPolicy(ctx context.Context, iamAgg *iam_repo.Aggregate, existingPolicy *IAMLoginPolicyWriteModel, policy *domain.LoginPolicy) error {
|
|
|
|
err := r.defaultLoginPolicyWriteModelByID(ctx, existingPolicy)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-01-07 16:06:45 +01:00
|
|
|
if existingPolicy.State == domain.PolicyStateUnspecified || existingPolicy.State == domain.PolicyStateRemoved {
|
|
|
|
return caos_errs.ThrowNotFound(nil, "IAM-M0sif", "Errors.IAM.LoginPolicy.NotFound")
|
2021-01-04 14:52:13 +01:00
|
|
|
}
|
2021-02-08 11:30:30 +01:00
|
|
|
changedEvent, hasChanged := existingPolicy.NewChangedEvent(ctx, policy.AllowUsernamePassword, policy.AllowRegister, policy.AllowExternalIDP, policy.ForceMFA, policy.PasswordlessType)
|
2021-01-04 14:52:13 +01:00
|
|
|
if !hasChanged {
|
2021-01-07 16:06:45 +01:00
|
|
|
return caos_errs.ThrowPreconditionFailed(nil, "IAM-5M9vdd", "Errors.IAM.LoginPolicy.NotChanged")
|
2021-01-04 14:52:13 +01:00
|
|
|
}
|
|
|
|
iamAgg.PushEvents(changedEvent)
|
|
|
|
|
2021-01-06 10:47:55 +01:00
|
|
|
return nil
|
2021-01-04 14:52:13 +01:00
|
|
|
}
|
|
|
|
|
2021-01-05 09:33:45 +01:00
|
|
|
func (r *CommandSide) AddIDPProviderToDefaultLoginPolicy(ctx context.Context, idpProvider *domain.IDPProvider) (*domain.IDPProvider, error) {
|
2021-01-12 12:59:51 +01:00
|
|
|
idpModel := NewIAMIdentityProviderWriteModel(idpProvider.IDPConfigID)
|
2021-01-04 14:52:13 +01:00
|
|
|
err := r.eventstore.FilterToQueryReducer(ctx, idpModel)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-01-07 16:06:45 +01:00
|
|
|
if idpModel.State == domain.IdentityProviderStateActive {
|
2021-01-04 14:52:13 +01:00
|
|
|
return nil, caos_errs.ThrowAlreadyExists(nil, "IAM-2B0ps", "Errors.IAM.LoginPolicy.IDP.AlreadyExists")
|
|
|
|
}
|
|
|
|
|
|
|
|
iamAgg := IAMAggregateFromWriteModel(&idpModel.WriteModel)
|
|
|
|
iamAgg.PushEvents(iam_repo.NewIdentityProviderAddedEvent(ctx, idpProvider.IDPConfigID, domain.IdentityProviderType(idpProvider.Type)))
|
|
|
|
|
|
|
|
if err = r.eventstore.PushAggregate(ctx, idpModel, iamAgg); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-01-18 11:24:15 +01:00
|
|
|
return writeModelToIDPProvider(&idpModel.IdentityProviderWriteModel), nil
|
2021-01-04 14:52:13 +01:00
|
|
|
}
|
|
|
|
|
2021-01-18 11:24:15 +01:00
|
|
|
func (r *CommandSide) RemoveIDPProviderFromDefaultLoginPolicy(ctx context.Context, idpProvider *domain.IDPProvider) error {
|
2021-01-12 12:59:51 +01:00
|
|
|
idpModel := NewIAMIdentityProviderWriteModel(idpProvider.IDPConfigID)
|
2021-01-04 14:52:13 +01:00
|
|
|
err := r.eventstore.FilterToQueryReducer(ctx, idpModel)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-01-07 16:06:45 +01:00
|
|
|
if idpModel.State == domain.IdentityProviderStateUnspecified || idpModel.State == domain.IdentityProviderStateRemoved {
|
|
|
|
return caos_errs.ThrowNotFound(nil, "IAM-39fjs", "Errors.IAM.LoginPolicy.IDP.NotExisting")
|
2021-01-04 14:52:13 +01:00
|
|
|
}
|
|
|
|
iamAgg := IAMAggregateFromWriteModel(&idpModel.IdentityProviderWriteModel.WriteModel)
|
|
|
|
iamAgg.PushEvents(iam_repo.NewIdentityProviderRemovedEvent(ctx, idpProvider.IDPConfigID))
|
|
|
|
|
|
|
|
return r.eventstore.PushAggregate(ctx, idpModel, iamAgg)
|
|
|
|
}
|
|
|
|
|
2021-01-18 11:24:15 +01:00
|
|
|
func (r *CommandSide) AddSecondFactorToDefaultLoginPolicy(ctx context.Context, secondFactor domain.SecondFactorType) (domain.SecondFactorType, error) {
|
2021-01-12 12:59:51 +01:00
|
|
|
secondFactorModel := NewIAMSecondFactorWriteModel()
|
2021-01-06 10:47:55 +01:00
|
|
|
iamAgg := IAMAggregateFromWriteModel(&secondFactorModel.SecondFactorWriteModel.WriteModel)
|
|
|
|
err := r.addSecondFactorToDefaultLoginPolicy(ctx, nil, secondFactorModel, secondFactor)
|
2021-01-04 14:52:13 +01:00
|
|
|
if err != nil {
|
2021-01-18 11:24:15 +01:00
|
|
|
return domain.SecondFactorTypeUnspecified, err
|
2021-01-04 14:52:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if err = r.eventstore.PushAggregate(ctx, secondFactorModel, iamAgg); err != nil {
|
2021-01-18 11:24:15 +01:00
|
|
|
return domain.SecondFactorTypeUnspecified, err
|
2021-01-04 14:52:13 +01:00
|
|
|
}
|
|
|
|
|
2021-01-18 11:24:15 +01:00
|
|
|
return domain.SecondFactorType(secondFactorModel.MFAType), nil
|
2021-01-04 14:52:13 +01:00
|
|
|
}
|
|
|
|
|
2021-01-18 11:24:15 +01:00
|
|
|
func (r *CommandSide) addSecondFactorToDefaultLoginPolicy(ctx context.Context, iamAgg *iam_repo.Aggregate, secondFactorModel *IAMSecondFactorWriteModel, secondFactor domain.SecondFactorType) error {
|
2021-01-06 10:47:55 +01:00
|
|
|
err := r.eventstore.FilterToQueryReducer(ctx, secondFactorModel)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-01-07 16:06:45 +01:00
|
|
|
if secondFactorModel.State == domain.FactorStateActive {
|
2021-01-06 10:47:55 +01:00
|
|
|
return caos_errs.ThrowAlreadyExists(nil, "IAM-2B0ps", "Errors.IAM.LoginPolicy.MFA.AlreadyExists")
|
|
|
|
}
|
|
|
|
|
|
|
|
iamAgg.PushEvents(iam_repo.NewLoginPolicySecondFactorAddedEvent(ctx, domain.SecondFactorType(secondFactor)))
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-18 11:24:15 +01:00
|
|
|
func (r *CommandSide) RemoveSecondFactorFromDefaultLoginPolicy(ctx context.Context, secondFactor domain.SecondFactorType) error {
|
2021-01-12 12:59:51 +01:00
|
|
|
secondFactorModel := NewIAMSecondFactorWriteModel()
|
2021-01-04 14:52:13 +01:00
|
|
|
err := r.eventstore.FilterToQueryReducer(ctx, secondFactorModel)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-01-07 16:06:45 +01:00
|
|
|
if secondFactorModel.State == domain.FactorStateUnspecified || secondFactorModel.State == domain.FactorStateRemoved {
|
|
|
|
return caos_errs.ThrowNotFound(nil, "IAM-3M9od", "Errors.IAM.LoginPolicy.MFA.NotExisting")
|
2021-01-04 14:52:13 +01:00
|
|
|
}
|
|
|
|
iamAgg := IAMAggregateFromWriteModel(&secondFactorModel.SecondFactorWriteModel.WriteModel)
|
|
|
|
iamAgg.PushEvents(iam_repo.NewLoginPolicySecondFactorRemovedEvent(ctx, domain.SecondFactorType(secondFactor)))
|
|
|
|
|
|
|
|
return r.eventstore.PushAggregate(ctx, secondFactorModel, iamAgg)
|
|
|
|
}
|
|
|
|
|
2021-01-18 11:24:15 +01:00
|
|
|
func (r *CommandSide) AddMultiFactorToDefaultLoginPolicy(ctx context.Context, multiFactor domain.MultiFactorType) (domain.MultiFactorType, error) {
|
2021-01-12 12:59:51 +01:00
|
|
|
multiFactorModel := NewIAMMultiFactorWriteModel()
|
2021-01-06 10:47:55 +01:00
|
|
|
iamAgg := IAMAggregateFromWriteModel(&multiFactorModel.MultiFactoryWriteModel.WriteModel)
|
|
|
|
err := r.addMultiFactorToDefaultLoginPolicy(ctx, iamAgg, multiFactorModel, multiFactor)
|
2021-01-04 14:52:13 +01:00
|
|
|
if err != nil {
|
2021-01-18 11:24:15 +01:00
|
|
|
return domain.MultiFactorTypeUnspecified, err
|
2021-01-04 14:52:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if err = r.eventstore.PushAggregate(ctx, multiFactorModel, iamAgg); err != nil {
|
2021-01-18 11:24:15 +01:00
|
|
|
return domain.MultiFactorTypeUnspecified, err
|
2021-01-04 14:52:13 +01:00
|
|
|
}
|
|
|
|
|
2021-01-18 11:24:15 +01:00
|
|
|
return domain.MultiFactorType(multiFactorModel.MultiFactoryWriteModel.MFAType), nil
|
2021-01-04 14:52:13 +01:00
|
|
|
}
|
|
|
|
|
2021-01-18 11:24:15 +01:00
|
|
|
func (r *CommandSide) addMultiFactorToDefaultLoginPolicy(ctx context.Context, iamAgg *iam_repo.Aggregate, multiFactorModel *IAMMultiFactorWriteModel, multiFactor domain.MultiFactorType) error {
|
2021-01-06 10:47:55 +01:00
|
|
|
err := r.eventstore.FilterToQueryReducer(ctx, multiFactorModel)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-01-07 16:06:45 +01:00
|
|
|
if multiFactorModel.State == domain.FactorStateActive {
|
2021-01-06 10:47:55 +01:00
|
|
|
return caos_errs.ThrowAlreadyExists(nil, "IAM-3M9od", "Errors.IAM.LoginPolicy.MFA.AlreadyExists")
|
|
|
|
}
|
|
|
|
|
|
|
|
iamAgg.PushEvents(iam_repo.NewLoginPolicyMultiFactorAddedEvent(ctx, domain.MultiFactorType(multiFactor)))
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-18 11:24:15 +01:00
|
|
|
func (r *CommandSide) RemoveMultiFactorFromDefaultLoginPolicy(ctx context.Context, multiFactor domain.MultiFactorType) error {
|
2021-01-12 12:59:51 +01:00
|
|
|
multiFactorModel := NewIAMMultiFactorWriteModel()
|
2021-01-04 14:52:13 +01:00
|
|
|
err := r.eventstore.FilterToQueryReducer(ctx, multiFactorModel)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-01-07 16:06:45 +01:00
|
|
|
if multiFactorModel.State == domain.FactorStateUnspecified || multiFactorModel.State == domain.FactorStateRemoved {
|
|
|
|
return caos_errs.ThrowNotFound(nil, "IAM-3M9df", "Errors.IAM.LoginPolicy.MFA.NotExisting")
|
2021-01-04 14:52:13 +01:00
|
|
|
}
|
|
|
|
iamAgg := IAMAggregateFromWriteModel(&multiFactorModel.MultiFactoryWriteModel.WriteModel)
|
|
|
|
iamAgg.PushEvents(iam_repo.NewLoginPolicyMultiFactorRemovedEvent(ctx, domain.MultiFactorType(multiFactor)))
|
|
|
|
|
|
|
|
return r.eventstore.PushAggregate(ctx, multiFactorModel, iamAgg)
|
|
|
|
}
|
|
|
|
|
2021-01-06 10:47:55 +01:00
|
|
|
func (r *CommandSide) defaultLoginPolicyWriteModelByID(ctx context.Context, writeModel *IAMLoginPolicyWriteModel) (err error) {
|
2021-01-04 14:52:13 +01:00
|
|
|
ctx, span := tracing.NewSpan(ctx)
|
|
|
|
defer func() { span.EndWithError(err) }()
|
|
|
|
|
|
|
|
err = r.eventstore.FilterToQueryReducer(ctx, writeModel)
|
|
|
|
if err != nil {
|
2021-01-06 10:47:55 +01:00
|
|
|
return err
|
2021-01-04 14:52:13 +01:00
|
|
|
}
|
2021-01-06 10:47:55 +01:00
|
|
|
return nil
|
2021-01-04 14:52:13 +01:00
|
|
|
}
|