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"
|
2022-04-19 06:26:12 +00:00
|
|
|
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/config/systemdefaults"
|
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
|
|
|
v1 "github.com/zitadel/zitadel/internal/eventstore/v1"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore/v1/models"
|
|
|
|
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"
|
2020-09-18 11:26:28 +00:00
|
|
|
)
|
|
|
|
|
2020-12-18 15:47:45 +00:00
|
|
|
const (
|
|
|
|
idpProviderTable = "auth.idp_providers"
|
|
|
|
)
|
|
|
|
|
2020-09-18 11:26:28 +00:00
|
|
|
type IDPProvider 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 newIDPProvider(
|
|
|
|
h handler,
|
|
|
|
defaults systemdefaults.SystemDefaults,
|
2022-01-21 07:52:12 +00:00
|
|
|
queries *query2.Queries,
|
2020-12-18 15:47:45 +00:00
|
|
|
) *IDPProvider {
|
|
|
|
idpProvider := &IDPProvider{
|
|
|
|
handler: h,
|
|
|
|
systemDefaults: defaults,
|
2022-01-21 07:52:12 +00:00
|
|
|
queries: queries,
|
2020-12-18 15:47:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
idpProvider.subscribe()
|
|
|
|
|
|
|
|
return idpProvider
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *IDPProvider) 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 *IDPProvider) ViewModel() string {
|
2020-09-18 11:26:28 +00:00
|
|
|
return idpProviderTable
|
|
|
|
}
|
|
|
|
|
2021-07-06 11:36:35 +00:00
|
|
|
func (i *IDPProvider) Subscription() *v1.Subscription {
|
|
|
|
return i.subscription
|
|
|
|
}
|
|
|
|
|
2022-03-31 09:36:26 +00:00
|
|
|
func (_ *IDPProvider) AggregateTypes() []models.AggregateType {
|
|
|
|
return []es_models.AggregateType{instance.AggregateType, org.AggregateType}
|
2020-12-18 15:47:45 +00:00
|
|
|
}
|
|
|
|
|
2022-04-19 06:26:12 +00:00
|
|
|
func (i *IDPProvider) CurrentSequence(instanceID string) (uint64, error) {
|
|
|
|
sequence, err := i.view.GetLatestIDPProviderSequence(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 *IDPProvider) EventQuery(instanceIDs ...string) (*models.SearchQuery, error) {
|
|
|
|
sequences, err := i.view.GetLatestIDPProviderSequences(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
|
|
|
}
|
|
|
|
|
2022-03-31 09:36:26 +00:00
|
|
|
func (i *IDPProvider) Reduce(event *models.Event) (err error) {
|
2020-09-18 11:26:28 +00:00
|
|
|
switch event.AggregateType {
|
2022-03-31 09:36:26 +00:00
|
|
|
case instance.AggregateType, org.AggregateType:
|
2020-12-02 07:50:59 +00:00
|
|
|
err = i.processIdpProvider(event)
|
2020-09-18 11:26:28 +00:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-03-31 09:36:26 +00:00
|
|
|
func (i *IDPProvider) processIdpProvider(event *models.Event) (err error) {
|
2020-09-18 11:26:28 +00:00
|
|
|
provider := new(iam_view_model.IDPProviderView)
|
2022-03-31 09:36:26 +00:00
|
|
|
switch eventstore.EventType(event.Type) {
|
|
|
|
case instance.LoginPolicyIDPProviderAddedEventType, org.LoginPolicyIDPProviderAddedEventType:
|
2020-09-18 11:26:28 +00:00
|
|
|
err = provider.AppendEvent(event)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-12-02 07:50:59 +00:00
|
|
|
err = i.fillData(provider)
|
2022-03-31 09:36:26 +00:00
|
|
|
case instance.LoginPolicyIDPProviderRemovedEventType, instance.LoginPolicyIDPProviderCascadeRemovedEventType,
|
|
|
|
org.LoginPolicyIDPProviderRemovedEventType, org.LoginPolicyIDPProviderCascadeRemovedEventType:
|
2020-09-18 11:26:28 +00:00
|
|
|
err = provider.SetData(event)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-04-19 06:26:12 +00:00
|
|
|
return i.view.DeleteIDPProvider(event.AggregateID, provider.IDPConfigID, event.InstanceID, event)
|
2022-03-31 09:36:26 +00:00
|
|
|
case instance.IDPConfigChangedEventType, org.IDPConfigChangedEventType:
|
2020-10-20 12:26:19 +00:00
|
|
|
esConfig := new(iam_view_model.IDPConfigView)
|
|
|
|
providerType := iam_model.IDPProviderTypeSystem
|
2022-04-05 05:58:09 +00:00
|
|
|
if event.AggregateID != event.InstanceID {
|
2020-10-20 12:26:19 +00:00
|
|
|
providerType = iam_model.IDPProviderTypeOrg
|
|
|
|
}
|
2022-06-10 13:17:33 +00:00
|
|
|
err = esConfig.AppendEvent(providerType, event)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
providers, err := i.view.IDPProvidersByIDPConfigID(esConfig.IDPConfigID, event.InstanceID)
|
2020-09-18 11:26:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-01-21 07:52:12 +00:00
|
|
|
config := new(query2.IDP)
|
2022-04-05 05:58:09 +00:00
|
|
|
if event.AggregateID == event.InstanceID {
|
2022-03-28 08:05:09 +00:00
|
|
|
config, err = i.getDefaultIDPConfig(event.InstanceID, esConfig.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, esConfig.IDPConfigID)
|
2020-09-18 11:26:28 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, provider := range providers {
|
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.PutIDPProviders(event, providers...)
|
2022-03-31 09:36:26 +00:00
|
|
|
case org.LoginPolicyRemovedEventType:
|
2022-04-19 06:26:12 +00:00
|
|
|
return i.view.DeleteIDPProvidersByAggregateID(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.ProcessedIDPProviderSequence(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.PutIDPProvider(provider, event)
|
2020-09-18 11:26:28 +00:00
|
|
|
}
|
|
|
|
|
2020-12-02 07:50:59 +00:00
|
|
|
func (i *IDPProvider) fillData(provider *iam_view_model.IDPProviderView) (err error) {
|
2022-01-21 07:52:12 +00:00
|
|
|
var config *query2.IDP
|
2020-09-18 11:26:28 +00:00
|
|
|
if provider.IDPProviderType == int32(iam_model.IDPProviderTypeSystem) {
|
2022-03-28 08:05:09 +00:00
|
|
|
config, err = i.getDefaultIDPConfig(provider.InstanceID, provider.IDPConfigID)
|
2020-09-18 11:26:28 +00:00
|
|
|
} else {
|
2022-03-28 08:05:09 +00:00
|
|
|
config, err = i.getOrgIDPConfig(provider.InstanceID, provider.AggregateID, provider.IDPConfigID)
|
2020-09-18 11:26:28 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-12-02 07:50:59 +00:00
|
|
|
i.fillConfigData(provider, config)
|
2020-09-18 11:26:28 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-01-21 07:52:12 +00:00
|
|
|
func (i *IDPProvider) fillConfigData(provider *iam_view_model.IDPProviderView, config *query2.IDP) {
|
2020-09-18 11:26:28 +00:00
|
|
|
provider.Name = config.Name
|
2020-10-19 15:10:02 +00:00
|
|
|
provider.StylingType = int32(config.StylingType)
|
2022-01-21 07:52:12 +00:00
|
|
|
if config.OIDCIDP != nil {
|
|
|
|
provider.IDPConfigType = int32(domain.IDPConfigTypeOIDC)
|
|
|
|
} else if config.JWTIDP != nil {
|
|
|
|
provider.IDPConfigType = int32(domain.IDPConfigTypeJWT)
|
|
|
|
}
|
2022-02-01 17:12:18 +00:00
|
|
|
switch config.State {
|
|
|
|
case domain.IDPConfigStateActive:
|
|
|
|
provider.IDPState = int32(iam_model.IDPConfigStateActive)
|
|
|
|
case domain.IDPConfigStateInactive:
|
|
|
|
provider.IDPState = int32(iam_model.IDPConfigStateActive)
|
|
|
|
case domain.IDPConfigStateRemoved:
|
|
|
|
provider.IDPState = int32(iam_model.IDPConfigStateRemoved)
|
|
|
|
default:
|
|
|
|
provider.IDPState = int32(iam_model.IDPConfigStateActive)
|
|
|
|
}
|
2020-09-18 11:26:28 +00:00
|
|
|
}
|
|
|
|
|
2021-02-23 14:13:04 +00:00
|
|
|
func (i *IDPProvider) OnError(event *es_models.Event, err error) error {
|
2022-04-05 05:58:09 +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.GetLatestIDPProviderFailedEvent, i.view.ProcessedIDPProviderFailedEvent, i.view.ProcessedIDPProviderSequence, i.errorCountUntilSkip)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *IDPProvider) OnSuccess() error {
|
|
|
|
return spooler.HandleSuccess(i.view.UpdateIDPProviderSpoolerRunTimestamp)
|
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 *IDPProvider) 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 (u *IDPProvider) getDefaultIDPConfig(instanceID, idpConfigID string) (*query2.IDP, error) {
|
2022-06-14 05:51:00 +00:00
|
|
|
return u.queries.IDPByIDAndResourceOwner(withInstanceID(context.Background(), instanceID), false, idpConfigID, instanceID)
|
2021-02-22 13:08:47 +00:00
|
|
|
}
|