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/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/user"
|
2022-01-19 13:49:50 +00:00
|
|
|
)
|
|
|
|
|
2022-03-23 08:02:39 +00:00
|
|
|
const (
|
2022-09-01 07:10:07 +00:00
|
|
|
UserAuthMethodTable = "projections.user_auth_methods3"
|
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-06-14 05:51:00 +00:00
|
|
|
type userAuthMethodProjection struct {
|
2022-01-19 13:49:50 +00:00
|
|
|
crdb.StatementHandler
|
|
|
|
}
|
|
|
|
|
2022-06-14 05:51:00 +00:00
|
|
|
func newUserAuthMethodProjection(ctx context.Context, config crdb.StatementHandlerConfig) *userAuthMethodProjection {
|
|
|
|
p := new(userAuthMethodProjection)
|
2022-01-19 13:49:50 +00:00
|
|
|
config.ProjectionName = UserAuthMethodTable
|
|
|
|
config.Reducers = p.reducers()
|
2022-03-23 08:02:39 +00:00
|
|
|
config.InitCheck = crdb.NewTableCheck(
|
|
|
|
crdb.NewTable([]*crdb.Column{
|
|
|
|
crdb.NewColumn(UserAuthMethodUserIDCol, crdb.ColumnTypeText),
|
2022-09-01 07:10:07 +00:00
|
|
|
crdb.NewColumn(UserAuthMethodTypeCol, crdb.ColumnTypeEnum),
|
2022-03-23 08:02:39 +00:00
|
|
|
crdb.NewColumn(UserAuthMethodTokenIDCol, crdb.ColumnTypeText),
|
|
|
|
crdb.NewColumn(UserAuthMethodCreationDateCol, crdb.ColumnTypeTimestamp),
|
|
|
|
crdb.NewColumn(UserAuthMethodChangeDateCol, crdb.ColumnTypeTimestamp),
|
|
|
|
crdb.NewColumn(UserAuthMethodSequenceCol, crdb.ColumnTypeInt64),
|
|
|
|
crdb.NewColumn(UserAuthMethodStateCol, crdb.ColumnTypeEnum),
|
|
|
|
crdb.NewColumn(UserAuthMethodResourceOwnerCol, crdb.ColumnTypeText),
|
|
|
|
crdb.NewColumn(UserAuthMethodInstanceIDCol, crdb.ColumnTypeText),
|
|
|
|
crdb.NewColumn(UserAuthMethodNameCol, crdb.ColumnTypeText),
|
|
|
|
},
|
|
|
|
crdb.NewPrimaryKey(UserAuthMethodInstanceIDCol, UserAuthMethodUserIDCol, UserAuthMethodTypeCol, UserAuthMethodTokenIDCol),
|
2022-08-31 07:52:43 +00:00
|
|
|
crdb.WithIndex(crdb.NewIndex("auth_meth_ro_idx", []string{UserAuthMethodResourceOwnerCol})),
|
2022-03-23 08:02:39 +00:00
|
|
|
),
|
|
|
|
)
|
2022-01-19 13:49:50 +00:00
|
|
|
p.StatementHandler = crdb.NewStatementHandler(ctx, config)
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2022-06-14 05:51:00 +00:00
|
|
|
func (p *userAuthMethodProjection) reducers() []handler.AggregateReducer {
|
2022-01-19 13:49:50 +00:00
|
|
|
return []handler.AggregateReducer{
|
|
|
|
{
|
|
|
|
Aggregate: user.AggregateType,
|
|
|
|
EventRedusers: []handler.EventReducer{
|
|
|
|
{
|
|
|
|
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,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Event: user.HumanPasswordlessTokenRemovedType,
|
|
|
|
Reduce: p.reduceRemoveAuthMethod,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Event: user.HumanU2FTokenRemovedType,
|
|
|
|
Reduce: p.reduceRemoveAuthMethod,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Event: user.HumanMFAOTPRemovedType,
|
|
|
|
Reduce: p.reduceRemoveAuthMethod,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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:
|
|
|
|
methodType = domain.UserAuthMethodTypeOTP
|
|
|
|
default:
|
2022-03-23 08:02:39 +00:00
|
|
|
return nil, errors.ThrowInvalidArgumentf(nil, "PROJE-f92f", "reduce.wrong.event.type %v", []eventstore.EventType{user.HumanPasswordlessTokenAddedType, user.HumanU2FTokenAddedType})
|
2022-01-19 13:49:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return crdb.NewUpsertStatement(
|
|
|
|
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),
|
|
|
|
handler.NewCol(UserAuthMethodCreationDateCol, event.CreationDate()),
|
2022-01-20 12:21:59 +00:00
|
|
|
handler.NewCol(UserAuthMethodChangeDateCol, event.CreationDate()),
|
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:
|
|
|
|
methodType = domain.UserAuthMethodTypeOTP
|
|
|
|
|
|
|
|
default:
|
2022-03-23 08:02:39 +00:00
|
|
|
return nil, errors.ThrowInvalidArgumentf(nil, "PROJE-f92f", "reduce.wrong.event.type %v", []eventstore.EventType{user.HumanPasswordlessTokenAddedType, user.HumanU2FTokenAddedType})
|
2022-01-19 13:49:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return crdb.NewUpdateStatement(
|
|
|
|
event,
|
|
|
|
[]handler.Column{
|
2022-01-20 12:21:59 +00:00
|
|
|
handler.NewCol(UserAuthMethodChangeDateCol, event.CreationDate()),
|
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),
|
|
|
|
},
|
|
|
|
), 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:
|
|
|
|
methodType = domain.UserAuthMethodTypeOTP
|
|
|
|
|
|
|
|
default:
|
2022-03-23 08:02:39 +00:00
|
|
|
return nil, errors.ThrowInvalidArgumentf(nil, "PROJE-f92f", "reduce.wrong.event.type %v", []eventstore.EventType{user.HumanPasswordlessTokenAddedType, user.HumanU2FTokenAddedType})
|
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),
|
|
|
|
}
|
|
|
|
if tokenID != "" {
|
|
|
|
conditions = append(conditions, handler.NewCond(UserAuthMethodTokenIDCol, tokenID))
|
|
|
|
}
|
|
|
|
return crdb.NewDeleteStatement(
|
|
|
|
event,
|
|
|
|
conditions,
|
|
|
|
), nil
|
|
|
|
}
|