2022-01-19 13:49:50 +00:00
|
|
|
package projection
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
2023-10-19 10:19:10 +00:00
|
|
|
old_handler "github.com/zitadel/zitadel/internal/eventstore/handler"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore/handler/v2"
|
2022-10-20 12:36:52 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/repository/instance"
|
2022-11-30 16:01:17 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/repository/org"
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/repository/user"
|
2023-12-08 14:30:55 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/zerrors"
|
2022-01-19 13:49:50 +00:00
|
|
|
)
|
|
|
|
|
2022-03-23 08:02:39 +00:00
|
|
|
const (
|
2022-11-30 16:01:17 +00:00
|
|
|
UserAuthMethodTable = "projections.user_auth_methods4"
|
2022-03-23 08:02:39 +00:00
|
|
|
|
|
|
|
UserAuthMethodUserIDCol = "user_id"
|
|
|
|
UserAuthMethodTypeCol = "method_type"
|
|
|
|
UserAuthMethodTokenIDCol = "token_id"
|
|
|
|
UserAuthMethodCreationDateCol = "creation_date"
|
|
|
|
UserAuthMethodChangeDateCol = "change_date"
|
|
|
|
UserAuthMethodSequenceCol = "sequence"
|
|
|
|
UserAuthMethodResourceOwnerCol = "resource_owner"
|
|
|
|
UserAuthMethodInstanceIDCol = "instance_id"
|
|
|
|
UserAuthMethodStateCol = "state"
|
|
|
|
UserAuthMethodNameCol = "name"
|
2022-11-30 16:01:17 +00:00
|
|
|
UserAuthMethodOwnerRemovedCol = "owner_removed"
|
2022-03-23 08:02:39 +00:00
|
|
|
)
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
type userAuthMethodProjection struct{}
|
|
|
|
|
|
|
|
func newUserAuthMethodProjection(ctx context.Context, config handler.Config) *handler.Handler {
|
|
|
|
return handler.NewHandler(ctx, &config, new(userAuthMethodProjection))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*userAuthMethodProjection) Name() string {
|
|
|
|
return UserAuthMethodTable
|
2022-01-19 13:49:50 +00:00
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (*userAuthMethodProjection) Init() *old_handler.Check {
|
|
|
|
return handler.NewTableCheck(
|
|
|
|
handler.NewTable([]*handler.InitColumn{
|
|
|
|
handler.NewColumn(UserAuthMethodUserIDCol, handler.ColumnTypeText),
|
|
|
|
handler.NewColumn(UserAuthMethodTypeCol, handler.ColumnTypeEnum),
|
|
|
|
handler.NewColumn(UserAuthMethodTokenIDCol, handler.ColumnTypeText),
|
|
|
|
handler.NewColumn(UserAuthMethodCreationDateCol, handler.ColumnTypeTimestamp),
|
|
|
|
handler.NewColumn(UserAuthMethodChangeDateCol, handler.ColumnTypeTimestamp),
|
|
|
|
handler.NewColumn(UserAuthMethodSequenceCol, handler.ColumnTypeInt64),
|
|
|
|
handler.NewColumn(UserAuthMethodStateCol, handler.ColumnTypeEnum),
|
|
|
|
handler.NewColumn(UserAuthMethodResourceOwnerCol, handler.ColumnTypeText),
|
|
|
|
handler.NewColumn(UserAuthMethodInstanceIDCol, handler.ColumnTypeText),
|
|
|
|
handler.NewColumn(UserAuthMethodNameCol, handler.ColumnTypeText),
|
|
|
|
handler.NewColumn(UserAuthMethodOwnerRemovedCol, handler.ColumnTypeBool, handler.Default(false)),
|
2022-03-23 08:02:39 +00:00
|
|
|
},
|
2023-10-19 10:19:10 +00:00
|
|
|
handler.NewPrimaryKey(UserAuthMethodInstanceIDCol, UserAuthMethodUserIDCol, UserAuthMethodTypeCol, UserAuthMethodTokenIDCol),
|
|
|
|
handler.WithIndex(handler.NewIndex("resource_owner", []string{UserAuthMethodResourceOwnerCol})),
|
|
|
|
handler.WithIndex(handler.NewIndex("owner_removed", []string{UserAuthMethodOwnerRemovedCol})),
|
2022-03-23 08:02:39 +00:00
|
|
|
),
|
|
|
|
)
|
2022-01-19 13:49:50 +00:00
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (p *userAuthMethodProjection) Reducers() []handler.AggregateReducer {
|
2022-01-19 13:49:50 +00:00
|
|
|
return []handler.AggregateReducer{
|
|
|
|
{
|
|
|
|
Aggregate: user.AggregateType,
|
2023-10-19 10:19:10 +00:00
|
|
|
EventReducers: []handler.EventReducer{
|
2022-01-19 13:49:50 +00:00
|
|
|
{
|
|
|
|
Event: user.HumanPasswordlessTokenAddedType,
|
|
|
|
Reduce: p.reduceInitAuthMethod,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Event: user.HumanU2FTokenAddedType,
|
|
|
|
Reduce: p.reduceInitAuthMethod,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Event: user.HumanMFAOTPAddedType,
|
|
|
|
Reduce: p.reduceInitAuthMethod,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Event: user.HumanPasswordlessTokenVerifiedType,
|
|
|
|
Reduce: p.reduceActivateEvent,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Event: user.HumanU2FTokenVerifiedType,
|
|
|
|
Reduce: p.reduceActivateEvent,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Event: user.HumanMFAOTPVerifiedType,
|
|
|
|
Reduce: p.reduceActivateEvent,
|
|
|
|
},
|
2023-08-02 16:57:53 +00:00
|
|
|
{
|
|
|
|
Event: user.HumanOTPSMSAddedType,
|
|
|
|
Reduce: p.reduceAddAuthMethod,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Event: user.HumanOTPEmailAddedType,
|
|
|
|
Reduce: p.reduceAddAuthMethod,
|
|
|
|
},
|
2022-01-19 13:49:50 +00:00
|
|
|
{
|
|
|
|
Event: user.HumanPasswordlessTokenRemovedType,
|
|
|
|
Reduce: p.reduceRemoveAuthMethod,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Event: user.HumanU2FTokenRemovedType,
|
|
|
|
Reduce: p.reduceRemoveAuthMethod,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Event: user.HumanMFAOTPRemovedType,
|
|
|
|
Reduce: p.reduceRemoveAuthMethod,
|
|
|
|
},
|
2023-08-02 16:57:53 +00:00
|
|
|
{
|
|
|
|
Event: user.HumanOTPSMSRemovedType,
|
|
|
|
Reduce: p.reduceRemoveAuthMethod,
|
2023-08-15 12:47:05 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Event: user.HumanPhoneRemovedType,
|
|
|
|
Reduce: p.reduceRemoveAuthMethod,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Event: user.UserV1PhoneRemovedType,
|
|
|
|
Reduce: p.reduceRemoveAuthMethod,
|
2023-08-02 16:57:53 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Event: user.HumanOTPEmailRemovedType,
|
|
|
|
Reduce: p.reduceRemoveAuthMethod,
|
|
|
|
},
|
2022-01-19 13:49:50 +00:00
|
|
|
},
|
|
|
|
},
|
2022-11-30 16:01:17 +00:00
|
|
|
{
|
|
|
|
Aggregate: org.AggregateType,
|
2023-10-19 10:19:10 +00:00
|
|
|
EventReducers: []handler.EventReducer{
|
2022-11-30 16:01:17 +00:00
|
|
|
{
|
|
|
|
Event: org.OrgRemovedEventType,
|
|
|
|
Reduce: p.reduceOwnerRemoved,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-10-20 12:36:52 +00:00
|
|
|
{
|
|
|
|
Aggregate: instance.AggregateType,
|
2023-10-19 10:19:10 +00:00
|
|
|
EventReducers: []handler.EventReducer{
|
2022-10-20 12:36:52 +00:00
|
|
|
{
|
|
|
|
Event: instance.InstanceRemovedEventType,
|
|
|
|
Reduce: reduceInstanceRemovedHelper(UserAuthMethodInstanceIDCol),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-01-19 13:49:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-14 05:51:00 +00:00
|
|
|
func (p *userAuthMethodProjection) reduceInitAuthMethod(event eventstore.Event) (*handler.Statement, error) {
|
2022-01-19 13:49:50 +00:00
|
|
|
tokenID := ""
|
|
|
|
var methodType domain.UserAuthMethodType
|
|
|
|
switch e := event.(type) {
|
|
|
|
case *user.HumanPasswordlessAddedEvent:
|
|
|
|
methodType = domain.UserAuthMethodTypePasswordless
|
|
|
|
tokenID = e.WebAuthNTokenID
|
|
|
|
case *user.HumanU2FAddedEvent:
|
|
|
|
methodType = domain.UserAuthMethodTypeU2F
|
|
|
|
tokenID = e.WebAuthNTokenID
|
|
|
|
case *user.HumanOTPAddedEvent:
|
2023-08-02 16:57:53 +00:00
|
|
|
methodType = domain.UserAuthMethodTypeTOTP
|
2022-01-19 13:49:50 +00:00
|
|
|
default:
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInvalidArgumentf(nil, "PROJE-f92f", "reduce.wrong.event.type %v", []eventstore.EventType{user.HumanPasswordlessTokenAddedType, user.HumanU2FTokenAddedType})
|
2022-01-19 13:49:50 +00:00
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
return handler.NewUpsertStatement(
|
2022-01-19 13:49:50 +00:00
|
|
|
event,
|
2022-08-31 07:52:43 +00:00
|
|
|
[]handler.Column{
|
|
|
|
handler.NewCol(UserAuthMethodInstanceIDCol, nil),
|
|
|
|
handler.NewCol(UserAuthMethodUserIDCol, nil),
|
|
|
|
handler.NewCol(UserAuthMethodTypeCol, nil),
|
|
|
|
handler.NewCol(UserAuthMethodTokenIDCol, nil),
|
|
|
|
},
|
2022-01-19 13:49:50 +00:00
|
|
|
[]handler.Column{
|
|
|
|
handler.NewCol(UserAuthMethodTokenIDCol, tokenID),
|
2024-02-22 17:25:47 +00:00
|
|
|
handler.NewCol(UserAuthMethodCreationDateCol, handler.OnlySetValueOnInsert(UserAuthMethodTable, event.CreatedAt())),
|
2023-10-19 10:19:10 +00:00
|
|
|
handler.NewCol(UserAuthMethodChangeDateCol, event.CreatedAt()),
|
2022-01-19 13:49:50 +00:00
|
|
|
handler.NewCol(UserAuthMethodResourceOwnerCol, event.Aggregate().ResourceOwner),
|
2022-03-23 08:02:39 +00:00
|
|
|
handler.NewCol(UserAuthMethodInstanceIDCol, event.Aggregate().InstanceID),
|
2022-01-19 13:49:50 +00:00
|
|
|
handler.NewCol(UserAuthMethodUserIDCol, event.Aggregate().ID),
|
|
|
|
handler.NewCol(UserAuthMethodSequenceCol, event.Sequence()),
|
|
|
|
handler.NewCol(UserAuthMethodStateCol, domain.MFAStateNotReady),
|
|
|
|
handler.NewCol(UserAuthMethodTypeCol, methodType),
|
|
|
|
handler.NewCol(UserAuthMethodNameCol, ""),
|
|
|
|
},
|
|
|
|
), nil
|
|
|
|
}
|
|
|
|
|
2022-06-14 05:51:00 +00:00
|
|
|
func (p *userAuthMethodProjection) reduceActivateEvent(event eventstore.Event) (*handler.Statement, error) {
|
2022-01-19 13:49:50 +00:00
|
|
|
tokenID := ""
|
|
|
|
name := ""
|
|
|
|
var methodType domain.UserAuthMethodType
|
|
|
|
|
|
|
|
switch e := event.(type) {
|
|
|
|
case *user.HumanPasswordlessVerifiedEvent:
|
|
|
|
methodType = domain.UserAuthMethodTypePasswordless
|
|
|
|
tokenID = e.WebAuthNTokenID
|
|
|
|
name = e.WebAuthNTokenName
|
|
|
|
case *user.HumanU2FVerifiedEvent:
|
|
|
|
methodType = domain.UserAuthMethodTypeU2F
|
|
|
|
tokenID = e.WebAuthNTokenID
|
|
|
|
name = e.WebAuthNTokenName
|
|
|
|
case *user.HumanOTPVerifiedEvent:
|
2023-08-02 16:57:53 +00:00
|
|
|
methodType = domain.UserAuthMethodTypeTOTP
|
2022-01-19 13:49:50 +00:00
|
|
|
|
|
|
|
default:
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInvalidArgumentf(nil, "PROJE-f92f", "reduce.wrong.event.type %v", []eventstore.EventType{user.HumanPasswordlessTokenAddedType, user.HumanU2FTokenAddedType})
|
2022-01-19 13:49:50 +00:00
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
return handler.NewUpdateStatement(
|
2022-01-19 13:49:50 +00:00
|
|
|
event,
|
|
|
|
[]handler.Column{
|
2023-10-19 10:19:10 +00:00
|
|
|
handler.NewCol(UserAuthMethodChangeDateCol, event.CreatedAt()),
|
2022-01-19 13:49:50 +00:00
|
|
|
handler.NewCol(UserAuthMethodSequenceCol, event.Sequence()),
|
|
|
|
handler.NewCol(UserAuthMethodNameCol, name),
|
|
|
|
handler.NewCol(UserAuthMethodStateCol, domain.MFAStateReady),
|
|
|
|
},
|
|
|
|
[]handler.Condition{
|
|
|
|
handler.NewCond(UserAuthMethodUserIDCol, event.Aggregate().ID),
|
|
|
|
handler.NewCond(UserAuthMethodTypeCol, methodType),
|
|
|
|
handler.NewCond(UserAuthMethodResourceOwnerCol, event.Aggregate().ResourceOwner),
|
|
|
|
handler.NewCond(UserAuthMethodTokenIDCol, tokenID),
|
2022-11-10 10:59:33 +00:00
|
|
|
handler.NewCond(UserAuthMethodInstanceIDCol, event.Aggregate().InstanceID),
|
2022-01-19 13:49:50 +00:00
|
|
|
},
|
|
|
|
), nil
|
|
|
|
}
|
|
|
|
|
2023-08-02 16:57:53 +00:00
|
|
|
func (p *userAuthMethodProjection) reduceAddAuthMethod(event eventstore.Event) (*handler.Statement, error) {
|
|
|
|
var methodType domain.UserAuthMethodType
|
|
|
|
switch event.(type) {
|
|
|
|
case *user.HumanOTPSMSAddedEvent:
|
|
|
|
methodType = domain.UserAuthMethodTypeOTPSMS
|
|
|
|
case *user.HumanOTPEmailAddedEvent:
|
|
|
|
methodType = domain.UserAuthMethodTypeOTPEmail
|
|
|
|
default:
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInvalidArgumentf(nil, "PROJE-DS4g3", "reduce.wrong.event.type %v", []eventstore.EventType{user.HumanOTPSMSAddedType, user.HumanOTPEmailAddedType})
|
2023-08-02 16:57:53 +00:00
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
return handler.NewCreateStatement(
|
2023-08-02 16:57:53 +00:00
|
|
|
event,
|
|
|
|
[]handler.Column{
|
|
|
|
handler.NewCol(UserAuthMethodTokenIDCol, ""),
|
2023-10-19 10:19:10 +00:00
|
|
|
handler.NewCol(UserAuthMethodCreationDateCol, event.CreatedAt()),
|
|
|
|
handler.NewCol(UserAuthMethodChangeDateCol, event.CreatedAt()),
|
2023-08-02 16:57:53 +00:00
|
|
|
handler.NewCol(UserAuthMethodResourceOwnerCol, event.Aggregate().ResourceOwner),
|
|
|
|
handler.NewCol(UserAuthMethodInstanceIDCol, event.Aggregate().InstanceID),
|
|
|
|
handler.NewCol(UserAuthMethodUserIDCol, event.Aggregate().ID),
|
|
|
|
handler.NewCol(UserAuthMethodSequenceCol, event.Sequence()),
|
|
|
|
handler.NewCol(UserAuthMethodStateCol, domain.MFAStateReady),
|
|
|
|
handler.NewCol(UserAuthMethodTypeCol, methodType),
|
|
|
|
handler.NewCol(UserAuthMethodNameCol, ""),
|
|
|
|
},
|
|
|
|
), nil
|
|
|
|
}
|
|
|
|
|
2022-06-14 05:51:00 +00:00
|
|
|
func (p *userAuthMethodProjection) reduceRemoveAuthMethod(event eventstore.Event) (*handler.Statement, error) {
|
2022-01-19 13:49:50 +00:00
|
|
|
var tokenID string
|
|
|
|
var methodType domain.UserAuthMethodType
|
|
|
|
switch e := event.(type) {
|
|
|
|
case *user.HumanPasswordlessRemovedEvent:
|
|
|
|
methodType = domain.UserAuthMethodTypePasswordless
|
|
|
|
tokenID = e.WebAuthNTokenID
|
|
|
|
case *user.HumanU2FRemovedEvent:
|
|
|
|
methodType = domain.UserAuthMethodTypeU2F
|
|
|
|
tokenID = e.WebAuthNTokenID
|
|
|
|
case *user.HumanOTPRemovedEvent:
|
2023-08-02 16:57:53 +00:00
|
|
|
methodType = domain.UserAuthMethodTypeTOTP
|
|
|
|
case *user.HumanOTPSMSRemovedEvent,
|
|
|
|
*user.HumanPhoneRemovedEvent:
|
|
|
|
methodType = domain.UserAuthMethodTypeOTPSMS
|
|
|
|
case *user.HumanOTPEmailRemovedEvent:
|
|
|
|
methodType = domain.UserAuthMethodTypeOTPEmail
|
2022-01-19 13:49:50 +00:00
|
|
|
|
|
|
|
default:
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInvalidArgumentf(nil, "PROJE-f92f", "reduce.wrong.event.type %v",
|
2023-08-02 16:57:53 +00:00
|
|
|
[]eventstore.EventType{user.HumanPasswordlessTokenAddedType, user.HumanU2FTokenAddedType, user.HumanMFAOTPRemovedType,
|
|
|
|
user.HumanOTPSMSRemovedType, user.HumanPhoneRemovedType, user.HumanOTPEmailRemovedType})
|
2022-01-19 13:49:50 +00:00
|
|
|
}
|
|
|
|
conditions := []handler.Condition{
|
|
|
|
handler.NewCond(UserAuthMethodUserIDCol, event.Aggregate().ID),
|
|
|
|
handler.NewCond(UserAuthMethodTypeCol, methodType),
|
|
|
|
handler.NewCond(UserAuthMethodResourceOwnerCol, event.Aggregate().ResourceOwner),
|
2022-11-10 10:59:33 +00:00
|
|
|
handler.NewCond(UserAuthMethodInstanceIDCol, event.Aggregate().InstanceID),
|
2022-01-19 13:49:50 +00:00
|
|
|
}
|
|
|
|
if tokenID != "" {
|
|
|
|
conditions = append(conditions, handler.NewCond(UserAuthMethodTokenIDCol, tokenID))
|
|
|
|
}
|
2023-10-19 10:19:10 +00:00
|
|
|
return handler.NewDeleteStatement(
|
2022-01-19 13:49:50 +00:00
|
|
|
event,
|
|
|
|
conditions,
|
|
|
|
), nil
|
|
|
|
}
|
2022-11-30 16:01:17 +00:00
|
|
|
|
|
|
|
func (p *userAuthMethodProjection) reduceOwnerRemoved(event eventstore.Event) (*handler.Statement, error) {
|
|
|
|
e, ok := event.(*org.OrgRemovedEvent)
|
|
|
|
if !ok {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInvalidArgumentf(nil, "PROJE-FwDZ8", "reduce.wrong.event.type %s", org.OrgRemovedEventType)
|
2022-11-30 16:01:17 +00:00
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
return handler.NewDeleteStatement(
|
2022-11-30 16:01:17 +00:00
|
|
|
e,
|
|
|
|
[]handler.Condition{
|
|
|
|
handler.NewCond(UserAuthMethodInstanceIDCol, e.Aggregate().InstanceID),
|
|
|
|
handler.NewCond(UserAuthMethodResourceOwnerCol, e.Aggregate().ID),
|
|
|
|
},
|
|
|
|
), nil
|
|
|
|
}
|