fix: idp provider

This commit is contained in:
Fabiennne 2020-12-15 12:00:53 +01:00
parent 6532072288
commit 762941f0ea
13 changed files with 82 additions and 192 deletions

View File

@ -273,8 +273,8 @@ func writeModelToIDPOIDCConfig(wm *oidc.ConfigWriteModel) *model.OIDCIDPConfig {
func writeModelToIDPProvider(wm *idpprovider.WriteModel) *model.IDPProvider { func writeModelToIDPProvider(wm *idpprovider.WriteModel) *model.IDPProvider {
return &model.IDPProvider{ return &model.IDPProvider{
ObjectRoot: writeModelToObjectRoot(wm.WriteModel), ObjectRoot: writeModelToObjectRoot(wm.WriteModel.WriteModel),
IDPConfigID: wm.Provider.IDPConfigID, IDPConfigID: wm.IDPConfigID,
Type: model.IDPProviderType(wm.Provider.IDPProviderType), Type: model.IDPProviderType(wm.IDPProviderType),
} }
} }

View File

@ -9,9 +9,9 @@ import (
iam_login "github.com/caos/zitadel/internal/v2/repository/iam/policy/login" iam_login "github.com/caos/zitadel/internal/v2/repository/iam/policy/login"
iam_factor "github.com/caos/zitadel/internal/v2/repository/iam/policy/login/factors" iam_factor "github.com/caos/zitadel/internal/v2/repository/iam/policy/login/factors"
"github.com/caos/zitadel/internal/v2/repository/iam/policy/login/idpprovider" "github.com/caos/zitadel/internal/v2/repository/iam/policy/login/idpprovider"
"github.com/caos/zitadel/internal/v2/repository/idp/provider"
"github.com/caos/zitadel/internal/v2/repository/policy/login" "github.com/caos/zitadel/internal/v2/repository/policy/login"
"github.com/caos/zitadel/internal/v2/repository/policy/login/factors" "github.com/caos/zitadel/internal/v2/repository/policy/login/factors"
idpprovider2 "github.com/caos/zitadel/internal/v2/repository/policy/login/idpprovider"
) )
func (r *Repository) AddLoginPolicy(ctx context.Context, policy *iam_model.LoginPolicy) (*iam_model.LoginPolicy, error) { func (r *Repository) AddLoginPolicy(ctx context.Context, policy *iam_model.LoginPolicy) (*iam_model.LoginPolicy, error) {
@ -67,8 +67,8 @@ func (r *Repository) AddIDPProviderToLoginPolicy(ctx context.Context, idpProvide
return nil, err return nil, err
} }
aggregate := iam_repo.AggregateFromWriteModel(&writeModel.WriteModel). aggregate := iam_repo.AggregateFromWriteModel(&writeModel.WriteModel.WriteModel).
PushLoginPolicyIDPProviderAddedEvent(ctx, idpProvider.IDPConfigID, provider.Type(idpProvider.Type)) PushLoginPolicyIDPProviderAddedEvent(ctx, idpProvider.IDPConfigID, idpprovider2.Type(idpProvider.Type))
if err = r.eventstore.PushAggregate(ctx, writeModel, aggregate); err != nil { if err = r.eventstore.PushAggregate(ctx, writeModel, aggregate); err != nil {
return nil, err return nil, err
@ -83,8 +83,8 @@ func (r *Repository) RemoveIDPProviderFromLoginPolicy(ctx context.Context, idpPr
if err != nil { if err != nil {
return err return err
} }
aggregate := iam_repo.AggregateFromWriteModel(&writeModel.WriteModel). aggregate := iam_repo.AggregateFromWriteModel(&writeModel.WriteModel.WriteModel).
PushLoginPolicyIDPProviderAddedEvent(ctx, idpProvider.IDPConfigID, provider.Type(idpProvider.Type)) PushLoginPolicyIDPProviderAddedEvent(ctx, idpProvider.IDPConfigID, idpprovider2.Type(idpProvider.Type))
return r.eventstore.PushAggregate(ctx, writeModel, aggregate) return r.eventstore.PushAggregate(ctx, writeModel, aggregate)
} }

View File

@ -15,9 +15,9 @@ import (
"github.com/caos/zitadel/internal/v2/repository/iam/policy/password_lockout" "github.com/caos/zitadel/internal/v2/repository/iam/policy/password_lockout"
"github.com/caos/zitadel/internal/v2/repository/idp" "github.com/caos/zitadel/internal/v2/repository/idp"
"github.com/caos/zitadel/internal/v2/repository/idp/oidc" "github.com/caos/zitadel/internal/v2/repository/idp/oidc"
"github.com/caos/zitadel/internal/v2/repository/idp/provider"
"github.com/caos/zitadel/internal/v2/repository/policy/login" "github.com/caos/zitadel/internal/v2/repository/policy/login"
"github.com/caos/zitadel/internal/v2/repository/policy/login/factors" "github.com/caos/zitadel/internal/v2/repository/policy/login/factors"
idpprovider2 "github.com/caos/zitadel/internal/v2/repository/policy/login/idpprovider"
) )
const ( const (
@ -283,7 +283,7 @@ func (a *Aggregate) PushIDPOIDCConfigChanged(
func (a *Aggregate) PushLoginPolicyIDPProviderAddedEvent( func (a *Aggregate) PushLoginPolicyIDPProviderAddedEvent(
ctx context.Context, ctx context.Context,
idpConfigID string, idpConfigID string,
providerType provider.Type, providerType idpprovider2.Type,
) *Aggregate { ) *Aggregate {
a.Aggregate = *a.PushEvents(idpprovider.NewAddedEvent(ctx, idpConfigID, providerType)) a.Aggregate = *a.PushEvents(idpprovider.NewAddedEvent(ctx, idpConfigID, providerType))
@ -293,7 +293,7 @@ func (a *Aggregate) PushLoginPolicyIDPProviderAddedEvent(
func (a *Aggregate) PushLoginPolicyIDPProviderRemovedEvent( func (a *Aggregate) PushLoginPolicyIDPProviderRemovedEvent(
ctx context.Context, ctx context.Context,
idpConfigID string, idpConfigID string,
providerType provider.Type, providerType idpprovider2.Type,
) *Aggregate { ) *Aggregate {
a.Aggregate = *a.PushEvents(idpprovider.NewRemovedEvent(ctx, idpConfigID)) a.Aggregate = *a.PushEvents(idpprovider.NewRemovedEvent(ctx, idpConfigID))

View File

@ -7,7 +7,6 @@ import (
"github.com/caos/zitadel/internal/eventstore/v2" "github.com/caos/zitadel/internal/eventstore/v2"
"github.com/caos/zitadel/internal/eventstore/v2/repository" "github.com/caos/zitadel/internal/eventstore/v2/repository"
"github.com/caos/zitadel/internal/v2/repository/idp/provider"
) )
type AddedEvent struct { type AddedEvent struct {
@ -17,7 +16,7 @@ type AddedEvent struct {
func NewAddedEvent( func NewAddedEvent(
ctx context.Context, ctx context.Context,
idpConfigID string, idpConfigID string,
idpProviderType provider.Type, idpProviderType idpprovider.Type,
) *AddedEvent { ) *AddedEvent {
return &AddedEvent{ return &AddedEvent{

View File

@ -10,57 +10,39 @@ const (
) )
type WriteModel struct { type WriteModel struct {
eventstore.WriteModel idpprovider.WriteModel
Provider idpprovider.WriteModel
idpConfigID string
iamID string
IsRemoved bool IsRemoved bool
} }
func NewWriteModel(iamID, idpConfigID string) *WriteModel { func NewWriteModel(iamID, idpConfigID string) *WriteModel {
return &WriteModel{ return &WriteModel{
iamID: iamID, WriteModel: idpprovider.WriteModel{
idpConfigID: idpConfigID, WriteModel: eventstore.WriteModel{
AggregateID: iamID,
},
IDPConfigID: idpConfigID,
},
IsRemoved: false,
} }
} }
func (wm *WriteModel) AppendEvents(events ...eventstore.EventReader) { func (wm *WriteModel) AppendEvents(events ...eventstore.EventReader) {
wm.WriteModel.AppendEvents(events...)
for _, event := range events { for _, event := range events {
switch e := event.(type) { switch e := event.(type) {
case *AddedEvent: case *AddedEvent:
if e.IDPConfigID != wm.idpConfigID { if e.IDPConfigID != wm.IDPConfigID {
continue continue
} }
wm.Provider.AppendEvents(&e.AddedEvent) wm.WriteModel.AppendEvents(&e.AddedEvent)
} }
} }
} }
func (wm *WriteModel) Reduce() error { func (wm *WriteModel) Reduce() error {
for _, event := range wm.Events {
switch e := event.(type) {
case *AddedEvent:
if e.IDPConfigID != wm.idpConfigID {
continue
}
wm.IsRemoved = false
case *RemovedEvent:
if e.IDPConfigID != wm.idpConfigID {
continue
}
wm.IsRemoved = true
}
}
if err := wm.Provider.Reduce(); err != nil {
return err
}
return wm.WriteModel.Reduce() return wm.WriteModel.Reduce()
} }
func (wm *WriteModel) Query() *eventstore.SearchQueryBuilder { func (wm *WriteModel) Query() *eventstore.SearchQueryBuilder {
return eventstore.NewSearchQueryBuilder(eventstore.ColumnsEvent, AggregateType). return eventstore.NewSearchQueryBuilder(eventstore.ColumnsEvent, AggregateType).
AggregateIDs(wm.iamID) AggregateIDs(wm.AggregateID)
} }

View File

@ -3,7 +3,7 @@ package idp
import ( import (
"github.com/caos/zitadel/internal/eventstore/v2" "github.com/caos/zitadel/internal/eventstore/v2"
"github.com/caos/zitadel/internal/v2/repository/idp/oidc" "github.com/caos/zitadel/internal/v2/repository/idp/oidc"
"github.com/caos/zitadel/internal/v2/repository/idp/provider" "github.com/caos/zitadel/internal/v2/repository/policy/login/idpprovider"
) )
type ConfigReadModel struct { type ConfigReadModel struct {
@ -13,7 +13,7 @@ type ConfigReadModel struct {
ConfigID string ConfigID string
Name string Name string
StylingType StylingType StylingType StylingType
ProviderType provider.Type ProviderType idpprovider.Type
OIDCConfig *oidc.ConfigReadModel OIDCConfig *oidc.ConfigReadModel
} }

View File

@ -1,50 +0,0 @@
package provider
import (
"encoding/json"
"github.com/caos/zitadel/internal/errors"
"github.com/caos/zitadel/internal/eventstore/v2"
"github.com/caos/zitadel/internal/eventstore/v2/repository"
)
const (
AddedEventType = "idpprovider.added"
)
type AddedEvent struct {
eventstore.BaseEvent
IDPConfigID string `json:"idpConfigId"`
IDPProviderType Type `json:"idpProviderType"`
}
func (e *AddedEvent) Data() interface{} {
return e
}
func NewAddedEvent(
base *eventstore.BaseEvent,
idpConfigID string,
idpProviderType Type,
) *AddedEvent {
return &AddedEvent{
BaseEvent: *base,
IDPConfigID: idpConfigID,
IDPProviderType: idpProviderType,
}
}
func AddedEventEventMapper(event *repository.Event) (eventstore.EventReader, error) {
e := &AddedEvent{
BaseEvent: *eventstore.BaseEventFromRepo(event),
}
err := json.Unmarshal(event.Data, e)
if err != nil {
return nil, errors.ThrowInternal(err, "PROVI-bfNnp", "Errors.Internal")
}
return e, nil
}

View File

@ -1,47 +0,0 @@
package provider
import (
"encoding/json"
"github.com/caos/zitadel/internal/errors"
"github.com/caos/zitadel/internal/eventstore/v2"
"github.com/caos/zitadel/internal/eventstore/v2/repository"
)
const (
RemovedEventType = "idpprovider.removed"
)
type RemovedEvent struct {
eventstore.BaseEvent
IDPConfigID string `json:"idpConfigId"`
}
func (e *RemovedEvent) Data() interface{} {
return e
}
func NewRemovedEvent(
base *eventstore.BaseEvent,
idpConfigID string,
) *RemovedEvent {
return &RemovedEvent{
BaseEvent: *base,
IDPConfigID: idpConfigID,
}
}
func RemovedEventEventMapper(event *repository.Event) (eventstore.EventReader, error) {
e := &RemovedEvent{
BaseEvent: *eventstore.BaseEventFromRepo(event),
}
err := json.Unmarshal(event.Data, e)
if err != nil {
return nil, errors.ThrowInternal(err, "PROVI-6H0KQ", "Errors.Internal")
}
return e, nil
}

View File

@ -1,21 +0,0 @@
package provider
import "github.com/caos/zitadel/internal/eventstore/v2"
type WriteModel struct {
eventstore.WriteModel
IDPConfigID string
IDPProviderType Type
}
func (wm *WriteModel) Reduce() error {
for _, event := range wm.Events {
switch e := event.(type) {
case *AddedEvent:
wm.IDPConfigID = e.IDPConfigID
wm.IDPProviderType = e.IDPProviderType
}
}
return wm.WriteModel.Reduce()
}

View File

@ -5,7 +5,7 @@ import (
"github.com/caos/zitadel/internal/errors" "github.com/caos/zitadel/internal/errors"
"github.com/caos/zitadel/internal/eventstore/v2" "github.com/caos/zitadel/internal/eventstore/v2"
"github.com/caos/zitadel/internal/eventstore/v2/repository" "github.com/caos/zitadel/internal/eventstore/v2/repository"
"github.com/caos/zitadel/internal/v2/repository/idp/provider" "github.com/caos/zitadel/internal/v2/repository/policy/login/idpprovider"
) )
const ( const (
@ -13,8 +13,8 @@ const (
LoginPolicyAddedEventType = loginPolicyPrefix + "added" LoginPolicyAddedEventType = loginPolicyPrefix + "added"
LoginPolicyChangedEventType = loginPolicyPrefix + "changed" LoginPolicyChangedEventType = loginPolicyPrefix + "changed"
LoginPolicyRemovedEventType = loginPolicyPrefix + "removed" LoginPolicyRemovedEventType = loginPolicyPrefix + "removed"
LoginPolicyIDPProviderAddedEventType = loginPolicyPrefix + provider.AddedEventType LoginPolicyIDPProviderAddedEventType = loginPolicyPrefix + idpprovider.AddedEventType
LoginPolicyIDPProviderRemovedEventType = loginPolicyPrefix + provider.RemovedEventType LoginPolicyIDPProviderRemovedEventType = loginPolicyPrefix + idpprovider.RemovedEventType
) )
type AddedEvent struct { type AddedEvent struct {

View File

@ -1,42 +1,62 @@
package idpprovider package idpprovider
import ( import (
"encoding/json"
"github.com/caos/zitadel/internal/errors"
"github.com/caos/zitadel/internal/eventstore/v2" "github.com/caos/zitadel/internal/eventstore/v2"
"github.com/caos/zitadel/internal/eventstore/v2/repository" "github.com/caos/zitadel/internal/eventstore/v2/repository"
"github.com/caos/zitadel/internal/v2/repository/idp/provider" )
const (
AddedEventType = "idpprovider.added"
RemovedEventType = "idpprovider.removed"
) )
type AddedEvent struct { type AddedEvent struct {
provider.AddedEvent eventstore.BaseEvent
IDPConfigID string `json:"idpConfigId"`
IDPProviderType Type `json:"idpProviderType"`
}
func (e *AddedEvent) Data() interface{} {
return e
} }
func NewAddedEvent( func NewAddedEvent(
base *eventstore.BaseEvent, base *eventstore.BaseEvent,
idpConfigID string, idpConfigID string,
idpProviderType provider.Type, idpProviderType Type,
) *AddedEvent { ) *AddedEvent {
return &AddedEvent{ return &AddedEvent{
AddedEvent: *provider.NewAddedEvent( *base,
base, idpConfigID,
idpConfigID, idpProviderType,
idpProviderType),
} }
} }
func AddedEventMapper(event *repository.Event) (eventstore.EventReader, error) { func AddedEventMapper(event *repository.Event) (eventstore.EventReader, error) {
e, err := provider.AddedEventEventMapper(event) e := &AddedEvent{
if err != nil { BaseEvent: *eventstore.BaseEventFromRepo(event),
return nil, err
} }
return &AddedEvent{ err := json.Unmarshal(event.Data, e)
AddedEvent: *e.(*provider.AddedEvent), if err != nil {
}, nil return nil, errors.ThrowInternal(err, "PROVI-bfNnp", "Errors.Internal")
}
return e, nil
} }
type RemovedEvent struct { type RemovedEvent struct {
provider.RemovedEvent eventstore.BaseEvent
IDPConfigID string `json:"idpConfigId"`
}
func (e *RemovedEvent) Data() interface{} {
return e
} }
func NewRemovedEvent( func NewRemovedEvent(
@ -44,17 +64,20 @@ func NewRemovedEvent(
idpConfigID string, idpConfigID string,
) *RemovedEvent { ) *RemovedEvent {
return &RemovedEvent{ return &RemovedEvent{
RemovedEvent: *provider.NewRemovedEvent(base, idpConfigID), BaseEvent: *base,
IDPConfigID: idpConfigID,
} }
} }
func RemovedEventMapper(event *repository.Event) (eventstore.EventReader, error) { func RemovedEventMapper(event *repository.Event) (eventstore.EventReader, error) {
e, err := provider.RemovedEventEventMapper(event) e := &RemovedEvent{
if err != nil { BaseEvent: *eventstore.BaseEventFromRepo(event),
return nil, err
} }
return &RemovedEvent{ err := json.Unmarshal(event.Data, e)
RemovedEvent: *e.(*provider.RemovedEvent), if err != nil {
}, nil return nil, errors.ThrowInternal(err, "PROVI-6H0KQ", "Errors.Internal")
}
return e, nil
} }

View File

@ -1,4 +1,4 @@
package provider package idpprovider
type Type int8 type Type int8

View File

@ -2,18 +2,22 @@ package idpprovider
import ( import (
"github.com/caos/zitadel/internal/eventstore/v2" "github.com/caos/zitadel/internal/eventstore/v2"
"github.com/caos/zitadel/internal/v2/repository/idp/provider"
) )
type WriteModel struct { type WriteModel struct {
provider.WriteModel eventstore.WriteModel
IDPConfigID string
IDPProviderType Type
} }
func (wm *WriteModel) AppendEvents(events ...eventstore.EventReader) { func (wm *WriteModel) Reduce() error {
for _, event := range events { for _, event := range wm.Events {
switch e := event.(type) { switch e := event.(type) {
case *AddedEvent: case *AddedEvent:
wm.WriteModel.AppendEvents(&e.AddedEvent) wm.IDPConfigID = e.IDPConfigID
wm.IDPProviderType = e.IDPProviderType
} }
} }
return wm.WriteModel.Reduce()
} }