mirror of
https://github.com/zitadel/zitadel.git
synced 2025-08-12 08:27:32 +00:00
chore: move the go code into a subfolder
This commit is contained in:
28
apps/api/internal/notification/senders/chain.go
Normal file
28
apps/api/internal/notification/senders/chain.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package senders
|
||||
|
||||
import "github.com/zitadel/zitadel/internal/notification/channels"
|
||||
|
||||
var _ channels.NotificationChannel = (*Chain)(nil)
|
||||
|
||||
type Chain struct {
|
||||
channels []channels.NotificationChannel
|
||||
}
|
||||
|
||||
func ChainChannels(channel ...channels.NotificationChannel) *Chain {
|
||||
return &Chain{channels: channel}
|
||||
}
|
||||
|
||||
// HandleMessage returns a non nil error from a provider immediately if any occurs
|
||||
// messages are sent to channels in the same order they were provided to ChainChannels()
|
||||
func (c *Chain) HandleMessage(message channels.Message) error {
|
||||
for i := range c.channels {
|
||||
if err := c.channels[i].HandleMessage(message); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Chain) Len() int {
|
||||
return len(c.channels)
|
||||
}
|
24
apps/api/internal/notification/senders/code_verifier.go
Normal file
24
apps/api/internal/notification/senders/code_verifier.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package senders
|
||||
|
||||
type CodeGenerator interface {
|
||||
VerifyCode(verificationID, code string) error
|
||||
}
|
||||
|
||||
type CodeGeneratorInfo struct {
|
||||
ID string `json:"id,omitempty"`
|
||||
VerificationID string `json:"verificationId,omitempty"`
|
||||
}
|
||||
|
||||
func (c *CodeGeneratorInfo) GetID() string {
|
||||
if c == nil {
|
||||
return ""
|
||||
}
|
||||
return c.ID
|
||||
}
|
||||
|
||||
func (c *CodeGeneratorInfo) GetVerificationID() string {
|
||||
if c == nil {
|
||||
return ""
|
||||
}
|
||||
return c.VerificationID
|
||||
}
|
28
apps/api/internal/notification/senders/debug.go
Normal file
28
apps/api/internal/notification/senders/debug.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package senders
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/zitadel/zitadel/internal/notification/channels"
|
||||
"github.com/zitadel/zitadel/internal/notification/channels/fs"
|
||||
"github.com/zitadel/zitadel/internal/notification/channels/log"
|
||||
)
|
||||
|
||||
func debugChannels(ctx context.Context, getFileSystemProvider func(ctx context.Context) (*fs.Config, error), getLogProvider func(ctx context.Context) (*log.Config, error)) []channels.NotificationChannel {
|
||||
var (
|
||||
providers []channels.NotificationChannel
|
||||
)
|
||||
|
||||
if fsProvider, err := getFileSystemProvider(ctx); err == nil {
|
||||
p, err := fs.InitFSChannel(*fsProvider)
|
||||
if err == nil {
|
||||
providers = append(providers, p)
|
||||
}
|
||||
}
|
||||
|
||||
if logProvider, err := getLogProvider(ctx); err == nil {
|
||||
providers = append(providers, log.InitStdoutChannel(*logProvider))
|
||||
}
|
||||
|
||||
return providers
|
||||
}
|
68
apps/api/internal/notification/senders/email.go
Normal file
68
apps/api/internal/notification/senders/email.go
Normal file
@@ -0,0 +1,68 @@
|
||||
package senders
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/zitadel/logging"
|
||||
|
||||
"github.com/zitadel/zitadel/internal/api/authz"
|
||||
"github.com/zitadel/zitadel/internal/notification/channels"
|
||||
"github.com/zitadel/zitadel/internal/notification/channels/email"
|
||||
"github.com/zitadel/zitadel/internal/notification/channels/fs"
|
||||
"github.com/zitadel/zitadel/internal/notification/channels/instrumenting"
|
||||
"github.com/zitadel/zitadel/internal/notification/channels/log"
|
||||
"github.com/zitadel/zitadel/internal/notification/channels/smtp"
|
||||
"github.com/zitadel/zitadel/internal/notification/channels/webhook"
|
||||
)
|
||||
|
||||
const smtpSpanName = "smtp.NotificationChannel"
|
||||
|
||||
func EmailChannels(
|
||||
ctx context.Context,
|
||||
emailConfig *email.Config,
|
||||
getFileSystemProvider func(ctx context.Context) (*fs.Config, error),
|
||||
getLogProvider func(ctx context.Context) (*log.Config, error),
|
||||
successMetricName,
|
||||
failureMetricName string,
|
||||
) (chain *Chain, err error) {
|
||||
channels := make([]channels.NotificationChannel, 0, 3)
|
||||
if emailConfig.SMTPConfig != nil {
|
||||
p, err := smtp.InitChannel(emailConfig.SMTPConfig)
|
||||
logging.WithFields(
|
||||
"instance", authz.GetInstance(ctx).InstanceID(),
|
||||
).OnError(err).Debug("initializing SMTP channel failed")
|
||||
if err == nil {
|
||||
channels = append(
|
||||
channels,
|
||||
instrumenting.Wrap(
|
||||
ctx,
|
||||
p,
|
||||
smtpSpanName,
|
||||
successMetricName,
|
||||
failureMetricName,
|
||||
),
|
||||
)
|
||||
}
|
||||
}
|
||||
if emailConfig.WebhookConfig != nil {
|
||||
webhookChannel, err := webhook.InitChannel(ctx, *emailConfig.WebhookConfig)
|
||||
logging.WithFields(
|
||||
"instance", authz.GetInstance(ctx).InstanceID(),
|
||||
"callurl", emailConfig.WebhookConfig.CallURL,
|
||||
).OnError(err).Debug("initializing JSON channel failed")
|
||||
if err == nil {
|
||||
channels = append(
|
||||
channels,
|
||||
instrumenting.Wrap(
|
||||
ctx,
|
||||
webhookChannel,
|
||||
webhookSpanName,
|
||||
successMetricName,
|
||||
failureMetricName,
|
||||
),
|
||||
)
|
||||
}
|
||||
}
|
||||
channels = append(channels, debugChannels(ctx, getFileSystemProvider, getLogProvider)...)
|
||||
return ChainChannels(channels...), nil
|
||||
}
|
3
apps/api/internal/notification/senders/gen_mock.go
Normal file
3
apps/api/internal/notification/senders/gen_mock.go
Normal file
@@ -0,0 +1,3 @@
|
||||
package senders
|
||||
|
||||
//go:generate mockgen -package mock -destination ./mock/code_generator.mock.go github.com/zitadel/zitadel/internal/notification/senders CodeGenerator
|
@@ -0,0 +1,53 @@
|
||||
// Code generated by MockGen. DO NOT EDIT.
|
||||
// Source: github.com/zitadel/zitadel/internal/notification/senders (interfaces: CodeGenerator)
|
||||
//
|
||||
// Generated by this command:
|
||||
//
|
||||
// mockgen -package mock -destination ./mock/code_generator.mock.go github.com/zitadel/zitadel/internal/notification/senders CodeGenerator
|
||||
//
|
||||
|
||||
// Package mock is a generated GoMock package.
|
||||
package mock
|
||||
|
||||
import (
|
||||
reflect "reflect"
|
||||
|
||||
gomock "go.uber.org/mock/gomock"
|
||||
)
|
||||
|
||||
// MockCodeGenerator is a mock of CodeGenerator interface.
|
||||
type MockCodeGenerator struct {
|
||||
ctrl *gomock.Controller
|
||||
recorder *MockCodeGeneratorMockRecorder
|
||||
}
|
||||
|
||||
// MockCodeGeneratorMockRecorder is the mock recorder for MockCodeGenerator.
|
||||
type MockCodeGeneratorMockRecorder struct {
|
||||
mock *MockCodeGenerator
|
||||
}
|
||||
|
||||
// NewMockCodeGenerator creates a new mock instance.
|
||||
func NewMockCodeGenerator(ctrl *gomock.Controller) *MockCodeGenerator {
|
||||
mock := &MockCodeGenerator{ctrl: ctrl}
|
||||
mock.recorder = &MockCodeGeneratorMockRecorder{mock}
|
||||
return mock
|
||||
}
|
||||
|
||||
// EXPECT returns an object that allows the caller to indicate expected use.
|
||||
func (m *MockCodeGenerator) EXPECT() *MockCodeGeneratorMockRecorder {
|
||||
return m.recorder
|
||||
}
|
||||
|
||||
// VerifyCode mocks base method.
|
||||
func (m *MockCodeGenerator) VerifyCode(arg0, arg1 string) error {
|
||||
m.ctrl.T.Helper()
|
||||
ret := m.ctrl.Call(m, "VerifyCode", arg0, arg1)
|
||||
ret0, _ := ret[0].(error)
|
||||
return ret0
|
||||
}
|
||||
|
||||
// VerifyCode indicates an expected call of VerifyCode.
|
||||
func (mr *MockCodeGeneratorMockRecorder) VerifyCode(arg0, arg1 any) *gomock.Call {
|
||||
mr.mock.ctrl.T.Helper()
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "VerifyCode", reflect.TypeOf((*MockCodeGenerator)(nil).VerifyCode), arg0, arg1)
|
||||
}
|
@@ -0,0 +1,49 @@
|
||||
package senders
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/zitadel/logging"
|
||||
|
||||
"github.com/zitadel/zitadel/internal/api/authz"
|
||||
"github.com/zitadel/zitadel/internal/notification/channels"
|
||||
"github.com/zitadel/zitadel/internal/notification/channels/fs"
|
||||
"github.com/zitadel/zitadel/internal/notification/channels/instrumenting"
|
||||
"github.com/zitadel/zitadel/internal/notification/channels/log"
|
||||
"github.com/zitadel/zitadel/internal/notification/channels/set"
|
||||
)
|
||||
|
||||
const setSpanName = "security_event_token.NotificationChannel"
|
||||
|
||||
func SecurityEventTokenChannels(
|
||||
ctx context.Context,
|
||||
setConfig set.Config,
|
||||
getFileSystemProvider func(ctx context.Context) (*fs.Config, error),
|
||||
getLogProvider func(ctx context.Context) (*log.Config, error),
|
||||
successMetricName,
|
||||
failureMetricName string,
|
||||
) (*Chain, error) {
|
||||
if err := setConfig.Validate(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
channels := make([]channels.NotificationChannel, 0, 3)
|
||||
setChannel, err := set.InitChannel(ctx, setConfig)
|
||||
logging.WithFields(
|
||||
"instance", authz.GetInstance(ctx).InstanceID(),
|
||||
"callurl", setConfig.CallURL,
|
||||
).OnError(err).Debug("initializing SET channel failed")
|
||||
if err == nil {
|
||||
channels = append(
|
||||
channels,
|
||||
instrumenting.Wrap(
|
||||
ctx,
|
||||
setChannel,
|
||||
setSpanName,
|
||||
successMetricName,
|
||||
failureMetricName,
|
||||
),
|
||||
)
|
||||
}
|
||||
channels = append(channels, debugChannels(ctx, getFileSystemProvider, getLogProvider)...)
|
||||
return ChainChannels(channels...), nil
|
||||
}
|
62
apps/api/internal/notification/senders/sms.go
Normal file
62
apps/api/internal/notification/senders/sms.go
Normal file
@@ -0,0 +1,62 @@
|
||||
package senders
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/zitadel/logging"
|
||||
|
||||
"github.com/zitadel/zitadel/internal/api/authz"
|
||||
"github.com/zitadel/zitadel/internal/notification/channels"
|
||||
"github.com/zitadel/zitadel/internal/notification/channels/fs"
|
||||
"github.com/zitadel/zitadel/internal/notification/channels/instrumenting"
|
||||
"github.com/zitadel/zitadel/internal/notification/channels/log"
|
||||
"github.com/zitadel/zitadel/internal/notification/channels/sms"
|
||||
"github.com/zitadel/zitadel/internal/notification/channels/twilio"
|
||||
"github.com/zitadel/zitadel/internal/notification/channels/webhook"
|
||||
)
|
||||
|
||||
const twilioSpanName = "twilio.NotificationChannel"
|
||||
|
||||
func SMSChannels(
|
||||
ctx context.Context,
|
||||
smsConfig *sms.Config,
|
||||
getFileSystemProvider func(ctx context.Context) (*fs.Config, error),
|
||||
getLogProvider func(ctx context.Context) (*log.Config, error),
|
||||
successMetricName,
|
||||
failureMetricName string,
|
||||
) (chain *Chain, err error) {
|
||||
channels := make([]channels.NotificationChannel, 0, 3)
|
||||
if smsConfig.TwilioConfig != nil {
|
||||
channels = append(
|
||||
channels,
|
||||
instrumenting.Wrap(
|
||||
ctx,
|
||||
twilio.InitChannel(*smsConfig.TwilioConfig),
|
||||
twilioSpanName,
|
||||
successMetricName,
|
||||
failureMetricName,
|
||||
),
|
||||
)
|
||||
}
|
||||
if smsConfig.WebhookConfig != nil {
|
||||
webhookChannel, err := webhook.InitChannel(ctx, *smsConfig.WebhookConfig)
|
||||
logging.WithFields(
|
||||
"instance", authz.GetInstance(ctx).InstanceID(),
|
||||
"callurl", smsConfig.WebhookConfig.CallURL,
|
||||
).OnError(err).Debug("initializing JSON channel failed")
|
||||
if err == nil {
|
||||
channels = append(
|
||||
channels,
|
||||
instrumenting.Wrap(
|
||||
ctx,
|
||||
webhookChannel,
|
||||
webhookSpanName,
|
||||
successMetricName,
|
||||
failureMetricName,
|
||||
),
|
||||
)
|
||||
}
|
||||
}
|
||||
channels = append(channels, debugChannels(ctx, getFileSystemProvider, getLogProvider)...)
|
||||
return ChainChannels(channels...), nil
|
||||
}
|
49
apps/api/internal/notification/senders/webhook.go
Normal file
49
apps/api/internal/notification/senders/webhook.go
Normal file
@@ -0,0 +1,49 @@
|
||||
package senders
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/zitadel/logging"
|
||||
|
||||
"github.com/zitadel/zitadel/internal/api/authz"
|
||||
"github.com/zitadel/zitadel/internal/notification/channels"
|
||||
"github.com/zitadel/zitadel/internal/notification/channels/fs"
|
||||
"github.com/zitadel/zitadel/internal/notification/channels/instrumenting"
|
||||
"github.com/zitadel/zitadel/internal/notification/channels/log"
|
||||
"github.com/zitadel/zitadel/internal/notification/channels/webhook"
|
||||
)
|
||||
|
||||
const webhookSpanName = "webhook.NotificationChannel"
|
||||
|
||||
func WebhookChannels(
|
||||
ctx context.Context,
|
||||
webhookConfig webhook.Config,
|
||||
getFileSystemProvider func(ctx context.Context) (*fs.Config, error),
|
||||
getLogProvider func(ctx context.Context) (*log.Config, error),
|
||||
successMetricName,
|
||||
failureMetricName string,
|
||||
) (*Chain, error) {
|
||||
if err := webhookConfig.Validate(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
channels := make([]channels.NotificationChannel, 0, 3)
|
||||
webhookChannel, err := webhook.InitChannel(ctx, webhookConfig)
|
||||
logging.WithFields(
|
||||
"instance", authz.GetInstance(ctx).InstanceID(),
|
||||
"callurl", webhookConfig.CallURL,
|
||||
).OnError(err).Debug("initializing JSON channel failed")
|
||||
if err == nil {
|
||||
channels = append(
|
||||
channels,
|
||||
instrumenting.Wrap(
|
||||
ctx,
|
||||
webhookChannel,
|
||||
webhookSpanName,
|
||||
successMetricName,
|
||||
failureMetricName,
|
||||
),
|
||||
)
|
||||
}
|
||||
channels = append(channels, debugChannels(ctx, getFileSystemProvider, getLogProvider)...)
|
||||
return ChainChannels(channels...), nil
|
||||
}
|
Reference in New Issue
Block a user