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 {
return &model.IDPProvider{
ObjectRoot: writeModelToObjectRoot(wm.WriteModel),
IDPConfigID: wm.Provider.IDPConfigID,
Type: model.IDPProviderType(wm.Provider.IDPProviderType),
ObjectRoot: writeModelToObjectRoot(wm.WriteModel.WriteModel),
IDPConfigID: wm.IDPConfigID,
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_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/idp/provider"
"github.com/caos/zitadel/internal/v2/repository/policy/login"
"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) {
@ -67,8 +67,8 @@ func (r *Repository) AddIDPProviderToLoginPolicy(ctx context.Context, idpProvide
return nil, err
}
aggregate := iam_repo.AggregateFromWriteModel(&writeModel.WriteModel).
PushLoginPolicyIDPProviderAddedEvent(ctx, idpProvider.IDPConfigID, provider.Type(idpProvider.Type))
aggregate := iam_repo.AggregateFromWriteModel(&writeModel.WriteModel.WriteModel).
PushLoginPolicyIDPProviderAddedEvent(ctx, idpProvider.IDPConfigID, idpprovider2.Type(idpProvider.Type))
if err = r.eventstore.PushAggregate(ctx, writeModel, aggregate); err != nil {
return nil, err
@ -83,8 +83,8 @@ func (r *Repository) RemoveIDPProviderFromLoginPolicy(ctx context.Context, idpPr
if err != nil {
return err
}
aggregate := iam_repo.AggregateFromWriteModel(&writeModel.WriteModel).
PushLoginPolicyIDPProviderAddedEvent(ctx, idpProvider.IDPConfigID, provider.Type(idpProvider.Type))
aggregate := iam_repo.AggregateFromWriteModel(&writeModel.WriteModel.WriteModel).
PushLoginPolicyIDPProviderAddedEvent(ctx, idpProvider.IDPConfigID, idpprovider2.Type(idpProvider.Type))
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/idp"
"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/factors"
idpprovider2 "github.com/caos/zitadel/internal/v2/repository/policy/login/idpprovider"
)
const (
@ -283,7 +283,7 @@ func (a *Aggregate) PushIDPOIDCConfigChanged(
func (a *Aggregate) PushLoginPolicyIDPProviderAddedEvent(
ctx context.Context,
idpConfigID string,
providerType provider.Type,
providerType idpprovider2.Type,
) *Aggregate {
a.Aggregate = *a.PushEvents(idpprovider.NewAddedEvent(ctx, idpConfigID, providerType))
@ -293,7 +293,7 @@ func (a *Aggregate) PushLoginPolicyIDPProviderAddedEvent(
func (a *Aggregate) PushLoginPolicyIDPProviderRemovedEvent(
ctx context.Context,
idpConfigID string,
providerType provider.Type,
providerType idpprovider2.Type,
) *Aggregate {
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/repository"
"github.com/caos/zitadel/internal/v2/repository/idp/provider"
)
type AddedEvent struct {
@ -17,7 +16,7 @@ type AddedEvent struct {
func NewAddedEvent(
ctx context.Context,
idpConfigID string,
idpProviderType provider.Type,
idpProviderType idpprovider.Type,
) *AddedEvent {
return &AddedEvent{

View File

@ -10,57 +10,39 @@ const (
)
type WriteModel struct {
eventstore.WriteModel
Provider idpprovider.WriteModel
idpConfigID string
iamID string
idpprovider.WriteModel
IsRemoved bool
}
func NewWriteModel(iamID, idpConfigID string) *WriteModel {
return &WriteModel{
iamID: iamID,
idpConfigID: idpConfigID,
WriteModel: idpprovider.WriteModel{
WriteModel: eventstore.WriteModel{
AggregateID: iamID,
},
IDPConfigID: idpConfigID,
},
IsRemoved: false,
}
}
func (wm *WriteModel) AppendEvents(events ...eventstore.EventReader) {
wm.WriteModel.AppendEvents(events...)
for _, event := range events {
switch e := event.(type) {
case *AddedEvent:
if e.IDPConfigID != wm.idpConfigID {
if e.IDPConfigID != wm.IDPConfigID {
continue
}
wm.Provider.AppendEvents(&e.AddedEvent)
wm.WriteModel.AppendEvents(&e.AddedEvent)
}
}
}
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()
}
func (wm *WriteModel) Query() *eventstore.SearchQueryBuilder {
return eventstore.NewSearchQueryBuilder(eventstore.ColumnsEvent, AggregateType).
AggregateIDs(wm.iamID)
AggregateIDs(wm.AggregateID)
}

View File

@ -3,7 +3,7 @@ package idp
import (
"github.com/caos/zitadel/internal/eventstore/v2"
"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 {
@ -13,7 +13,7 @@ type ConfigReadModel struct {
ConfigID string
Name string
StylingType StylingType
ProviderType provider.Type
ProviderType idpprovider.Type
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/eventstore/v2"
"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 (
@ -13,8 +13,8 @@ const (
LoginPolicyAddedEventType = loginPolicyPrefix + "added"
LoginPolicyChangedEventType = loginPolicyPrefix + "changed"
LoginPolicyRemovedEventType = loginPolicyPrefix + "removed"
LoginPolicyIDPProviderAddedEventType = loginPolicyPrefix + provider.AddedEventType
LoginPolicyIDPProviderRemovedEventType = loginPolicyPrefix + provider.RemovedEventType
LoginPolicyIDPProviderAddedEventType = loginPolicyPrefix + idpprovider.AddedEventType
LoginPolicyIDPProviderRemovedEventType = loginPolicyPrefix + idpprovider.RemovedEventType
)
type AddedEvent struct {

View File

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

View File

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