2021-01-04 13:52:13 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-02-23 14:13:04 +00:00
|
|
|
"github.com/caos/zitadel/internal/domain"
|
2021-01-04 13:52:13 +00:00
|
|
|
caos_errs "github.com/caos/zitadel/internal/errors"
|
|
|
|
)
|
|
|
|
|
2021-02-24 10:17:39 +00:00
|
|
|
func (c *Commands) ChangeDefaultIDPOIDCConfig(ctx context.Context, config *domain.OIDCIDPConfig) (*domain.OIDCIDPConfig, error) {
|
2021-01-12 11:59:51 +00:00
|
|
|
existingConfig := NewIAMIDPOIDCConfigWriteModel(config.IDPConfigID)
|
2021-02-24 10:17:39 +00:00
|
|
|
err := c.eventstore.FilterToQueryReducer(ctx, existingConfig)
|
2021-01-04 13:52:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if existingConfig.State == domain.IDPConfigStateRemoved || existingConfig.State == domain.IDPConfigStateUnspecified {
|
|
|
|
return nil, caos_errs.ThrowAlreadyExists(nil, "IAM-67J9d", "Errors.IAM.IDPConfig.AlreadyExists")
|
|
|
|
}
|
|
|
|
|
2021-02-18 13:48:27 +00:00
|
|
|
iamAgg := IAMAggregateFromWriteModel(&existingConfig.WriteModel)
|
2021-01-04 13:52:13 +00:00
|
|
|
changedEvent, hasChanged, err := existingConfig.NewChangedEvent(
|
|
|
|
ctx,
|
2021-02-18 13:48:27 +00:00
|
|
|
iamAgg,
|
2021-01-20 10:06:52 +00:00
|
|
|
config.IDPConfigID,
|
2021-01-04 13:52:13 +00:00
|
|
|
config.ClientID,
|
|
|
|
config.Issuer,
|
|
|
|
config.ClientSecretString,
|
2021-02-24 10:17:39 +00:00
|
|
|
c.idpConfigSecretCrypto,
|
2021-01-05 08:33:45 +00:00
|
|
|
config.IDPDisplayNameMapping,
|
|
|
|
config.UsernameMapping,
|
2021-01-04 13:52:13 +00:00
|
|
|
config.Scopes...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if !hasChanged {
|
2021-01-07 15:06:45 +00:00
|
|
|
return nil, caos_errs.ThrowPreconditionFailed(nil, "IAM-4M9vs", "Errors.IAM.LabelPolicy.NotChanged")
|
2021-01-04 13:52:13 +00:00
|
|
|
}
|
|
|
|
|
2021-02-24 10:17:39 +00:00
|
|
|
pushedEvents, err := c.eventstore.PushEvents(ctx, changedEvent)
|
2021-02-18 13:48:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = AppendAndReduce(existingConfig, pushedEvents...)
|
2021-01-04 13:52:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return writeModelToIDPOIDCConfig(&existingConfig.OIDCConfigWriteModel), nil
|
|
|
|
}
|