2021-09-14 13:15:01 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/caos/zitadel/internal/domain"
|
|
|
|
caos_errs "github.com/caos/zitadel/internal/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (c *Commands) ChangeDefaultIDPJWTConfig(ctx context.Context, config *domain.JWTIDPConfig) (*domain.JWTIDPConfig, error) {
|
|
|
|
if config.IDPConfigID == "" {
|
|
|
|
return nil, caos_errs.ThrowInvalidArgument(nil, "IAM-m9322", "Errors.IDMissing")
|
|
|
|
}
|
|
|
|
existingConfig := NewIAMIDPJWTConfigWriteModel(config.IDPConfigID)
|
|
|
|
err := c.eventstore.FilterToQueryReducer(ctx, existingConfig)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if existingConfig.State == domain.IDPConfigStateRemoved || existingConfig.State == domain.IDPConfigStateUnspecified {
|
|
|
|
return nil, caos_errs.ThrowNotFound(nil, "IAM-2m00d", "Errors.IAM.IDPConfig.AlreadyExists")
|
|
|
|
}
|
|
|
|
|
|
|
|
iamAgg := IAMAggregateFromWriteModel(&existingConfig.WriteModel)
|
|
|
|
changedEvent, hasChanged, err := existingConfig.NewChangedEvent(
|
|
|
|
ctx,
|
|
|
|
iamAgg,
|
|
|
|
config.IDPConfigID,
|
|
|
|
config.JWTEndpoint,
|
|
|
|
config.Issuer,
|
|
|
|
config.KeysEndpoint,
|
|
|
|
config.HeaderName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if !hasChanged {
|
|
|
|
return nil, caos_errs.ThrowPreconditionFailed(nil, "IAM-3n9gg", "Errors.IAM.IDPConfig.NotChanged")
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|