2021-01-04 13:52:13 +00:00
|
|
|
package query
|
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 OIDCConfigReadModel struct {
|
2020-11-25 19:04:32 +00:00
|
|
|
eventstore.ReadModel
|
|
|
|
|
|
|
|
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
|
2020-11-25 19:04:32 +00:00
|
|
|
}
|
|
|
|
|
2021-01-04 13:52:13 +00:00
|
|
|
func (rm *OIDCConfigReadModel) Reduce() error {
|
2020-11-25 19:04:32 +00:00
|
|
|
for _, event := range rm.Events {
|
|
|
|
switch e := event.(type) {
|
2021-01-04 13:52:13 +00:00
|
|
|
case *idpconfig.OIDCConfigAddedEvent:
|
2020-11-27 10:30:56 +00:00
|
|
|
rm.reduceConfigAddedEvent(e)
|
2021-01-04 13:52:13 +00:00
|
|
|
case *idpconfig.OIDCConfigChangedEvent:
|
2020-11-27 10:30:56 +00:00
|
|
|
rm.reduceConfigChangedEvent(e)
|
2020-11-25 19:04:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return rm.ReadModel.Reduce()
|
|
|
|
}
|
2020-11-27 10:30:56 +00:00
|
|
|
|
2021-01-04 13:52:13 +00:00
|
|
|
func (rm *OIDCConfigReadModel) reduceConfigAddedEvent(e *idpconfig.OIDCConfigAddedEvent) {
|
2020-11-27 10:30:56 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-01-04 13:52:13 +00:00
|
|
|
func (rm *OIDCConfigReadModel) reduceConfigChangedEvent(e *idpconfig.OIDCConfigChangedEvent) {
|
2020-11-27 10:30:56 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|