zitadel/internal/v2/repository/idp/oidc/config_write_model.go

61 lines
1.3 KiB
Go
Raw Normal View History

2020-11-25 20:04:32 +01:00
package oidc
import (
"github.com/caos/zitadel/internal/crypto"
"github.com/caos/zitadel/internal/eventstore/v2"
)
type ConfigWriteModel struct {
eventstore.WriteModel
IDPConfigID string
ClientID string
ClientSecret *crypto.CryptoValue
Issuer string
Scopes []string
IDPDisplayNameMapping MappingField
UserNameMapping MappingField
}
func (wm *ConfigWriteModel) Reduce() error {
for _, event := range wm.Events {
switch e := event.(type) {
case *ConfigAddedEvent:
2020-11-27 11:30:56 +01:00
wm.reduceConfigAddedEvent(e)
2020-11-25 20:04:32 +01:00
case *ConfigChangedEvent:
2020-11-27 11:30:56 +01:00
wm.reduceConfigChangedEvent(e)
2020-11-25 20:04:32 +01:00
}
}
return wm.WriteModel.Reduce()
}
2020-11-27 11:30:56 +01:00
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
}
}