mirror of
https://github.com/zitadel/zitadel.git
synced 2024-12-15 12:27:59 +00:00
3f345b1ade
* fix: org tests * fix: org tests * fix: user grant test * fix: user grant test * fix: project and project role test * fix: project grant test * fix: project grant test * fix: project member, grant member, app changed tests * fix: application tests * fix: application tests * fix: add oidc app test * fix: add oidc app test * fix: add api keys test * fix: iam policies * fix: iam and org member tests * fix: idp config tests * fix: iam tests * fix: user tests * fix: user tests * fix: user tests * fix: user tests * fix: user tests * fix: user tests * fix: user tests * fix: user tests * fix: user tests * fix: user tests * fix: org domain test * fix: org tests * fix: org tests * fix: implement org idps * fix: pr requests * fix: email tests * fix: fix idp check * fix: fix user profile
239 lines
7.3 KiB
Go
239 lines
7.3 KiB
Go
package handler
|
|
|
|
import (
|
|
"context"
|
|
"github.com/caos/zitadel/internal/domain"
|
|
"github.com/caos/zitadel/internal/eventstore/v1"
|
|
es_sdk "github.com/caos/zitadel/internal/eventstore/v1/sdk"
|
|
iam_view "github.com/caos/zitadel/internal/iam/repository/view"
|
|
org_model "github.com/caos/zitadel/internal/org/model"
|
|
"github.com/caos/zitadel/internal/org/repository/view"
|
|
|
|
"github.com/caos/logging"
|
|
|
|
"github.com/caos/zitadel/internal/config/systemdefaults"
|
|
caos_errs "github.com/caos/zitadel/internal/errors"
|
|
es_models "github.com/caos/zitadel/internal/eventstore/v1/models"
|
|
"github.com/caos/zitadel/internal/eventstore/v1/query"
|
|
"github.com/caos/zitadel/internal/eventstore/v1/spooler"
|
|
iam_model "github.com/caos/zitadel/internal/iam/model"
|
|
iam_es_model "github.com/caos/zitadel/internal/iam/repository/eventsourcing/model"
|
|
iam_view_model "github.com/caos/zitadel/internal/iam/repository/view/model"
|
|
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"
|
|
)
|
|
|
|
const (
|
|
externalIDPTable = "management.user_external_idps"
|
|
)
|
|
|
|
type ExternalIDP struct {
|
|
handler
|
|
systemDefaults systemdefaults.SystemDefaults
|
|
subscription *v1.Subscription
|
|
}
|
|
|
|
func newExternalIDP(
|
|
handler handler,
|
|
systemDefaults systemdefaults.SystemDefaults,
|
|
) *ExternalIDP {
|
|
h := &ExternalIDP{
|
|
handler: handler,
|
|
systemDefaults: systemDefaults,
|
|
}
|
|
|
|
h.subscribe()
|
|
|
|
return h
|
|
}
|
|
|
|
func (m *ExternalIDP) subscribe() {
|
|
m.subscription = m.es.Subscribe(m.AggregateTypes()...)
|
|
go func() {
|
|
for event := range m.subscription.Events {
|
|
query.ReduceEvent(m, event)
|
|
}
|
|
}()
|
|
}
|
|
|
|
func (i *ExternalIDP) ViewModel() string {
|
|
return externalIDPTable
|
|
}
|
|
|
|
func (_ *ExternalIDP) AggregateTypes() []es_models.AggregateType {
|
|
return []es_models.AggregateType{model.UserAggregate, iam_es_model.IAMAggregate, org_es_model.OrgAggregate}
|
|
}
|
|
|
|
func (i *ExternalIDP) CurrentSequence() (uint64, error) {
|
|
sequence, err := i.view.GetLatestExternalIDPSequence()
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return sequence.CurrentSequence, nil
|
|
}
|
|
|
|
func (i *ExternalIDP) EventQuery() (*es_models.SearchQuery, error) {
|
|
sequence, err := i.view.GetLatestExternalIDPSequence()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return es_models.NewSearchQuery().
|
|
AggregateTypeFilter(i.AggregateTypes()...).
|
|
LatestSequenceFilter(sequence.CurrentSequence), nil
|
|
}
|
|
|
|
func (i *ExternalIDP) Reduce(event *es_models.Event) (err error) {
|
|
switch event.AggregateType {
|
|
case model.UserAggregate:
|
|
err = i.processUser(event)
|
|
case iam_es_model.IAMAggregate, org_es_model.OrgAggregate:
|
|
err = i.processIdpConfig(event)
|
|
}
|
|
return err
|
|
}
|
|
|
|
func (i *ExternalIDP) processUser(event *es_models.Event) (err error) {
|
|
externalIDP := new(usr_view_model.ExternalIDPView)
|
|
switch event.Type {
|
|
case model.HumanExternalIDPAdded:
|
|
err = externalIDP.AppendEvent(event)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
err = i.fillData(externalIDP)
|
|
case model.HumanExternalIDPRemoved, model.HumanExternalIDPCascadeRemoved:
|
|
err = externalIDP.SetData(event)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return i.view.DeleteExternalIDP(externalIDP.ExternalUserID, externalIDP.IDPConfigID, event)
|
|
case model.UserRemoved:
|
|
return i.view.DeleteExternalIDPsByUserID(event.AggregateID, event)
|
|
default:
|
|
return i.view.ProcessedExternalIDPSequence(event)
|
|
}
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return i.view.PutExternalIDP(externalIDP, event)
|
|
}
|
|
|
|
func (i *ExternalIDP) processIdpConfig(event *es_models.Event) (err error) {
|
|
switch event.Type {
|
|
case iam_es_model.IDPConfigChanged, org_es_model.IDPConfigChanged:
|
|
configView := new(iam_view_model.IDPConfigView)
|
|
config := new(iam_model.IDPConfig)
|
|
if event.Type == iam_es_model.IDPConfigChanged {
|
|
configView.AppendEvent(iam_model.IDPProviderTypeSystem, event)
|
|
} else {
|
|
configView.AppendEvent(iam_model.IDPProviderTypeOrg, event)
|
|
}
|
|
exterinalIDPs, err := i.view.ExternalIDPsByIDPConfigID(configView.IDPConfigID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if event.AggregateType == iam_es_model.IAMAggregate {
|
|
config, err = i.getDefaultIDPConfig(context.Background(), configView.IDPConfigID)
|
|
} else {
|
|
config, err = i.getOrgIDPConfig(context.Background(), event.AggregateID, configView.IDPConfigID)
|
|
}
|
|
if err != nil {
|
|
return err
|
|
}
|
|
for _, provider := range exterinalIDPs {
|
|
i.fillConfigData(provider, config)
|
|
}
|
|
return i.view.PutExternalIDPs(event, exterinalIDPs...)
|
|
default:
|
|
return i.view.ProcessedExternalIDPSequence(event)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (i *ExternalIDP) fillData(externalIDP *usr_view_model.ExternalIDPView) error {
|
|
config, err := i.getOrgIDPConfig(context.Background(), externalIDP.ResourceOwner, externalIDP.IDPConfigID)
|
|
if caos_errs.IsNotFound(err) {
|
|
config, err = i.getDefaultIDPConfig(context.Background(), externalIDP.IDPConfigID)
|
|
}
|
|
if err != nil {
|
|
return err
|
|
}
|
|
i.fillConfigData(externalIDP, config)
|
|
return nil
|
|
}
|
|
|
|
func (i *ExternalIDP) fillConfigData(externalIDP *usr_view_model.ExternalIDPView, config *iam_model.IDPConfig) {
|
|
externalIDP.IDPName = config.Name
|
|
}
|
|
|
|
func (i *ExternalIDP) OnError(event *es_models.Event, err error) error {
|
|
logging.LogWithFields("SPOOL-4Rsu8", "id", event.AggregateID).WithError(err).Warn("something went wrong in idp provider handler")
|
|
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)
|
|
}
|
|
|
|
func (i *ExternalIDP) getOrgIDPConfig(ctx context.Context, aggregateID, idpConfigID string) (*iam_model.IDPConfig, error) {
|
|
existing, err := i.getOrgByID(ctx, aggregateID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if _, i := existing.GetIDP(idpConfigID); i != nil {
|
|
return i, nil
|
|
}
|
|
return nil, caos_errs.ThrowNotFound(nil, "EVENT-22n7G", "Errors.IDP.NotExisting")
|
|
}
|
|
|
|
func (i *ExternalIDP) getOrgByID(ctx context.Context, orgID string) (*org_model.Org, error) {
|
|
query, err := view.OrgByIDQuery(orgID, 0)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
esOrg := &org_es_model.Org{
|
|
ObjectRoot: es_models.ObjectRoot{
|
|
AggregateID: orgID,
|
|
},
|
|
}
|
|
err = es_sdk.Filter(ctx, i.Eventstore().FilterEvents, esOrg.AppendEvents, query)
|
|
if err != nil && !caos_errs.IsNotFound(err) {
|
|
return nil, err
|
|
}
|
|
if esOrg.Sequence == 0 {
|
|
return nil, caos_errs.ThrowNotFound(nil, "EVENT-4m0fs", "Errors.Org.NotFound")
|
|
}
|
|
|
|
return org_es_model.OrgToModel(esOrg), nil
|
|
}
|
|
|
|
func (u *ExternalIDP) getIAMByID(ctx context.Context) (*iam_model.IAM, error) {
|
|
query, err := iam_view.IAMByIDQuery(domain.IAMID, 0)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
iam := &iam_es_model.IAM{
|
|
ObjectRoot: es_models.ObjectRoot{
|
|
AggregateID: domain.IAMID,
|
|
},
|
|
}
|
|
err = es_sdk.Filter(ctx, u.Eventstore().FilterEvents, iam.AppendEvents, query)
|
|
if err != nil && caos_errs.IsNotFound(err) && iam.Sequence == 0 {
|
|
return nil, err
|
|
}
|
|
return iam_es_model.IAMToModel(iam), nil
|
|
}
|
|
|
|
func (u *ExternalIDP) getDefaultIDPConfig(ctx context.Context, idpConfigID string) (*iam_model.IDPConfig, error) {
|
|
existing, err := u.getIAMByID(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if _, existingIDP := existing.GetIDP(idpConfigID); existingIDP != nil {
|
|
return existingIDP, nil
|
|
}
|
|
return nil, caos_errs.ThrowNotFound(nil, "EVENT-22Nv8", "Errors.IAM.IdpNotExisting")
|
|
}
|