2022-02-25 15:05:06 +00:00
|
|
|
package projection
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/errors"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore/handler"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore/handler/crdb"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/instance"
|
2022-02-25 15:05:06 +00:00
|
|
|
)
|
|
|
|
|
2022-03-23 08:02:39 +00:00
|
|
|
const (
|
2022-12-02 12:54:28 +00:00
|
|
|
OIDCSettingsProjectionTable = "projections.oidc_settings2"
|
2022-03-23 08:02:39 +00: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"
|
|
|
|
)
|
|
|
|
|
2022-06-14 05:51:00 +00:00
|
|
|
type oidcSettingsProjection struct {
|
2022-02-25 15:05:06 +00:00
|
|
|
crdb.StatementHandler
|
|
|
|
}
|
|
|
|
|
2022-06-14 05:51:00 +00:00
|
|
|
func newOIDCSettingsProjection(ctx context.Context, config crdb.StatementHandlerConfig) *oidcSettingsProjection {
|
|
|
|
p := new(oidcSettingsProjection)
|
2022-02-25 15:05:06 +00:00
|
|
|
config.ProjectionName = OIDCSettingsProjectionTable
|
|
|
|
config.Reducers = p.reducers()
|
2022-03-23 08:02:39 +00:00
|
|
|
config.InitCheck = crdb.NewTableCheck(
|
|
|
|
crdb.NewTable([]*crdb.Column{
|
|
|
|
crdb.NewColumn(OIDCSettingsColumnAggregateID, crdb.ColumnTypeText),
|
|
|
|
crdb.NewColumn(OIDCSettingsColumnCreationDate, crdb.ColumnTypeTimestamp),
|
|
|
|
crdb.NewColumn(OIDCSettingsColumnChangeDate, crdb.ColumnTypeTimestamp),
|
|
|
|
crdb.NewColumn(OIDCSettingsColumnResourceOwner, crdb.ColumnTypeText),
|
|
|
|
crdb.NewColumn(OIDCSettingsColumnInstanceID, crdb.ColumnTypeText),
|
|
|
|
crdb.NewColumn(OIDCSettingsColumnSequence, crdb.ColumnTypeInt64),
|
|
|
|
crdb.NewColumn(OIDCSettingsColumnAccessTokenLifetime, crdb.ColumnTypeInt64),
|
|
|
|
crdb.NewColumn(OIDCSettingsColumnIdTokenLifetime, crdb.ColumnTypeInt64),
|
|
|
|
crdb.NewColumn(OIDCSettingsColumnRefreshTokenIdleExpiration, crdb.ColumnTypeInt64),
|
|
|
|
crdb.NewColumn(OIDCSettingsColumnRefreshTokenExpiration, crdb.ColumnTypeInt64),
|
|
|
|
},
|
|
|
|
crdb.NewPrimaryKey(OIDCSettingsColumnInstanceID, OIDCSettingsColumnAggregateID),
|
|
|
|
),
|
|
|
|
)
|
2022-02-25 15:05:06 +00:00
|
|
|
p.StatementHandler = crdb.NewStatementHandler(ctx, config)
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2022-06-14 05:51:00 +00:00
|
|
|
func (p *oidcSettingsProjection) reducers() []handler.AggregateReducer {
|
2022-02-25 15:05:06 +00:00
|
|
|
return []handler.AggregateReducer{
|
|
|
|
{
|
2022-09-27 10:53:49 +00:00
|
|
|
Aggregate: instance.AggregateType,
|
2022-02-25 15:05:06 +00:00
|
|
|
EventRedusers: []handler.EventReducer{
|
|
|
|
{
|
2022-03-24 16:21:34 +00:00
|
|
|
Event: instance.OIDCSettingsAddedEventType,
|
2022-02-25 15:05:06 +00:00
|
|
|
Reduce: p.reduceOIDCSettingsAdded,
|
|
|
|
},
|
|
|
|
{
|
2022-03-24 16:21:34 +00:00
|
|
|
Event: instance.OIDCSettingsChangedEventType,
|
2022-02-25 15:05:06 +00:00
|
|
|
Reduce: p.reduceOIDCSettingsChanged,
|
|
|
|
},
|
2022-10-20 12:36:52 +00:00
|
|
|
{
|
|
|
|
Event: instance.InstanceRemovedEventType,
|
|
|
|
Reduce: reduceInstanceRemovedHelper(OIDCSettingsColumnInstanceID),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-02-25 15:05:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-14 05:51:00 +00:00
|
|
|
func (p *oidcSettingsProjection) reduceOIDCSettingsAdded(event eventstore.Event) (*handler.Statement, error) {
|
2022-03-24 16:21:34 +00:00
|
|
|
e, ok := event.(*instance.OIDCSettingsAddedEvent)
|
2022-02-25 15:05:06 +00:00
|
|
|
if !ok {
|
2022-03-24 16:21:34 +00:00
|
|
|
return nil, errors.ThrowInvalidArgumentf(nil, "HANDL-f9nwf", "reduce.wrong.event.type %s", instance.OIDCSettingsAddedEventType)
|
2022-02-25 15:05:06 +00:00
|
|
|
}
|
|
|
|
return crdb.NewCreateStatement(
|
|
|
|
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 08:02:39 +00:00
|
|
|
handler.NewCol(OIDCSettingsColumnInstanceID, e.Aggregate().InstanceID),
|
2022-02-25 15:05:06 +00: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 05:51:00 +00:00
|
|
|
func (p *oidcSettingsProjection) reduceOIDCSettingsChanged(event eventstore.Event) (*handler.Statement, error) {
|
2022-03-24 16:21:34 +00:00
|
|
|
e, ok := event.(*instance.OIDCSettingsChangedEvent)
|
2022-02-25 15:05:06 +00:00
|
|
|
if !ok {
|
2022-03-24 16:21:34 +00:00
|
|
|
return nil, errors.ThrowInvalidArgumentf(nil, "HANDL-8JJ2d", "reduce.wrong.event.type %s", instance.OIDCSettingsChangedEventType)
|
2022-02-25 15:05:06 +00: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))
|
|
|
|
}
|
|
|
|
return crdb.NewUpdateStatement(
|
|
|
|
e,
|
|
|
|
columns,
|
|
|
|
[]handler.Condition{
|
|
|
|
handler.NewCond(OIDCSettingsColumnAggregateID, e.Aggregate().ID),
|
2022-11-10 10:59:33 +00:00
|
|
|
handler.NewCond(OIDCSettingsColumnInstanceID, e.Aggregate().InstanceID),
|
2022-02-25 15:05:06 +00:00
|
|
|
},
|
|
|
|
), nil
|
|
|
|
}
|