zitadel/internal/v2/command/oidc_config_model.go

71 lines
1.9 KiB
Go
Raw Normal View History

package command
2020-11-25 19:04:32 +00:00
import (
"github.com/caos/zitadel/internal/crypto"
"github.com/caos/zitadel/internal/eventstore/v2"
"github.com/caos/zitadel/internal/v2/domain"
"github.com/caos/zitadel/internal/v2/repository/idpconfig"
2020-11-25 19:04:32 +00:00
)
type OIDCConfigWriteModel struct {
2020-11-25 19:04:32 +00:00
eventstore.WriteModel
IDPConfigID string
ClientID string
ClientSecret *crypto.CryptoValue
Issuer string
Scopes []string
IDPDisplayNameMapping domain.OIDCMappingField
UserNameMapping domain.OIDCMappingField
State domain.IDPConfigState
2020-11-25 19:04:32 +00:00
}
func (wm *OIDCConfigWriteModel) Reduce() error {
2020-11-25 19:04:32 +00:00
for _, event := range wm.Events {
switch e := event.(type) {
case *idpconfig.OIDCConfigAddedEvent:
2020-11-27 10:30:56 +00:00
wm.reduceConfigAddedEvent(e)
case *idpconfig.OIDCConfigChangedEvent:
2020-11-27 10:30:56 +00:00
wm.reduceConfigChangedEvent(e)
case *idpconfig.IDPConfigDeactivatedEvent:
wm.State = domain.IDPConfigStateInactive
case *idpconfig.IDPConfigReactivatedEvent:
wm.State = domain.IDPConfigStateActive
case *idpconfig.IDPConfigRemovedEvent:
wm.State = domain.IDPConfigStateRemoved
2020-11-25 19:04:32 +00:00
}
}
return wm.WriteModel.Reduce()
}
2020-11-27 10:30:56 +00:00
func (wm *OIDCConfigWriteModel) reduceConfigAddedEvent(e *idpconfig.OIDCConfigAddedEvent) {
2020-11-27 10:30:56 +00:00
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.State = domain.IDPConfigStateActive
2020-11-27 10:30:56 +00:00
}
func (wm *OIDCConfigWriteModel) reduceConfigChangedEvent(e *idpconfig.OIDCConfigChangedEvent) {
if e.ClientID != nil {
wm.ClientID = *e.ClientID
2020-11-27 10:30:56 +00:00
}
if e.Issuer != nil {
wm.Issuer = *e.Issuer
2020-11-27 10:30:56 +00:00
}
if len(e.Scopes) > 0 {
wm.Scopes = e.Scopes
}
if e.IDPDisplayNameMapping != nil && e.IDPDisplayNameMapping.Valid() {
wm.IDPDisplayNameMapping = *e.IDPDisplayNameMapping
2020-11-27 10:30:56 +00:00
}
if e.UserNameMapping != nil && e.UserNameMapping.Valid() {
wm.UserNameMapping = *e.UserNameMapping
2020-11-27 10:30:56 +00:00
}
}