2021-01-04 13:52:13 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-01-12 11:59:51 +00:00
|
|
|
"reflect"
|
|
|
|
|
2021-01-04 13:52:13 +00:00
|
|
|
"github.com/caos/zitadel/internal/crypto"
|
|
|
|
"github.com/caos/zitadel/internal/eventstore/v2"
|
|
|
|
"github.com/caos/zitadel/internal/v2/domain"
|
|
|
|
"github.com/caos/zitadel/internal/v2/repository/iam"
|
2021-01-20 10:06:52 +00:00
|
|
|
"github.com/caos/zitadel/internal/v2/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-01-04 13:52:13 +00:00
|
|
|
return eventstore.NewSearchQueryBuilder(eventstore.ColumnsEvent, iam.AggregateType).
|
2021-01-12 11:59:51 +00:00
|
|
|
AggregateIDs(wm.AggregateID).
|
|
|
|
ResourceOwner(wm.ResourceOwner)
|
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-01-20 10:06:52 +00:00
|
|
|
idpConfigID,
|
2021-01-04 13:52:13 +00:00
|
|
|
clientID,
|
|
|
|
issuer,
|
|
|
|
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
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
|
|
|
changeEvent, err := iam.NewIDPOIDCConfigChangedEvent(ctx, idpConfigID, changes)
|
|
|
|
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
|
|
|
}
|