2022-02-21 12:22:20 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/crypto"
|
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
|
|
|
caos_errs "github.com/zitadel/zitadel/internal/errors"
|
|
|
|
"github.com/zitadel/zitadel/internal/notification/channels/twilio"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/instance"
|
2022-02-21 12:22:20 +00:00
|
|
|
)
|
|
|
|
|
2023-03-16 17:24:30 +00:00
|
|
|
func (c *Commands) AddSMSConfigTwilio(ctx context.Context, instanceID string, config *twilio.Config) (string, *domain.ObjectDetails, error) {
|
2022-02-21 12:22:20 +00:00
|
|
|
id, err := c.idGenerator.Next()
|
|
|
|
if err != nil {
|
|
|
|
return "", nil, err
|
|
|
|
}
|
2022-04-05 05:58:09 +00:00
|
|
|
smsConfigWriteModel, err := c.getSMSConfig(ctx, instanceID, id)
|
2022-02-21 12:22:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var token *crypto.CryptoValue
|
|
|
|
if config.Token != "" {
|
2022-04-20 14:59:37 +00:00
|
|
|
token, err = crypto.Encrypt([]byte(config.Token), c.smsEncryption)
|
2022-02-21 12:22:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-24 16:21:34 +00:00
|
|
|
iamAgg := InstanceAggregateFromWriteModel(&smsConfigWriteModel.WriteModel)
|
|
|
|
pushedEvents, err := c.eventstore.Push(ctx, instance.NewSMSConfigTwilioAddedEvent(
|
2022-02-21 12:22:20 +00:00
|
|
|
ctx,
|
|
|
|
iamAgg,
|
|
|
|
id,
|
|
|
|
config.SID,
|
|
|
|
config.SenderNumber,
|
|
|
|
token))
|
|
|
|
if err != nil {
|
|
|
|
return "", nil, err
|
|
|
|
}
|
|
|
|
err = AppendAndReduce(smsConfigWriteModel, pushedEvents...)
|
|
|
|
if err != nil {
|
|
|
|
return "", nil, err
|
|
|
|
}
|
|
|
|
return id, writeModelToObjectDetails(&smsConfigWriteModel.WriteModel), nil
|
|
|
|
}
|
|
|
|
|
2023-03-16 17:24:30 +00:00
|
|
|
func (c *Commands) ChangeSMSConfigTwilio(ctx context.Context, instanceID, id string, config *twilio.Config) (*domain.ObjectDetails, error) {
|
2022-02-21 12:22:20 +00:00
|
|
|
if id == "" {
|
|
|
|
return nil, caos_errs.ThrowInvalidArgument(nil, "SMS-e9jwf", "Errors.IDMissing")
|
|
|
|
}
|
2022-04-05 05:58:09 +00:00
|
|
|
smsConfigWriteModel, err := c.getSMSConfig(ctx, instanceID, id)
|
2022-02-21 12:22:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if !smsConfigWriteModel.State.Exists() || smsConfigWriteModel.Twilio == nil {
|
|
|
|
return nil, caos_errs.ThrowNotFound(nil, "COMMAND-2m9fw", "Errors.SMSConfig.NotFound")
|
|
|
|
}
|
2022-03-24 16:21:34 +00:00
|
|
|
iamAgg := InstanceAggregateFromWriteModel(&smsConfigWriteModel.WriteModel)
|
2022-02-21 12:22:20 +00:00
|
|
|
|
|
|
|
changedEvent, hasChanged, err := smsConfigWriteModel.NewChangedEvent(
|
|
|
|
ctx,
|
|
|
|
iamAgg,
|
|
|
|
id,
|
|
|
|
config.SID,
|
|
|
|
config.SenderNumber)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if !hasChanged {
|
|
|
|
return nil, caos_errs.ThrowPreconditionFailed(nil, "COMMAND-jf9wk", "Errors.NoChangesFound")
|
|
|
|
}
|
|
|
|
pushedEvents, err := c.eventstore.Push(ctx, changedEvent)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = AppendAndReduce(smsConfigWriteModel, pushedEvents...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return writeModelToObjectDetails(&smsConfigWriteModel.WriteModel), nil
|
|
|
|
}
|
|
|
|
|
2022-04-05 05:58:09 +00:00
|
|
|
func (c *Commands) ChangeSMSConfigTwilioToken(ctx context.Context, instanceID, id, token string) (*domain.ObjectDetails, error) {
|
|
|
|
smsConfigWriteModel, err := c.getSMSConfig(ctx, instanceID, id)
|
2022-02-21 12:22:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if !smsConfigWriteModel.State.Exists() || smsConfigWriteModel.Twilio == nil {
|
|
|
|
return nil, caos_errs.ThrowNotFound(nil, "COMMAND-fj9wf", "Errors.SMSConfig.NotFound")
|
|
|
|
}
|
2022-03-24 16:21:34 +00:00
|
|
|
iamAgg := InstanceAggregateFromWriteModel(&smsConfigWriteModel.WriteModel)
|
2022-04-20 14:59:37 +00:00
|
|
|
newtoken, err := crypto.Encrypt([]byte(token), c.smsEncryption)
|
2022-02-21 12:22:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-03-24 16:21:34 +00:00
|
|
|
pushedEvents, err := c.eventstore.Push(ctx, instance.NewSMSConfigTokenChangedEvent(
|
2022-02-21 12:22:20 +00:00
|
|
|
ctx,
|
|
|
|
iamAgg,
|
|
|
|
id,
|
|
|
|
newtoken))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = AppendAndReduce(smsConfigWriteModel, pushedEvents...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return writeModelToObjectDetails(&smsConfigWriteModel.WriteModel), nil
|
|
|
|
}
|
|
|
|
|
2022-06-13 06:34:11 +00:00
|
|
|
func (c *Commands) ActivateSMSConfig(ctx context.Context, instanceID, id string) (*domain.ObjectDetails, error) {
|
2022-02-21 12:22:20 +00:00
|
|
|
if id == "" {
|
|
|
|
return nil, caos_errs.ThrowInvalidArgument(nil, "SMS-dn93n", "Errors.IDMissing")
|
|
|
|
}
|
2022-04-05 05:58:09 +00:00
|
|
|
smsConfigWriteModel, err := c.getSMSConfig(ctx, instanceID, id)
|
2022-02-21 12:22:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-06-13 06:34:11 +00:00
|
|
|
if !smsConfigWriteModel.State.Exists() {
|
2022-02-21 12:22:20 +00:00
|
|
|
return nil, caos_errs.ThrowNotFound(nil, "COMMAND-sn9we", "Errors.SMSConfig.NotFound")
|
|
|
|
}
|
|
|
|
if smsConfigWriteModel.State == domain.SMSConfigStateActive {
|
|
|
|
return nil, caos_errs.ThrowNotFound(nil, "COMMAND-sn9we", "Errors.SMSConfig.AlreadyActive")
|
|
|
|
}
|
2022-03-24 16:21:34 +00:00
|
|
|
iamAgg := InstanceAggregateFromWriteModel(&smsConfigWriteModel.WriteModel)
|
|
|
|
pushedEvents, err := c.eventstore.Push(ctx, instance.NewSMSConfigTwilioActivatedEvent(
|
2022-02-21 12:22:20 +00:00
|
|
|
ctx,
|
|
|
|
iamAgg,
|
|
|
|
id))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = AppendAndReduce(smsConfigWriteModel, pushedEvents...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return writeModelToObjectDetails(&smsConfigWriteModel.WriteModel), nil
|
|
|
|
}
|
|
|
|
|
2022-06-13 06:34:11 +00:00
|
|
|
func (c *Commands) DeactivateSMSConfig(ctx context.Context, instanceID, id string) (*domain.ObjectDetails, error) {
|
2022-02-21 12:22:20 +00:00
|
|
|
if id == "" {
|
|
|
|
return nil, caos_errs.ThrowInvalidArgument(nil, "SMS-frkwf", "Errors.IDMissing")
|
|
|
|
}
|
2022-04-05 05:58:09 +00:00
|
|
|
smsConfigWriteModel, err := c.getSMSConfig(ctx, instanceID, id)
|
2022-02-21 12:22:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-06-13 06:34:11 +00:00
|
|
|
if !smsConfigWriteModel.State.Exists() {
|
2022-02-21 12:22:20 +00:00
|
|
|
return nil, caos_errs.ThrowNotFound(nil, "COMMAND-s39Kg", "Errors.SMSConfig.NotFound")
|
|
|
|
}
|
|
|
|
if smsConfigWriteModel.State == domain.SMSConfigStateInactive {
|
|
|
|
return nil, caos_errs.ThrowNotFound(nil, "COMMAND-dm9e3", "Errors.SMSConfig.AlreadyDeactivated")
|
|
|
|
}
|
|
|
|
|
2022-03-24 16:21:34 +00:00
|
|
|
iamAgg := InstanceAggregateFromWriteModel(&smsConfigWriteModel.WriteModel)
|
|
|
|
pushedEvents, err := c.eventstore.Push(ctx, instance.NewSMSConfigDeactivatedEvent(
|
2022-02-21 12:22:20 +00:00
|
|
|
ctx,
|
|
|
|
iamAgg,
|
|
|
|
id))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = AppendAndReduce(smsConfigWriteModel, pushedEvents...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return writeModelToObjectDetails(&smsConfigWriteModel.WriteModel), nil
|
|
|
|
}
|
|
|
|
|
2022-06-10 10:39:38 +00:00
|
|
|
func (c *Commands) RemoveSMSConfig(ctx context.Context, instanceID, id string) (*domain.ObjectDetails, error) {
|
2022-02-21 12:22:20 +00:00
|
|
|
if id == "" {
|
|
|
|
return nil, caos_errs.ThrowInvalidArgument(nil, "SMS-3j9fs", "Errors.IDMissing")
|
|
|
|
}
|
2022-04-05 05:58:09 +00:00
|
|
|
smsConfigWriteModel, err := c.getSMSConfig(ctx, instanceID, id)
|
2022-02-21 12:22:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-06-10 10:39:38 +00:00
|
|
|
if !smsConfigWriteModel.State.Exists() {
|
2022-02-21 12:22:20 +00:00
|
|
|
return nil, caos_errs.ThrowNotFound(nil, "COMMAND-sn9we", "Errors.SMSConfig.NotFound")
|
|
|
|
}
|
|
|
|
|
2022-03-24 16:21:34 +00:00
|
|
|
iamAgg := InstanceAggregateFromWriteModel(&smsConfigWriteModel.WriteModel)
|
|
|
|
pushedEvents, err := c.eventstore.Push(ctx, instance.NewSMSConfigRemovedEvent(
|
2022-02-21 12:22:20 +00:00
|
|
|
ctx,
|
|
|
|
iamAgg,
|
|
|
|
id))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = AppendAndReduce(smsConfigWriteModel, pushedEvents...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return writeModelToObjectDetails(&smsConfigWriteModel.WriteModel), nil
|
|
|
|
}
|
2022-04-05 05:58:09 +00:00
|
|
|
func (c *Commands) getSMSConfig(ctx context.Context, instanceID, id string) (_ *IAMSMSConfigWriteModel, err error) {
|
|
|
|
writeModel := NewIAMSMSConfigWriteModel(instanceID, id)
|
2022-02-21 12:22:20 +00:00
|
|
|
err = c.eventstore.FilterToQueryReducer(ctx, writeModel)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return writeModel, nil
|
|
|
|
}
|