zitadel/internal/command/oidc_config_model.go

81 lines
2.3 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/domain"
"github.com/caos/zitadel/internal/eventstore"
"github.com/caos/zitadel/internal/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
AuthorizationEndpoint string
TokenEndpoint string
Scopes []string
2020-11-25 19:04:32 +00:00
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.AuthorizationEndpoint = e.AuthorizationEndpoint
wm.TokenEndpoint = e.TokenEndpoint
2020-11-27 10:30:56 +00:00
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 e.AuthorizationEndpoint != nil {
wm.AuthorizationEndpoint = *e.AuthorizationEndpoint
}
if e.TokenEndpoint != nil {
wm.TokenEndpoint = *e.TokenEndpoint
}
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
}
}