2020-09-18 11:26:28 +00:00
|
|
|
package handler
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-01-21 07:52:12 +00:00
|
|
|
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/logging"
|
|
|
|
|
|
|
|
"github.com/zitadel/zitadel/internal/config/systemdefaults"
|
|
|
|
caos_errs "github.com/zitadel/zitadel/internal/errors"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
|
|
|
v1 "github.com/zitadel/zitadel/internal/eventstore/v1"
|
|
|
|
es_models "github.com/zitadel/zitadel/internal/eventstore/v1/models"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore/v1/query"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore/v1/spooler"
|
|
|
|
iam_model "github.com/zitadel/zitadel/internal/iam/model"
|
|
|
|
iam_view_model "github.com/zitadel/zitadel/internal/iam/repository/view/model"
|
|
|
|
query2 "github.com/zitadel/zitadel/internal/query"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/instance"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/org"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/user"
|
|
|
|
usr_view_model "github.com/zitadel/zitadel/internal/user/repository/view/model"
|
2020-09-18 11:26:28 +00:00
|
|
|
)
|
|
|
|
|
2020-12-18 15:47:45 +00:00
|
|
|
const (
|
|
|
|
externalIDPTable = "auth.user_external_idps"
|
|
|
|
)
|
|
|
|
|
2020-09-18 11:26:28 +00:00
|
|
|
type ExternalIDP struct {
|
|
|
|
handler
|
|
|
|
systemDefaults systemdefaults.SystemDefaults
|
2021-02-23 14:13:04 +00:00
|
|
|
subscription *v1.Subscription
|
2022-01-21 07:52:12 +00:00
|
|
|
queries *query2.Queries
|
2020-09-18 11:26:28 +00:00
|
|
|
}
|
|
|
|
|
2020-12-18 15:47:45 +00:00
|
|
|
func newExternalIDP(
|
|
|
|
handler handler,
|
|
|
|
defaults systemdefaults.SystemDefaults,
|
2022-01-21 07:52:12 +00:00
|
|
|
queries *query2.Queries,
|
2020-12-18 15:47:45 +00:00
|
|
|
) *ExternalIDP {
|
|
|
|
h := &ExternalIDP{
|
|
|
|
handler: handler,
|
|
|
|
systemDefaults: defaults,
|
2022-01-21 07:52:12 +00:00
|
|
|
queries: queries,
|
2020-12-18 15:47:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
h.subscribe()
|
|
|
|
|
|
|
|
return h
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *ExternalIDP) subscribe() {
|
|
|
|
i.subscription = i.es.Subscribe(i.AggregateTypes()...)
|
|
|
|
go func() {
|
|
|
|
for event := range i.subscription.Events {
|
|
|
|
query.ReduceEvent(i, event)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
2020-09-18 11:26:28 +00:00
|
|
|
|
2020-12-02 07:50:59 +00:00
|
|
|
func (i *ExternalIDP) ViewModel() string {
|
2020-09-18 11:26:28 +00:00
|
|
|
return externalIDPTable
|
|
|
|
}
|
|
|
|
|
2021-07-06 11:36:35 +00:00
|
|
|
func (i *ExternalIDP) Subscription() *v1.Subscription {
|
|
|
|
return i.subscription
|
|
|
|
}
|
|
|
|
|
2020-12-18 15:47:45 +00:00
|
|
|
func (_ *ExternalIDP) AggregateTypes() []es_models.AggregateType {
|
2022-03-31 09:36:26 +00:00
|
|
|
return []es_models.AggregateType{user.AggregateType, instance.AggregateType, org.AggregateType}
|
2020-12-18 15:47:45 +00:00
|
|
|
}
|
|
|
|
|
2022-04-19 06:26:12 +00:00
|
|
|
func (i *ExternalIDP) CurrentSequence(instanceID string) (uint64, error) {
|
|
|
|
sequence, err := i.view.GetLatestExternalIDPSequence(instanceID)
|
2020-12-18 15:47:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return sequence.CurrentSequence, nil
|
|
|
|
}
|
|
|
|
|
2022-07-22 10:08:39 +00:00
|
|
|
func (i *ExternalIDP) EventQuery(instanceIDs ...string) (*es_models.SearchQuery, error) {
|
|
|
|
sequences, err := i.view.GetLatestExternalIDPSequences(instanceIDs...)
|
2020-09-18 11:26:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-07-22 10:08:39 +00:00
|
|
|
return newSearchQuery(sequences, i.AggregateTypes(), instanceIDs), nil
|
2020-09-18 11:26:28 +00:00
|
|
|
}
|
|
|
|
|
2021-02-23 14:13:04 +00:00
|
|
|
func (i *ExternalIDP) Reduce(event *es_models.Event) (err error) {
|
2020-09-18 11:26:28 +00:00
|
|
|
switch event.AggregateType {
|
2022-03-31 09:36:26 +00:00
|
|
|
case user.AggregateType:
|
2020-12-02 07:50:59 +00:00
|
|
|
err = i.processUser(event)
|
2022-03-31 09:36:26 +00:00
|
|
|
case instance.AggregateType, org.AggregateType:
|
2020-12-02 07:50:59 +00:00
|
|
|
err = i.processIdpConfig(event)
|
2020-09-18 11:26:28 +00:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-02-23 14:13:04 +00:00
|
|
|
func (i *ExternalIDP) processUser(event *es_models.Event) (err error) {
|
2020-09-18 11:26:28 +00:00
|
|
|
externalIDP := new(usr_view_model.ExternalIDPView)
|
2022-03-31 09:36:26 +00:00
|
|
|
switch eventstore.EventType(event.Type) {
|
|
|
|
case user.UserIDPLinkAddedType:
|
2020-09-18 11:26:28 +00:00
|
|
|
err = externalIDP.AppendEvent(event)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-12-02 07:50:59 +00:00
|
|
|
err = i.fillData(externalIDP)
|
2022-03-31 09:36:26 +00:00
|
|
|
case user.UserIDPLinkRemovedType, user.UserIDPLinkCascadeRemovedType:
|
2020-09-18 11:26:28 +00:00
|
|
|
err = externalIDP.SetData(event)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-04-19 06:26:12 +00:00
|
|
|
return i.view.DeleteExternalIDP(externalIDP.ExternalUserID, externalIDP.IDPConfigID, externalIDP.InstanceID, event)
|
2022-03-31 09:36:26 +00:00
|
|
|
case user.UserRemovedType:
|
2022-04-19 06:26:12 +00:00
|
|
|
return i.view.DeleteExternalIDPsByUserID(event.AggregateID, event.InstanceID, event)
|
2020-09-18 11:26:28 +00:00
|
|
|
default:
|
2020-12-18 15:47:45 +00:00
|
|
|
return i.view.ProcessedExternalIDPSequence(event)
|
2020-09-18 11:26:28 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-12-18 15:47:45 +00:00
|
|
|
return i.view.PutExternalIDP(externalIDP, event)
|
2020-09-18 11:26:28 +00:00
|
|
|
}
|
|
|
|
|
2021-02-23 14:13:04 +00:00
|
|
|
func (i *ExternalIDP) processIdpConfig(event *es_models.Event) (err error) {
|
2022-03-31 09:36:26 +00:00
|
|
|
switch eventstore.EventType(event.Type) {
|
|
|
|
case instance.IDPConfigChangedEventType, org.IDPConfigChangedEventType:
|
2020-10-02 06:02:09 +00:00
|
|
|
configView := new(iam_view_model.IDPConfigView)
|
2022-01-21 07:52:12 +00:00
|
|
|
config := new(query2.IDP)
|
2022-03-31 09:36:26 +00:00
|
|
|
if eventstore.EventType(event.Type) == instance.IDPConfigChangedEventType {
|
2020-10-02 06:02:09 +00:00
|
|
|
configView.AppendEvent(iam_model.IDPProviderTypeSystem, event)
|
|
|
|
} else {
|
|
|
|
configView.AppendEvent(iam_model.IDPProviderTypeOrg, event)
|
|
|
|
}
|
2022-04-19 06:26:12 +00:00
|
|
|
exterinalIDPs, err := i.view.ExternalIDPsByIDPConfigID(configView.IDPConfigID, configView.InstanceID)
|
2020-09-18 11:26:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-03-31 09:36:26 +00:00
|
|
|
if event.AggregateType == instance.AggregateType {
|
2022-03-28 08:05:09 +00:00
|
|
|
config, err = i.getDefaultIDPConfig(event.InstanceID, configView.IDPConfigID)
|
2020-09-18 11:26:28 +00:00
|
|
|
} else {
|
2022-03-28 08:05:09 +00:00
|
|
|
config, err = i.getOrgIDPConfig(event.InstanceID, 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-18 15:47:45 +00:00
|
|
|
return i.view.PutExternalIDPs(event, exterinalIDPs...)
|
2020-09-18 11:26:28 +00:00
|
|
|
default:
|
2020-12-18 15:47:45 +00:00
|
|
|
return i.view.ProcessedExternalIDPSequence(event)
|
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 {
|
2022-03-28 08:05:09 +00:00
|
|
|
config, err := i.getOrgIDPConfig(externalIDP.InstanceID, externalIDP.ResourceOwner, externalIDP.IDPConfigID)
|
2020-09-18 11:26:28 +00:00
|
|
|
if caos_errs.IsNotFound(err) {
|
2022-03-28 08:05:09 +00:00
|
|
|
config, err = i.getDefaultIDPConfig(externalIDP.InstanceID, 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
|
|
|
|
}
|
|
|
|
|
2022-01-21 07:52:12 +00:00
|
|
|
func (i *ExternalIDP) fillConfigData(externalIDP *usr_view_model.ExternalIDPView, config *query2.IDP) {
|
2020-09-18 11:26:28 +00:00
|
|
|
externalIDP.IDPName = config.Name
|
|
|
|
}
|
|
|
|
|
2021-02-23 14:13:04 +00:00
|
|
|
func (i *ExternalIDP) OnError(event *es_models.Event, err error) error {
|
2022-03-31 09:36:26 +00:00
|
|
|
logging.WithFields("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
|
|
|
}
|
2021-02-22 13:08:47 +00:00
|
|
|
|
2022-03-28 08:05:09 +00:00
|
|
|
func (i *ExternalIDP) getOrgIDPConfig(instanceID, aggregateID, idpConfigID string) (*query2.IDP, error) {
|
2022-06-14 05:51:00 +00:00
|
|
|
return i.queries.IDPByIDAndResourceOwner(withInstanceID(context.Background(), instanceID), false, idpConfigID, aggregateID)
|
2021-02-22 13:08:47 +00:00
|
|
|
}
|
|
|
|
|
2022-03-28 08:05:09 +00:00
|
|
|
func (i *ExternalIDP) getDefaultIDPConfig(instanceID, idpConfigID string) (*query2.IDP, error) {
|
2022-06-14 05:51:00 +00:00
|
|
|
return i.queries.IDPByIDAndResourceOwner(withInstanceID(context.Background(), instanceID), false, idpConfigID, instanceID)
|
2021-02-22 13:08:47 +00:00
|
|
|
}
|