fix: properly handle otp sms challenge notification in session api (#7653)

* fix: properly handle otp sms challenge notification in session api

* small fix
This commit is contained in:
Livio Spring 2024-03-27 18:48:14 +01:00 committed by GitHub
parent 56df515e5f
commit 1e53aab4b4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 43 additions and 19 deletions

View File

@ -6,21 +6,45 @@ import (
"github.com/zitadel/zitadel/internal/eventstore" "github.com/zitadel/zitadel/internal/eventstore"
) )
func (n *NotificationQueries) IsAlreadyHandled(ctx context.Context, event eventstore.Event, data map[string]interface{}, aggregateType eventstore.AggregateType, eventTypes ...eventstore.EventType) (bool, error) { type alreadyHandled struct {
events, err := n.es.Filter( event eventstore.Event
ctx, eventTypes []eventstore.EventType
eventstore.NewSearchQueryBuilder(eventstore.ColumnsEvent). data map[string]interface{}
InstanceID(event.Aggregate().InstanceID).
SequenceGreater(event.Sequence()). handled bool
AddQuery(). }
AggregateTypes(aggregateType).
AggregateIDs(event.Aggregate().ID). func (a *alreadyHandled) Reduce() error {
EventTypes(eventTypes...). return nil
EventData(data). }
Builder(),
) func (a *alreadyHandled) AppendEvents(event ...eventstore.Event) {
if len(event) > 0 {
a.handled = true
}
}
func (a *alreadyHandled) Query() *eventstore.SearchQueryBuilder {
return eventstore.NewSearchQueryBuilder(eventstore.ColumnsEvent).
InstanceID(a.event.Aggregate().InstanceID).
SequenceGreater(a.event.Sequence()).
AddQuery().
AggregateTypes(a.event.Aggregate().Type).
AggregateIDs(a.event.Aggregate().ID).
EventTypes(a.eventTypes...).
EventData(a.data).
Builder()
}
func (n *NotificationQueries) IsAlreadyHandled(ctx context.Context, event eventstore.Event, data map[string]interface{}, eventTypes ...eventstore.EventType) (bool, error) {
already := &alreadyHandled{
event: event,
eventTypes: eventTypes,
data: data,
}
err := n.es.FilterToQueryReducer(ctx, already)
if err != nil { if err != nil {
return false, err return false, err
} }
return len(events) > 0, nil return already.handled, nil
} }

View File

@ -65,7 +65,7 @@ func (u *quotaNotifier) reduceNotificationDue(event eventstore.Event) (*handler.
return handler.NewStatement(event, func(ex handler.Executer, projectionName string) error { return handler.NewStatement(event, func(ex handler.Executer, projectionName string) error {
ctx := HandlerContext(event.Aggregate()) ctx := HandlerContext(event.Aggregate())
alreadyHandled, err := u.queries.IsAlreadyHandled(ctx, event, map[string]interface{}{"dueEventID": e.ID}, quota.AggregateType, quota.NotifiedEventType) alreadyHandled, err := u.queries.IsAlreadyHandled(ctx, event, map[string]interface{}{"dueEventID": e.ID}, quota.NotifiedEventType)
if err != nil { if err != nil {
return err return err
} }

View File

@ -378,7 +378,7 @@ func (u *userNotifier) reduceOTPSMS(
if err != nil { if err != nil {
return nil, err return nil, err
} }
err = sentCommand(ctx, userID, resourceOwner) err = sentCommand(ctx, event.Aggregate().ID, event.Aggregate().ResourceOwner)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -514,7 +514,7 @@ func (u *userNotifier) reduceDomainClaimed(event eventstore.Event) (*handler.Sta
} }
return handler.NewStatement(event, func(ex handler.Executer, projectionName string) error { return handler.NewStatement(event, func(ex handler.Executer, projectionName string) error {
ctx := HandlerContext(event.Aggregate()) ctx := HandlerContext(event.Aggregate())
alreadyHandled, err := u.queries.IsAlreadyHandled(ctx, event, nil, user.AggregateType, alreadyHandled, err := u.queries.IsAlreadyHandled(ctx, event, nil,
user.UserDomainClaimedType, user.UserDomainClaimedSentType) user.UserDomainClaimedType, user.UserDomainClaimedSentType)
if err != nil { if err != nil {
return err return err
@ -616,7 +616,7 @@ func (u *userNotifier) reducePasswordChanged(event eventstore.Event) (*handler.S
return handler.NewStatement(event, func(ex handler.Executer, projectionName string) error { return handler.NewStatement(event, func(ex handler.Executer, projectionName string) error {
ctx := HandlerContext(event.Aggregate()) ctx := HandlerContext(event.Aggregate())
alreadyHandled, err := u.queries.IsAlreadyHandled(ctx, event, nil, user.AggregateType, user.HumanPasswordChangeSentType) alreadyHandled, err := u.queries.IsAlreadyHandled(ctx, event, nil, user.HumanPasswordChangeSentType)
if err != nil { if err != nil {
return err return err
} }
@ -722,5 +722,5 @@ func (u *userNotifier) checkIfCodeAlreadyHandledOrExpired(ctx context.Context, e
if event.CreatedAt().Add(expiry).Before(time.Now().UTC()) { if event.CreatedAt().Add(expiry).Before(time.Now().UTC()) {
return true, nil return true, nil
} }
return u.queries.IsAlreadyHandled(ctx, event, data, user.AggregateType, eventTypes...) return u.queries.IsAlreadyHandled(ctx, event, data, eventTypes...)
} }