feat(api): add oidc and jwt provider template (#5290)

Adds possibility to manage OIDC and JWT template based providers
This commit is contained in:
Livio Spring
2023-02-27 16:32:18 +01:00
committed by GitHub
parent 9396e8b2f5
commit 80003939ad
29 changed files with 4338 additions and 295 deletions

View File

@@ -83,15 +83,212 @@ func (wm *InstanceOAuthIDPWriteModel) NewChangedEvent(
scopes,
options,
)
if err != nil {
if err != nil || len(changes) == 0 {
return nil, err
}
if len(changes) == 0 {
return nil, nil
}
return instance.NewOAuthIDPChangedEvent(ctx, aggregate, id, changes)
}
type InstanceOIDCIDPWriteModel struct {
OIDCIDPWriteModel
}
func NewOIDCInstanceIDPWriteModel(instanceID, id string) *InstanceOIDCIDPWriteModel {
return &InstanceOIDCIDPWriteModel{
OIDCIDPWriteModel{
WriteModel: eventstore.WriteModel{
AggregateID: instanceID,
ResourceOwner: instanceID,
},
ID: id,
},
}
}
func (wm *InstanceOIDCIDPWriteModel) Reduce() error {
return wm.OIDCIDPWriteModel.Reduce()
}
func (wm *InstanceOIDCIDPWriteModel) AppendEvents(events ...eventstore.Event) {
for _, event := range events {
switch e := event.(type) {
case *instance.OIDCIDPAddedEvent:
wm.OIDCIDPWriteModel.AppendEvents(&e.OIDCIDPAddedEvent)
case *instance.OIDCIDPChangedEvent:
wm.OIDCIDPWriteModel.AppendEvents(&e.OIDCIDPChangedEvent)
case *instance.IDPRemovedEvent:
wm.OIDCIDPWriteModel.AppendEvents(&e.RemovedEvent)
// old events
case *instance.IDPConfigAddedEvent:
wm.OIDCIDPWriteModel.AppendEvents(&e.IDPConfigAddedEvent)
case *instance.IDPConfigChangedEvent:
wm.OIDCIDPWriteModel.AppendEvents(&e.IDPConfigChangedEvent)
case *instance.IDPOIDCConfigAddedEvent:
wm.OIDCIDPWriteModel.AppendEvents(&e.OIDCConfigAddedEvent)
case *instance.IDPOIDCConfigChangedEvent:
wm.OIDCIDPWriteModel.AppendEvents(&e.OIDCConfigChangedEvent)
case *instance.IDPConfigRemovedEvent:
wm.OIDCIDPWriteModel.AppendEvents(&e.IDPConfigRemovedEvent)
default:
wm.OIDCIDPWriteModel.AppendEvents(e)
}
}
}
func (wm *InstanceOIDCIDPWriteModel) Query() *eventstore.SearchQueryBuilder {
return eventstore.NewSearchQueryBuilder(eventstore.ColumnsEvent).
ResourceOwner(wm.ResourceOwner).
AddQuery().
AggregateTypes(instance.AggregateType).
AggregateIDs(wm.AggregateID).
EventTypes(
instance.OIDCIDPAddedEventType,
instance.OIDCIDPChangedEventType,
instance.IDPRemovedEventType,
).
EventData(map[string]interface{}{"id": wm.ID}).
Or(). // old events
AggregateTypes(instance.AggregateType).
AggregateIDs(wm.AggregateID).
EventTypes(
instance.IDPConfigAddedEventType,
instance.IDPConfigChangedEventType,
instance.IDPOIDCConfigAddedEventType,
instance.IDPOIDCConfigChangedEventType,
instance.IDPConfigRemovedEventType,
).
EventData(map[string]interface{}{"idpConfigId": wm.ID}).
Builder()
}
func (wm *InstanceOIDCIDPWriteModel) NewChangedEvent(
ctx context.Context,
aggregate *eventstore.Aggregate,
id,
name,
issuer,
clientID,
clientSecretString string,
secretCrypto crypto.Crypto,
scopes []string,
options idp.Options,
) (*instance.OIDCIDPChangedEvent, error) {
changes, err := wm.OIDCIDPWriteModel.NewChanges(
name,
issuer,
clientID,
clientSecretString,
secretCrypto,
scopes,
options,
)
if err != nil || len(changes) == 0 {
return nil, err
}
return instance.NewOIDCIDPChangedEvent(ctx, aggregate, id, changes)
}
type InstanceJWTIDPWriteModel struct {
JWTIDPWriteModel
}
func NewJWTInstanceIDPWriteModel(instanceID, id string) *InstanceJWTIDPWriteModel {
return &InstanceJWTIDPWriteModel{
JWTIDPWriteModel{
WriteModel: eventstore.WriteModel{
AggregateID: instanceID,
ResourceOwner: instanceID,
},
ID: id,
},
}
}
func (wm *InstanceJWTIDPWriteModel) Reduce() error {
return wm.JWTIDPWriteModel.Reduce()
}
func (wm *InstanceJWTIDPWriteModel) AppendEvents(events ...eventstore.Event) {
for _, event := range events {
switch e := event.(type) {
case *instance.JWTIDPAddedEvent:
wm.JWTIDPWriteModel.AppendEvents(&e.JWTIDPAddedEvent)
case *instance.JWTIDPChangedEvent:
wm.JWTIDPWriteModel.AppendEvents(&e.JWTIDPChangedEvent)
case *instance.IDPRemovedEvent:
wm.JWTIDPWriteModel.AppendEvents(&e.RemovedEvent)
// old events
case *instance.IDPConfigAddedEvent:
wm.JWTIDPWriteModel.AppendEvents(&e.IDPConfigAddedEvent)
case *instance.IDPConfigChangedEvent:
wm.JWTIDPWriteModel.AppendEvents(&e.IDPConfigChangedEvent)
case *instance.IDPJWTConfigAddedEvent:
wm.JWTIDPWriteModel.AppendEvents(&e.JWTConfigAddedEvent)
case *instance.IDPJWTConfigChangedEvent:
wm.JWTIDPWriteModel.AppendEvents(&e.JWTConfigChangedEvent)
case *instance.IDPConfigRemovedEvent:
wm.JWTIDPWriteModel.AppendEvents(&e.IDPConfigRemovedEvent)
default:
wm.JWTIDPWriteModel.AppendEvents(e)
}
}
}
func (wm *InstanceJWTIDPWriteModel) Query() *eventstore.SearchQueryBuilder {
return eventstore.NewSearchQueryBuilder(eventstore.ColumnsEvent).
ResourceOwner(wm.ResourceOwner).
AddQuery().
AggregateTypes(instance.AggregateType).
AggregateIDs(wm.AggregateID).
EventTypes(
instance.JWTIDPAddedEventType,
instance.JWTIDPChangedEventType,
instance.IDPRemovedEventType,
).
EventData(map[string]interface{}{"id": wm.ID}).
Or(). // old events
AggregateTypes(instance.AggregateType).
AggregateIDs(wm.AggregateID).
EventTypes(
instance.IDPConfigAddedEventType,
instance.IDPConfigChangedEventType,
instance.IDPJWTConfigAddedEventType,
instance.IDPJWTConfigChangedEventType,
instance.IDPConfigRemovedEventType,
).
EventData(map[string]interface{}{"idpConfigId": wm.ID}).
Builder()
}
func (wm *InstanceJWTIDPWriteModel) NewChangedEvent(
ctx context.Context,
aggregate *eventstore.Aggregate,
id,
name,
issuer,
jwtEndpoint,
keysEndpoint,
headerName string,
options idp.Options,
) (*instance.JWTIDPChangedEvent, error) {
changes, err := wm.JWTIDPWriteModel.NewChanges(
name,
issuer,
jwtEndpoint,
keysEndpoint,
headerName,
options,
)
if err != nil || len(changes) == 0 {
return nil, err
}
return instance.NewJWTIDPChangedEvent(ctx, aggregate, id, changes)
}
type InstanceGoogleIDPWriteModel struct {
GoogleIDPWriteModel
}
@@ -153,12 +350,9 @@ func (wm *InstanceGoogleIDPWriteModel) NewChangedEvent(
) (*instance.GoogleIDPChangedEvent, error) {
changes, err := wm.GoogleIDPWriteModel.NewChanges(name, clientID, clientSecretString, secretCrypto, scopes, options)
if err != nil {
if err != nil || len(changes) == 0 {
return nil, err
}
if len(changes) == 0 {
return nil, nil
}
return instance.NewGoogleIDPChangedEvent(ctx, aggregate, id, changes)
}
@@ -245,12 +439,9 @@ func (wm *InstanceLDAPIDPWriteModel) NewChangedEvent(
attributes,
options,
)
if err != nil {
if err != nil || len(changes) == 0 {
return nil, err
}
if len(changes) == 0 {
return nil, nil
}
return instance.NewLDAPIDPChangedEvent(ctx, aggregate, id, oldName, changes)
}
@@ -279,18 +470,20 @@ func (wm *InstanceIDPRemoveWriteModel) AppendEvents(events ...eventstore.Event)
switch e := event.(type) {
case *instance.OAuthIDPAddedEvent:
wm.IDPRemoveWriteModel.AppendEvents(&e.OAuthIDPAddedEvent)
case *instance.OAuthIDPChangedEvent:
wm.IDPRemoveWriteModel.AppendEvents(&e.OAuthIDPChangedEvent)
case *instance.OIDCIDPAddedEvent:
wm.IDPRemoveWriteModel.AppendEvents(&e.OIDCIDPAddedEvent)
case *instance.JWTIDPAddedEvent:
wm.IDPRemoveWriteModel.AppendEvents(&e.JWTIDPAddedEvent)
case *instance.GoogleIDPAddedEvent:
wm.IDPRemoveWriteModel.AppendEvents(&e.GoogleIDPAddedEvent)
case *instance.GoogleIDPChangedEvent:
wm.IDPRemoveWriteModel.AppendEvents(&e.GoogleIDPChangedEvent)
case *instance.LDAPIDPAddedEvent:
wm.IDPRemoveWriteModel.AppendEvents(&e.LDAPIDPAddedEvent)
case *instance.LDAPIDPChangedEvent:
wm.IDPRemoveWriteModel.AppendEvents(&e.LDAPIDPChangedEvent)
case *instance.IDPRemovedEvent:
wm.IDPRemoveWriteModel.AppendEvents(&e.RemovedEvent)
case *instance.IDPConfigAddedEvent:
wm.IDPRemoveWriteModel.AppendEvents(&e.IDPConfigAddedEvent)
case *instance.IDPConfigRemovedEvent:
wm.IDPRemoveWriteModel.AppendEvents(&e.IDPConfigRemovedEvent)
default:
wm.IDPRemoveWriteModel.AppendEvents(e)
}
@@ -305,13 +498,20 @@ func (wm *InstanceIDPRemoveWriteModel) Query() *eventstore.SearchQueryBuilder {
AggregateIDs(wm.AggregateID).
EventTypes(
instance.OAuthIDPAddedEventType,
instance.OAuthIDPChangedEventType,
instance.OIDCIDPAddedEventType,
instance.JWTIDPAddedEventType,
instance.GoogleIDPAddedEventType,
instance.GoogleIDPChangedEventType,
instance.LDAPIDPAddedEventType,
instance.LDAPIDPChangedEventType,
instance.IDPRemovedEventType,
).
EventData(map[string]interface{}{"id": wm.ID}).
Or(). // old events
AggregateTypes(instance.AggregateType).
AggregateIDs(wm.AggregateID).
EventTypes(
instance.IDPConfigAddedEventType,
instance.IDPConfigRemovedEventType,
).
EventData(map[string]interface{}{"idpConfigId": wm.ID}).
Builder()
}