2021-01-04 14:52:13 +01:00
|
|
|
package query
|
2020-11-25 20:04:32 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/caos/zitadel/internal/crypto"
|
2021-02-23 15:13:04 +01:00
|
|
|
"github.com/caos/zitadel/internal/domain"
|
|
|
|
"github.com/caos/zitadel/internal/eventstore"
|
|
|
|
"github.com/caos/zitadel/internal/repository/idpconfig"
|
2020-11-25 20:04:32 +01:00
|
|
|
)
|
|
|
|
|
2021-01-04 14:52:13 +01:00
|
|
|
type OIDCConfigReadModel struct {
|
2020-11-25 20:04:32 +01:00
|
|
|
eventstore.ReadModel
|
|
|
|
|
|
|
|
IDPConfigID string
|
|
|
|
ClientID string
|
|
|
|
ClientSecret *crypto.CryptoValue
|
|
|
|
Issuer string
|
2021-07-06 16:39:48 +02:00
|
|
|
AuthorizationEndpoint string
|
|
|
|
TokenEndpoint string
|
2020-11-25 20:04:32 +01:00
|
|
|
Scopes []string
|
2021-01-04 14:52:13 +01:00
|
|
|
IDPDisplayNameMapping domain.OIDCMappingField
|
|
|
|
UserNameMapping domain.OIDCMappingField
|
2020-11-25 20:04:32 +01:00
|
|
|
}
|
|
|
|
|
2021-01-04 14:52:13 +01:00
|
|
|
func (rm *OIDCConfigReadModel) Reduce() error {
|
2020-11-25 20:04:32 +01:00
|
|
|
for _, event := range rm.Events {
|
|
|
|
switch e := event.(type) {
|
2021-01-04 14:52:13 +01:00
|
|
|
case *idpconfig.OIDCConfigAddedEvent:
|
2020-11-27 11:30:56 +01:00
|
|
|
rm.reduceConfigAddedEvent(e)
|
2021-01-04 14:52:13 +01:00
|
|
|
case *idpconfig.OIDCConfigChangedEvent:
|
2020-11-27 11:30:56 +01:00
|
|
|
rm.reduceConfigChangedEvent(e)
|
2020-11-25 20:04:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return rm.ReadModel.Reduce()
|
|
|
|
}
|
2020-11-27 11:30:56 +01:00
|
|
|
|
2021-01-04 14:52:13 +01:00
|
|
|
func (rm *OIDCConfigReadModel) reduceConfigAddedEvent(e *idpconfig.OIDCConfigAddedEvent) {
|
2020-11-27 11:30:56 +01:00
|
|
|
rm.IDPConfigID = e.IDPConfigID
|
|
|
|
rm.ClientID = e.ClientID
|
|
|
|
rm.ClientSecret = e.ClientSecret
|
|
|
|
rm.Issuer = e.Issuer
|
2021-07-06 16:39:48 +02:00
|
|
|
rm.AuthorizationEndpoint = e.AuthorizationEndpoint
|
|
|
|
rm.TokenEndpoint = e.TokenEndpoint
|
2020-11-27 11:30:56 +01:00
|
|
|
rm.Scopes = e.Scopes
|
|
|
|
rm.IDPDisplayNameMapping = e.IDPDisplayNameMapping
|
|
|
|
rm.UserNameMapping = e.UserNameMapping
|
|
|
|
}
|
|
|
|
|
2021-01-04 14:52:13 +01:00
|
|
|
func (rm *OIDCConfigReadModel) reduceConfigChangedEvent(e *idpconfig.OIDCConfigChangedEvent) {
|
2021-01-20 11:06:52 +01:00
|
|
|
if e.ClientID != nil {
|
|
|
|
rm.ClientID = *e.ClientID
|
2020-11-27 11:30:56 +01:00
|
|
|
}
|
2021-01-20 11:06:52 +01:00
|
|
|
if e.Issuer != nil {
|
|
|
|
rm.Issuer = *e.Issuer
|
2020-11-27 11:30:56 +01:00
|
|
|
}
|
2021-07-06 16:39:48 +02:00
|
|
|
if e.AuthorizationEndpoint != nil {
|
|
|
|
rm.AuthorizationEndpoint = *e.AuthorizationEndpoint
|
|
|
|
}
|
|
|
|
if e.TokenEndpoint != nil {
|
|
|
|
rm.TokenEndpoint = *e.TokenEndpoint
|
|
|
|
}
|
2020-11-27 11:30:56 +01:00
|
|
|
if len(e.Scopes) > 0 {
|
|
|
|
rm.Scopes = e.Scopes
|
|
|
|
}
|
2021-01-20 11:06:52 +01:00
|
|
|
if e.IDPDisplayNameMapping != nil && e.IDPDisplayNameMapping.Valid() {
|
|
|
|
rm.IDPDisplayNameMapping = *e.IDPDisplayNameMapping
|
2020-11-27 11:30:56 +01:00
|
|
|
}
|
2021-01-20 11:06:52 +01:00
|
|
|
if e.UserNameMapping != nil && e.UserNameMapping.Valid() {
|
|
|
|
rm.UserNameMapping = *e.UserNameMapping
|
2020-11-27 11:30:56 +01:00
|
|
|
}
|
|
|
|
}
|