mirror of
https://github.com/zitadel/zitadel.git
synced 2024-12-20 23:07:33 +00:00
3549a8b64e
* move mgmt pkg * begin package restructure * rename auth package to authz * begin start api * move auth * move admin * fix merge * configs and interceptors * interceptor * revert generate-grpc.sh * some cleanups * console * move console * fix tests and merging * js linting * merge * merging and configs * change k8s base to current ports * fixes * cleanup * regenerate proto * remove unnecessary whitespace * missing param * go mod tidy * fix merging * move login pkg * cleanup * move api pkgs again * fix pkg naming * fix generate-static.sh for login * update workflow * fixes * logging * remove duplicate * comment for optional gateway interfaces * regenerate protos * fix proto imports for grpc web * protos * grpc web generate * grpc web generate * fix changes * add translation interceptor * fix merging * regenerate mgmt proto
173 lines
5.8 KiB
Go
173 lines
5.8 KiB
Go
package handler
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/caos/logging"
|
|
|
|
"github.com/caos/zitadel/internal/api/authz"
|
|
sd "github.com/caos/zitadel/internal/config/systemdefaults"
|
|
"github.com/caos/zitadel/internal/crypto"
|
|
"github.com/caos/zitadel/internal/eventstore"
|
|
"github.com/caos/zitadel/internal/eventstore/models"
|
|
"github.com/caos/zitadel/internal/eventstore/spooler"
|
|
"github.com/caos/zitadel/internal/i18n"
|
|
"github.com/caos/zitadel/internal/notification/types"
|
|
"github.com/caos/zitadel/internal/user/repository/eventsourcing"
|
|
usr_event "github.com/caos/zitadel/internal/user/repository/eventsourcing"
|
|
es_model "github.com/caos/zitadel/internal/user/repository/eventsourcing/model"
|
|
)
|
|
|
|
type Notification struct {
|
|
handler
|
|
eventstore eventstore.Eventstore
|
|
userEvents *usr_event.UserEventstore
|
|
systemDefaults sd.SystemDefaults
|
|
AesCrypto crypto.EncryptionAlgorithm
|
|
i18n *i18n.Translator
|
|
statikDir http.FileSystem
|
|
}
|
|
|
|
const (
|
|
notificationTable = "notification.notifications"
|
|
NotifyUserID = "NOTIFICATION"
|
|
)
|
|
|
|
func (n *Notification) MinimumCycleDuration() time.Duration { return n.cycleDuration }
|
|
|
|
func (n *Notification) ViewModel() string {
|
|
return notificationTable
|
|
}
|
|
|
|
func (n *Notification) EventQuery() (*models.SearchQuery, error) {
|
|
sequence, err := n.view.GetLatestNotificationSequence()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return eventsourcing.UserQuery(sequence), nil
|
|
}
|
|
|
|
func (n *Notification) Reduce(event *models.Event) (err error) {
|
|
switch event.Type {
|
|
case es_model.InitializedUserCodeAdded:
|
|
err = n.handleInitUserCode(event)
|
|
case es_model.UserEmailCodeAdded:
|
|
err = n.handleEmailVerificationCode(event)
|
|
case es_model.UserPhoneCodeAdded:
|
|
err = n.handlePhoneVerificationCode(event)
|
|
case es_model.UserPasswordCodeAdded:
|
|
err = n.handlePasswordCode(event)
|
|
default:
|
|
return n.view.ProcessedNotificationSequence(event.Sequence)
|
|
}
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return n.view.ProcessedNotificationSequence(event.Sequence)
|
|
}
|
|
|
|
func (n *Notification) handleInitUserCode(event *models.Event) (err error) {
|
|
alreadyHandled, err := n.checkIfCodeAlreadyHandled(event.AggregateID, event.Sequence, es_model.InitializedUserCodeAdded, es_model.InitializedUserCodeSent)
|
|
if err != nil || alreadyHandled {
|
|
return err
|
|
}
|
|
initCode := new(es_model.InitUserCode)
|
|
initCode.SetData(event)
|
|
user, err := n.view.NotifyUserByID(event.AggregateID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
err = types.SendUserInitCode(n.statikDir, n.i18n, user, initCode, n.systemDefaults, n.AesCrypto)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return n.userEvents.InitCodeSent(getSetNotifyContextData(event.ResourceOwner), event.AggregateID)
|
|
}
|
|
|
|
func (n *Notification) handlePasswordCode(event *models.Event) (err error) {
|
|
alreadyHandled, err := n.checkIfCodeAlreadyHandled(event.AggregateID, event.Sequence, es_model.UserPasswordCodeAdded, es_model.UserPasswordCodeSent)
|
|
if err != nil || alreadyHandled {
|
|
return err
|
|
}
|
|
pwCode := new(es_model.PasswordCode)
|
|
pwCode.SetData(event)
|
|
user, err := n.view.NotifyUserByID(event.AggregateID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
err = types.SendPasswordCode(n.statikDir, n.i18n, user, pwCode, n.systemDefaults, n.AesCrypto)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return n.userEvents.PasswordCodeSent(getSetNotifyContextData(event.ResourceOwner), event.AggregateID)
|
|
}
|
|
|
|
func (n *Notification) handleEmailVerificationCode(event *models.Event) (err error) {
|
|
alreadyHandled, err := n.checkIfCodeAlreadyHandled(event.AggregateID, event.Sequence, es_model.UserEmailCodeAdded, es_model.UserEmailCodeSent)
|
|
if err != nil || alreadyHandled {
|
|
return nil
|
|
}
|
|
emailCode := new(es_model.EmailCode)
|
|
emailCode.SetData(event)
|
|
user, err := n.view.NotifyUserByID(event.AggregateID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
err = types.SendEmailVerificationCode(n.statikDir, n.i18n, user, emailCode, n.systemDefaults, n.AesCrypto)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return n.userEvents.EmailVerificationCodeSent(getSetNotifyContextData(event.ResourceOwner), event.AggregateID)
|
|
}
|
|
|
|
func (n *Notification) handlePhoneVerificationCode(event *models.Event) (err error) {
|
|
alreadyHandled, err := n.checkIfCodeAlreadyHandled(event.AggregateID, event.Sequence, es_model.UserPhoneCodeAdded, es_model.UserPhoneCodeSent)
|
|
if err != nil || alreadyHandled {
|
|
return nil
|
|
}
|
|
phoneCode := new(es_model.PhoneCode)
|
|
phoneCode.SetData(event)
|
|
user, err := n.view.NotifyUserByID(event.AggregateID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
err = types.SendPhoneVerificationCode(n.i18n, user, phoneCode, n.systemDefaults, n.AesCrypto)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return n.userEvents.PhoneVerificationCodeSent(getSetNotifyContextData(event.ResourceOwner), event.AggregateID)
|
|
}
|
|
|
|
func (n *Notification) checkIfCodeAlreadyHandled(userID string, sequence uint64, addedType, sentType models.EventType) (bool, error) {
|
|
events, err := n.getUserEvents(userID, sequence)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
for _, event := range events {
|
|
if event.Type == addedType || event.Type == sentType {
|
|
return true, nil
|
|
}
|
|
}
|
|
return false, nil
|
|
}
|
|
|
|
func (n *Notification) getUserEvents(userID string, sequence uint64) ([]*models.Event, error) {
|
|
query, err := eventsourcing.UserByIDQuery(userID, sequence)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return n.eventstore.FilterEvents(context.Background(), query)
|
|
}
|
|
|
|
func (n *Notification) OnError(event *models.Event, err error) error {
|
|
logging.LogWithFields("SPOOL-s9opc", "id", event.AggregateID, "sequence", event.Sequence).WithError(err).Warn("something went wrong in notification handler")
|
|
return spooler.HandleError(event, err, n.view.GetLatestNotificationFailedEvent, n.view.ProcessedNotificationFailedEvent, n.view.ProcessedNotificationSequence, n.errorCountUntilSkip)
|
|
}
|
|
|
|
func getSetNotifyContextData(orgID string) context.Context {
|
|
return authz.SetCtxData(context.Background(), authz.CtxData{UserID: NotifyUserID, OrgID: orgID})
|
|
}
|