2021-01-04 13:52:13 +00:00
|
|
|
package command
|
2020-11-25 19:04:32 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/caos/zitadel/internal/crypto"
|
|
|
|
"github.com/caos/zitadel/internal/eventstore/v2"
|
2021-01-04 13:52:13 +00:00
|
|
|
"github.com/caos/zitadel/internal/v2/domain"
|
|
|
|
"github.com/caos/zitadel/internal/v2/repository/idpconfig"
|
2020-11-25 19:04:32 +00:00
|
|
|
)
|
|
|
|
|
2021-01-04 13:52:13 +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
|
|
|
|
|
2021-01-04 13:52:13 +00:00
|
|
|
IDPDisplayNameMapping domain.OIDCMappingField
|
|
|
|
UserNameMapping domain.OIDCMappingField
|
|
|
|
State domain.IDPConfigState
|
2020-11-25 19:04:32 +00:00
|
|
|
}
|
|
|
|
|
2021-01-04 13:52:13 +00:00
|
|
|
func (wm *OIDCConfigWriteModel) Reduce() error {
|
2020-11-25 19:04:32 +00:00
|
|
|
for _, event := range wm.Events {
|
|
|
|
switch e := event.(type) {
|
2021-01-04 13:52:13 +00:00
|
|
|
case *idpconfig.OIDCConfigAddedEvent:
|
2020-11-27 10:30:56 +00:00
|
|
|
wm.reduceConfigAddedEvent(e)
|
2021-01-04 13:52:13 +00:00
|
|
|
case *idpconfig.OIDCConfigChangedEvent:
|
2020-11-27 10:30:56 +00:00
|
|
|
wm.reduceConfigChangedEvent(e)
|
2021-01-04 13:52:13 +00:00
|
|
|
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
|
|
|
|
2021-01-04 13:52:13 +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
|
2021-01-04 13:52:13 +00:00
|
|
|
wm.State = domain.IDPConfigStateActive
|
2020-11-27 10:30:56 +00:00
|
|
|
}
|
|
|
|
|
2021-01-04 13:52:13 +00:00
|
|
|
func (wm *OIDCConfigWriteModel) reduceConfigChangedEvent(e *idpconfig.OIDCConfigChangedEvent) {
|
2021-01-20 10:06:52 +00:00
|
|
|
if e.ClientID != nil {
|
|
|
|
wm.ClientID = *e.ClientID
|
2020-11-27 10:30:56 +00:00
|
|
|
}
|
2021-01-20 10:06:52 +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
|
|
|
|
}
|
2021-01-20 10:06:52 +00:00
|
|
|
if e.IDPDisplayNameMapping != nil && e.IDPDisplayNameMapping.Valid() {
|
|
|
|
wm.IDPDisplayNameMapping = *e.IDPDisplayNameMapping
|
2020-11-27 10:30:56 +00:00
|
|
|
}
|
2021-01-20 10:06:52 +00:00
|
|
|
if e.UserNameMapping != nil && e.UserNameMapping.Valid() {
|
|
|
|
wm.UserNameMapping = *e.UserNameMapping
|
2020-11-27 10:30:56 +00:00
|
|
|
}
|
|
|
|
}
|