2021-01-04 14:52:13 +01:00
|
|
|
package idpconfig
|
|
|
|
|
|
|
|
import (
|
2022-04-27 01:01:45 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
2023-12-08 16:30:55 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/zerrors"
|
2021-01-04 14:52:13 +01:00
|
|
|
)
|
|
|
|
|
2021-01-21 10:49:38 +01:00
|
|
|
const (
|
2021-02-15 13:31:24 +01:00
|
|
|
UniqueIDPConfigNameType = "idp_config_names"
|
2021-01-21 10:49:38 +01:00
|
|
|
)
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func NewAddIDPConfigNameUniqueConstraint(idpConfigName, resourceOwner string) *eventstore.UniqueConstraint {
|
2021-01-21 10:49:38 +01:00
|
|
|
return eventstore.NewAddEventUniqueConstraint(
|
2021-02-15 13:31:24 +01:00
|
|
|
UniqueIDPConfigNameType,
|
2021-01-21 10:49:38 +01:00
|
|
|
idpConfigName+resourceOwner,
|
|
|
|
"Errors.IDPConfig.AlreadyExists")
|
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func NewRemoveIDPConfigNameUniqueConstraint(idpConfigName, resourceOwner string) *eventstore.UniqueConstraint {
|
|
|
|
return eventstore.NewRemoveUniqueConstraint(
|
2021-02-15 13:31:24 +01:00
|
|
|
UniqueIDPConfigNameType,
|
2021-01-21 10:49:38 +01:00
|
|
|
idpConfigName+resourceOwner)
|
|
|
|
}
|
|
|
|
|
2021-01-04 14:52:13 +01:00
|
|
|
type IDPConfigAddedEvent struct {
|
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
|
2021-09-10 09:49:49 +02:00
|
|
|
ConfigID string `json:"idpConfigId"`
|
|
|
|
Name string `json:"name,omitempty"`
|
|
|
|
Typ domain.IDPConfigType `json:"idpType,omitempty"`
|
|
|
|
StylingType domain.IDPConfigStylingType `json:"stylingType,omitempty"`
|
|
|
|
AutoRegister bool `json:"autoRegister,omitempty"`
|
2021-01-04 14:52:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewIDPConfigAddedEvent(
|
|
|
|
base *eventstore.BaseEvent,
|
2021-01-21 10:49:38 +01:00
|
|
|
configID,
|
2021-01-04 14:52:13 +01:00
|
|
|
name string,
|
|
|
|
configType domain.IDPConfigType,
|
|
|
|
stylingType domain.IDPConfigStylingType,
|
2021-09-10 09:49:49 +02:00
|
|
|
autoRegister bool,
|
2021-01-04 14:52:13 +01:00
|
|
|
) *IDPConfigAddedEvent {
|
|
|
|
return &IDPConfigAddedEvent{
|
2021-09-10 09:49:49 +02:00
|
|
|
BaseEvent: *base,
|
|
|
|
ConfigID: configID,
|
|
|
|
Name: name,
|
|
|
|
StylingType: stylingType,
|
|
|
|
Typ: configType,
|
|
|
|
AutoRegister: autoRegister,
|
2021-01-04 14:52:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func (e *IDPConfigAddedEvent) Payload() interface{} {
|
2021-01-04 14:52:13 +01:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func (e *IDPConfigAddedEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
|
|
|
|
return []*eventstore.UniqueConstraint{NewAddIDPConfigNameUniqueConstraint(e.Name, e.Aggregate().ResourceOwner)}
|
2021-01-21 10:49:38 +01:00
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func IDPConfigAddedEventMapper(event eventstore.Event) (eventstore.Event, error) {
|
2021-01-04 14:52:13 +01:00
|
|
|
e := &IDPConfigAddedEvent{
|
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
err := event.Unmarshal(e)
|
2021-01-04 14:52:13 +01:00
|
|
|
if err != nil {
|
2023-12-08 16:30:55 +02:00
|
|
|
return nil, zerrors.ThrowInternal(err, "OIDC-plaBZ", "unable to unmarshal event")
|
2021-01-04 14:52:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return e, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type IDPConfigChangedEvent struct {
|
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
|
2021-09-10 09:49:49 +02:00
|
|
|
ConfigID string `json:"idpConfigId"`
|
|
|
|
Name *string `json:"name,omitempty"`
|
|
|
|
StylingType *domain.IDPConfigStylingType `json:"stylingType,omitempty"`
|
|
|
|
AutoRegister *bool `json:"autoRegister,omitempty"`
|
|
|
|
oldName string `json:"-"`
|
2021-01-04 14:52:13 +01:00
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func (e *IDPConfigChangedEvent) Payload() interface{} {
|
2021-01-04 14:52:13 +01:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func (e *IDPConfigChangedEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
|
2021-02-15 13:31:24 +01:00
|
|
|
if e.oldName == "" {
|
|
|
|
return nil
|
|
|
|
}
|
2023-10-19 12:19:10 +02:00
|
|
|
return []*eventstore.UniqueConstraint{
|
2021-02-18 14:48:27 +01:00
|
|
|
NewRemoveIDPConfigNameUniqueConstraint(e.oldName, e.Aggregate().ResourceOwner),
|
|
|
|
NewAddIDPConfigNameUniqueConstraint(*e.Name, e.Aggregate().ResourceOwner),
|
2021-02-15 13:31:24 +01:00
|
|
|
}
|
2021-01-21 10:49:38 +01:00
|
|
|
}
|
|
|
|
|
2021-01-04 14:52:13 +01:00
|
|
|
func NewIDPConfigChangedEvent(
|
|
|
|
base *eventstore.BaseEvent,
|
2021-02-15 13:31:24 +01:00
|
|
|
configID,
|
|
|
|
oldName string,
|
2021-01-20 11:06:52 +01:00
|
|
|
changes []IDPConfigChanges,
|
|
|
|
) (*IDPConfigChangedEvent, error) {
|
|
|
|
if len(changes) == 0 {
|
2023-12-08 16:30:55 +02:00
|
|
|
return nil, zerrors.ThrowPreconditionFailed(nil, "IDPCONFIG-Dsg21", "Errors.NoChangesFound")
|
2021-01-20 11:06:52 +01:00
|
|
|
}
|
|
|
|
changeEvent := &IDPConfigChangedEvent{
|
2021-01-04 14:52:13 +01:00
|
|
|
BaseEvent: *base,
|
2021-01-20 11:06:52 +01:00
|
|
|
ConfigID: configID,
|
2021-02-15 13:31:24 +01:00
|
|
|
oldName: oldName,
|
2021-01-20 11:06:52 +01:00
|
|
|
}
|
|
|
|
for _, change := range changes {
|
|
|
|
change(changeEvent)
|
|
|
|
}
|
|
|
|
return changeEvent, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type IDPConfigChanges func(*IDPConfigChangedEvent)
|
|
|
|
|
|
|
|
func ChangeName(name string) func(*IDPConfigChangedEvent) {
|
|
|
|
return func(e *IDPConfigChangedEvent) {
|
|
|
|
e.Name = &name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ChangeStyleType(styleType domain.IDPConfigStylingType) func(*IDPConfigChangedEvent) {
|
|
|
|
return func(e *IDPConfigChangedEvent) {
|
|
|
|
e.StylingType = &styleType
|
2021-01-04 14:52:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-10 09:49:49 +02:00
|
|
|
func ChangeAutoRegister(autoRegister bool) func(*IDPConfigChangedEvent) {
|
|
|
|
return func(e *IDPConfigChangedEvent) {
|
|
|
|
e.AutoRegister = &autoRegister
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func IDPConfigChangedEventMapper(event eventstore.Event) (eventstore.Event, error) {
|
2021-01-04 14:52:13 +01:00
|
|
|
e := &IDPConfigChangedEvent{
|
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
err := event.Unmarshal(e)
|
2021-01-04 14:52:13 +01:00
|
|
|
if err != nil {
|
2023-12-08 16:30:55 +02:00
|
|
|
return nil, zerrors.ThrowInternal(err, "OIDC-plaBZ", "unable to unmarshal event")
|
2021-01-04 14:52:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return e, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type IDPConfigDeactivatedEvent struct {
|
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
|
2021-01-20 11:06:52 +01:00
|
|
|
ConfigID string `json:"idpConfigId"`
|
2021-01-04 14:52:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewIDPConfigDeactivatedEvent(
|
|
|
|
base *eventstore.BaseEvent,
|
|
|
|
configID string,
|
|
|
|
) *IDPConfigDeactivatedEvent {
|
|
|
|
|
|
|
|
return &IDPConfigDeactivatedEvent{
|
|
|
|
BaseEvent: *base,
|
|
|
|
ConfigID: configID,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func (e *IDPConfigDeactivatedEvent) Payload() interface{} {
|
2021-01-04 14:52:13 +01:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func (e *IDPConfigDeactivatedEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
|
2021-01-21 10:49:38 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func IDPConfigDeactivatedEventMapper(event eventstore.Event) (eventstore.Event, error) {
|
2021-01-04 14:52:13 +01:00
|
|
|
e := &IDPConfigDeactivatedEvent{
|
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
err := event.Unmarshal(e)
|
2021-01-04 14:52:13 +01:00
|
|
|
if err != nil {
|
2023-12-08 16:30:55 +02:00
|
|
|
return nil, zerrors.ThrowInternal(err, "OIDC-plaBZ", "unable to unmarshal event")
|
2021-01-04 14:52:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return e, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type IDPConfigReactivatedEvent struct {
|
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
|
2021-01-20 11:06:52 +01:00
|
|
|
ConfigID string `json:"idpConfigId"`
|
2021-01-04 14:52:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewIDPConfigReactivatedEvent(
|
|
|
|
base *eventstore.BaseEvent,
|
|
|
|
configID string,
|
|
|
|
) *IDPConfigReactivatedEvent {
|
|
|
|
|
|
|
|
return &IDPConfigReactivatedEvent{
|
|
|
|
BaseEvent: *base,
|
|
|
|
ConfigID: configID,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func (e *IDPConfigReactivatedEvent) Payload() interface{} {
|
2021-01-04 14:52:13 +01:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func (e *IDPConfigReactivatedEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
|
2021-01-21 10:49:38 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func IDPConfigReactivatedEventMapper(event eventstore.Event) (eventstore.Event, error) {
|
2021-01-04 14:52:13 +01:00
|
|
|
e := &IDPConfigReactivatedEvent{
|
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
err := event.Unmarshal(e)
|
2021-01-04 14:52:13 +01:00
|
|
|
if err != nil {
|
2023-12-08 16:30:55 +02:00
|
|
|
return nil, zerrors.ThrowInternal(err, "OIDC-plaBZ", "unable to unmarshal event")
|
2021-01-04 14:52:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return e, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type IDPConfigRemovedEvent struct {
|
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
|
2021-01-20 11:06:52 +01:00
|
|
|
ConfigID string `json:"idpConfigId"`
|
2021-10-06 10:16:00 +02:00
|
|
|
name string
|
2021-01-04 14:52:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewIDPConfigRemovedEvent(
|
|
|
|
base *eventstore.BaseEvent,
|
|
|
|
configID string,
|
2021-01-21 10:49:38 +01:00
|
|
|
name string,
|
2021-01-04 14:52:13 +01:00
|
|
|
) *IDPConfigRemovedEvent {
|
|
|
|
|
|
|
|
return &IDPConfigRemovedEvent{
|
|
|
|
BaseEvent: *base,
|
|
|
|
ConfigID: configID,
|
2021-10-06 10:16:00 +02:00
|
|
|
name: name,
|
2021-01-04 14:52:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func (e *IDPConfigRemovedEvent) Payload() interface{} {
|
2021-01-04 14:52:13 +01:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func (e *IDPConfigRemovedEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
|
|
|
|
return []*eventstore.UniqueConstraint{NewRemoveIDPConfigNameUniqueConstraint(e.name, e.Aggregate().ResourceOwner)}
|
2021-01-21 10:49:38 +01:00
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func IDPConfigRemovedEventMapper(event eventstore.Event) (eventstore.Event, error) {
|
2021-01-04 14:52:13 +01:00
|
|
|
e := &IDPConfigRemovedEvent{
|
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
err := event.Unmarshal(e)
|
2021-01-04 14:52:13 +01:00
|
|
|
if err != nil {
|
2023-12-08 16:30:55 +02:00
|
|
|
return nil, zerrors.ThrowInternal(err, "OIDC-plaBZ", "unable to unmarshal event")
|
2021-01-04 14:52:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return e, nil
|
|
|
|
}
|