2020-09-18 11:26:28 +00:00
|
|
|
package handler
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"github.com/caos/logging"
|
|
|
|
"github.com/caos/zitadel/internal/config/systemdefaults"
|
|
|
|
caos_errs "github.com/caos/zitadel/internal/errors"
|
|
|
|
"github.com/caos/zitadel/internal/eventstore/models"
|
|
|
|
es_models "github.com/caos/zitadel/internal/eventstore/models"
|
|
|
|
"github.com/caos/zitadel/internal/eventstore/spooler"
|
|
|
|
iam_model "github.com/caos/zitadel/internal/iam/model"
|
2020-10-02 06:02:09 +00:00
|
|
|
"github.com/caos/zitadel/internal/iam/repository/eventsourcing"
|
2020-09-18 11:26:28 +00:00
|
|
|
iam_es_model "github.com/caos/zitadel/internal/iam/repository/eventsourcing/model"
|
2020-10-02 06:02:09 +00:00
|
|
|
iam_view_model "github.com/caos/zitadel/internal/iam/repository/view/model"
|
|
|
|
org_es "github.com/caos/zitadel/internal/org/repository/eventsourcing"
|
|
|
|
org_es_model "github.com/caos/zitadel/internal/org/repository/eventsourcing/model"
|
|
|
|
"github.com/caos/zitadel/internal/user/repository/eventsourcing/model"
|
|
|
|
usr_view_model "github.com/caos/zitadel/internal/user/repository/view/model"
|
2020-09-18 11:26:28 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type ExternalIDP struct {
|
|
|
|
handler
|
|
|
|
systemDefaults systemdefaults.SystemDefaults
|
|
|
|
iamEvents *eventsourcing.IAMEventstore
|
|
|
|
orgEvents *org_es.OrgEventstore
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
externalIDPTable = "auth.user_external_idps"
|
|
|
|
)
|
|
|
|
|
2020-12-02 07:50:59 +00:00
|
|
|
func (i *ExternalIDP) ViewModel() string {
|
2020-09-18 11:26:28 +00:00
|
|
|
return externalIDPTable
|
|
|
|
}
|
|
|
|
|
2020-12-02 07:50:59 +00:00
|
|
|
func (i *ExternalIDP) EventQuery() (*models.SearchQuery, error) {
|
|
|
|
sequence, err := i.view.GetLatestExternalIDPSequence()
|
2020-09-18 11:26:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return es_models.NewSearchQuery().
|
|
|
|
AggregateTypeFilter(model.UserAggregate, iam_es_model.IAMAggregate, org_es_model.OrgAggregate).
|
|
|
|
LatestSequenceFilter(sequence.CurrentSequence), nil
|
|
|
|
}
|
|
|
|
|
2020-12-02 07:50:59 +00:00
|
|
|
func (i *ExternalIDP) Reduce(event *models.Event) (err error) {
|
2020-09-18 11:26:28 +00:00
|
|
|
switch event.AggregateType {
|
|
|
|
case model.UserAggregate:
|
2020-12-02 07:50:59 +00:00
|
|
|
err = i.processUser(event)
|
2020-09-18 11:26:28 +00:00
|
|
|
case iam_es_model.IAMAggregate, org_es_model.OrgAggregate:
|
2020-12-02 07:50:59 +00:00
|
|
|
err = i.processIdpConfig(event)
|
2020-09-18 11:26:28 +00:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-12-02 07:50:59 +00:00
|
|
|
func (i *ExternalIDP) processUser(event *models.Event) (err error) {
|
2020-09-18 11:26:28 +00:00
|
|
|
externalIDP := new(usr_view_model.ExternalIDPView)
|
|
|
|
switch event.Type {
|
|
|
|
case model.HumanExternalIDPAdded:
|
|
|
|
err = externalIDP.AppendEvent(event)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-12-02 07:50:59 +00:00
|
|
|
err = i.fillData(externalIDP)
|
2020-09-18 11:26:28 +00:00
|
|
|
case model.HumanExternalIDPRemoved, model.HumanExternalIDPCascadeRemoved:
|
|
|
|
err = externalIDP.SetData(event)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-12-02 07:50:59 +00:00
|
|
|
return i.view.DeleteExternalIDP(externalIDP.ExternalUserID, externalIDP.IDPConfigID, event.Sequence, event.CreationDate)
|
2020-10-07 06:16:42 +00:00
|
|
|
case model.UserRemoved:
|
2020-12-02 07:50:59 +00:00
|
|
|
return i.view.DeleteExternalIDPsByUserID(event.AggregateID, event.Sequence, event.CreationDate)
|
2020-09-18 11:26:28 +00:00
|
|
|
default:
|
2020-12-02 07:50:59 +00:00
|
|
|
return i.view.ProcessedExternalIDPSequence(event.Sequence, event.CreationDate)
|
2020-09-18 11:26:28 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-12-02 07:50:59 +00:00
|
|
|
return i.view.PutExternalIDP(externalIDP, externalIDP.Sequence, event.CreationDate)
|
2020-09-18 11:26:28 +00:00
|
|
|
}
|
|
|
|
|
2020-12-02 07:50:59 +00:00
|
|
|
func (i *ExternalIDP) processIdpConfig(event *models.Event) (err error) {
|
2020-09-18 11:26:28 +00:00
|
|
|
switch event.Type {
|
|
|
|
case iam_es_model.IDPConfigChanged, org_es_model.IDPConfigChanged:
|
2020-10-02 06:02:09 +00:00
|
|
|
configView := new(iam_view_model.IDPConfigView)
|
2020-09-18 11:26:28 +00:00
|
|
|
config := new(iam_model.IDPConfig)
|
2020-10-02 06:02:09 +00:00
|
|
|
if event.Type == iam_es_model.IDPConfigChanged {
|
|
|
|
configView.AppendEvent(iam_model.IDPProviderTypeSystem, event)
|
|
|
|
} else {
|
|
|
|
configView.AppendEvent(iam_model.IDPProviderTypeOrg, event)
|
|
|
|
}
|
2020-12-02 07:50:59 +00:00
|
|
|
exterinalIDPs, err := i.view.ExternalIDPsByIDPConfigID(configView.IDPConfigID)
|
2020-09-18 11:26:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if event.AggregateType == iam_es_model.IAMAggregate {
|
2020-12-02 07:50:59 +00:00
|
|
|
config, err = i.iamEvents.GetIDPConfig(context.Background(), event.AggregateID, configView.IDPConfigID)
|
2020-09-18 11:26:28 +00:00
|
|
|
} else {
|
2020-12-02 07:50:59 +00:00
|
|
|
config, err = i.orgEvents.GetIDPConfig(context.Background(), event.AggregateID, configView.IDPConfigID)
|
2020-09-18 11:26:28 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, provider := range exterinalIDPs {
|
2020-12-02 07:50:59 +00:00
|
|
|
i.fillConfigData(provider, config)
|
2020-09-18 11:26:28 +00:00
|
|
|
}
|
2020-12-02 07:50:59 +00:00
|
|
|
return i.view.PutExternalIDPs(event.Sequence, event.CreationDate, exterinalIDPs...)
|
2020-09-18 11:26:28 +00:00
|
|
|
default:
|
2020-12-02 07:50:59 +00:00
|
|
|
return i.view.ProcessedExternalIDPSequence(event.Sequence, event.CreationDate)
|
2020-09-18 11:26:28 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-12-02 07:50:59 +00:00
|
|
|
func (i *ExternalIDP) fillData(externalIDP *usr_view_model.ExternalIDPView) error {
|
|
|
|
config, err := i.orgEvents.GetIDPConfig(context.Background(), externalIDP.ResourceOwner, externalIDP.IDPConfigID)
|
2020-09-18 11:26:28 +00:00
|
|
|
if caos_errs.IsNotFound(err) {
|
2020-12-02 07:50:59 +00:00
|
|
|
config, err = i.iamEvents.GetIDPConfig(context.Background(), i.systemDefaults.IamID, externalIDP.IDPConfigID)
|
2020-09-18 11:26:28 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-12-02 07:50:59 +00:00
|
|
|
i.fillConfigData(externalIDP, config)
|
2020-09-18 11:26:28 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-12-02 07:50:59 +00:00
|
|
|
func (i *ExternalIDP) fillConfigData(externalIDP *usr_view_model.ExternalIDPView, config *iam_model.IDPConfig) {
|
2020-09-18 11:26:28 +00:00
|
|
|
externalIDP.IDPName = config.Name
|
|
|
|
}
|
|
|
|
|
2020-12-02 07:50:59 +00:00
|
|
|
func (i *ExternalIDP) OnError(event *models.Event, err error) error {
|
2020-09-18 11:26:28 +00:00
|
|
|
logging.LogWithFields("SPOOL-4Rsu8", "id", event.AggregateID).WithError(err).Warn("something went wrong in idp provider handler")
|
2020-12-02 07:50:59 +00:00
|
|
|
return spooler.HandleError(event, err, i.view.GetLatestExternalIDPFailedEvent, i.view.ProcessedExternalIDPFailedEvent, i.view.ProcessedExternalIDPSequence, i.errorCountUntilSkip)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *ExternalIDP) OnSuccess() error {
|
|
|
|
return spooler.HandleSuccess(i.view.UpdateExternalIDPSpoolerRunTimestamp)
|
2020-09-18 11:26:28 +00:00
|
|
|
}
|