2023-03-28 22:09:06 +00:00
|
|
|
package handlers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-08-24 09:41:52 +00:00
|
|
|
"strings"
|
2023-03-28 22:09:06 +00:00
|
|
|
"time"
|
|
|
|
|
2023-10-10 13:20:53 +00:00
|
|
|
http_util "github.com/zitadel/zitadel/internal/api/http"
|
2023-08-24 09:41:52 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/api/ui/login"
|
2023-03-28 22:09:06 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/crypto"
|
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
2023-10-19 10:19:10 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/eventstore/handler/v2"
|
2024-09-26 07:14:33 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/notification/senders"
|
2023-03-28 22:09:06 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/notification/types"
|
2023-08-24 09:41:52 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/query"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/session"
|
2023-03-28 22:09:06 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/repository/user"
|
2023-12-08 14:30:55 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/zerrors"
|
2023-03-28 22:09:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
UserNotificationsProjectionTable = "projections.notifications"
|
|
|
|
)
|
|
|
|
|
|
|
|
type userNotifier struct {
|
2023-10-10 13:20:53 +00:00
|
|
|
commands Commands
|
2023-03-28 22:09:06 +00:00
|
|
|
queries *NotificationQueries
|
2023-10-10 13:20:53 +00:00
|
|
|
channels types.ChannelChains
|
2023-08-24 09:41:52 +00:00
|
|
|
otpEmailTmpl string
|
2023-03-28 22:09:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewUserNotifier(
|
|
|
|
ctx context.Context,
|
2023-10-19 10:19:10 +00:00
|
|
|
config handler.Config,
|
2023-10-10 13:20:53 +00:00
|
|
|
commands Commands,
|
2023-03-28 22:09:06 +00:00
|
|
|
queries *NotificationQueries,
|
2023-10-10 13:20:53 +00:00
|
|
|
channels types.ChannelChains,
|
2023-08-24 09:41:52 +00:00
|
|
|
otpEmailTmpl string,
|
2023-10-19 10:19:10 +00:00
|
|
|
) *handler.Handler {
|
|
|
|
return handler.NewHandler(ctx, &config, &userNotifier{
|
|
|
|
commands: commands,
|
|
|
|
queries: queries,
|
|
|
|
otpEmailTmpl: otpEmailTmpl,
|
|
|
|
channels: channels,
|
|
|
|
})
|
2023-03-28 22:09:06 +00:00
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (u *userNotifier) Name() string {
|
|
|
|
return UserNotificationsProjectionTable
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *userNotifier) Reducers() []handler.AggregateReducer {
|
2023-03-28 22:09:06 +00:00
|
|
|
return []handler.AggregateReducer{
|
|
|
|
{
|
|
|
|
Aggregate: user.AggregateType,
|
2023-10-19 10:19:10 +00:00
|
|
|
EventReducers: []handler.EventReducer{
|
2023-03-28 22:09:06 +00:00
|
|
|
{
|
|
|
|
Event: user.UserV1InitialCodeAddedType,
|
|
|
|
Reduce: u.reduceInitCodeAdded,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Event: user.HumanInitialCodeAddedType,
|
|
|
|
Reduce: u.reduceInitCodeAdded,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Event: user.UserV1EmailCodeAddedType,
|
|
|
|
Reduce: u.reduceEmailCodeAdded,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Event: user.HumanEmailCodeAddedType,
|
|
|
|
Reduce: u.reduceEmailCodeAdded,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Event: user.UserV1PasswordCodeAddedType,
|
|
|
|
Reduce: u.reducePasswordCodeAdded,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Event: user.HumanPasswordCodeAddedType,
|
|
|
|
Reduce: u.reducePasswordCodeAdded,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Event: user.UserDomainClaimedType,
|
|
|
|
Reduce: u.reduceDomainClaimed,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Event: user.HumanPasswordlessInitCodeRequestedType,
|
|
|
|
Reduce: u.reducePasswordlessCodeRequested,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Event: user.UserV1PhoneCodeAddedType,
|
|
|
|
Reduce: u.reducePhoneCodeAdded,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Event: user.HumanPhoneCodeAddedType,
|
|
|
|
Reduce: u.reducePhoneCodeAdded,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Event: user.HumanPasswordChangedType,
|
|
|
|
Reduce: u.reducePasswordChanged,
|
|
|
|
},
|
2023-08-15 12:47:05 +00:00
|
|
|
{
|
|
|
|
Event: user.HumanOTPSMSCodeAddedType,
|
|
|
|
Reduce: u.reduceOTPSMSCodeAdded,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Event: user.HumanOTPEmailCodeAddedType,
|
|
|
|
Reduce: u.reduceOTPEmailCodeAdded,
|
|
|
|
},
|
2024-09-11 10:53:55 +00:00
|
|
|
{
|
|
|
|
Event: user.HumanInviteCodeAddedType,
|
|
|
|
Reduce: u.reduceInviteCodeAdded,
|
|
|
|
},
|
2023-03-28 22:09:06 +00:00
|
|
|
},
|
|
|
|
},
|
2023-08-24 09:41:52 +00:00
|
|
|
{
|
|
|
|
Aggregate: session.AggregateType,
|
2023-10-19 10:19:10 +00:00
|
|
|
EventReducers: []handler.EventReducer{
|
2023-08-24 09:41:52 +00:00
|
|
|
{
|
|
|
|
Event: session.OTPSMSChallengedType,
|
|
|
|
Reduce: u.reduceSessionOTPSMSChallenged,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Event: session.OTPEmailChallengedType,
|
|
|
|
Reduce: u.reduceSessionOTPEmailChallenged,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2023-03-28 22:09:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *userNotifier) reduceInitCodeAdded(event eventstore.Event) (*handler.Statement, error) {
|
|
|
|
e, ok := event.(*user.HumanInitialCodeAddedEvent)
|
|
|
|
if !ok {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInvalidArgumentf(nil, "HANDL-EFe2f", "reduce.wrong.event.type %s", user.HumanInitialCodeAddedType)
|
2023-03-28 22:09:06 +00:00
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
return handler.NewStatement(event, func(ex handler.Executer, projectionName string) error {
|
|
|
|
ctx := HandlerContext(event.Aggregate())
|
|
|
|
alreadyHandled, err := u.checkIfCodeAlreadyHandledOrExpired(ctx, event, e.Expiry, nil,
|
|
|
|
user.UserV1InitialCodeAddedType, user.UserV1InitialCodeSentType,
|
|
|
|
user.HumanInitialCodeAddedType, user.HumanInitialCodeSentType)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if alreadyHandled {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
code, err := crypto.DecryptString(e.Code, u.queries.UserDataCrypto)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
colors, err := u.queries.ActiveLabelPolicyByOrg(ctx, e.Aggregate().ResourceOwner, false)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-03-28 22:09:06 +00:00
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
template, err := u.queries.MailTemplateByOrg(ctx, e.Aggregate().ResourceOwner, false)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-11-21 12:11:38 +00:00
|
|
|
notifyUser, err := u.queries.GetNotifyUserByID(ctx, true, e.Aggregate().ID)
|
2023-10-19 10:19:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
translator, err := u.queries.GetTranslatorWithOrgTexts(ctx, notifyUser.ResourceOwner, domain.InitCodeMessageType)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx, err = u.queries.Origin(ctx, e)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = types.SendEmail(ctx, u.channels, string(template.Template), translator, notifyUser, colors, e).
|
2024-04-24 15:50:58 +00:00
|
|
|
SendUserInitCode(ctx, notifyUser, code, e.AuthRequestID)
|
2023-10-19 10:19:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return u.commands.HumanInitCodeSent(ctx, e.Aggregate().ResourceOwner, e.Aggregate().ID)
|
|
|
|
}), nil
|
2023-03-28 22:09:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (u *userNotifier) reduceEmailCodeAdded(event eventstore.Event) (*handler.Statement, error) {
|
|
|
|
e, ok := event.(*user.HumanEmailCodeAddedEvent)
|
|
|
|
if !ok {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInvalidArgumentf(nil, "HANDL-SWf3g", "reduce.wrong.event.type %s", user.HumanEmailCodeAddedType)
|
2023-03-28 22:09:06 +00:00
|
|
|
}
|
2023-08-03 04:42:59 +00:00
|
|
|
|
2023-04-25 07:02:29 +00:00
|
|
|
if e.CodeReturned {
|
2023-10-19 10:19:10 +00:00
|
|
|
return handler.NewNoOpStatement(e), nil
|
2023-03-28 22:09:06 +00:00
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
return handler.NewStatement(event, func(ex handler.Executer, projectionName string) error {
|
|
|
|
ctx := HandlerContext(event.Aggregate())
|
|
|
|
alreadyHandled, err := u.checkIfCodeAlreadyHandledOrExpired(ctx, event, e.Expiry, nil,
|
|
|
|
user.UserV1EmailCodeAddedType, user.UserV1EmailCodeSentType,
|
|
|
|
user.HumanEmailCodeAddedType, user.HumanEmailCodeSentType)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if alreadyHandled {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
code, err := crypto.DecryptString(e.Code, u.queries.UserDataCrypto)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
colors, err := u.queries.ActiveLabelPolicyByOrg(ctx, e.Aggregate().ResourceOwner, false)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-03-28 22:09:06 +00:00
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
template, err := u.queries.MailTemplateByOrg(ctx, e.Aggregate().ResourceOwner, false)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-11-21 12:11:38 +00:00
|
|
|
notifyUser, err := u.queries.GetNotifyUserByID(ctx, true, e.Aggregate().ID)
|
2023-10-19 10:19:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
translator, err := u.queries.GetTranslatorWithOrgTexts(ctx, notifyUser.ResourceOwner, domain.VerifyEmailMessageType)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx, err = u.queries.Origin(ctx, e)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = types.SendEmail(ctx, u.channels, string(template.Template), translator, notifyUser, colors, e).
|
2024-04-24 15:50:58 +00:00
|
|
|
SendEmailVerificationCode(ctx, notifyUser, code, e.URLTemplate, e.AuthRequestID)
|
2023-10-19 10:19:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return u.commands.HumanEmailVerificationCodeSent(ctx, e.Aggregate().ResourceOwner, e.Aggregate().ID)
|
|
|
|
}), nil
|
2023-03-28 22:09:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (u *userNotifier) reducePasswordCodeAdded(event eventstore.Event) (*handler.Statement, error) {
|
|
|
|
e, ok := event.(*user.HumanPasswordCodeAddedEvent)
|
|
|
|
if !ok {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInvalidArgumentf(nil, "HANDL-Eeg3s", "reduce.wrong.event.type %s", user.HumanPasswordCodeAddedType)
|
2023-03-28 22:09:06 +00:00
|
|
|
}
|
2023-06-20 15:34:06 +00:00
|
|
|
if e.CodeReturned {
|
2023-10-19 10:19:10 +00:00
|
|
|
return handler.NewNoOpStatement(e), nil
|
2023-03-28 22:09:06 +00:00
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
return handler.NewStatement(event, func(ex handler.Executer, projectionName string) error {
|
|
|
|
ctx := HandlerContext(event.Aggregate())
|
|
|
|
alreadyHandled, err := u.checkIfCodeAlreadyHandledOrExpired(ctx, event, e.Expiry, nil,
|
|
|
|
user.UserV1PasswordCodeAddedType, user.UserV1PasswordCodeSentType,
|
|
|
|
user.HumanPasswordCodeAddedType, user.HumanPasswordCodeSentType)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if alreadyHandled {
|
|
|
|
return nil
|
|
|
|
}
|
2024-09-26 07:14:33 +00:00
|
|
|
var code string
|
|
|
|
if e.Code != nil {
|
|
|
|
code, err = crypto.DecryptString(e.Code, u.queries.UserDataCrypto)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-10-19 10:19:10 +00:00
|
|
|
}
|
|
|
|
colors, err := u.queries.ActiveLabelPolicyByOrg(ctx, e.Aggregate().ResourceOwner, false)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-03-28 22:09:06 +00:00
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
template, err := u.queries.MailTemplateByOrg(ctx, e.Aggregate().ResourceOwner, false)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-11-21 12:11:38 +00:00
|
|
|
notifyUser, err := u.queries.GetNotifyUserByID(ctx, true, e.Aggregate().ID)
|
2023-10-19 10:19:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
translator, err := u.queries.GetTranslatorWithOrgTexts(ctx, notifyUser.ResourceOwner, domain.PasswordResetMessageType)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx, err = u.queries.Origin(ctx, e)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-09-26 07:14:33 +00:00
|
|
|
generatorInfo := new(senders.CodeGeneratorInfo)
|
2023-10-19 10:19:10 +00:00
|
|
|
notify := types.SendEmail(ctx, u.channels, string(template.Template), translator, notifyUser, colors, e)
|
|
|
|
if e.NotificationType == domain.NotificationTypeSms {
|
2024-09-26 07:14:33 +00:00
|
|
|
notify = types.SendSMS(ctx, u.channels, translator, notifyUser, colors, e, generatorInfo)
|
2023-10-19 10:19:10 +00:00
|
|
|
}
|
2024-04-24 15:50:58 +00:00
|
|
|
err = notify.SendPasswordCode(ctx, notifyUser, code, e.URLTemplate, e.AuthRequestID)
|
2023-10-19 10:19:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-09-26 07:14:33 +00:00
|
|
|
return u.commands.PasswordCodeSent(ctx, e.Aggregate().ResourceOwner, e.Aggregate().ID, generatorInfo)
|
2023-10-19 10:19:10 +00:00
|
|
|
}), nil
|
2023-03-28 22:09:06 +00:00
|
|
|
}
|
|
|
|
|
2023-08-15 12:47:05 +00:00
|
|
|
func (u *userNotifier) reduceOTPSMSCodeAdded(event eventstore.Event) (*handler.Statement, error) {
|
|
|
|
e, ok := event.(*user.HumanOTPSMSCodeAddedEvent)
|
|
|
|
if !ok {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInvalidArgumentf(nil, "HANDL-ASF3g", "reduce.wrong.event.type %s", user.HumanOTPSMSCodeAddedType)
|
2023-08-15 12:47:05 +00:00
|
|
|
}
|
2023-08-24 09:41:52 +00:00
|
|
|
return u.reduceOTPSMS(
|
|
|
|
e,
|
|
|
|
e.Code,
|
|
|
|
e.Expiry,
|
|
|
|
e.Aggregate().ID,
|
|
|
|
e.Aggregate().ResourceOwner,
|
|
|
|
u.commands.HumanOTPSMSCodeSent,
|
|
|
|
user.HumanOTPSMSCodeAddedType,
|
|
|
|
user.HumanOTPSMSCodeSentType,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *userNotifier) reduceSessionOTPSMSChallenged(event eventstore.Event) (*handler.Statement, error) {
|
|
|
|
e, ok := event.(*session.OTPSMSChallengedEvent)
|
|
|
|
if !ok {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInvalidArgumentf(nil, "HANDL-Sk32L", "reduce.wrong.event.type %s", session.OTPSMSChallengedType)
|
2023-08-24 09:41:52 +00:00
|
|
|
}
|
|
|
|
if e.CodeReturned {
|
2023-10-19 10:19:10 +00:00
|
|
|
return handler.NewNoOpStatement(e), nil
|
2023-08-24 09:41:52 +00:00
|
|
|
}
|
2023-08-15 12:47:05 +00:00
|
|
|
ctx := HandlerContext(event.Aggregate())
|
2023-08-24 09:41:52 +00:00
|
|
|
s, err := u.queries.SessionByID(ctx, true, e.Aggregate().ID, "")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return u.reduceOTPSMS(
|
|
|
|
e,
|
|
|
|
e.Code,
|
|
|
|
e.Expiry,
|
|
|
|
s.UserFactor.UserID,
|
|
|
|
s.UserFactor.ResourceOwner,
|
|
|
|
u.commands.OTPSMSSent,
|
|
|
|
session.OTPSMSChallengedType,
|
|
|
|
session.OTPSMSSentType,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *userNotifier) reduceOTPSMS(
|
|
|
|
event eventstore.Event,
|
|
|
|
code *crypto.CryptoValue,
|
|
|
|
expiry time.Duration,
|
|
|
|
userID,
|
|
|
|
resourceOwner string,
|
2024-09-26 07:14:33 +00:00
|
|
|
sentCommand func(ctx context.Context, userID, resourceOwner string, generatorInfo *senders.CodeGeneratorInfo) (err error),
|
2023-08-24 09:41:52 +00:00
|
|
|
eventTypes ...eventstore.EventType,
|
|
|
|
) (*handler.Statement, error) {
|
|
|
|
ctx := HandlerContext(event.Aggregate())
|
|
|
|
alreadyHandled, err := u.checkIfCodeAlreadyHandledOrExpired(ctx, event, expiry, nil, eventTypes...)
|
2023-08-15 12:47:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if alreadyHandled {
|
2023-10-19 10:19:10 +00:00
|
|
|
return handler.NewNoOpStatement(event), nil
|
2023-08-15 12:47:05 +00:00
|
|
|
}
|
2024-09-26 07:14:33 +00:00
|
|
|
var plainCode string
|
|
|
|
if code != nil {
|
|
|
|
plainCode, err = crypto.DecryptString(code, u.queries.UserDataCrypto)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-08-15 12:47:05 +00:00
|
|
|
}
|
2023-08-24 09:41:52 +00:00
|
|
|
colors, err := u.queries.ActiveLabelPolicyByOrg(ctx, resourceOwner, false)
|
2023-08-15 12:47:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-11-21 12:11:38 +00:00
|
|
|
notifyUser, err := u.queries.GetNotifyUserByID(ctx, true, userID)
|
2023-08-15 12:47:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
translator, err := u.queries.GetTranslatorWithOrgTexts(ctx, notifyUser.ResourceOwner, domain.VerifySMSOTPMessageType)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-10-10 13:20:53 +00:00
|
|
|
ctx, err = u.queries.Origin(ctx, event)
|
2023-08-15 12:47:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-09-26 07:14:33 +00:00
|
|
|
generatorInfo := new(senders.CodeGeneratorInfo)
|
|
|
|
notify := types.SendSMS(ctx, u.channels, translator, notifyUser, colors, event, generatorInfo)
|
2023-10-10 13:20:53 +00:00
|
|
|
err = notify.SendOTPSMSCode(ctx, plainCode, expiry)
|
2023-08-15 12:47:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-09-26 07:14:33 +00:00
|
|
|
err = sentCommand(ctx, event.Aggregate().ID, event.Aggregate().ResourceOwner, generatorInfo)
|
2023-08-15 12:47:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-10-19 10:19:10 +00:00
|
|
|
return handler.NewNoOpStatement(event), nil
|
2023-08-15 12:47:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (u *userNotifier) reduceOTPEmailCodeAdded(event eventstore.Event) (*handler.Statement, error) {
|
|
|
|
e, ok := event.(*user.HumanOTPEmailCodeAddedEvent)
|
|
|
|
if !ok {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInvalidArgumentf(nil, "HANDL-JL3hw", "reduce.wrong.event.type %s", user.HumanOTPEmailCodeAddedType)
|
2023-08-15 12:47:05 +00:00
|
|
|
}
|
2023-08-24 09:41:52 +00:00
|
|
|
var authRequestID string
|
|
|
|
if e.AuthRequestInfo != nil {
|
|
|
|
authRequestID = e.AuthRequestInfo.ID
|
|
|
|
}
|
|
|
|
url := func(code, origin string, _ *query.NotifyUser) (string, error) {
|
|
|
|
return login.OTPLink(origin, authRequestID, code, domain.MFATypeOTPEmail), nil
|
|
|
|
}
|
|
|
|
return u.reduceOTPEmail(
|
|
|
|
e,
|
|
|
|
e.Code,
|
|
|
|
e.Expiry,
|
|
|
|
e.Aggregate().ID,
|
|
|
|
e.Aggregate().ResourceOwner,
|
|
|
|
url,
|
|
|
|
u.commands.HumanOTPEmailCodeSent,
|
|
|
|
user.HumanOTPEmailCodeAddedType,
|
|
|
|
user.HumanOTPEmailCodeSentType,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *userNotifier) reduceSessionOTPEmailChallenged(event eventstore.Event) (*handler.Statement, error) {
|
|
|
|
e, ok := event.(*session.OTPEmailChallengedEvent)
|
|
|
|
if !ok {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInvalidArgumentf(nil, "HANDL-zbsgt", "reduce.wrong.event.type %s", session.OTPEmailChallengedType)
|
2023-08-24 09:41:52 +00:00
|
|
|
}
|
|
|
|
if e.ReturnCode {
|
2023-10-19 10:19:10 +00:00
|
|
|
return handler.NewNoOpStatement(e), nil
|
2023-08-24 09:41:52 +00:00
|
|
|
}
|
2023-08-15 12:47:05 +00:00
|
|
|
ctx := HandlerContext(event.Aggregate())
|
2023-08-24 09:41:52 +00:00
|
|
|
s, err := u.queries.SessionByID(ctx, true, e.Aggregate().ID, "")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
url := func(code, origin string, user *query.NotifyUser) (string, error) {
|
|
|
|
var buf strings.Builder
|
|
|
|
urlTmpl := origin + u.otpEmailTmpl
|
|
|
|
if e.URLTmpl != "" {
|
|
|
|
urlTmpl = e.URLTmpl
|
|
|
|
}
|
2024-10-10 13:53:32 +00:00
|
|
|
if err := domain.RenderOTPEmailURLTemplate(&buf, urlTmpl, code, user.ID, user.PreferredLoginName, user.DisplayName, e.Aggregate().ID, user.PreferredLanguage); err != nil {
|
2023-08-24 09:41:52 +00:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return buf.String(), nil
|
|
|
|
}
|
|
|
|
return u.reduceOTPEmail(
|
|
|
|
e,
|
|
|
|
e.Code,
|
|
|
|
e.Expiry,
|
|
|
|
s.UserFactor.UserID,
|
|
|
|
s.UserFactor.ResourceOwner,
|
|
|
|
url,
|
|
|
|
u.commands.OTPEmailSent,
|
|
|
|
user.HumanOTPEmailCodeAddedType,
|
|
|
|
user.HumanOTPEmailCodeSentType,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *userNotifier) reduceOTPEmail(
|
|
|
|
event eventstore.Event,
|
|
|
|
code *crypto.CryptoValue,
|
|
|
|
expiry time.Duration,
|
|
|
|
userID,
|
|
|
|
resourceOwner string,
|
|
|
|
urlTmpl func(code, origin string, user *query.NotifyUser) (string, error),
|
|
|
|
sentCommand func(ctx context.Context, userID string, resourceOwner string) (err error),
|
|
|
|
eventTypes ...eventstore.EventType,
|
|
|
|
) (*handler.Statement, error) {
|
|
|
|
ctx := HandlerContext(event.Aggregate())
|
|
|
|
alreadyHandled, err := u.checkIfCodeAlreadyHandledOrExpired(ctx, event, expiry, nil, eventTypes...)
|
2023-08-15 12:47:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if alreadyHandled {
|
2023-10-19 10:19:10 +00:00
|
|
|
return handler.NewNoOpStatement(event), nil
|
2023-08-15 12:47:05 +00:00
|
|
|
}
|
2023-08-24 09:41:52 +00:00
|
|
|
plainCode, err := crypto.DecryptString(code, u.queries.UserDataCrypto)
|
2023-08-15 12:47:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-08-24 09:41:52 +00:00
|
|
|
colors, err := u.queries.ActiveLabelPolicyByOrg(ctx, resourceOwner, false)
|
2023-08-15 12:47:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-08-24 09:41:52 +00:00
|
|
|
template, err := u.queries.MailTemplateByOrg(ctx, resourceOwner, false)
|
2023-08-15 12:47:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-11-21 12:11:38 +00:00
|
|
|
notifyUser, err := u.queries.GetNotifyUserByID(ctx, true, userID)
|
2023-08-15 12:47:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-08-24 09:41:52 +00:00
|
|
|
translator, err := u.queries.GetTranslatorWithOrgTexts(ctx, resourceOwner, domain.VerifyEmailOTPMessageType)
|
2023-08-15 12:47:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-10-10 13:20:53 +00:00
|
|
|
ctx, err = u.queries.Origin(ctx, event)
|
2023-08-15 12:47:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
feat: trusted (instance) domains (#8369)
# Which Problems Are Solved
ZITADEL currently selects the instance context based on a HTTP header
(see https://github.com/zitadel/zitadel/issues/8279#issue-2399959845 and
checks it against the list of instance domains. Let's call it instance
or API domain.
For any context based URL (e.g. OAuth, OIDC, SAML endpoints, links in
emails, ...) the requested domain (instance domain) will be used. Let's
call it the public domain.
In cases of proxied setups, all exposed domains (public domains) require
the domain to be managed as instance domain.
This can either be done using the "ExternalDomain" in the runtime config
or via system API, which requires a validation through CustomerPortal on
zitadel.cloud.
# How the Problems Are Solved
- Two new headers / header list are added:
- `InstanceHostHeaders`: an ordered list (first sent wins), which will
be used to match the instance.
(For backward compatibility: the `HTTP1HostHeader`, `HTTP2HostHeader`
and `forwarded`, `x-forwarded-for`, `x-forwarded-host` are checked
afterwards as well)
- `PublicHostHeaders`: an ordered list (first sent wins), which will be
used as public host / domain. This will be checked against a list of
trusted domains on the instance.
- The middleware intercepts all requests to the API and passes a
`DomainCtx` object with the hosts and protocol into the context
(previously only a computed `origin` was passed)
- HTTP / GRPC server do not longer try to match the headers to instances
themself, but use the passed `http.DomainContext` in their interceptors.
- The `RequestedHost` and `RequestedDomain` from authz.Instance are
removed in favor of the `http.DomainContext`
- When authenticating to or signing out from Console UI, the current
`http.DomainContext(ctx).Origin` (already checked by instance
interceptor for validity) is used to compute and dynamically add a
`redirect_uri` and `post_logout_redirect_uri`.
- Gateway passes all configured host headers (previously only did
`x-zitadel-*`)
- Admin API allows to manage trusted domain
# Additional Changes
None
# Additional Context
- part of #8279
- open topics:
- "single-instance" mode
- Console UI
2024-07-31 15:00:38 +00:00
|
|
|
url, err := urlTmpl(plainCode, http_util.DomainContext(ctx).Origin(), notifyUser)
|
2023-08-24 09:41:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2023-08-15 12:47:05 +00:00
|
|
|
}
|
2023-10-10 13:20:53 +00:00
|
|
|
notify := types.SendEmail(ctx, u.channels, string(template.Template), translator, notifyUser, colors, event)
|
|
|
|
err = notify.SendOTPEmailCode(ctx, url, plainCode, expiry)
|
2023-08-15 12:47:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-08-24 09:41:52 +00:00
|
|
|
err = sentCommand(ctx, event.Aggregate().ID, event.Aggregate().ResourceOwner)
|
2023-08-15 12:47:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-10-19 10:19:10 +00:00
|
|
|
return handler.NewNoOpStatement(event), nil
|
2023-08-15 12:47:05 +00:00
|
|
|
}
|
|
|
|
|
2023-03-28 22:09:06 +00:00
|
|
|
func (u *userNotifier) reduceDomainClaimed(event eventstore.Event) (*handler.Statement, error) {
|
|
|
|
e, ok := event.(*user.DomainClaimedEvent)
|
|
|
|
if !ok {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInvalidArgumentf(nil, "HANDL-Drh5w", "reduce.wrong.event.type %s", user.UserDomainClaimedType)
|
2023-03-28 22:09:06 +00:00
|
|
|
}
|
2023-10-19 10:19:10 +00:00
|
|
|
return handler.NewStatement(event, func(ex handler.Executer, projectionName string) error {
|
|
|
|
ctx := HandlerContext(event.Aggregate())
|
2024-03-27 17:48:14 +00:00
|
|
|
alreadyHandled, err := u.queries.IsAlreadyHandled(ctx, event, nil,
|
2023-10-19 10:19:10 +00:00
|
|
|
user.UserDomainClaimedType, user.UserDomainClaimedSentType)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if alreadyHandled {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
colors, err := u.queries.ActiveLabelPolicyByOrg(ctx, e.Aggregate().ResourceOwner, false)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-03-28 22:09:06 +00:00
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
template, err := u.queries.MailTemplateByOrg(ctx, e.Aggregate().ResourceOwner, false)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-03-28 22:09:06 +00:00
|
|
|
|
2023-11-21 12:11:38 +00:00
|
|
|
notifyUser, err := u.queries.GetNotifyUserByID(ctx, true, e.Aggregate().ID)
|
2023-10-19 10:19:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
translator, err := u.queries.GetTranslatorWithOrgTexts(ctx, notifyUser.ResourceOwner, domain.DomainClaimedMessageType)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx, err = u.queries.Origin(ctx, e)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = types.SendEmail(ctx, u.channels, string(template.Template), translator, notifyUser, colors, e).
|
|
|
|
SendDomainClaimed(ctx, notifyUser, e.UserName)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return u.commands.UserDomainClaimedSent(ctx, e.Aggregate().ResourceOwner, e.Aggregate().ID)
|
|
|
|
}), nil
|
2023-03-28 22:09:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (u *userNotifier) reducePasswordlessCodeRequested(event eventstore.Event) (*handler.Statement, error) {
|
|
|
|
e, ok := event.(*user.HumanPasswordlessInitCodeRequestedEvent)
|
|
|
|
if !ok {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInvalidArgumentf(nil, "HANDL-EDtjd", "reduce.wrong.event.type %s", user.HumanPasswordlessInitCodeAddedType)
|
2023-03-28 22:09:06 +00:00
|
|
|
}
|
2023-05-24 10:22:00 +00:00
|
|
|
if e.CodeReturned {
|
2023-10-19 10:19:10 +00:00
|
|
|
return handler.NewNoOpStatement(e), nil
|
2023-03-28 22:09:06 +00:00
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
return handler.NewStatement(event, func(ex handler.Executer, projectionName string) error {
|
|
|
|
ctx := HandlerContext(event.Aggregate())
|
|
|
|
alreadyHandled, err := u.checkIfCodeAlreadyHandledOrExpired(ctx, event, e.Expiry, map[string]interface{}{"id": e.ID}, user.HumanPasswordlessInitCodeSentType)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if alreadyHandled {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
code, err := crypto.DecryptString(e.Code, u.queries.UserDataCrypto)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
colors, err := u.queries.ActiveLabelPolicyByOrg(ctx, e.Aggregate().ResourceOwner, false)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-03-28 22:09:06 +00:00
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
template, err := u.queries.MailTemplateByOrg(ctx, e.Aggregate().ResourceOwner, false)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-11-21 12:11:38 +00:00
|
|
|
notifyUser, err := u.queries.GetNotifyUserByID(ctx, true, e.Aggregate().ID)
|
2023-10-19 10:19:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
translator, err := u.queries.GetTranslatorWithOrgTexts(ctx, notifyUser.ResourceOwner, domain.PasswordlessRegistrationMessageType)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx, err = u.queries.Origin(ctx, e)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = types.SendEmail(ctx, u.channels, string(template.Template), translator, notifyUser, colors, e).
|
|
|
|
SendPasswordlessRegistrationLink(ctx, notifyUser, code, e.ID, e.URLTemplate)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return u.commands.HumanPasswordlessInitCodeSent(ctx, e.Aggregate().ID, e.Aggregate().ResourceOwner, e.ID)
|
|
|
|
}), nil
|
2023-03-28 22:09:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (u *userNotifier) reducePasswordChanged(event eventstore.Event) (*handler.Statement, error) {
|
|
|
|
e, ok := event.(*user.HumanPasswordChangedEvent)
|
|
|
|
if !ok {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInvalidArgumentf(nil, "HANDL-Yko2z8", "reduce.wrong.event.type %s", user.HumanPasswordChangedType)
|
2023-03-28 22:09:06 +00:00
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
return handler.NewStatement(event, func(ex handler.Executer, projectionName string) error {
|
|
|
|
ctx := HandlerContext(event.Aggregate())
|
2024-03-27 17:48:14 +00:00
|
|
|
alreadyHandled, err := u.queries.IsAlreadyHandled(ctx, event, nil, user.HumanPasswordChangeSentType)
|
2023-10-19 10:19:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if alreadyHandled {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
notificationPolicy, err := u.queries.NotificationPolicyByOrg(ctx, true, e.Aggregate().ResourceOwner, false)
|
2023-12-08 14:30:55 +00:00
|
|
|
if zerrors.IsNotFound(err) {
|
2023-10-19 10:19:10 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !notificationPolicy.PasswordChange {
|
|
|
|
return nil
|
|
|
|
}
|
2023-03-28 22:09:06 +00:00
|
|
|
|
|
|
|
colors, err := u.queries.ActiveLabelPolicyByOrg(ctx, e.Aggregate().ResourceOwner, false)
|
|
|
|
if err != nil {
|
2023-10-19 10:19:10 +00:00
|
|
|
return err
|
2023-03-28 22:09:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template, err := u.queries.MailTemplateByOrg(ctx, e.Aggregate().ResourceOwner, false)
|
|
|
|
if err != nil {
|
2023-10-19 10:19:10 +00:00
|
|
|
return err
|
2023-03-28 22:09:06 +00:00
|
|
|
}
|
|
|
|
|
2023-11-21 12:11:38 +00:00
|
|
|
notifyUser, err := u.queries.GetNotifyUserByID(ctx, true, e.Aggregate().ID)
|
2023-03-28 22:09:06 +00:00
|
|
|
if err != nil {
|
2023-10-19 10:19:10 +00:00
|
|
|
return err
|
2023-03-28 22:09:06 +00:00
|
|
|
}
|
|
|
|
translator, err := u.queries.GetTranslatorWithOrgTexts(ctx, notifyUser.ResourceOwner, domain.PasswordChangeMessageType)
|
|
|
|
if err != nil {
|
2023-10-19 10:19:10 +00:00
|
|
|
return err
|
2023-03-28 22:09:06 +00:00
|
|
|
}
|
2023-10-10 13:20:53 +00:00
|
|
|
ctx, err = u.queries.Origin(ctx, e)
|
2023-03-28 22:09:06 +00:00
|
|
|
if err != nil {
|
2023-10-19 10:19:10 +00:00
|
|
|
return err
|
2023-03-28 22:09:06 +00:00
|
|
|
}
|
2023-10-10 13:20:53 +00:00
|
|
|
err = types.SendEmail(ctx, u.channels, string(template.Template), translator, notifyUser, colors, e).
|
|
|
|
SendPasswordChange(ctx, notifyUser)
|
2023-03-28 22:09:06 +00:00
|
|
|
if err != nil {
|
2023-10-19 10:19:10 +00:00
|
|
|
return err
|
2023-03-28 22:09:06 +00:00
|
|
|
}
|
2023-10-19 10:19:10 +00:00
|
|
|
return u.commands.PasswordChangeSent(ctx, e.Aggregate().ResourceOwner, e.Aggregate().ID)
|
|
|
|
}), nil
|
2023-03-28 22:09:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (u *userNotifier) reducePhoneCodeAdded(event eventstore.Event) (*handler.Statement, error) {
|
|
|
|
e, ok := event.(*user.HumanPhoneCodeAddedEvent)
|
|
|
|
if !ok {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInvalidArgumentf(nil, "HANDL-He83g", "reduce.wrong.event.type %s", user.HumanPhoneCodeAddedType)
|
2023-03-28 22:09:06 +00:00
|
|
|
}
|
2023-08-03 04:42:59 +00:00
|
|
|
if e.CodeReturned {
|
2023-10-19 10:19:10 +00:00
|
|
|
return handler.NewNoOpStatement(e), nil
|
2023-03-28 22:09:06 +00:00
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
return handler.NewStatement(event, func(ex handler.Executer, projectionName string) error {
|
|
|
|
ctx := HandlerContext(event.Aggregate())
|
|
|
|
alreadyHandled, err := u.checkIfCodeAlreadyHandledOrExpired(ctx, event, e.Expiry, nil,
|
|
|
|
user.UserV1PhoneCodeAddedType, user.UserV1PhoneCodeSentType,
|
|
|
|
user.HumanPhoneCodeAddedType, user.HumanPhoneCodeSentType)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if alreadyHandled {
|
|
|
|
return nil
|
|
|
|
}
|
2024-09-26 07:14:33 +00:00
|
|
|
var code string
|
|
|
|
if e.Code != nil {
|
|
|
|
code, err = crypto.DecryptString(e.Code, u.queries.UserDataCrypto)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-10-19 10:19:10 +00:00
|
|
|
}
|
|
|
|
colors, err := u.queries.ActiveLabelPolicyByOrg(ctx, e.Aggregate().ResourceOwner, false)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-11-21 12:11:38 +00:00
|
|
|
notifyUser, err := u.queries.GetNotifyUserByID(ctx, true, e.Aggregate().ID)
|
2023-10-19 10:19:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
translator, err := u.queries.GetTranslatorWithOrgTexts(ctx, notifyUser.ResourceOwner, domain.VerifyPhoneMessageType)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx, err = u.queries.Origin(ctx, e)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-09-26 07:14:33 +00:00
|
|
|
generatorInfo := new(senders.CodeGeneratorInfo)
|
|
|
|
if err = types.SendSMS(ctx, u.channels, translator, notifyUser, colors, e, generatorInfo).
|
|
|
|
SendPhoneVerificationCode(ctx, code); err != nil {
|
2023-10-19 10:19:10 +00:00
|
|
|
return err
|
|
|
|
}
|
2024-09-26 07:14:33 +00:00
|
|
|
return u.commands.HumanPhoneVerificationCodeSent(ctx, e.Aggregate().ResourceOwner, e.Aggregate().ID, generatorInfo)
|
2023-10-19 10:19:10 +00:00
|
|
|
}), nil
|
2023-03-28 22:09:06 +00:00
|
|
|
}
|
|
|
|
|
2024-09-11 10:53:55 +00:00
|
|
|
func (u *userNotifier) reduceInviteCodeAdded(event eventstore.Event) (*handler.Statement, error) {
|
|
|
|
e, ok := event.(*user.HumanInviteCodeAddedEvent)
|
|
|
|
if !ok {
|
|
|
|
return nil, zerrors.ThrowInvalidArgumentf(nil, "HANDL-Eeg3s", "reduce.wrong.event.type %s", user.HumanInviteCodeAddedType)
|
|
|
|
}
|
|
|
|
if e.CodeReturned {
|
|
|
|
return handler.NewNoOpStatement(e), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return handler.NewStatement(event, func(ex handler.Executer, projectionName string) error {
|
|
|
|
ctx := HandlerContext(event.Aggregate())
|
|
|
|
alreadyHandled, err := u.checkIfCodeAlreadyHandledOrExpired(ctx, event, e.Expiry, nil,
|
|
|
|
user.HumanInviteCodeAddedType, user.HumanInviteCodeSentType)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if alreadyHandled {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
code, err := crypto.DecryptString(e.Code, u.queries.UserDataCrypto)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
colors, err := u.queries.ActiveLabelPolicyByOrg(ctx, e.Aggregate().ResourceOwner, false)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
template, err := u.queries.MailTemplateByOrg(ctx, e.Aggregate().ResourceOwner, false)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
notifyUser, err := u.queries.GetNotifyUserByID(ctx, true, e.Aggregate().ID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
translator, err := u.queries.GetTranslatorWithOrgTexts(ctx, notifyUser.ResourceOwner, domain.InviteUserMessageType)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx, err = u.queries.Origin(ctx, e)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
notify := types.SendEmail(ctx, u.channels, string(template.Template), translator, notifyUser, colors, e)
|
|
|
|
err = notify.SendInviteCode(ctx, notifyUser, code, e.ApplicationName, e.URLTemplate, e.AuthRequestID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return u.commands.InviteCodeSent(ctx, e.Aggregate().ID, e.Aggregate().ResourceOwner)
|
|
|
|
}), nil
|
|
|
|
}
|
|
|
|
|
2023-03-28 22:09:06 +00:00
|
|
|
func (u *userNotifier) checkIfCodeAlreadyHandledOrExpired(ctx context.Context, event eventstore.Event, expiry time.Duration, data map[string]interface{}, eventTypes ...eventstore.EventType) (bool, error) {
|
2024-09-26 07:14:33 +00:00
|
|
|
if expiry > 0 && event.CreatedAt().Add(expiry).Before(time.Now().UTC()) {
|
2023-03-28 22:09:06 +00:00
|
|
|
return true, nil
|
|
|
|
}
|
2024-03-27 17:48:14 +00:00
|
|
|
return u.queries.IsAlreadyHandled(ctx, event, data, eventTypes...)
|
2023-03-28 22:09:06 +00:00
|
|
|
}
|