2021-01-04 13:52:13 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-01-03 08:19:07 +00:00
|
|
|
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
|
|
|
caos_errs "github.com/zitadel/zitadel/internal/errors"
|
2021-01-04 13:52:13 +00:00
|
|
|
)
|
|
|
|
|
2021-02-24 10:17:39 +00:00
|
|
|
func (c *Commands) ChangeDefaultIDPOIDCConfig(ctx context.Context, config *domain.OIDCIDPConfig) (*domain.OIDCIDPConfig, error) {
|
2021-03-19 10:12:56 +00:00
|
|
|
if config.IDPConfigID == "" {
|
2022-03-24 16:21:34 +00:00
|
|
|
return nil, caos_errs.ThrowInvalidArgument(nil, "INSTANCE-9djf8", "Errors.IDMissing")
|
2021-03-19 10:12:56 +00:00
|
|
|
}
|
2022-04-05 05:58:09 +00:00
|
|
|
existingConfig := NewInstanceIDPOIDCConfigWriteModel(ctx, 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 {
|
2022-03-24 16:21:34 +00:00
|
|
|
return nil, caos_errs.ThrowNotFound(nil, "INSTANCE-67J9d", "Errors.IAM.IDPConfig.AlreadyExists")
|
2021-01-04 13:52:13 +00:00
|
|
|
}
|
|
|
|
|
2022-03-24 16:21:34 +00:00
|
|
|
instanceAgg := InstanceAggregateFromWriteModel(&existingConfig.WriteModel)
|
2021-01-04 13:52:13 +00:00
|
|
|
changedEvent, hasChanged, err := existingConfig.NewChangedEvent(
|
|
|
|
ctx,
|
2022-03-24 16:21:34 +00:00
|
|
|
instanceAgg,
|
2021-01-20 10:06:52 +00:00
|
|
|
config.IDPConfigID,
|
2021-01-04 13:52:13 +00:00
|
|
|
config.ClientID,
|
|
|
|
config.Issuer,
|
2021-07-06 14:39:48 +00:00
|
|
|
config.AuthorizationEndpoint,
|
|
|
|
config.TokenEndpoint,
|
2021-01-04 13:52:13 +00:00
|
|
|
config.ClientSecretString,
|
2022-04-20 14:59:37 +00:00
|
|
|
c.idpConfigEncryption,
|
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 {
|
2022-05-16 12:00:33 +00:00
|
|
|
return nil, caos_errs.ThrowPreconditionFailed(nil, "INSTANCE-d8kwF", "Errors.IAM.IDPConfig.NotChanged")
|
2021-01-04 13:52:13 +00:00
|
|
|
}
|
|
|
|
|
2022-01-03 08:19:07 +00:00
|
|
|
pushedEvents, err := c.eventstore.Push(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
|
|
|
|
}
|