fix(notification): get origin from all relevant events and fix nil pointer (#6726)

This commit is contained in:
Livio Spring 2023-10-13 17:45:38 +03:00 committed by GitHub
parent 0af1c65c4c
commit ce719a3fa4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 57 additions and 27 deletions

View File

@ -2,9 +2,10 @@ package handlers
import ( import (
"context" "context"
"fmt"
"net/url" "net/url"
"github.com/zitadel/logging"
"github.com/zitadel/zitadel/internal/api/authz" "github.com/zitadel/zitadel/internal/api/authz"
http_utils "github.com/zitadel/zitadel/internal/api/http" http_utils "github.com/zitadel/zitadel/internal/api/http"
"github.com/zitadel/zitadel/internal/errors" "github.com/zitadel/zitadel/internal/errors"
@ -18,11 +19,13 @@ type OriginEvent interface {
} }
func (n *NotificationQueries) Origin(ctx context.Context, e eventstore.Event) (context.Context, error) { func (n *NotificationQueries) Origin(ctx context.Context, e eventstore.Event) (context.Context, error) {
var origin string
originEvent, ok := e.(OriginEvent) originEvent, ok := e.(OriginEvent)
if !ok { if !ok {
return ctx, errors.ThrowInternal(fmt.Errorf("event of type %T doesn't implement OriginEvent", e), "NOTIF-3m9fs", "Errors.Internal") logging.Errorf("event of type %T doesn't implement OriginEvent", e)
} else {
origin = originEvent.TriggerOrigin()
} }
origin := originEvent.TriggerOrigin()
if origin != "" { if origin != "" {
originURL, err := url.Parse(origin) originURL, err := url.Parse(origin)
if err != nil { if err != nil {

View File

@ -33,7 +33,7 @@ func generateEmail(
if err != nil { if err != nil {
return err return err
} }
if emailChannels.Len() == 0 { if emailChannels == nil || emailChannels.Len() == 0 {
return errors.ThrowPreconditionFailed(nil, "MAIL-83nof", "Errors.Notification.Channels.NotPresent") return errors.ThrowPreconditionFailed(nil, "MAIL-83nof", "Errors.Notification.Channels.NotPresent")
} }
return emailChannels.HandleMessage(message) return emailChannels.HandleMessage(message)

View File

@ -4,6 +4,7 @@ import (
"context" "context"
"github.com/zitadel/logging" "github.com/zitadel/logging"
"github.com/zitadel/zitadel/internal/errors" "github.com/zitadel/zitadel/internal/errors"
"github.com/zitadel/zitadel/internal/eventstore" "github.com/zitadel/zitadel/internal/eventstore"
"github.com/zitadel/zitadel/internal/notification/messages" "github.com/zitadel/zitadel/internal/notification/messages"
@ -21,7 +22,7 @@ func generateSms(
number := "" number := ""
smsChannels, twilioConfig, err := channels.SMS(ctx) smsChannels, twilioConfig, err := channels.SMS(ctx)
logging.OnError(err).Error("could not create sms channel") logging.OnError(err).Error("could not create sms channel")
if smsChannels.Len() == 0 { if smsChannels == nil || smsChannels.Len() == 0 {
return errors.ThrowPreconditionFailed(nil, "PHONE-w8nfow", "Errors.Notification.Channels.NotPresent") return errors.ThrowPreconditionFailed(nil, "PHONE-w8nfow", "Errors.Notification.Channels.NotPresent")
} }
if err == nil { if err == nil {

View File

@ -315,6 +315,7 @@ type OTPSMSChallengedEvent struct {
Code *crypto.CryptoValue `json:"code"` Code *crypto.CryptoValue `json:"code"`
Expiry time.Duration `json:"expiry"` Expiry time.Duration `json:"expiry"`
CodeReturned bool `json:"codeReturned,omitempty"` CodeReturned bool `json:"codeReturned,omitempty"`
TriggeredAtOrigin string `json:"triggerOrigin,omitempty"`
} }
func (e *OTPSMSChallengedEvent) Data() interface{} { func (e *OTPSMSChallengedEvent) Data() interface{} {
@ -329,6 +330,10 @@ func (e *OTPSMSChallengedEvent) SetBaseEvent(base *eventstore.BaseEvent) {
e.BaseEvent = *base e.BaseEvent = *base
} }
func (e *OTPSMSChallengedEvent) TriggerOrigin() string {
return e.TriggeredAtOrigin
}
func NewOTPSMSChallengedEvent( func NewOTPSMSChallengedEvent(
ctx context.Context, ctx context.Context,
aggregate *eventstore.Aggregate, aggregate *eventstore.Aggregate,
@ -345,6 +350,7 @@ func NewOTPSMSChallengedEvent(
Code: code, Code: code,
Expiry: expiry, Expiry: expiry,
CodeReturned: codeReturned, CodeReturned: codeReturned,
TriggeredAtOrigin: http.ComposedOrigin(ctx),
} }
} }

View File

@ -5,6 +5,7 @@ import (
"encoding/json" "encoding/json"
"time" "time"
"github.com/zitadel/zitadel/internal/api/http"
"github.com/zitadel/zitadel/internal/eventstore" "github.com/zitadel/zitadel/internal/eventstore"
"github.com/zitadel/zitadel/internal/crypto" "github.com/zitadel/zitadel/internal/crypto"
@ -281,6 +282,7 @@ type HumanOTPSMSCodeAddedEvent struct {
Code *crypto.CryptoValue `json:"code,omitempty"` Code *crypto.CryptoValue `json:"code,omitempty"`
Expiry time.Duration `json:"expiry,omitempty"` Expiry time.Duration `json:"expiry,omitempty"`
TriggeredAtOrigin string `json:"triggerOrigin,omitempty"`
*AuthRequestInfo *AuthRequestInfo
} }
@ -296,6 +298,10 @@ func (e *HumanOTPSMSCodeAddedEvent) SetBaseEvent(event *eventstore.BaseEvent) {
e.BaseEvent = *event e.BaseEvent = *event
} }
func (e *HumanOTPSMSCodeAddedEvent) TriggerOrigin() string {
return e.TriggeredAtOrigin
}
func NewHumanOTPSMSCodeAddedEvent( func NewHumanOTPSMSCodeAddedEvent(
ctx context.Context, ctx context.Context,
aggregate *eventstore.Aggregate, aggregate *eventstore.Aggregate,
@ -311,6 +317,7 @@ func NewHumanOTPSMSCodeAddedEvent(
), ),
Code: code, Code: code,
Expiry: expiry, Expiry: expiry,
TriggeredAtOrigin: http.ComposedOrigin(ctx),
AuthRequestInfo: info, AuthRequestInfo: info,
} }
} }
@ -475,6 +482,7 @@ type HumanOTPEmailCodeAddedEvent struct {
Code *crypto.CryptoValue `json:"code,omitempty"` Code *crypto.CryptoValue `json:"code,omitempty"`
Expiry time.Duration `json:"expiry,omitempty"` Expiry time.Duration `json:"expiry,omitempty"`
TriggeredAtOrigin string `json:"triggerOrigin,omitempty"`
*AuthRequestInfo *AuthRequestInfo
} }
@ -490,6 +498,10 @@ func (e *HumanOTPEmailCodeAddedEvent) SetBaseEvent(event *eventstore.BaseEvent)
e.BaseEvent = *event e.BaseEvent = *event
} }
func (e *HumanOTPEmailCodeAddedEvent) TriggerOrigin() string {
return e.TriggeredAtOrigin
}
func NewHumanOTPEmailCodeAddedEvent( func NewHumanOTPEmailCodeAddedEvent(
ctx context.Context, ctx context.Context,
aggregate *eventstore.Aggregate, aggregate *eventstore.Aggregate,
@ -506,6 +518,7 @@ func NewHumanOTPEmailCodeAddedEvent(
Code: code, Code: code,
Expiry: expiry, Expiry: expiry,
AuthRequestInfo: info, AuthRequestInfo: info,
TriggeredAtOrigin: http.ComposedOrigin(ctx),
} }
} }

View File

@ -5,6 +5,7 @@ import (
"encoding/json" "encoding/json"
"time" "time"
"github.com/zitadel/zitadel/internal/api/http"
"github.com/zitadel/zitadel/internal/crypto" "github.com/zitadel/zitadel/internal/crypto"
"github.com/zitadel/zitadel/internal/domain" "github.com/zitadel/zitadel/internal/domain"
"github.com/zitadel/zitadel/internal/errors" "github.com/zitadel/zitadel/internal/errors"
@ -152,6 +153,7 @@ type HumanPhoneCodeAddedEvent struct {
Code *crypto.CryptoValue `json:"code,omitempty"` Code *crypto.CryptoValue `json:"code,omitempty"`
Expiry time.Duration `json:"expiry,omitempty"` Expiry time.Duration `json:"expiry,omitempty"`
CodeReturned bool `json:"code_returned,omitempty"` CodeReturned bool `json:"code_returned,omitempty"`
TriggeredAtOrigin string `json:"triggerOrigin,omitempty"`
} }
func (e *HumanPhoneCodeAddedEvent) Data() interface{} { func (e *HumanPhoneCodeAddedEvent) Data() interface{} {
@ -162,6 +164,10 @@ func (e *HumanPhoneCodeAddedEvent) UniqueConstraints() []*eventstore.EventUnique
return nil return nil
} }
func (e *HumanPhoneCodeAddedEvent) TriggerOrigin() string {
return e.TriggeredAtOrigin
}
func NewHumanPhoneCodeAddedEvent( func NewHumanPhoneCodeAddedEvent(
ctx context.Context, ctx context.Context,
aggregate *eventstore.Aggregate, aggregate *eventstore.Aggregate,
@ -186,6 +192,7 @@ func NewHumanPhoneCodeAddedEventV2(
Code: code, Code: code,
Expiry: expiry, Expiry: expiry,
CodeReturned: codeReturned, CodeReturned: codeReturned,
TriggeredAtOrigin: http.ComposedOrigin(ctx),
} }
} }