2021-01-20 10:06:52 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"reflect"
|
|
|
|
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
2021-07-06 11:55:57 +00:00
|
|
|
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/crypto"
|
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/idpconfig"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/org"
|
2021-01-20 10:06:52 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type IDPOIDCConfigWriteModel struct {
|
|
|
|
OIDCConfigWriteModel
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewOrgIDPOIDCConfigWriteModel(idpConfigID, orgID string) *IDPOIDCConfigWriteModel {
|
|
|
|
return &IDPOIDCConfigWriteModel{
|
|
|
|
OIDCConfigWriteModel{
|
|
|
|
WriteModel: eventstore.WriteModel{
|
|
|
|
AggregateID: orgID,
|
|
|
|
ResourceOwner: orgID,
|
|
|
|
},
|
|
|
|
IDPConfigID: idpConfigID,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-03 08:19:07 +00:00
|
|
|
func (wm *IDPOIDCConfigWriteModel) AppendEvents(events ...eventstore.Event) {
|
2021-01-20 10:06:52 +00:00
|
|
|
for _, event := range events {
|
|
|
|
switch e := event.(type) {
|
|
|
|
case *org.IDPOIDCConfigAddedEvent:
|
|
|
|
if wm.IDPConfigID != e.IDPConfigID {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
wm.OIDCConfigWriteModel.AppendEvents(&e.OIDCConfigAddedEvent)
|
|
|
|
case *org.IDPOIDCConfigChangedEvent:
|
|
|
|
if wm.IDPConfigID != e.IDPConfigID {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
wm.OIDCConfigWriteModel.AppendEvents(&e.OIDCConfigChangedEvent)
|
|
|
|
case *org.IDPConfigReactivatedEvent:
|
|
|
|
if wm.IDPConfigID != e.ConfigID {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
wm.OIDCConfigWriteModel.AppendEvents(&e.IDPConfigReactivatedEvent)
|
|
|
|
case *org.IDPConfigDeactivatedEvent:
|
|
|
|
if wm.IDPConfigID != e.ConfigID {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
wm.OIDCConfigWriteModel.AppendEvents(&e.IDPConfigDeactivatedEvent)
|
|
|
|
case *org.IDPConfigRemovedEvent:
|
|
|
|
if wm.IDPConfigID != e.ConfigID {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
wm.OIDCConfigWriteModel.AppendEvents(&e.IDPConfigRemovedEvent)
|
|
|
|
default:
|
|
|
|
wm.OIDCConfigWriteModel.AppendEvents(e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *IDPOIDCConfigWriteModel) Reduce() error {
|
|
|
|
if err := wm.OIDCConfigWriteModel.Reduce(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return wm.WriteModel.Reduce()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *IDPOIDCConfigWriteModel) Query() *eventstore.SearchQueryBuilder {
|
2021-07-06 11:55:57 +00:00
|
|
|
return eventstore.NewSearchQueryBuilder(eventstore.ColumnsEvent).
|
2021-02-18 13:48:27 +00:00
|
|
|
ResourceOwner(wm.ResourceOwner).
|
2021-07-06 11:55:57 +00:00
|
|
|
AddQuery().
|
|
|
|
AggregateTypes(org.AggregateType).
|
|
|
|
AggregateIDs(wm.AggregateID).
|
2021-02-18 13:48:27 +00:00
|
|
|
EventTypes(
|
|
|
|
org.IDPOIDCConfigAddedEventType,
|
|
|
|
org.IDPOIDCConfigChangedEventType,
|
|
|
|
org.IDPConfigReactivatedEventType,
|
|
|
|
org.IDPConfigDeactivatedEventType,
|
2021-07-06 11:55:57 +00:00
|
|
|
org.IDPConfigRemovedEventType).
|
|
|
|
Builder()
|
2021-01-20 10:06:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *IDPOIDCConfigWriteModel) NewChangedEvent(
|
|
|
|
ctx context.Context,
|
2021-02-18 13:48:27 +00:00
|
|
|
aggregate *eventstore.Aggregate,
|
2021-01-20 10:06:52 +00:00
|
|
|
idpConfigID,
|
|
|
|
clientID,
|
|
|
|
issuer,
|
2021-07-06 14:39:48 +00:00
|
|
|
authorizationEndpoint,
|
|
|
|
tokenEndpoint,
|
2021-01-20 10:06:52 +00:00
|
|
|
clientSecretString string,
|
|
|
|
secretCrypto crypto.Crypto,
|
|
|
|
idpDisplayNameMapping,
|
|
|
|
userNameMapping domain.OIDCMappingField,
|
|
|
|
scopes ...string,
|
|
|
|
) (*org.IDPOIDCConfigChangedEvent, bool, error) {
|
|
|
|
|
|
|
|
changes := make([]idpconfig.OIDCConfigChanges, 0)
|
|
|
|
var clientSecret *crypto.CryptoValue
|
|
|
|
var err error
|
|
|
|
if clientSecretString != "" {
|
|
|
|
clientSecret, err = crypto.Crypt([]byte(clientSecretString), secretCrypto)
|
|
|
|
if err != nil {
|
|
|
|
return nil, false, err
|
|
|
|
}
|
|
|
|
changes = append(changes, idpconfig.ChangeClientSecret(clientSecret))
|
|
|
|
}
|
|
|
|
if wm.ClientID != clientID {
|
|
|
|
changes = append(changes, idpconfig.ChangeClientID(clientID))
|
|
|
|
}
|
|
|
|
if wm.Issuer != issuer {
|
|
|
|
changes = append(changes, idpconfig.ChangeIssuer(issuer))
|
|
|
|
}
|
2021-07-06 14:39:48 +00:00
|
|
|
if wm.AuthorizationEndpoint != authorizationEndpoint {
|
|
|
|
changes = append(changes, idpconfig.ChangeAuthorizationEndpoint(authorizationEndpoint))
|
|
|
|
}
|
|
|
|
if wm.TokenEndpoint != tokenEndpoint {
|
|
|
|
changes = append(changes, idpconfig.ChangeTokenEndpoint(tokenEndpoint))
|
|
|
|
}
|
2021-01-20 10:06:52 +00:00
|
|
|
if idpDisplayNameMapping.Valid() && wm.IDPDisplayNameMapping != idpDisplayNameMapping {
|
|
|
|
changes = append(changes, idpconfig.ChangeIDPDisplayNameMapping(idpDisplayNameMapping))
|
|
|
|
}
|
|
|
|
if userNameMapping.Valid() && wm.UserNameMapping != userNameMapping {
|
|
|
|
changes = append(changes, idpconfig.ChangeUserNameMapping(userNameMapping))
|
|
|
|
}
|
2021-03-19 10:12:56 +00:00
|
|
|
if !reflect.DeepEqual(wm.Scopes, scopes) {
|
2021-01-20 10:06:52 +00:00
|
|
|
changes = append(changes, idpconfig.ChangeScopes(scopes))
|
|
|
|
}
|
|
|
|
if len(changes) == 0 {
|
|
|
|
return nil, false, nil
|
|
|
|
}
|
2021-02-18 13:48:27 +00:00
|
|
|
changeEvent, err := org.NewIDPOIDCConfigChangedEvent(ctx, aggregate, idpConfigID, changes)
|
2021-01-20 10:06:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, false, err
|
|
|
|
}
|
|
|
|
return changeEvent, true, nil
|
|
|
|
}
|