2022-02-25 16:05:06 +01:00
|
|
|
package projection
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2022-04-27 01:01:45 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
2023-10-19 12:19:10 +02:00
|
|
|
old_handler "github.com/zitadel/zitadel/internal/eventstore/handler"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore/handler/v2"
|
2022-04-27 01:01:45 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/repository/instance"
|
2023-12-08 16:30:55 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/zerrors"
|
2022-02-25 16:05:06 +01:00
|
|
|
)
|
|
|
|
|
2022-03-23 09:02:39 +01:00
|
|
|
const (
|
2022-12-02 13:54:28 +01:00
|
|
|
OIDCSettingsProjectionTable = "projections.oidc_settings2"
|
2022-03-23 09:02:39 +01:00
|
|
|
|
|
|
|
OIDCSettingsColumnAggregateID = "aggregate_id"
|
|
|
|
OIDCSettingsColumnCreationDate = "creation_date"
|
|
|
|
OIDCSettingsColumnChangeDate = "change_date"
|
|
|
|
OIDCSettingsColumnResourceOwner = "resource_owner"
|
|
|
|
OIDCSettingsColumnInstanceID = "instance_id"
|
|
|
|
OIDCSettingsColumnSequence = "sequence"
|
|
|
|
OIDCSettingsColumnAccessTokenLifetime = "access_token_lifetime"
|
|
|
|
OIDCSettingsColumnIdTokenLifetime = "id_token_lifetime"
|
|
|
|
OIDCSettingsColumnRefreshTokenIdleExpiration = "refresh_token_idle_expiration"
|
|
|
|
OIDCSettingsColumnRefreshTokenExpiration = "refresh_token_expiration"
|
|
|
|
)
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
type oidcSettingsProjection struct{}
|
|
|
|
|
|
|
|
func newOIDCSettingsProjection(ctx context.Context, config handler.Config) *handler.Handler {
|
|
|
|
return handler.NewHandler(ctx, &config, new(oidcSettingsProjection))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*oidcSettingsProjection) Name() string {
|
|
|
|
return OIDCSettingsProjectionTable
|
2022-02-25 16:05:06 +01:00
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func (*oidcSettingsProjection) Init() *old_handler.Check {
|
|
|
|
return handler.NewTableCheck(
|
|
|
|
handler.NewTable([]*handler.InitColumn{
|
|
|
|
handler.NewColumn(OIDCSettingsColumnAggregateID, handler.ColumnTypeText),
|
|
|
|
handler.NewColumn(OIDCSettingsColumnCreationDate, handler.ColumnTypeTimestamp),
|
|
|
|
handler.NewColumn(OIDCSettingsColumnChangeDate, handler.ColumnTypeTimestamp),
|
|
|
|
handler.NewColumn(OIDCSettingsColumnResourceOwner, handler.ColumnTypeText),
|
|
|
|
handler.NewColumn(OIDCSettingsColumnInstanceID, handler.ColumnTypeText),
|
|
|
|
handler.NewColumn(OIDCSettingsColumnSequence, handler.ColumnTypeInt64),
|
|
|
|
handler.NewColumn(OIDCSettingsColumnAccessTokenLifetime, handler.ColumnTypeInt64),
|
|
|
|
handler.NewColumn(OIDCSettingsColumnIdTokenLifetime, handler.ColumnTypeInt64),
|
|
|
|
handler.NewColumn(OIDCSettingsColumnRefreshTokenIdleExpiration, handler.ColumnTypeInt64),
|
|
|
|
handler.NewColumn(OIDCSettingsColumnRefreshTokenExpiration, handler.ColumnTypeInt64),
|
2022-03-23 09:02:39 +01:00
|
|
|
},
|
2023-10-19 12:19:10 +02:00
|
|
|
handler.NewPrimaryKey(OIDCSettingsColumnInstanceID, OIDCSettingsColumnAggregateID),
|
2022-03-23 09:02:39 +01:00
|
|
|
),
|
|
|
|
)
|
2022-02-25 16:05:06 +01:00
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func (p *oidcSettingsProjection) Reducers() []handler.AggregateReducer {
|
2022-02-25 16:05:06 +01:00
|
|
|
return []handler.AggregateReducer{
|
|
|
|
{
|
2022-09-27 11:53:49 +01:00
|
|
|
Aggregate: instance.AggregateType,
|
2023-10-19 12:19:10 +02:00
|
|
|
EventReducers: []handler.EventReducer{
|
2022-02-25 16:05:06 +01:00
|
|
|
{
|
2022-03-24 17:21:34 +01:00
|
|
|
Event: instance.OIDCSettingsAddedEventType,
|
2022-02-25 16:05:06 +01:00
|
|
|
Reduce: p.reduceOIDCSettingsAdded,
|
|
|
|
},
|
|
|
|
{
|
2022-03-24 17:21:34 +01:00
|
|
|
Event: instance.OIDCSettingsChangedEventType,
|
2022-02-25 16:05:06 +01:00
|
|
|
Reduce: p.reduceOIDCSettingsChanged,
|
|
|
|
},
|
2022-10-20 13:36:52 +01:00
|
|
|
{
|
|
|
|
Event: instance.InstanceRemovedEventType,
|
|
|
|
Reduce: reduceInstanceRemovedHelper(OIDCSettingsColumnInstanceID),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-02-25 16:05:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-14 07:51:00 +02:00
|
|
|
func (p *oidcSettingsProjection) reduceOIDCSettingsAdded(event eventstore.Event) (*handler.Statement, error) {
|
2022-03-24 17:21:34 +01:00
|
|
|
e, ok := event.(*instance.OIDCSettingsAddedEvent)
|
2022-02-25 16:05:06 +01:00
|
|
|
if !ok {
|
2023-12-08 16:30:55 +02:00
|
|
|
return nil, zerrors.ThrowInvalidArgumentf(nil, "HANDL-f9nwf", "reduce.wrong.event.type %s", instance.OIDCSettingsAddedEventType)
|
2022-02-25 16:05:06 +01:00
|
|
|
}
|
2023-10-19 12:19:10 +02:00
|
|
|
return handler.NewCreateStatement(
|
2022-02-25 16:05:06 +01:00
|
|
|
e,
|
|
|
|
[]handler.Column{
|
|
|
|
handler.NewCol(OIDCSettingsColumnAggregateID, e.Aggregate().ID),
|
|
|
|
handler.NewCol(OIDCSettingsColumnCreationDate, e.CreationDate()),
|
|
|
|
handler.NewCol(OIDCSettingsColumnChangeDate, e.CreationDate()),
|
|
|
|
handler.NewCol(OIDCSettingsColumnResourceOwner, e.Aggregate().ResourceOwner),
|
2022-03-23 09:02:39 +01:00
|
|
|
handler.NewCol(OIDCSettingsColumnInstanceID, e.Aggregate().InstanceID),
|
2022-02-25 16:05:06 +01:00
|
|
|
handler.NewCol(OIDCSettingsColumnSequence, e.Sequence()),
|
|
|
|
handler.NewCol(OIDCSettingsColumnAccessTokenLifetime, e.AccessTokenLifetime),
|
|
|
|
handler.NewCol(OIDCSettingsColumnIdTokenLifetime, e.IdTokenLifetime),
|
|
|
|
handler.NewCol(OIDCSettingsColumnRefreshTokenIdleExpiration, e.RefreshTokenIdleExpiration),
|
|
|
|
handler.NewCol(OIDCSettingsColumnRefreshTokenExpiration, e.RefreshTokenExpiration),
|
|
|
|
},
|
|
|
|
), nil
|
|
|
|
}
|
|
|
|
|
2022-06-14 07:51:00 +02:00
|
|
|
func (p *oidcSettingsProjection) reduceOIDCSettingsChanged(event eventstore.Event) (*handler.Statement, error) {
|
2022-03-24 17:21:34 +01:00
|
|
|
e, ok := event.(*instance.OIDCSettingsChangedEvent)
|
2022-02-25 16:05:06 +01:00
|
|
|
if !ok {
|
2023-12-08 16:30:55 +02:00
|
|
|
return nil, zerrors.ThrowInvalidArgumentf(nil, "HANDL-8JJ2d", "reduce.wrong.event.type %s", instance.OIDCSettingsChangedEventType)
|
2022-02-25 16:05:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
columns := make([]handler.Column, 0, 6)
|
|
|
|
columns = append(columns,
|
|
|
|
handler.NewCol(OIDCSettingsColumnChangeDate, e.CreationDate()),
|
|
|
|
handler.NewCol(OIDCSettingsColumnSequence, e.Sequence()),
|
|
|
|
)
|
|
|
|
if e.AccessTokenLifetime != nil {
|
|
|
|
columns = append(columns, handler.NewCol(OIDCSettingsColumnAccessTokenLifetime, *e.AccessTokenLifetime))
|
|
|
|
}
|
|
|
|
if e.IdTokenLifetime != nil {
|
|
|
|
columns = append(columns, handler.NewCol(OIDCSettingsColumnIdTokenLifetime, *e.IdTokenLifetime))
|
|
|
|
}
|
|
|
|
if e.RefreshTokenIdleExpiration != nil {
|
|
|
|
columns = append(columns, handler.NewCol(OIDCSettingsColumnRefreshTokenIdleExpiration, *e.RefreshTokenIdleExpiration))
|
|
|
|
}
|
|
|
|
if e.RefreshTokenExpiration != nil {
|
|
|
|
columns = append(columns, handler.NewCol(OIDCSettingsColumnRefreshTokenExpiration, *e.RefreshTokenExpiration))
|
|
|
|
}
|
2023-10-19 12:19:10 +02:00
|
|
|
return handler.NewUpdateStatement(
|
2022-02-25 16:05:06 +01:00
|
|
|
e,
|
|
|
|
columns,
|
|
|
|
[]handler.Condition{
|
|
|
|
handler.NewCond(OIDCSettingsColumnAggregateID, e.Aggregate().ID),
|
2022-11-10 11:59:33 +01:00
|
|
|
handler.NewCond(OIDCSettingsColumnInstanceID, e.Aggregate().InstanceID),
|
2022-02-25 16:05:06 +01:00
|
|
|
},
|
|
|
|
), nil
|
|
|
|
}
|