zitadel/internal/query/projection/secret_generator.go
Fabi 7d6c933485
feat: OIDC setting (#3245)
* feat: add oidc config struct

* feat: oidc config command side

* feat: oidc configuration query side

* feat: add translations

* feat: add tests

* feat: add translations

* feat: rename oidc config to oidc settings

* feat: rename oidc config to oidc settings
2022-02-25 16:05:06 +01:00

145 lines
5.6 KiB
Go

package projection
import (
"context"
"github.com/caos/logging"
"github.com/caos/zitadel/internal/errors"
"github.com/caos/zitadel/internal/eventstore"
"github.com/caos/zitadel/internal/eventstore/handler"
"github.com/caos/zitadel/internal/eventstore/handler/crdb"
"github.com/caos/zitadel/internal/repository/iam"
"github.com/caos/zitadel/internal/repository/project"
)
type SecretGeneratorProjection struct {
crdb.StatementHandler
}
const (
SecretGeneratorProjectionTable = "zitadel.projections.secret_generators"
)
func NewSecretGeneratorProjection(ctx context.Context, config crdb.StatementHandlerConfig) *SecretGeneratorProjection {
p := new(SecretGeneratorProjection)
config.ProjectionName = SecretGeneratorProjectionTable
config.Reducers = p.reducers()
p.StatementHandler = crdb.NewStatementHandler(ctx, config)
return p
}
func (p *SecretGeneratorProjection) reducers() []handler.AggregateReducer {
return []handler.AggregateReducer{
{
Aggregate: project.AggregateType,
EventRedusers: []handler.EventReducer{
{
Event: iam.SecretGeneratorAddedEventType,
Reduce: p.reduceSecretGeneratorAdded,
},
{
Event: iam.SecretGeneratorChangedEventType,
Reduce: p.reduceSecretGeneratorChanged,
},
{
Event: iam.SecretGeneratorRemovedEventType,
Reduce: p.reduceSecretGeneratorRemoved,
},
},
},
}
}
const (
SecretGeneratorColumnGeneratorType = "generator_type"
SecretGeneratorColumnAggregateID = "aggregate_id"
SecretGeneratorColumnCreationDate = "creation_date"
SecretGeneratorColumnChangeDate = "change_date"
SecretGeneratorColumnResourceOwner = "resource_owner"
SecretGeneratorColumnSequence = "sequence"
SecretGeneratorColumnLength = "length"
SecretGeneratorColumnExpiry = "expiry"
SecretGeneratorColumnIncludeLowerLetters = "include_lower_letters"
SecretGeneratorColumnIncludeUpperLetters = "include_upper_letters"
SecretGeneratorColumnIncludeDigits = "include_digits"
SecretGeneratorColumnIncludeSymbols = "include_symbols"
)
func (p *SecretGeneratorProjection) reduceSecretGeneratorAdded(event eventstore.Event) (*handler.Statement, error) {
e, ok := event.(*iam.SecretGeneratorAddedEvent)
if !ok {
logging.LogWithFields("HANDL-nf9sl", "seq", event.Sequence(), "expectedType", iam.SecretGeneratorAddedEventType).Error("wrong event type")
return nil, errors.ThrowInvalidArgument(nil, "HANDL-sk99F", "reduce.wrong.event.type")
}
return crdb.NewCreateStatement(
e,
[]handler.Column{
handler.NewCol(SecretGeneratorColumnAggregateID, e.Aggregate().ID),
handler.NewCol(SecretGeneratorColumnGeneratorType, e.GeneratorType),
handler.NewCol(SecretGeneratorColumnCreationDate, e.CreationDate()),
handler.NewCol(SecretGeneratorColumnChangeDate, e.CreationDate()),
handler.NewCol(SecretGeneratorColumnResourceOwner, e.Aggregate().ResourceOwner),
handler.NewCol(SecretGeneratorColumnSequence, e.Sequence()),
handler.NewCol(SecretGeneratorColumnLength, e.Length),
handler.NewCol(SecretGeneratorColumnExpiry, e.Expiry),
handler.NewCol(SecretGeneratorColumnIncludeLowerLetters, e.IncludeLowerLetters),
handler.NewCol(SecretGeneratorColumnIncludeUpperLetters, e.IncludeUpperLetters),
handler.NewCol(SecretGeneratorColumnIncludeDigits, e.IncludeDigits),
handler.NewCol(SecretGeneratorColumnIncludeSymbols, e.IncludeSymbols),
},
), nil
}
func (p *SecretGeneratorProjection) reduceSecretGeneratorChanged(event eventstore.Event) (*handler.Statement, error) {
e, ok := event.(*iam.SecretGeneratorChangedEvent)
if !ok {
logging.LogWithFields("HANDL-sn9jd", "seq", event.Sequence(), "expected", iam.SecretGeneratorChangedEventType).Error("wrong event type")
return nil, errors.ThrowInvalidArgument(nil, "HANDL-s00Fs", "reduce.wrong.event.type")
}
columns := make([]handler.Column, 0, 7)
columns = append(columns, handler.NewCol(SecretGeneratorColumnChangeDate, e.CreationDate()),
handler.NewCol(SecretGeneratorColumnSequence, e.Sequence()))
if e.Length != nil {
columns = append(columns, handler.NewCol(SecretGeneratorColumnLength, *e.Length))
}
if e.Expiry != nil {
columns = append(columns, handler.NewCol(SecretGeneratorColumnExpiry, *e.Expiry))
}
if e.IncludeLowerLetters != nil {
columns = append(columns, handler.NewCol(SecretGeneratorColumnIncludeLowerLetters, *e.IncludeLowerLetters))
}
if e.IncludeUpperLetters != nil {
columns = append(columns, handler.NewCol(SecretGeneratorColumnIncludeUpperLetters, *e.IncludeUpperLetters))
}
if e.IncludeDigits != nil {
columns = append(columns, handler.NewCol(SecretGeneratorColumnIncludeDigits, *e.IncludeDigits))
}
if e.IncludeSymbols != nil {
columns = append(columns, handler.NewCol(SecretGeneratorColumnIncludeSymbols, *e.IncludeSymbols))
}
return crdb.NewUpdateStatement(
e,
columns,
[]handler.Condition{
handler.NewCond(SecretGeneratorColumnAggregateID, e.Aggregate().ID),
handler.NewCond(SecretGeneratorColumnGeneratorType, e.GeneratorType),
},
), nil
}
func (p *SecretGeneratorProjection) reduceSecretGeneratorRemoved(event eventstore.Event) (*handler.Statement, error) {
e, ok := event.(*iam.SecretGeneratorRemovedEvent)
if !ok {
logging.LogWithFields("HANDL-30oEF", "seq", event.Sequence(), "expectedType", iam.SecretGeneratorRemovedEventType).Error("wrong event type")
return nil, errors.ThrowInvalidArgument(nil, "HANDL-fmiIf", "reduce.wrong.event.type")
}
return crdb.NewDeleteStatement(
e,
[]handler.Condition{
handler.NewCond(SecretGeneratorColumnAggregateID, e.Aggregate().ID),
handler.NewCond(SecretGeneratorColumnGeneratorType, e.GeneratorType),
},
), nil
}