zitadel/internal/v2/repository/iam/idp_oidc_config.go

151 lines
3.4 KiB
Go
Raw Normal View History

2020-11-25 19:04:32 +00:00
package iam
import (
"context"
"github.com/caos/zitadel/internal/crypto"
"github.com/caos/zitadel/internal/eventstore/v2"
2020-11-26 12:14:07 +00:00
"github.com/caos/zitadel/internal/eventstore/v2/repository"
2020-11-25 19:04:32 +00:00
"github.com/caos/zitadel/internal/v2/repository/idp/oidc"
)
const (
2020-11-30 09:41:10 +00:00
IDPOIDCConfigAddedEventType eventstore.EventType = "iam.idp." + oidc.ConfigAddedEventType
IDPOIDCConfigChangedEventType eventstore.EventType = "iam.idp." + oidc.ConfigChangedEventType
2020-11-25 19:04:32 +00:00
)
type IDPOIDCConfigWriteModel struct {
2020-11-27 12:29:35 +00:00
eventstore.WriteModel
2020-11-25 19:04:32 +00:00
oidc.ConfigWriteModel
2020-11-27 10:30:56 +00:00
iamID string
idpConfigID string
}
func NewIDPOIDCConfigWriteModel(iamID, idpConfigID string) *IDPOIDCConfigWriteModel {
return &IDPOIDCConfigWriteModel{
iamID: iamID,
idpConfigID: idpConfigID,
}
2020-11-25 19:04:32 +00:00
}
2020-11-27 10:30:56 +00:00
func (wm *IDPOIDCConfigWriteModel) AppendEvents(events ...eventstore.EventReader) {
2020-11-27 12:29:35 +00:00
wm.WriteModel.AppendEvents(events...)
2020-11-25 19:04:32 +00:00
for _, event := range events {
switch e := event.(type) {
case *IDPOIDCConfigAddedEvent:
2020-11-27 10:30:56 +00:00
if wm.idpConfigID != e.IDPConfigID {
continue
}
wm.ConfigWriteModel.AppendEvents(&e.ConfigAddedEvent)
2020-11-25 19:04:32 +00:00
case *IDPOIDCConfigChangedEvent:
2020-11-27 10:30:56 +00:00
if wm.idpConfigID != e.IDPConfigID {
continue
}
wm.ConfigWriteModel.AppendEvents(&e.ConfigChangedEvent)
default:
wm.ConfigWriteModel.AppendEvents(e)
2020-11-25 19:04:32 +00:00
}
}
}
2020-11-27 12:29:35 +00:00
func (wm *IDPOIDCConfigWriteModel) Reduce() error {
if err := wm.ConfigWriteModel.Reduce(); err != nil {
return err
}
return wm.WriteModel.Reduce()
}
2020-12-01 13:44:19 +00:00
func (wm *IDPOIDCConfigWriteModel) Query() *eventstore.SearchQueryBuilder {
return eventstore.NewSearchQueryBuilder(eventstore.ColumnsEvent, AggregateType).
2020-11-27 12:29:35 +00:00
AggregateIDs(wm.iamID)
}
2020-11-25 19:04:32 +00:00
type IDPOIDCConfigAddedEvent struct {
oidc.ConfigAddedEvent
}
func NewIDPOIDCConfigAddedEvent(
ctx context.Context,
clientID,
idpConfigID,
issuer string,
clientSecret *crypto.CryptoValue,
idpDisplayNameMapping,
userNameMapping oidc.MappingField,
scopes ...string,
) *IDPOIDCConfigAddedEvent {
return &IDPOIDCConfigAddedEvent{
ConfigAddedEvent: *oidc.NewConfigAddedEvent(
eventstore.NewBaseEventForPush(
ctx,
IDPOIDCConfigAddedEventType,
),
clientID,
idpConfigID,
issuer,
clientSecret,
idpDisplayNameMapping,
userNameMapping,
scopes...,
),
}
}
2020-11-26 12:14:07 +00:00
func IDPOIDCConfigAddedEventMapper(event *repository.Event) (eventstore.EventReader, error) {
e, err := oidc.ConfigAddedEventMapper(event)
if err != nil {
return nil, err
}
return &IDPOIDCConfigAddedEvent{ConfigAddedEvent: *e.(*oidc.ConfigAddedEvent)}, nil
2020-11-26 12:14:07 +00:00
}
2020-11-25 19:04:32 +00:00
type IDPOIDCConfigChangedEvent struct {
oidc.ConfigChangedEvent
}
func NewIDPOIDCConfigChangedEvent(
ctx context.Context,
current *IDPOIDCConfigWriteModel,
clientID,
issuer string,
clientSecret *crypto.CryptoValue,
idpDisplayNameMapping,
userNameMapping oidc.MappingField,
scopes ...string,
) (*IDPOIDCConfigChangedEvent, error) {
event, err := oidc.NewConfigChangedEvent(
eventstore.NewBaseEventForPush(
ctx,
2020-11-27 10:30:56 +00:00
IDPOIDCConfigChangedEventType,
2020-11-25 19:04:32 +00:00
),
&current.ConfigWriteModel,
clientID,
issuer,
clientSecret,
idpDisplayNameMapping,
userNameMapping,
scopes...,
)
if err != nil {
return nil, err
}
return &IDPOIDCConfigChangedEvent{
ConfigChangedEvent: *event,
}, nil
}
2020-11-26 12:14:07 +00:00
func IDPOIDCConfigChangedEventMapper(event *repository.Event) (eventstore.EventReader, error) {
e, err := oidc.ConfigChangedEventMapper(event)
if err != nil {
return nil, err
}
return &IDPOIDCConfigChangedEvent{ConfigChangedEvent: *e.(*oidc.ConfigChangedEvent)}, nil
2020-11-26 12:14:07 +00:00
}