2021-01-20 10:06:52 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
caos_errs "github.com/caos/zitadel/internal/errors"
|
|
|
|
"github.com/caos/zitadel/internal/v2/domain"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (r *CommandSide) ChangeIDPOIDCConfig(ctx context.Context, config *domain.OIDCIDPConfig) (*domain.OIDCIDPConfig, error) {
|
|
|
|
existingConfig := NewOrgIDPOIDCConfigWriteModel(config.IDPConfigID, config.AggregateID)
|
|
|
|
err := r.eventstore.FilterToQueryReducer(ctx, existingConfig)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if existingConfig.State == domain.IDPConfigStateRemoved || existingConfig.State == domain.IDPConfigStateUnspecified {
|
|
|
|
return nil, caos_errs.ThrowAlreadyExists(nil, "Org-67J9d", "Errors.Org.IDPConfig.AlreadyExists")
|
|
|
|
}
|
|
|
|
|
2021-02-18 13:48:27 +00:00
|
|
|
orgAgg := OrgAggregateFromWriteModel(&existingConfig.WriteModel)
|
2021-01-20 10:06:52 +00:00
|
|
|
changedEvent, hasChanged, err := existingConfig.NewChangedEvent(
|
|
|
|
ctx,
|
2021-02-18 13:48:27 +00:00
|
|
|
orgAgg,
|
2021-01-20 10:06:52 +00:00
|
|
|
config.IDPConfigID,
|
|
|
|
config.ClientID,
|
|
|
|
config.Issuer,
|
|
|
|
config.ClientSecretString,
|
|
|
|
r.idpConfigSecretCrypto,
|
|
|
|
config.IDPDisplayNameMapping,
|
|
|
|
config.UsernameMapping,
|
|
|
|
config.Scopes...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if !hasChanged {
|
|
|
|
return nil, caos_errs.ThrowPreconditionFailed(nil, "Org-4M9vs", "Errors.Org.LabelPolicy.NotChanged")
|
|
|
|
}
|
|
|
|
|
2021-02-18 13:48:27 +00:00
|
|
|
pushedEvents, err := r.eventstore.PushEvents(ctx, changedEvent)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = AppendAndReduce(existingConfig, pushedEvents...)
|
2021-01-20 10:06:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return writeModelToIDPOIDCConfig(&existingConfig.OIDCConfigWriteModel), nil
|
|
|
|
}
|