2021-01-04 13:52:13 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-01-12 11:59:51 +00:00
|
|
|
"reflect"
|
|
|
|
|
2021-07-06 11:55:57 +00:00
|
|
|
"github.com/caos/zitadel/internal/eventstore"
|
|
|
|
|
2021-01-04 13:52:13 +00:00
|
|
|
"github.com/caos/zitadel/internal/crypto"
|
2021-02-23 14:13:04 +00:00
|
|
|
"github.com/caos/zitadel/internal/domain"
|
|
|
|
"github.com/caos/zitadel/internal/repository/iam"
|
|
|
|
"github.com/caos/zitadel/internal/repository/idpconfig"
|
2021-01-04 13:52:13 +00:00
|
|
|
)
|
|
|
|
|
2021-01-20 10:06:52 +00:00
|
|
|
type IAMIDPOIDCConfigWriteModel struct {
|
2021-01-04 13:52:13 +00:00
|
|
|
OIDCConfigWriteModel
|
|
|
|
}
|
|
|
|
|
2021-01-20 10:06:52 +00:00
|
|
|
func NewIAMIDPOIDCConfigWriteModel(idpConfigID string) *IAMIDPOIDCConfigWriteModel {
|
|
|
|
return &IAMIDPOIDCConfigWriteModel{
|
2021-01-04 13:52:13 +00:00
|
|
|
OIDCConfigWriteModel{
|
|
|
|
WriteModel: eventstore.WriteModel{
|
2021-01-12 11:59:51 +00:00
|
|
|
AggregateID: domain.IAMID,
|
|
|
|
ResourceOwner: domain.IAMID,
|
2021-01-04 13:52:13 +00:00
|
|
|
},
|
|
|
|
IDPConfigID: idpConfigID,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-20 10:06:52 +00:00
|
|
|
func (wm *IAMIDPOIDCConfigWriteModel) AppendEvents(events ...eventstore.EventReader) {
|
2021-01-04 13:52:13 +00:00
|
|
|
for _, event := range events {
|
|
|
|
switch e := event.(type) {
|
|
|
|
case *iam.IDPOIDCConfigAddedEvent:
|
|
|
|
if wm.IDPConfigID != e.IDPConfigID {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
wm.OIDCConfigWriteModel.AppendEvents(&e.OIDCConfigAddedEvent)
|
|
|
|
case *iam.IDPOIDCConfigChangedEvent:
|
|
|
|
if wm.IDPConfigID != e.IDPConfigID {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
wm.OIDCConfigWriteModel.AppendEvents(&e.OIDCConfigChangedEvent)
|
|
|
|
case *iam.IDPConfigReactivatedEvent:
|
|
|
|
if wm.IDPConfigID != e.ConfigID {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
wm.OIDCConfigWriteModel.AppendEvents(&e.IDPConfigReactivatedEvent)
|
|
|
|
case *iam.IDPConfigDeactivatedEvent:
|
|
|
|
if wm.IDPConfigID != e.ConfigID {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
wm.OIDCConfigWriteModel.AppendEvents(&e.IDPConfigDeactivatedEvent)
|
|
|
|
case *iam.IDPConfigRemovedEvent:
|
|
|
|
if wm.IDPConfigID != e.ConfigID {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
wm.OIDCConfigWriteModel.AppendEvents(&e.IDPConfigRemovedEvent)
|
|
|
|
default:
|
|
|
|
wm.OIDCConfigWriteModel.AppendEvents(e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-20 10:06:52 +00:00
|
|
|
func (wm *IAMIDPOIDCConfigWriteModel) Reduce() error {
|
2021-01-04 13:52:13 +00:00
|
|
|
if err := wm.OIDCConfigWriteModel.Reduce(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return wm.WriteModel.Reduce()
|
|
|
|
}
|
|
|
|
|
2021-01-20 10:06:52 +00:00
|
|
|
func (wm *IAMIDPOIDCConfigWriteModel) 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(iam.AggregateType).
|
|
|
|
AggregateIDs(wm.AggregateID).
|
2021-02-18 13:48:27 +00:00
|
|
|
EventTypes(
|
|
|
|
iam.IDPOIDCConfigAddedEventType,
|
|
|
|
iam.IDPOIDCConfigChangedEventType,
|
|
|
|
iam.IDPConfigReactivatedEventType,
|
|
|
|
iam.IDPConfigDeactivatedEventType,
|
2021-07-06 11:55:57 +00:00
|
|
|
iam.IDPConfigRemovedEventType).
|
|
|
|
Builder()
|
2021-01-04 13:52:13 +00:00
|
|
|
}
|
|
|
|
|
2021-01-20 10:06:52 +00:00
|
|
|
func (wm *IAMIDPOIDCConfigWriteModel) NewChangedEvent(
|
2021-01-04 13:52:13 +00:00
|
|
|
ctx context.Context,
|
2021-02-18 13:48:27 +00:00
|
|
|
aggregate *eventstore.Aggregate,
|
2021-01-20 10:06:52 +00:00
|
|
|
idpConfigID,
|
2021-01-04 13:52:13 +00:00
|
|
|
clientID,
|
|
|
|
issuer,
|
2021-07-06 14:39:48 +00:00
|
|
|
authorizationEndpoint,
|
|
|
|
tokenEndpoint,
|
2021-01-04 13:52:13 +00:00
|
|
|
clientSecretString string,
|
|
|
|
secretCrypto crypto.Crypto,
|
|
|
|
idpDisplayNameMapping,
|
|
|
|
userNameMapping domain.OIDCMappingField,
|
|
|
|
scopes ...string,
|
|
|
|
) (*iam.IDPOIDCConfigChangedEvent, bool, error) {
|
2021-01-20 10:06:52 +00:00
|
|
|
|
|
|
|
changes := make([]idpconfig.OIDCConfigChanges, 0)
|
2021-01-04 13:52:13 +00:00
|
|
|
var clientSecret *crypto.CryptoValue
|
|
|
|
var err error
|
|
|
|
if clientSecretString != "" {
|
|
|
|
clientSecret, err = crypto.Crypt([]byte(clientSecretString), secretCrypto)
|
|
|
|
if err != nil {
|
|
|
|
return nil, false, err
|
|
|
|
}
|
2021-01-20 10:06:52 +00:00
|
|
|
changes = append(changes, idpconfig.ChangeClientSecret(clientSecret))
|
2021-01-04 13:52:13 +00:00
|
|
|
}
|
|
|
|
if wm.ClientID != clientID {
|
2021-01-20 10:06:52 +00:00
|
|
|
changes = append(changes, idpconfig.ChangeClientID(clientID))
|
2021-01-04 13:52:13 +00:00
|
|
|
}
|
|
|
|
if wm.Issuer != issuer {
|
2021-01-20 10:06:52 +00:00
|
|
|
changes = append(changes, idpconfig.ChangeIssuer(issuer))
|
2021-01-04 13:52:13 +00:00
|
|
|
}
|
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-04 13:52:13 +00:00
|
|
|
if idpDisplayNameMapping.Valid() && wm.IDPDisplayNameMapping != idpDisplayNameMapping {
|
2021-01-20 10:06:52 +00:00
|
|
|
changes = append(changes, idpconfig.ChangeIDPDisplayNameMapping(idpDisplayNameMapping))
|
2021-01-04 13:52:13 +00:00
|
|
|
}
|
|
|
|
if userNameMapping.Valid() && wm.UserNameMapping != userNameMapping {
|
2021-01-20 10:06:52 +00:00
|
|
|
changes = append(changes, idpconfig.ChangeUserNameMapping(userNameMapping))
|
2021-01-04 13:52:13 +00:00
|
|
|
}
|
2021-02-08 10:30:30 +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 := iam.NewIDPOIDCConfigChangedEvent(ctx, aggregate, idpConfigID, changes)
|
2021-01-20 10:06:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, false, err
|
2021-01-04 13:52:13 +00:00
|
|
|
}
|
2021-01-20 10:06:52 +00:00
|
|
|
return changeEvent, true, nil
|
2021-01-04 13:52:13 +00:00
|
|
|
}
|