2022-03-24 16:21:34 +00:00
|
|
|
package instance
|
2022-02-21 12:22:20 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/crypto"
|
|
|
|
"github.com/zitadel/zitadel/internal/errors"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
2022-02-21 12:22:20 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
smsConfigPrefix = "sms.config"
|
|
|
|
smsConfigTwilioPrefix = "twilio."
|
2022-03-24 16:21:34 +00:00
|
|
|
SMSConfigTwilioAddedEventType = instanceEventTypePrefix + smsConfigPrefix + smsConfigTwilioPrefix + "added"
|
|
|
|
SMSConfigTwilioChangedEventType = instanceEventTypePrefix + smsConfigPrefix + smsConfigTwilioPrefix + "changed"
|
|
|
|
SMSConfigTwilioTokenChangedEventType = instanceEventTypePrefix + smsConfigPrefix + smsConfigTwilioPrefix + "token.changed"
|
|
|
|
SMSConfigActivatedEventType = instanceEventTypePrefix + smsConfigPrefix + smsConfigTwilioPrefix + "activated"
|
|
|
|
SMSConfigDeactivatedEventType = instanceEventTypePrefix + smsConfigPrefix + smsConfigTwilioPrefix + "deactivated"
|
|
|
|
SMSConfigRemovedEventType = instanceEventTypePrefix + smsConfigPrefix + smsConfigTwilioPrefix + "removed"
|
2022-02-21 12:22:20 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type SMSConfigTwilioAddedEvent struct {
|
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
|
|
|
|
ID string `json:"id,omitempty"`
|
|
|
|
SID string `json:"sid,omitempty"`
|
|
|
|
Token *crypto.CryptoValue `json:"token,omitempty"`
|
|
|
|
SenderNumber string `json:"senderNumber,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewSMSConfigTwilioAddedEvent(
|
|
|
|
ctx context.Context,
|
|
|
|
aggregate *eventstore.Aggregate,
|
|
|
|
id,
|
|
|
|
sid,
|
|
|
|
senderNumber string,
|
|
|
|
token *crypto.CryptoValue,
|
|
|
|
) *SMSConfigTwilioAddedEvent {
|
|
|
|
return &SMSConfigTwilioAddedEvent{
|
|
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
|
|
|
ctx,
|
|
|
|
aggregate,
|
|
|
|
SMSConfigTwilioAddedEventType,
|
|
|
|
),
|
|
|
|
ID: id,
|
|
|
|
SID: sid,
|
|
|
|
Token: token,
|
|
|
|
SenderNumber: senderNumber,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (e *SMSConfigTwilioAddedEvent) Payload() interface{} {
|
2022-02-21 12:22:20 +00:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (e *SMSConfigTwilioAddedEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
|
2022-02-21 12:22:20 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func SMSConfigTwilioAddedEventMapper(event eventstore.Event) (eventstore.Event, error) {
|
2022-02-21 12:22:20 +00:00
|
|
|
smsConfigAdded := &SMSConfigTwilioAddedEvent{
|
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}
|
2023-10-19 10:19:10 +00:00
|
|
|
err := event.Unmarshal(smsConfigAdded)
|
2022-02-21 12:22:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.ThrowInternal(err, "IAM-smwiR", "unable to unmarshal sms config twilio added")
|
|
|
|
}
|
|
|
|
|
|
|
|
return smsConfigAdded, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type SMSConfigTwilioChangedEvent struct {
|
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
|
|
|
|
ID string `json:"id,omitempty"`
|
|
|
|
SID *string `json:"sid,omitempty"`
|
|
|
|
SenderNumber *string `json:"senderNumber,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewSMSConfigTwilioChangedEvent(
|
|
|
|
ctx context.Context,
|
|
|
|
aggregate *eventstore.Aggregate,
|
|
|
|
id string,
|
|
|
|
changes []SMSConfigTwilioChanges,
|
|
|
|
) (*SMSConfigTwilioChangedEvent, error) {
|
|
|
|
if len(changes) == 0 {
|
|
|
|
return nil, errors.ThrowPreconditionFailed(nil, "IAM-smn8e", "Errors.NoChangesFound")
|
|
|
|
}
|
|
|
|
changeEvent := &SMSConfigTwilioChangedEvent{
|
|
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
|
|
|
ctx,
|
|
|
|
aggregate,
|
|
|
|
SMSConfigTwilioChangedEventType,
|
|
|
|
),
|
|
|
|
ID: id,
|
|
|
|
}
|
|
|
|
for _, change := range changes {
|
|
|
|
change(changeEvent)
|
|
|
|
}
|
|
|
|
return changeEvent, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type SMSConfigTwilioChanges func(event *SMSConfigTwilioChangedEvent)
|
|
|
|
|
|
|
|
func ChangeSMSConfigTwilioSID(sid string) func(event *SMSConfigTwilioChangedEvent) {
|
|
|
|
return func(e *SMSConfigTwilioChangedEvent) {
|
|
|
|
e.SID = &sid
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ChangeSMSConfigTwilioSenderNumber(senderNumber string) func(event *SMSConfigTwilioChangedEvent) {
|
|
|
|
return func(e *SMSConfigTwilioChangedEvent) {
|
|
|
|
e.SenderNumber = &senderNumber
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (e *SMSConfigTwilioChangedEvent) Payload() interface{} {
|
2022-02-21 12:22:20 +00:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (e *SMSConfigTwilioChangedEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
|
2022-02-21 12:22:20 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func SMSConfigTwilioChangedEventMapper(event eventstore.Event) (eventstore.Event, error) {
|
2022-02-21 12:22:20 +00:00
|
|
|
smsConfigChanged := &SMSConfigTwilioChangedEvent{
|
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}
|
2023-10-19 10:19:10 +00:00
|
|
|
err := event.Unmarshal(smsConfigChanged)
|
2022-02-21 12:22:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.ThrowInternal(err, "IAM-smwiR", "unable to unmarshal sms config twilio added")
|
|
|
|
}
|
|
|
|
|
|
|
|
return smsConfigChanged, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type SMSConfigTwilioTokenChangedEvent struct {
|
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
|
|
|
|
ID string `json:"id,omitempty"`
|
2022-06-13 06:34:11 +00:00
|
|
|
Token *crypto.CryptoValue `json:"token,omitempty"`
|
2022-02-21 12:22:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewSMSConfigTokenChangedEvent(
|
|
|
|
ctx context.Context,
|
|
|
|
aggregate *eventstore.Aggregate,
|
|
|
|
id string,
|
|
|
|
token *crypto.CryptoValue,
|
|
|
|
) *SMSConfigTwilioTokenChangedEvent {
|
|
|
|
return &SMSConfigTwilioTokenChangedEvent{
|
|
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
|
|
|
ctx,
|
|
|
|
aggregate,
|
|
|
|
SMSConfigTwilioTokenChangedEventType,
|
|
|
|
),
|
|
|
|
ID: id,
|
|
|
|
Token: token,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (e *SMSConfigTwilioTokenChangedEvent) Payload() interface{} {
|
2022-02-21 12:22:20 +00:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (e *SMSConfigTwilioTokenChangedEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
|
2022-02-21 12:22:20 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func SMSConfigTwilioTokenChangedEventMapper(event eventstore.Event) (eventstore.Event, error) {
|
2022-02-21 12:22:20 +00:00
|
|
|
smtpConfigTokenChagned := &SMSConfigTwilioTokenChangedEvent{
|
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}
|
2023-10-19 10:19:10 +00:00
|
|
|
err := event.Unmarshal(smtpConfigTokenChagned)
|
2022-02-21 12:22:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.ThrowInternal(err, "IAM-fi9Wf", "unable to unmarshal sms config token changed")
|
|
|
|
}
|
|
|
|
|
|
|
|
return smtpConfigTokenChagned, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type SMSConfigActivatedEvent struct {
|
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
ID string `json:"id,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewSMSConfigTwilioActivatedEvent(
|
|
|
|
ctx context.Context,
|
|
|
|
aggregate *eventstore.Aggregate,
|
|
|
|
id string,
|
|
|
|
) *SMSConfigActivatedEvent {
|
|
|
|
return &SMSConfigActivatedEvent{
|
|
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
|
|
|
ctx,
|
|
|
|
aggregate,
|
|
|
|
SMSConfigActivatedEventType,
|
|
|
|
),
|
|
|
|
ID: id,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (e *SMSConfigActivatedEvent) Payload() interface{} {
|
2022-02-21 12:22:20 +00:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (e *SMSConfigActivatedEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
|
2022-02-21 12:22:20 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func SMSConfigActivatedEventMapper(event eventstore.Event) (eventstore.Event, error) {
|
2022-02-21 12:22:20 +00:00
|
|
|
smsConfigActivated := &SMSConfigActivatedEvent{
|
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}
|
2023-10-19 10:19:10 +00:00
|
|
|
err := event.Unmarshal(smsConfigActivated)
|
2022-02-21 12:22:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.ThrowInternal(err, "IAM-dn92f", "unable to unmarshal sms config twilio activated changed")
|
|
|
|
}
|
|
|
|
|
|
|
|
return smsConfigActivated, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type SMSConfigDeactivatedEvent struct {
|
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
ID string `json:"id,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewSMSConfigDeactivatedEvent(
|
|
|
|
ctx context.Context,
|
|
|
|
aggregate *eventstore.Aggregate,
|
|
|
|
id string,
|
|
|
|
) *SMSConfigDeactivatedEvent {
|
|
|
|
return &SMSConfigDeactivatedEvent{
|
|
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
|
|
|
ctx,
|
|
|
|
aggregate,
|
|
|
|
SMSConfigDeactivatedEventType,
|
|
|
|
),
|
|
|
|
ID: id,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (e *SMSConfigDeactivatedEvent) Payload() interface{} {
|
2022-02-21 12:22:20 +00:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (e *SMSConfigDeactivatedEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
|
2022-02-21 12:22:20 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func SMSConfigDeactivatedEventMapper(event eventstore.Event) (eventstore.Event, error) {
|
2022-02-21 12:22:20 +00:00
|
|
|
smsConfigDeactivated := &SMSConfigDeactivatedEvent{
|
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}
|
2023-10-19 10:19:10 +00:00
|
|
|
err := event.Unmarshal(smsConfigDeactivated)
|
2022-02-21 12:22:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.ThrowInternal(err, "IAM-dn92f", "unable to unmarshal sms config twilio deactivated changed")
|
|
|
|
}
|
|
|
|
|
|
|
|
return smsConfigDeactivated, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type SMSConfigRemovedEvent struct {
|
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
ID string `json:"id,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewSMSConfigRemovedEvent(
|
|
|
|
ctx context.Context,
|
|
|
|
aggregate *eventstore.Aggregate,
|
|
|
|
id string,
|
|
|
|
) *SMSConfigRemovedEvent {
|
|
|
|
return &SMSConfigRemovedEvent{
|
|
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
|
|
|
ctx,
|
|
|
|
aggregate,
|
|
|
|
SMSConfigRemovedEventType,
|
|
|
|
),
|
|
|
|
ID: id,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (e *SMSConfigRemovedEvent) Payload() interface{} {
|
2022-02-21 12:22:20 +00:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (e *SMSConfigRemovedEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
|
2022-02-21 12:22:20 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func SMSConfigRemovedEventMapper(event eventstore.Event) (eventstore.Event, error) {
|
2022-02-21 12:22:20 +00:00
|
|
|
smsConfigRemoved := &SMSConfigRemovedEvent{
|
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}
|
2023-10-19 10:19:10 +00:00
|
|
|
err := event.Unmarshal(smsConfigRemoved)
|
2022-02-21 12:22:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.ThrowInternal(err, "IAM-99iNF", "unable to unmarshal sms config removed")
|
|
|
|
}
|
|
|
|
|
|
|
|
return smsConfigRemoved, nil
|
|
|
|
}
|