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"
|
2023-12-08 14:30:55 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/zerrors"
|
2021-09-14 13:15:01 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func (c *Commands) ChangeDefaultIDPJWTConfig(ctx context.Context, config *domain.JWTIDPConfig) (*domain.JWTIDPConfig, error) {
|
|
|
|
if config.IDPConfigID == "" {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.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 {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.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 {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.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
|
|
|
|
}
|