fix: move v2 pkgs (#1331)

* fix: move eventstore pkgs

* fix: move eventstore pkgs

* fix: remove v2 view

* fix: remove v2 view
This commit is contained in:
Fabi
2021-02-23 15:13:04 +01:00
committed by GitHub
parent 57b277bc7c
commit d8e42744b4
797 changed files with 2116 additions and 2224 deletions

View File

@@ -0,0 +1,260 @@
package idpconfig
import (
"encoding/json"
"github.com/caos/zitadel/internal/eventstore"
"github.com/caos/zitadel/internal/domain"
"github.com/caos/zitadel/internal/errors"
"github.com/caos/zitadel/internal/eventstore/repository"
)
const (
UniqueIDPConfigNameType = "idp_config_names"
)
func NewAddIDPConfigNameUniqueConstraint(idpConfigName, resourceOwner string) *eventstore.EventUniqueConstraint {
return eventstore.NewAddEventUniqueConstraint(
UniqueIDPConfigNameType,
idpConfigName+resourceOwner,
"Errors.IDPConfig.AlreadyExists")
}
func NewRemoveIDPConfigNameUniqueConstraint(idpConfigName, resourceOwner string) *eventstore.EventUniqueConstraint {
return eventstore.NewRemoveEventUniqueConstraint(
UniqueIDPConfigNameType,
idpConfigName+resourceOwner)
}
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,
configID,
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
}
func (e *IDPConfigAddedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
return []*eventstore.EventUniqueConstraint{NewAddIDPConfigNameUniqueConstraint(e.Name, e.Aggregate().ResourceOwner)}
}
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:"-"`
ConfigID string `json:"idpConfigId"`
Name *string `json:"name,omitempty"`
StylingType *domain.IDPConfigStylingType `json:"stylingType,omitempty"`
oldName string `json:"-"`
}
func (e *IDPConfigChangedEvent) Data() interface{} {
return e
}
func (e *IDPConfigChangedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
if e.oldName == "" {
return nil
}
return []*eventstore.EventUniqueConstraint{
NewRemoveIDPConfigNameUniqueConstraint(e.oldName, e.Aggregate().ResourceOwner),
NewAddIDPConfigNameUniqueConstraint(*e.Name, e.Aggregate().ResourceOwner),
}
}
func NewIDPConfigChangedEvent(
base *eventstore.BaseEvent,
configID,
oldName string,
changes []IDPConfigChanges,
) (*IDPConfigChangedEvent, error) {
if len(changes) == 0 {
return nil, errors.ThrowPreconditionFailed(nil, "IDPCONFIG-Dsg21", "Errors.NoChangesFound")
}
changeEvent := &IDPConfigChangedEvent{
BaseEvent: *base,
ConfigID: configID,
oldName: oldName,
}
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
}
}
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:"-"`
ConfigID string `json:"idpConfigId"`
}
func NewIDPConfigDeactivatedEvent(
base *eventstore.BaseEvent,
configID string,
) *IDPConfigDeactivatedEvent {
return &IDPConfigDeactivatedEvent{
BaseEvent: *base,
ConfigID: configID,
}
}
func (e *IDPConfigDeactivatedEvent) Data() interface{} {
return e
}
func (e *IDPConfigDeactivatedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
return nil
}
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:"-"`
ConfigID string `json:"idpConfigId"`
}
func NewIDPConfigReactivatedEvent(
base *eventstore.BaseEvent,
configID string,
) *IDPConfigReactivatedEvent {
return &IDPConfigReactivatedEvent{
BaseEvent: *base,
ConfigID: configID,
}
}
func (e *IDPConfigReactivatedEvent) Data() interface{} {
return e
}
func (e *IDPConfigReactivatedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
return nil
}
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:"-"`
ConfigID string `json:"idpConfigId"`
Name string
}
func NewIDPConfigRemovedEvent(
base *eventstore.BaseEvent,
configID string,
name string,
) *IDPConfigRemovedEvent {
return &IDPConfigRemovedEvent{
BaseEvent: *base,
ConfigID: configID,
Name: name,
}
}
func (e *IDPConfigRemovedEvent) Data() interface{} {
return e
}
func (e *IDPConfigRemovedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
return []*eventstore.EventUniqueConstraint{NewRemoveIDPConfigNameUniqueConstraint(e.Name, e.Aggregate().ResourceOwner)}
}
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
}

View File

@@ -0,0 +1,164 @@
package idpconfig
import (
"encoding/json"
"github.com/caos/zitadel/internal/eventstore"
"github.com/caos/zitadel/internal/crypto"
"github.com/caos/zitadel/internal/domain"
"github.com/caos/zitadel/internal/errors"
"github.com/caos/zitadel/internal/eventstore/repository"
)
const (
OIDCConfigAddedEventType eventstore.EventType = "oidc.config.added"
ConfigChangedEventType eventstore.EventType = "oidc.config.changed"
)
type OIDCConfigAddedEvent struct {
eventstore.BaseEvent `json:"-"`
IDPConfigID string `json:"idpConfigId"`
ClientID string `json:"clientId,omitempty"`
ClientSecret *crypto.CryptoValue `json:"clientSecret,omitempty"`
Issuer string `json:"issuer,omitempty"`
Scopes []string `json:"scopes,omitempty"`
IDPDisplayNameMapping domain.OIDCMappingField `json:"idpDisplayNameMapping,omitempty"`
UserNameMapping domain.OIDCMappingField `json:"usernameMapping,omitempty"`
}
func (e *OIDCConfigAddedEvent) Data() interface{} {
return e
}
func (e *OIDCConfigAddedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
return nil
}
func NewOIDCConfigAddedEvent(
base *eventstore.BaseEvent,
clientID,
idpConfigID,
issuer string,
clientSecret *crypto.CryptoValue,
idpDisplayNameMapping,
userNameMapping domain.OIDCMappingField,
scopes ...string,
) *OIDCConfigAddedEvent {
return &OIDCConfigAddedEvent{
BaseEvent: *base,
IDPConfigID: idpConfigID,
ClientID: clientID,
ClientSecret: clientSecret,
Issuer: issuer,
Scopes: scopes,
IDPDisplayNameMapping: idpDisplayNameMapping,
UserNameMapping: userNameMapping,
}
}
func OIDCConfigAddedEventMapper(event *repository.Event) (eventstore.EventReader, error) {
e := &OIDCConfigAddedEvent{
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 OIDCConfigChangedEvent struct {
eventstore.BaseEvent `json:"-"`
IDPConfigID string `json:"idpConfigId"`
ClientID *string `json:"clientId,omitempty"`
ClientSecret *crypto.CryptoValue `json:"clientSecret,omitempty"`
Issuer *string `json:"issuer,omitempty"`
Scopes []string `json:"scopes,omitempty"`
IDPDisplayNameMapping *domain.OIDCMappingField `json:"idpDisplayNameMapping,omitempty"`
UserNameMapping *domain.OIDCMappingField `json:"usernameMapping,omitempty"`
}
func (e *OIDCConfigChangedEvent) Data() interface{} {
return e
}
func (e *OIDCConfigChangedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
return nil
}
func NewOIDCConfigChangedEvent(
base *eventstore.BaseEvent,
idpConfigID string,
changes []OIDCConfigChanges,
) (*OIDCConfigChangedEvent, error) {
if len(changes) == 0 {
return nil, errors.ThrowPreconditionFailed(nil, "IDPCONFIG-ADzr5", "Errors.NoChangesFound")
}
changeEvent := &OIDCConfigChangedEvent{
BaseEvent: *base,
IDPConfigID: idpConfigID,
}
for _, change := range changes {
change(changeEvent)
}
return changeEvent, nil
}
type OIDCConfigChanges func(*OIDCConfigChangedEvent)
func ChangeClientID(clientID string) func(*OIDCConfigChangedEvent) {
return func(e *OIDCConfigChangedEvent) {
e.ClientID = &clientID
}
}
func ChangeClientSecret(secret *crypto.CryptoValue) func(*OIDCConfigChangedEvent) {
return func(e *OIDCConfigChangedEvent) {
e.ClientSecret = secret
}
}
func ChangeIssuer(issuer string) func(*OIDCConfigChangedEvent) {
return func(e *OIDCConfigChangedEvent) {
e.Issuer = &issuer
}
}
func ChangeIDPDisplayNameMapping(idpDisplayNameMapping domain.OIDCMappingField) func(*OIDCConfigChangedEvent) {
return func(e *OIDCConfigChangedEvent) {
e.IDPDisplayNameMapping = &idpDisplayNameMapping
}
}
func ChangeUserNameMapping(userNameMapping domain.OIDCMappingField) func(*OIDCConfigChangedEvent) {
return func(e *OIDCConfigChangedEvent) {
e.UserNameMapping = &userNameMapping
}
}
func ChangeScopes(scopes []string) func(*OIDCConfigChangedEvent) {
return func(e *OIDCConfigChangedEvent) {
e.Scopes = scopes
}
}
func OIDCConfigChangedEventMapper(event *repository.Event) (eventstore.EventReader, error) {
e := &OIDCConfigChangedEvent{
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
}