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/api/authz"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/idpconfig"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/instance"
|
2021-09-14 13:15:01 +00:00
|
|
|
)
|
|
|
|
|
2022-03-24 16:21:34 +00:00
|
|
|
type InstanceIDPJWTConfigWriteModel struct {
|
2021-09-14 13:15:01 +00:00
|
|
|
JWTConfigWriteModel
|
|
|
|
}
|
|
|
|
|
2022-04-05 05:58:09 +00:00
|
|
|
func NewInstanceIDPJWTConfigWriteModel(ctx context.Context, idpConfigID string) *InstanceIDPJWTConfigWriteModel {
|
2022-03-24 16:21:34 +00:00
|
|
|
return &InstanceIDPJWTConfigWriteModel{
|
2021-09-14 13:15:01 +00:00
|
|
|
JWTConfigWriteModel{
|
|
|
|
WriteModel: eventstore.WriteModel{
|
2022-04-05 05:58:09 +00:00
|
|
|
AggregateID: authz.GetInstance(ctx).InstanceID(),
|
|
|
|
ResourceOwner: authz.GetInstance(ctx).InstanceID(),
|
2021-09-14 13:15:01 +00:00
|
|
|
},
|
|
|
|
IDPConfigID: idpConfigID,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-24 16:21:34 +00:00
|
|
|
func (wm *InstanceIDPJWTConfigWriteModel) AppendEvents(events ...eventstore.Event) {
|
2021-09-14 13:15:01 +00:00
|
|
|
for _, event := range events {
|
|
|
|
switch e := event.(type) {
|
2022-03-24 16:21:34 +00:00
|
|
|
case *instance.IDPJWTConfigAddedEvent:
|
2021-09-14 13:15:01 +00:00
|
|
|
if wm.IDPConfigID != e.IDPConfigID {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
wm.JWTConfigWriteModel.AppendEvents(&e.JWTConfigAddedEvent)
|
2022-03-24 16:21:34 +00:00
|
|
|
case *instance.IDPJWTConfigChangedEvent:
|
2021-09-14 13:15:01 +00:00
|
|
|
if wm.IDPConfigID != e.IDPConfigID {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
wm.JWTConfigWriteModel.AppendEvents(&e.JWTConfigChangedEvent)
|
2022-03-24 16:21:34 +00:00
|
|
|
case *instance.IDPConfigReactivatedEvent:
|
2021-09-14 13:15:01 +00:00
|
|
|
if wm.IDPConfigID != e.ConfigID {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
wm.JWTConfigWriteModel.AppendEvents(&e.IDPConfigReactivatedEvent)
|
2022-03-24 16:21:34 +00:00
|
|
|
case *instance.IDPConfigDeactivatedEvent:
|
2021-09-14 13:15:01 +00:00
|
|
|
if wm.IDPConfigID != e.ConfigID {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
wm.JWTConfigWriteModel.AppendEvents(&e.IDPConfigDeactivatedEvent)
|
2022-03-24 16:21:34 +00:00
|
|
|
case *instance.IDPConfigRemovedEvent:
|
2021-09-14 13:15:01 +00:00
|
|
|
if wm.IDPConfigID != e.ConfigID {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
wm.JWTConfigWriteModel.AppendEvents(&e.IDPConfigRemovedEvent)
|
|
|
|
default:
|
|
|
|
wm.JWTConfigWriteModel.AppendEvents(e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-24 16:21:34 +00:00
|
|
|
func (wm *InstanceIDPJWTConfigWriteModel) Reduce() error {
|
2021-09-14 13:15:01 +00:00
|
|
|
if err := wm.JWTConfigWriteModel.Reduce(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return wm.WriteModel.Reduce()
|
|
|
|
}
|
|
|
|
|
2022-03-24 16:21:34 +00:00
|
|
|
func (wm *InstanceIDPJWTConfigWriteModel) Query() *eventstore.SearchQueryBuilder {
|
2021-09-14 13:15:01 +00:00
|
|
|
return eventstore.NewSearchQueryBuilder(eventstore.ColumnsEvent).
|
|
|
|
ResourceOwner(wm.ResourceOwner).
|
|
|
|
AddQuery().
|
2022-03-24 16:21:34 +00:00
|
|
|
AggregateTypes(instance.AggregateType).
|
2021-09-14 13:15:01 +00:00
|
|
|
AggregateIDs(wm.AggregateID).
|
|
|
|
EventTypes(
|
2022-03-24 16:21:34 +00:00
|
|
|
instance.IDPJWTConfigAddedEventType,
|
|
|
|
instance.IDPJWTConfigChangedEventType,
|
|
|
|
instance.IDPConfigReactivatedEventType,
|
|
|
|
instance.IDPConfigDeactivatedEventType,
|
|
|
|
instance.IDPConfigRemovedEventType).
|
2021-09-14 13:15:01 +00:00
|
|
|
Builder()
|
|
|
|
}
|
|
|
|
|
2022-03-24 16:21:34 +00:00
|
|
|
func (wm *InstanceIDPJWTConfigWriteModel) NewChangedEvent(
|
2021-09-14 13:15:01 +00:00
|
|
|
ctx context.Context,
|
|
|
|
aggregate *eventstore.Aggregate,
|
|
|
|
idpConfigID,
|
|
|
|
jwtEndpoint,
|
|
|
|
issuer,
|
|
|
|
keysEndpoint,
|
|
|
|
headerName string,
|
2022-03-24 16:21:34 +00:00
|
|
|
) (*instance.IDPJWTConfigChangedEvent, bool, error) {
|
2021-09-14 13:15:01 +00:00
|
|
|
|
|
|
|
changes := make([]idpconfig.JWTConfigChanges, 0)
|
|
|
|
if wm.JWTEndpoint != jwtEndpoint {
|
|
|
|
changes = append(changes, idpconfig.ChangeJWTEndpoint(jwtEndpoint))
|
|
|
|
}
|
|
|
|
if wm.Issuer != issuer {
|
|
|
|
changes = append(changes, idpconfig.ChangeJWTIssuer(issuer))
|
|
|
|
}
|
|
|
|
if wm.KeysEndpoint != keysEndpoint {
|
|
|
|
changes = append(changes, idpconfig.ChangeKeysEndpoint(keysEndpoint))
|
|
|
|
}
|
|
|
|
if wm.HeaderName != headerName {
|
|
|
|
changes = append(changes, idpconfig.ChangeHeaderName(headerName))
|
|
|
|
}
|
|
|
|
if len(changes) == 0 {
|
|
|
|
return nil, false, nil
|
|
|
|
}
|
2022-03-24 16:21:34 +00:00
|
|
|
changeEvent, err := instance.NewIDPJWTConfigChangedEvent(ctx, aggregate, idpConfigID, changes)
|
2021-09-14 13:15:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, false, err
|
|
|
|
}
|
|
|
|
return changeEvent, true, nil
|
|
|
|
}
|