2021-09-14 13:15:01 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
|
|
|
caos_errs "github.com/zitadel/zitadel/internal/errors"
|
2021-09-14 13:15:01 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func (c *Commands) ChangeDefaultIDPJWTConfig(ctx context.Context, config *domain.JWTIDPConfig) (*domain.JWTIDPConfig, error) {
|
|
|
|
if config.IDPConfigID == "" {
|
2022-03-24 16:21:34 +00:00
|
|
|
return nil, caos_errs.ThrowInvalidArgument(nil, "INSTANCE-m9322", "Errors.IDMissing")
|
2021-09-14 13:15:01 +00:00
|
|
|
}
|
2022-04-05 05:58:09 +00:00
|
|
|
existingConfig := NewInstanceIDPJWTConfigWriteModel(ctx, config.IDPConfigID)
|
2021-09-14 13:15:01 +00:00
|
|
|
err := c.eventstore.FilterToQueryReducer(ctx, existingConfig)
|
|
|
|
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-2m00d", "Errors.IAM.IDPConfig.AlreadyExists")
|
2021-09-14 13:15:01 +00:00
|
|
|
}
|
|
|
|
|
2022-03-24 16:21:34 +00:00
|
|
|
instanceAgg := InstanceAggregateFromWriteModel(&existingConfig.WriteModel)
|
2021-09-14 13:15:01 +00:00
|
|
|
changedEvent, hasChanged, err := existingConfig.NewChangedEvent(
|
|
|
|
ctx,
|
2022-03-24 16:21:34 +00:00
|
|
|
instanceAgg,
|
2021-09-14 13:15:01 +00:00
|
|
|
config.IDPConfigID,
|
|
|
|
config.JWTEndpoint,
|
|
|
|
config.Issuer,
|
|
|
|
config.KeysEndpoint,
|
|
|
|
config.HeaderName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if !hasChanged {
|
2022-03-24 16:21:34 +00:00
|
|
|
return nil, caos_errs.ThrowPreconditionFailed(nil, "INSTANCE-3n9gg", "Errors.IAM.IDPConfig.NotChanged")
|
2021-09-14 13:15:01 +00:00
|
|
|
}
|
|
|
|
|
2022-01-03 08:19:07 +00:00
|
|
|
pushedEvents, err := c.eventstore.Push(ctx, changedEvent)
|
2021-09-14 13:15:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = AppendAndReduce(existingConfig, pushedEvents...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return writeModelToIDPJWTConfig(&existingConfig.JWTConfigWriteModel), nil
|
|
|
|
}
|