This commit is contained in:
adlerhurst
2020-11-27 11:30:56 +01:00
parent 3bd4d3a8e3
commit 9487e8bdeb
20 changed files with 424 additions and 183 deletions

View File

@@ -153,14 +153,13 @@ func (a *Aggregate) PushIDPOIDCConfigChanged(
ctx context.Context,
current *IDPOIDCConfigWriteModel,
clientID,
idpConfigID,
issuer string,
clientSecret *crypto.CryptoValue,
idpDisplayNameMapping,
userNameMapping oidc.MappingField,
scopes ...string,
) *Aggregate {
event, err := NewIDPOIDCConfigChangedEvent(ctx, current, clientID, idpConfigID, issuer, clientSecret, idpDisplayNameMapping, userNameMapping, scopes...)
event, err := NewIDPOIDCConfigChangedEvent(ctx, current, clientID, issuer, clientSecret, idpDisplayNameMapping, userNameMapping, scopes...)
if err != nil {
return a
}

View File

@@ -18,6 +18,16 @@ const (
type IDPConfigReadModel struct {
idp.ConfigReadModel
iamID string
configID string
}
func NewIDPConfigReadModel(iamID, configID string) *IDPConfigReadModel {
return &IDPConfigReadModel{
iamID: iamID,
configID: configID,
}
}
func (rm *IDPConfigReadModel) AppendEvents(events ...eventstore.EventReader) {
@@ -41,34 +51,89 @@ func (rm *IDPConfigReadModel) AppendEvents(events ...eventstore.EventReader) {
}
}
type IDPConfigWriteModel struct {
idp.ConfigWriteModel
func (rm *IDPConfigReadModel) Query() *eventstore.SearchQueryFactory {
return eventstore.NewSearchQueryFactory(eventstore.ColumnsEvent, AggregateType).
AggregateIDs(rm.iamID).
EventData(map[string]interface{}{
"idpConfigId": rm.configID,
})
}
func (rm *IDPConfigWriteModel) AppendEvents(events ...eventstore.EventReader) {
type IDPConfigWriteModel struct {
eventstore.WriteModel
idp.ConfigWriteModel
iamID string
configID string
}
func NewIDPConfigWriteModel(iamID, configID string) *IDPConfigWriteModel {
return &IDPConfigWriteModel{
iamID: iamID,
configID: configID,
}
}
func (wm *IDPConfigWriteModel) Query() *eventstore.SearchQueryFactory {
return eventstore.NewSearchQueryFactory(eventstore.ColumnsEvent, AggregateType).
AggregateIDs(wm.iamID)
}
func (wm *IDPConfigWriteModel) AppendEvents(events ...eventstore.EventReader) {
wm.WriteModel.AppendEvents(events...)
for _, event := range events {
switch e := event.(type) {
case *IDPConfigAddedEvent:
rm.ConfigWriteModel.AppendEvents(&e.ConfigAddedEvent)
if wm.configID != e.ConfigID {
continue
}
wm.ConfigWriteModel.AppendEvents(&e.ConfigAddedEvent)
case *IDPConfigChangedEvent:
rm.ConfigWriteModel.AppendEvents(&e.ConfigChangedEvent)
if wm.configID != e.ConfigID {
continue
}
wm.ConfigWriteModel.AppendEvents(&e.ConfigChangedEvent)
case *IDPConfigDeactivatedEvent:
rm.ConfigWriteModel.AppendEvents(&e.ConfigDeactivatedEvent)
if wm.configID != e.ConfigID {
continue
}
wm.ConfigWriteModel.AppendEvents(&e.ConfigDeactivatedEvent)
case *IDPConfigReactivatedEvent:
rm.ConfigWriteModel.AppendEvents(&e.ConfigReactivatedEvent)
if wm.configID != e.ConfigID {
continue
}
wm.ConfigWriteModel.AppendEvents(&e.ConfigReactivatedEvent)
case *IDPConfigRemovedEvent:
rm.ConfigWriteModel.AppendEvents(&e.ConfigRemovedEvent)
case *idp.ConfigAddedEvent,
*idp.ConfigChangedEvent,
*idp.ConfigDeactivatedEvent,
*idp.ConfigReactivatedEvent,
*idp.ConfigRemovedEvent:
rm.ConfigWriteModel.AppendEvents(e)
if wm.configID != e.ConfigID {
continue
}
wm.ConfigWriteModel.AppendEvents(&e.ConfigRemovedEvent)
case *IDPOIDCConfigAddedEvent:
if wm.configID != e.IDPConfigID {
continue
}
wm.ConfigWriteModel.AppendEvents(&e.ConfigAddedEvent)
case *IDPOIDCConfigChangedEvent:
if wm.configID != e.IDPConfigID {
continue
}
wm.ConfigWriteModel.AppendEvents(&e.ConfigChangedEvent)
}
}
}
func (wm *IDPConfigWriteModel) Reduce() error {
if err := wm.ConfigWriteModel.Reduce(); err != nil {
return err
}
return wm.WriteModel.Reduce()
}
func (wm *IDPConfigWriteModel) AppendAndReduce(events ...eventstore.EventReader) error {
wm.AppendEvents(events...)
return wm.Reduce()
}
type IDPConfigAddedEvent struct {
idp.ConfigAddedEvent
}

View File

@@ -16,19 +16,38 @@ const (
type IDPOIDCConfigWriteModel struct {
oidc.ConfigWriteModel
iamID string
idpConfigID string
}
func (rm *IDPOIDCConfigWriteModel) AppendEvents(events ...eventstore.EventReader) {
func NewIDPOIDCConfigWriteModel(iamID, idpConfigID string) *IDPOIDCConfigWriteModel {
return &IDPOIDCConfigWriteModel{
iamID: iamID,
idpConfigID: idpConfigID,
}
}
func (wm *IDPOIDCConfigWriteModel) Query() *eventstore.SearchQueryFactory {
return eventstore.NewSearchQueryFactory(eventstore.ColumnsEvent, AggregateType).
AggregateIDs(wm.iamID)
}
func (wm *IDPOIDCConfigWriteModel) AppendEvents(events ...eventstore.EventReader) {
for _, event := range events {
switch e := event.(type) {
case *IDPOIDCConfigAddedEvent:
rm.ConfigWriteModel.AppendEvents(&e.ConfigAddedEvent)
if wm.idpConfigID != e.IDPConfigID {
continue
}
wm.ConfigWriteModel.AppendEvents(&e.ConfigAddedEvent)
case *IDPOIDCConfigChangedEvent:
rm.ConfigWriteModel.AppendEvents(&e.ConfigChangedEvent)
case *oidc.ConfigAddedEvent,
*oidc.ConfigChangedEvent:
rm.ConfigWriteModel.AppendEvents(e)
if wm.idpConfigID != e.IDPConfigID {
continue
}
wm.ConfigWriteModel.AppendEvents(&e.ConfigChangedEvent)
default:
wm.ConfigWriteModel.AppendEvents(e)
}
}
}
@@ -82,7 +101,6 @@ func NewIDPOIDCConfigChangedEvent(
ctx context.Context,
current *IDPOIDCConfigWriteModel,
clientID,
idpConfigID,
issuer string,
clientSecret *crypto.CryptoValue,
idpDisplayNameMapping,
@@ -93,7 +111,7 @@ func NewIDPOIDCConfigChangedEvent(
event, err := oidc.NewConfigChangedEvent(
eventstore.NewBaseEventForPush(
ctx,
IDPOIDCConfigAddedEventType,
IDPOIDCConfigChangedEventType,
),
&current.ConfigWriteModel,
clientID,

View File

@@ -8,8 +8,6 @@ import (
type ConfigReadModel struct {
eventstore.ReadModel
Type ConfigType
State ConfigState
ConfigID string
Name string
@@ -26,13 +24,24 @@ func NewConfigReadModel(configID string) *ConfigReadModel {
}
func (rm *ConfigReadModel) AppendEvents(events ...eventstore.EventReader) {
rm.ReadModel.AppendEvents(events...)
for _, event := range events {
switch event.(type) {
switch e := event.(type) {
case *ConfigAddedEvent:
rm.ReadModel.AppendEvents(e)
case *ConfigChangedEvent:
rm.ReadModel.AppendEvents(e)
case *ConfigDeactivatedEvent:
rm.ReadModel.AppendEvents(e)
case *ConfigReactivatedEvent:
rm.ReadModel.AppendEvents(e)
case *ConfigRemovedEvent:
rm.ReadModel.AppendEvents(e)
case *oidc.ConfigAddedEvent:
rm.OIDCConfig = &oidc.ConfigReadModel{}
rm.ReadModel.AppendEvents(e)
rm.OIDCConfig.AppendEvents(event)
case *oidc.ConfigChangedEvent:
rm.ReadModel.AppendEvents(e)
rm.OIDCConfig.AppendEvents(event)
}
}
@@ -42,30 +51,42 @@ func (rm *ConfigReadModel) Reduce() error {
for _, event := range rm.Events {
switch e := event.(type) {
case *ConfigAddedEvent:
rm.ConfigID = e.ConfigID
rm.Name = e.Name
rm.StylingType = e.StylingType
rm.State = ConfigStateActive
rm.reduceConfigAddedEvent(e)
case *ConfigChangedEvent:
if e.Name != "" {
rm.Name = e.Name
}
if e.StylingType.Valid() {
rm.StylingType = e.StylingType
}
rm.reduceConfigChangedEvent(e)
case *ConfigDeactivatedEvent:
rm.State = ConfigStateInactive
rm.reduceConfigStateChanged(e.ConfigID, ConfigStateInactive)
case *ConfigReactivatedEvent:
rm.State = ConfigStateActive
rm.reduceConfigStateChanged(e.ConfigID, ConfigStateActive)
case *ConfigRemovedEvent:
rm.State = ConfigStateRemoved
case *oidc.ConfigAddedEvent:
rm.Type = ConfigTypeOIDC
rm.reduceConfigStateChanged(e.ConfigID, ConfigStateRemoved)
}
}
if err := rm.OIDCConfig.Reduce(); err != nil {
return err
if rm.OIDCConfig != nil {
if err := rm.OIDCConfig.Reduce(); err != nil {
return err
}
}
return rm.ReadModel.Reduce()
}
func (rm *ConfigReadModel) reduceConfigAddedEvent(e *ConfigAddedEvent) {
rm.ConfigID = e.ConfigID
rm.Name = e.Name
rm.StylingType = e.StylingType
rm.State = ConfigStateActive
}
func (rm *ConfigReadModel) reduceConfigChangedEvent(e *ConfigChangedEvent) {
if e.Name != "" {
rm.Name = e.Name
}
if e.StylingType.Valid() {
rm.StylingType = e.StylingType
}
}
func (rm *ConfigReadModel) reduceConfigStateChanged(configID string, state ConfigState) {
rm.State = state
}

View File

@@ -8,10 +8,13 @@ import (
type ConfigWriteModel struct {
eventstore.WriteModel
State ConfigState
ConfigID string
Name string
StylingType StylingType
OIDCConfig *oidc.ConfigWriteModel
OIDCConfig *oidc.ConfigWriteModel
}
func (rm *ConfigWriteModel) AppendEvents(events ...eventstore.EventReader) {
@@ -19,7 +22,7 @@ func (rm *ConfigWriteModel) AppendEvents(events ...eventstore.EventReader) {
for _, event := range events {
switch event.(type) {
case *oidc.ConfigAddedEvent:
rm.OIDCConfig = &oidc.ConfigWriteModel{}
rm.OIDCConfig = new(oidc.ConfigWriteModel)
rm.OIDCConfig.AppendEvents(event)
case *oidc.ConfigChangedEvent:
rm.OIDCConfig.AppendEvents(event)
@@ -31,20 +34,41 @@ func (rm *ConfigWriteModel) Reduce() error {
for _, event := range rm.Events {
switch e := event.(type) {
case *ConfigAddedEvent:
rm.ConfigID = e.ConfigID
rm.Name = e.Name
rm.StylingType = e.StylingType
rm.reduceConfigAddedEvent(e)
case *ConfigChangedEvent:
if e.Name != "" {
rm.Name = e.Name
}
if e.StylingType.Valid() {
rm.StylingType = e.StylingType
}
rm.reduceConfigChangedEvent(e)
case *ConfigDeactivatedEvent:
rm.reduceConfigStateChanged(e.ConfigID, ConfigStateInactive)
case *ConfigReactivatedEvent:
rm.reduceConfigStateChanged(e.ConfigID, ConfigStateActive)
case *ConfigRemovedEvent:
rm.reduceConfigStateChanged(e.ConfigID, ConfigStateRemoved)
}
}
if err := rm.OIDCConfig.Reduce(); err != nil {
return err
if rm.OIDCConfig != nil {
if err := rm.OIDCConfig.Reduce(); err != nil {
return err
}
}
return rm.WriteModel.Reduce()
}
func (rm *ConfigWriteModel) reduceConfigAddedEvent(e *ConfigAddedEvent) {
rm.ConfigID = e.ConfigID
rm.Name = e.Name
rm.StylingType = e.StylingType
rm.State = ConfigStateActive
}
func (rm *ConfigWriteModel) reduceConfigChangedEvent(e *ConfigChangedEvent) {
if e.Name != "" {
rm.Name = e.Name
}
if e.StylingType.Valid() {
rm.StylingType = e.StylingType
}
}
func (rm *ConfigWriteModel) reduceConfigStateChanged(configID string, state ConfigState) {
rm.State = state
}

View File

@@ -12,7 +12,7 @@ type ConfigAddedEvent struct {
eventstore.BaseEvent `json:"-"`
ConfigID string `json:"idpConfigId"`
Name string `json:"name"`
Name string `json:"name,omitempty"`
Typ ConfigType `json:"idpType,omitempty"`
StylingType StylingType `json:"stylingType,omitempty"`
}

View File

@@ -21,31 +21,39 @@ func (rm *ConfigReadModel) Reduce() error {
for _, event := range rm.Events {
switch e := event.(type) {
case *ConfigAddedEvent:
rm.IDPConfigID = e.IDPConfigID
rm.ClientID = e.ClientID
rm.ClientSecret = e.ClientSecret
rm.Issuer = e.Issuer
rm.Scopes = e.Scopes
rm.IDPDisplayNameMapping = e.IDPDisplayNameMapping
rm.UserNameMapping = e.UserNameMapping
rm.reduceConfigAddedEvent(e)
case *ConfigChangedEvent:
if e.ClientID != "" {
rm.ClientID = e.ClientID
}
if e.Issuer != "" {
rm.Issuer = e.Issuer
}
if len(e.Scopes) > 0 {
rm.Scopes = e.Scopes
}
if e.IDPDisplayNameMapping.Valid() {
rm.IDPDisplayNameMapping = e.IDPDisplayNameMapping
}
if e.UserNameMapping.Valid() {
rm.UserNameMapping = e.UserNameMapping
}
rm.reduceConfigChangedEvent(e)
}
}
return rm.ReadModel.Reduce()
}
func (rm *ConfigReadModel) reduceConfigAddedEvent(e *ConfigAddedEvent) {
rm.IDPConfigID = e.IDPConfigID
rm.ClientID = e.ClientID
rm.ClientSecret = e.ClientSecret
rm.Issuer = e.Issuer
rm.Scopes = e.Scopes
rm.IDPDisplayNameMapping = e.IDPDisplayNameMapping
rm.UserNameMapping = e.UserNameMapping
}
func (rm *ConfigReadModel) reduceConfigChangedEvent(e *ConfigChangedEvent) {
if e.ClientID != "" {
rm.ClientID = e.ClientID
}
if e.Issuer != "" {
rm.Issuer = e.Issuer
}
if len(e.Scopes) > 0 {
rm.Scopes = e.Scopes
}
if e.IDPDisplayNameMapping.Valid() {
rm.IDPDisplayNameMapping = e.IDPDisplayNameMapping
}
if e.UserNameMapping.Valid() {
rm.UserNameMapping = e.UserNameMapping
}
}

View File

@@ -22,31 +22,39 @@ func (wm *ConfigWriteModel) Reduce() error {
for _, event := range wm.Events {
switch e := event.(type) {
case *ConfigAddedEvent:
wm.IDPConfigID = e.IDPConfigID
wm.ClientID = e.ClientID
wm.ClientSecret = e.ClientSecret
wm.Issuer = e.Issuer
wm.Scopes = e.Scopes
wm.IDPDisplayNameMapping = e.IDPDisplayNameMapping
wm.UserNameMapping = e.UserNameMapping
wm.reduceConfigAddedEvent(e)
case *ConfigChangedEvent:
if e.ClientID != "" {
wm.ClientID = e.ClientID
}
if e.Issuer != "" {
wm.Issuer = e.Issuer
}
if len(e.Scopes) > 0 {
wm.Scopes = e.Scopes
}
if e.IDPDisplayNameMapping.Valid() {
wm.IDPDisplayNameMapping = e.IDPDisplayNameMapping
}
if e.UserNameMapping.Valid() {
wm.UserNameMapping = e.UserNameMapping
}
wm.reduceConfigChangedEvent(e)
}
}
return wm.WriteModel.Reduce()
}
func (wm *ConfigWriteModel) reduceConfigAddedEvent(e *ConfigAddedEvent) {
wm.IDPConfigID = e.IDPConfigID
wm.ClientID = e.ClientID
wm.ClientSecret = e.ClientSecret
wm.Issuer = e.Issuer
wm.Scopes = e.Scopes
wm.IDPDisplayNameMapping = e.IDPDisplayNameMapping
wm.UserNameMapping = e.UserNameMapping
}
func (wm *ConfigWriteModel) reduceConfigChangedEvent(e *ConfigChangedEvent) {
if e.ClientID != "" {
wm.ClientID = e.ClientID
}
if e.Issuer != "" {
wm.Issuer = e.Issuer
}
if len(e.Scopes) > 0 {
wm.Scopes = e.Scopes
}
if e.IDPDisplayNameMapping.Valid() {
wm.IDPDisplayNameMapping = e.IDPDisplayNameMapping
}
if e.UserNameMapping.Valid() {
wm.UserNameMapping = e.UserNameMapping
}
}

View File

@@ -13,13 +13,13 @@ type ConfigAddedEvent struct {
eventstore.BaseEvent
IDPConfigID string `json:"idpConfigId"`
ClientID string `json:"clientId"`
ClientSecret *crypto.CryptoValue `json:"clientSecret"`
Issuer string `json:"issuer"`
Scopes []string `json:"scpoes"`
ClientID string `json:"clientId,omitempty"`
ClientSecret *crypto.CryptoValue `json:"clientSecret,omitempty"`
Issuer string `json:"issuer,omitempty"`
Scopes []string `json:"scpoes,omitempty"`
IDPDisplayNameMapping MappingField `json:"idpDisplayNameMapping"`
UserNameMapping MappingField `json:"usernameMapping"`
IDPDisplayNameMapping MappingField `json:"idpDisplayNameMapping,omitempty"`
UserNameMapping MappingField `json:"usernameMapping,omitempty"`
}
func (e *ConfigAddedEvent) CheckPrevious() bool {

View File

@@ -16,13 +16,13 @@ type ConfigChangedEvent struct {
IDPConfigID string `json:"idpConfigId"`
ClientID string `json:"clientId"`
ClientSecret *crypto.CryptoValue `json:"clientSecret"`
Issuer string `json:"issuer"`
Scopes []string `json:"scpoes"`
ClientID string `json:"clientId,omitempty"`
ClientSecret *crypto.CryptoValue `json:"clientSecret,omitempty"`
Issuer string `json:"issuer,omitempty"`
Scopes []string `json:"scpoes,omitempty"`
IDPDisplayNameMapping MappingField `json:"idpDisplayNameMapping"`
UserNameMapping MappingField `json:"usernameMapping"`
IDPDisplayNameMapping MappingField `json:"idpDisplayNameMapping,omitempty"`
UserNameMapping MappingField `json:"usernameMapping,omitempty"`
}
func (e *ConfigChangedEvent) CheckPrevious() bool {
@@ -60,7 +60,7 @@ func NewConfigChangedEvent(
hasChanged = true
}
if clientSecret != nil && clientSecret != current.ClientSecret {
if clientSecret != nil {
event.ClientSecret = clientSecret
hasChanged = true
}

View File

@@ -46,8 +46,8 @@ func (rm *LabelPolicyReadModel) Reduce() error {
type LabelPolicyAddedEvent struct {
eventstore.BaseEvent `json:"-"`
PrimaryColor string `json:"primaryColor"`
SecondaryColor string `json:"secondaryColor"`
PrimaryColor string `json:"primaryColor,omitempty"`
SecondaryColor string `json:"secondaryColor,omitempty"`
}
func (e *LabelPolicyAddedEvent) CheckPrevious() bool {

View File

@@ -58,7 +58,7 @@ func (rm *PasswordComplexityPolicyReadModel) Reduce() error {
type PasswordComplexityPolicyAddedEvent struct {
eventstore.BaseEvent `json:"-"`
MinLength uint8 `json:"minLength"`
MinLength uint8 `json:"minLength,omitempty"`
HasLowercase bool `json:"hasLowercase"`
HasUpperCase bool `json:"hasUppercase"`
HasNumber bool `json:"hasNumber"`

View File

@@ -46,7 +46,7 @@ func (rm *PasswordLockoutPolicyReadModel) Reduce() error {
type PasswordLockoutPolicyAddedEvent struct {
eventstore.BaseEvent `json:"-"`
MaxAttempts uint8 `json:"maxAttempts"`
MaxAttempts uint8 `json:"maxAttempts,omitempty"`
ShowLockOutFailures bool `json:"showLockOutFailures"`
}