2022-02-25 15:05:06 +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"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/instance"
|
2022-02-25 15:05:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func (c *Commands) AddOIDCSettings(ctx context.Context, settings *domain.OIDCSettings) (*domain.ObjectDetails, error) {
|
|
|
|
oidcSettingWriteModel, err := c.getOIDCSettings(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if oidcSettingWriteModel.State.Exists() {
|
|
|
|
return nil, caos_errs.ThrowAlreadyExists(nil, "COMMAND-d9nlw", "Errors.OIDCSettings.AlreadyExists")
|
|
|
|
}
|
2022-03-24 16:21:34 +00:00
|
|
|
instanceAgg := InstanceAggregateFromWriteModel(&oidcSettingWriteModel.WriteModel)
|
|
|
|
pushedEvents, err := c.eventstore.Push(ctx, instance.NewOIDCSettingsAddedEvent(
|
2022-02-25 15:05:06 +00:00
|
|
|
ctx,
|
2022-03-24 16:21:34 +00:00
|
|
|
instanceAgg,
|
2022-02-25 15:05:06 +00:00
|
|
|
settings.AccessTokenLifetime,
|
|
|
|
settings.IdTokenLifetime,
|
|
|
|
settings.RefreshTokenIdleExpiration,
|
|
|
|
settings.RefreshTokenExpiration))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = AppendAndReduce(oidcSettingWriteModel, pushedEvents...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return writeModelToObjectDetails(&oidcSettingWriteModel.WriteModel), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Commands) ChangeOIDCSettings(ctx context.Context, settings *domain.OIDCSettings) (*domain.ObjectDetails, error) {
|
|
|
|
oidcSettingWriteModel, err := c.getOIDCSettings(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if !oidcSettingWriteModel.State.Exists() {
|
|
|
|
return nil, caos_errs.ThrowNotFound(nil, "COMMAND-8snEr", "Errors.OIDCSettings.NotFound")
|
|
|
|
}
|
2022-03-24 16:21:34 +00:00
|
|
|
instanceAgg := InstanceAggregateFromWriteModel(&oidcSettingWriteModel.WriteModel)
|
2022-02-25 15:05:06 +00:00
|
|
|
|
|
|
|
changedEvent, hasChanged, err := oidcSettingWriteModel.NewChangedEvent(
|
|
|
|
ctx,
|
2022-03-24 16:21:34 +00:00
|
|
|
instanceAgg,
|
2022-02-25 15:05:06 +00:00
|
|
|
settings.AccessTokenLifetime,
|
|
|
|
settings.IdTokenLifetime,
|
|
|
|
settings.RefreshTokenIdleExpiration,
|
|
|
|
settings.RefreshTokenExpiration)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if !hasChanged {
|
|
|
|
return nil, caos_errs.ThrowPreconditionFailed(nil, "COMMAND-398uF", "Errors.NoChangesFound")
|
|
|
|
}
|
|
|
|
pushedEvents, err := c.eventstore.Push(ctx, changedEvent)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = AppendAndReduce(oidcSettingWriteModel, pushedEvents...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return writeModelToObjectDetails(&oidcSettingWriteModel.WriteModel), nil
|
|
|
|
}
|
|
|
|
|
2022-03-24 16:21:34 +00:00
|
|
|
func (c *Commands) getOIDCSettings(ctx context.Context) (_ *InstanceOIDCSettingsWriteModel, err error) {
|
2022-04-05 05:58:09 +00:00
|
|
|
writeModel := NewInstanceOIDCSettingsWriteModel(ctx)
|
2022-02-25 15:05:06 +00:00
|
|
|
err = c.eventstore.FilterToQueryReducer(ctx, writeModel)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return writeModel, nil
|
|
|
|
}
|