2021-01-04 14:52:13 +01:00
|
|
|
package idpconfig
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2021-02-23 15:13:04 +01:00
|
|
|
"github.com/caos/zitadel/internal/eventstore"
|
2021-01-20 11:06:52 +01:00
|
|
|
|
2021-02-23 15:13:04 +01:00
|
|
|
"github.com/caos/zitadel/internal/domain"
|
2021-01-04 14:52:13 +01:00
|
|
|
"github.com/caos/zitadel/internal/errors"
|
2021-02-23 15:13:04 +01:00
|
|
|
"github.com/caos/zitadel/internal/eventstore/repository"
|
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
|
|
|
)
|
|
|
|
|
|
|
|
func NewAddIDPConfigNameUniqueConstraint(idpConfigName, resourceOwner string) *eventstore.EventUniqueConstraint {
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewRemoveIDPConfigNameUniqueConstraint(idpConfigName, resourceOwner string) *eventstore.EventUniqueConstraint {
|
|
|
|
return eventstore.NewRemoveEventUniqueConstraint(
|
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:"-"`
|
|
|
|
|
|
|
|
ConfigID string `json:"idpConfigId"`
|
|
|
|
Name string `json:"name,omitempty"`
|
|
|
|
Typ domain.IDPConfigType `json:"idpType,omitempty"`
|
|
|
|
StylingType domain.IDPConfigStylingType `json:"stylingType,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
|
|
|
) *IDPConfigAddedEvent {
|
|
|
|
return &IDPConfigAddedEvent{
|
|
|
|
BaseEvent: *base,
|
|
|
|
ConfigID: configID,
|
|
|
|
Name: name,
|
|
|
|
StylingType: stylingType,
|
|
|
|
Typ: configType,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *IDPConfigAddedEvent) Data() interface{} {
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2021-01-21 10:49:38 +01:00
|
|
|
func (e *IDPConfigAddedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
|
2021-02-18 14:48:27 +01:00
|
|
|
return []*eventstore.EventUniqueConstraint{NewAddIDPConfigNameUniqueConstraint(e.Name, e.Aggregate().ResourceOwner)}
|
2021-01-21 10:49:38 +01:00
|
|
|
}
|
|
|
|
|
2021-01-04 14:52:13 +01:00
|
|
|
func IDPConfigAddedEventMapper(event *repository.Event) (eventstore.EventReader, error) {
|
|
|
|
e := &IDPConfigAddedEvent{
|
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}
|
|
|
|
|
|
|
|
err := json.Unmarshal(event.Data, e)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.ThrowInternal(err, "OIDC-plaBZ", "unable to unmarshal event")
|
|
|
|
}
|
|
|
|
|
|
|
|
return e, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type IDPConfigChangedEvent struct {
|
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
|
2021-01-20 11:06:52 +01:00
|
|
|
ConfigID string `json:"idpConfigId"`
|
|
|
|
Name *string `json:"name,omitempty"`
|
|
|
|
StylingType *domain.IDPConfigStylingType `json:"stylingType,omitempty"`
|
2021-02-15 13:31:24 +01:00
|
|
|
oldName string `json:"-"`
|
2021-01-04 14:52:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e *IDPConfigChangedEvent) Data() interface{} {
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2021-01-21 10:49:38 +01:00
|
|
|
func (e *IDPConfigChangedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
|
2021-02-15 13:31:24 +01:00
|
|
|
if e.oldName == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return []*eventstore.EventUniqueConstraint{
|
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 {
|
|
|
|
return nil, errors.ThrowPreconditionFailed(nil, "IDPCONFIG-Dsg21", "Errors.NoChangesFound")
|
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func IDPConfigChangedEventMapper(event *repository.Event) (eventstore.EventReader, error) {
|
|
|
|
e := &IDPConfigChangedEvent{
|
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}
|
|
|
|
|
|
|
|
err := json.Unmarshal(event.Data, e)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.ThrowInternal(err, "OIDC-plaBZ", "unable to unmarshal event")
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *IDPConfigDeactivatedEvent) Data() interface{} {
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2021-01-21 10:49:38 +01:00
|
|
|
func (e *IDPConfigDeactivatedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-04 14:52:13 +01:00
|
|
|
func IDPConfigDeactivatedEventMapper(event *repository.Event) (eventstore.EventReader, error) {
|
|
|
|
e := &IDPConfigDeactivatedEvent{
|
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}
|
|
|
|
|
|
|
|
err := json.Unmarshal(event.Data, e)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.ThrowInternal(err, "OIDC-plaBZ", "unable to unmarshal event")
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *IDPConfigReactivatedEvent) Data() interface{} {
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2021-01-21 10:49:38 +01:00
|
|
|
func (e *IDPConfigReactivatedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-04 14:52:13 +01:00
|
|
|
func IDPConfigReactivatedEventMapper(event *repository.Event) (eventstore.EventReader, error) {
|
|
|
|
e := &IDPConfigReactivatedEvent{
|
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}
|
|
|
|
|
|
|
|
err := json.Unmarshal(event.Data, e)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.ThrowInternal(err, "OIDC-plaBZ", "unable to unmarshal event")
|
|
|
|
}
|
|
|
|
|
|
|
|
return e, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type IDPConfigRemovedEvent struct {
|
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
|
2021-01-20 11:06:52 +01:00
|
|
|
ConfigID string `json:"idpConfigId"`
|
2021-01-21 10:49:38 +01: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-01-21 10:49:38 +01:00
|
|
|
Name: name,
|
2021-01-04 14:52:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *IDPConfigRemovedEvent) Data() interface{} {
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2021-01-21 10:49:38 +01:00
|
|
|
func (e *IDPConfigRemovedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
|
2021-02-18 14:48:27 +01:00
|
|
|
return []*eventstore.EventUniqueConstraint{NewRemoveIDPConfigNameUniqueConstraint(e.Name, e.Aggregate().ResourceOwner)}
|
2021-01-21 10:49:38 +01:00
|
|
|
}
|
|
|
|
|
2021-01-04 14:52:13 +01:00
|
|
|
func IDPConfigRemovedEventMapper(event *repository.Event) (eventstore.EventReader, error) {
|
|
|
|
e := &IDPConfigRemovedEvent{
|
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}
|
|
|
|
|
|
|
|
err := json.Unmarshal(event.Data, e)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.ThrowInternal(err, "OIDC-plaBZ", "unable to unmarshal event")
|
|
|
|
}
|
|
|
|
|
|
|
|
return e, nil
|
|
|
|
}
|