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"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/instance"
|
2023-12-08 14:30:55 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/zerrors"
|
2022-02-21 12:22:20 +00:00
|
|
|
)
|
|
|
|
|
2024-09-06 13:11:36 +00:00
|
|
|
type AddTwilioConfig struct {
|
|
|
|
Details *domain.ObjectDetails
|
|
|
|
ResourceOwner string
|
|
|
|
ID string
|
|
|
|
|
2024-09-26 07:14:33 +00:00
|
|
|
Description string
|
|
|
|
SID string
|
|
|
|
Token string
|
|
|
|
SenderNumber string
|
|
|
|
VerifyServiceSID string
|
2024-09-06 13:11:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Commands) AddSMSConfigTwilio(ctx context.Context, config *AddTwilioConfig) (err error) {
|
|
|
|
if config.ResourceOwner == "" {
|
|
|
|
return zerrors.ThrowInvalidArgument(nil, "COMMAND-ZLrZhKSKq0", "Errors.ResourceOwnerMissing")
|
2022-02-21 12:22:20 +00:00
|
|
|
}
|
2024-09-06 13:11:36 +00:00
|
|
|
if config.ID == "" {
|
|
|
|
config.ID, err = c.idGenerator.Next()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
smsConfigWriteModel, err := c.getSMSConfig(ctx, config.ResourceOwner, config.ID)
|
2022-02-21 12:22:20 +00:00
|
|
|
if err != nil {
|
2024-09-06 13:11:36 +00:00
|
|
|
return err
|
2022-02-21 12:22:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2024-09-06 13:11:36 +00:00
|
|
|
return err
|
2022-02-21 12:22:20 +00:00
|
|
|
}
|
|
|
|
}
|
2024-09-06 13:11:36 +00:00
|
|
|
err = c.pushAppendAndReduce(ctx,
|
|
|
|
smsConfigWriteModel,
|
|
|
|
instance.NewSMSConfigTwilioAddedEvent(
|
|
|
|
ctx,
|
|
|
|
InstanceAggregateFromWriteModel(&smsConfigWriteModel.WriteModel),
|
|
|
|
config.ID,
|
|
|
|
config.Description,
|
|
|
|
config.SID,
|
|
|
|
config.SenderNumber,
|
|
|
|
token,
|
2024-09-26 07:14:33 +00:00
|
|
|
config.VerifyServiceSID,
|
2024-09-06 13:11:36 +00:00
|
|
|
),
|
|
|
|
)
|
2022-02-21 12:22:20 +00:00
|
|
|
if err != nil {
|
2024-09-06 13:11:36 +00:00
|
|
|
return err
|
2022-02-21 12:22:20 +00:00
|
|
|
}
|
2024-09-06 13:11:36 +00:00
|
|
|
config.Details = writeModelToObjectDetails(&smsConfigWriteModel.WriteModel)
|
|
|
|
return nil
|
2022-02-21 12:22:20 +00:00
|
|
|
}
|
|
|
|
|
2024-09-06 13:11:36 +00:00
|
|
|
type ChangeTwilioConfig struct {
|
|
|
|
Details *domain.ObjectDetails
|
|
|
|
ResourceOwner string
|
|
|
|
ID string
|
|
|
|
|
2024-09-26 07:14:33 +00:00
|
|
|
Description *string
|
|
|
|
SID *string
|
|
|
|
Token *string
|
|
|
|
SenderNumber *string
|
|
|
|
VerifyServiceSID *string
|
2024-09-06 13:11:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Commands) ChangeSMSConfigTwilio(ctx context.Context, config *ChangeTwilioConfig) (err error) {
|
|
|
|
if config.ResourceOwner == "" {
|
|
|
|
return zerrors.ThrowInvalidArgument(nil, "COMMAND-RHXryJwmFG", "Errors.ResourceOwnerMissing")
|
2022-02-21 12:22:20 +00:00
|
|
|
}
|
2024-09-06 13:11:36 +00:00
|
|
|
if config.ID == "" {
|
|
|
|
return zerrors.ThrowInvalidArgument(nil, "COMMAND-gMr93iNhTR", "Errors.IDMissing")
|
|
|
|
}
|
|
|
|
smsConfigWriteModel, err := c.getSMSConfig(ctx, config.ResourceOwner, config.ID)
|
2022-02-21 12:22:20 +00:00
|
|
|
if err != nil {
|
2024-09-06 13:11:36 +00:00
|
|
|
return err
|
2022-02-21 12:22:20 +00:00
|
|
|
}
|
|
|
|
if !smsConfigWriteModel.State.Exists() || smsConfigWriteModel.Twilio == nil {
|
2024-09-06 13:11:36 +00:00
|
|
|
return zerrors.ThrowNotFound(nil, "COMMAND-MUY0IFAf8O", "Errors.SMSConfig.NotFound")
|
2022-02-21 12:22:20 +00:00
|
|
|
}
|
2024-09-06 13:11:36 +00:00
|
|
|
changedEvent, hasChanged, err := smsConfigWriteModel.NewTwilioChangedEvent(
|
2022-02-21 12:22:20 +00:00
|
|
|
ctx,
|
2024-09-06 13:11:36 +00:00
|
|
|
InstanceAggregateFromWriteModel(&smsConfigWriteModel.WriteModel),
|
|
|
|
config.ID,
|
|
|
|
config.Description,
|
2022-02-21 12:22:20 +00:00
|
|
|
config.SID,
|
2024-09-26 07:14:33 +00:00
|
|
|
config.SenderNumber,
|
|
|
|
config.VerifyServiceSID,
|
|
|
|
)
|
2022-02-21 12:22:20 +00:00
|
|
|
if err != nil {
|
2024-09-06 13:11:36 +00:00
|
|
|
return err
|
2022-02-21 12:22:20 +00:00
|
|
|
}
|
|
|
|
if !hasChanged {
|
2024-09-06 13:11:36 +00:00
|
|
|
config.Details = writeModelToObjectDetails(&smsConfigWriteModel.WriteModel)
|
|
|
|
return nil
|
2022-02-21 12:22:20 +00:00
|
|
|
}
|
2024-09-06 13:11:36 +00:00
|
|
|
err = c.pushAppendAndReduce(ctx,
|
|
|
|
smsConfigWriteModel,
|
|
|
|
changedEvent,
|
|
|
|
)
|
2022-02-21 12:22:20 +00:00
|
|
|
if err != nil {
|
2024-09-06 13:11:36 +00:00
|
|
|
return err
|
2022-02-21 12:22:20 +00:00
|
|
|
}
|
2024-09-06 13:11:36 +00:00
|
|
|
config.Details = writeModelToObjectDetails(&smsConfigWriteModel.WriteModel)
|
|
|
|
return nil
|
2022-02-21 12:22:20 +00:00
|
|
|
}
|
|
|
|
|
2024-09-06 13:11:36 +00:00
|
|
|
func (c *Commands) ChangeSMSConfigTwilioToken(ctx context.Context, resourceOwner, id, token string) (*domain.ObjectDetails, error) {
|
|
|
|
if resourceOwner == "" {
|
|
|
|
return nil, zerrors.ThrowInvalidArgument(nil, "COMMAND-sLLA1HnMzj", "Errors.ResourceOwnerMissing")
|
|
|
|
}
|
|
|
|
if id == "" {
|
|
|
|
return nil, zerrors.ThrowInvalidArgument(nil, "SMS-PeNaqbC0r0", "Errors.IDMissing")
|
|
|
|
}
|
|
|
|
|
|
|
|
smsConfigWriteModel, err := c.getSMSConfig(ctx, resourceOwner, id)
|
2022-02-21 12:22:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if !smsConfigWriteModel.State.Exists() || smsConfigWriteModel.Twilio == nil {
|
2024-09-06 13:11:36 +00:00
|
|
|
return nil, zerrors.ThrowNotFound(nil, "COMMAND-ij3NhEHATp", "Errors.SMSConfig.NotFound")
|
2022-02-21 12:22:20 +00:00
|
|
|
}
|
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
|
|
|
|
}
|
2024-09-06 13:11:36 +00:00
|
|
|
err = c.pushAppendAndReduce(ctx,
|
|
|
|
smsConfigWriteModel,
|
|
|
|
instance.NewSMSConfigTokenChangedEvent(
|
|
|
|
ctx,
|
|
|
|
InstanceAggregateFromWriteModel(&smsConfigWriteModel.WriteModel),
|
|
|
|
id,
|
|
|
|
newtoken,
|
|
|
|
),
|
|
|
|
)
|
2022-02-21 12:22:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return writeModelToObjectDetails(&smsConfigWriteModel.WriteModel), nil
|
|
|
|
}
|
|
|
|
|
2024-09-06 13:11:36 +00:00
|
|
|
type AddSMSHTTP struct {
|
|
|
|
Details *domain.ObjectDetails
|
|
|
|
ResourceOwner string
|
|
|
|
ID string
|
|
|
|
|
|
|
|
Description string
|
|
|
|
Endpoint string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Commands) AddSMSConfigHTTP(ctx context.Context, config *AddSMSHTTP) (err error) {
|
|
|
|
if config.ResourceOwner == "" {
|
|
|
|
return zerrors.ThrowInvalidArgument(nil, "COMMAND-huy99qWjX4", "Errors.ResourceOwnerMissing")
|
2022-02-21 12:22:20 +00:00
|
|
|
}
|
2024-09-06 13:11:36 +00:00
|
|
|
if config.ID == "" {
|
|
|
|
config.ID, err = c.idGenerator.Next()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
smsConfigWriteModel, err := c.getSMSConfig(ctx, config.ResourceOwner, config.ID)
|
2022-02-21 12:22:20 +00:00
|
|
|
if err != nil {
|
2024-09-06 13:11:36 +00:00
|
|
|
return err
|
2022-02-21 12:22:20 +00:00
|
|
|
}
|
|
|
|
|
2024-09-06 13:11:36 +00:00
|
|
|
err = c.pushAppendAndReduce(ctx,
|
|
|
|
smsConfigWriteModel,
|
|
|
|
instance.NewSMSConfigHTTPAddedEvent(
|
|
|
|
ctx,
|
|
|
|
InstanceAggregateFromWriteModel(&smsConfigWriteModel.WriteModel),
|
|
|
|
config.ID,
|
|
|
|
config.Description,
|
|
|
|
config.Endpoint,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2022-02-21 12:22:20 +00:00
|
|
|
}
|
2024-09-06 13:11:36 +00:00
|
|
|
config.Details = writeModelToObjectDetails(&smsConfigWriteModel.WriteModel)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type ChangeSMSHTTP struct {
|
|
|
|
Details *domain.ObjectDetails
|
|
|
|
ResourceOwner string
|
|
|
|
ID string
|
|
|
|
|
|
|
|
Description *string
|
|
|
|
Endpoint *string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Commands) ChangeSMSConfigHTTP(ctx context.Context, config *ChangeSMSHTTP) (err error) {
|
|
|
|
if config.ResourceOwner == "" {
|
|
|
|
return zerrors.ThrowInvalidArgument(nil, "COMMAND-M622CFQnwK", "Errors.ResourceOwnerMissing")
|
|
|
|
}
|
|
|
|
if config.ID == "" {
|
|
|
|
return zerrors.ThrowInvalidArgument(nil, "COMMAND-phyb2e4Kll", "Errors.IDMissing")
|
|
|
|
}
|
|
|
|
smsConfigWriteModel, err := c.getSMSConfig(ctx, config.ResourceOwner, config.ID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !smsConfigWriteModel.State.Exists() || smsConfigWriteModel.HTTP == nil {
|
|
|
|
return zerrors.ThrowNotFound(nil, "COMMAND-6NW4I5Kqzj", "Errors.SMSConfig.NotFound")
|
2022-02-21 12:22:20 +00:00
|
|
|
}
|
2024-09-06 13:11:36 +00:00
|
|
|
changedEvent, hasChanged, err := smsConfigWriteModel.NewHTTPChangedEvent(
|
2022-02-21 12:22:20 +00:00
|
|
|
ctx,
|
2024-09-06 13:11:36 +00:00
|
|
|
InstanceAggregateFromWriteModel(&smsConfigWriteModel.WriteModel),
|
|
|
|
config.ID,
|
|
|
|
config.Description,
|
|
|
|
config.Endpoint)
|
2022-02-21 12:22:20 +00:00
|
|
|
if err != nil {
|
2024-09-06 13:11:36 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !hasChanged {
|
|
|
|
config.Details = writeModelToObjectDetails(&smsConfigWriteModel.WriteModel)
|
|
|
|
return nil
|
2022-02-21 12:22:20 +00:00
|
|
|
}
|
2024-09-06 13:11:36 +00:00
|
|
|
err = c.pushAppendAndReduce(ctx, smsConfigWriteModel, changedEvent)
|
2022-02-21 12:22:20 +00:00
|
|
|
if err != nil {
|
2024-09-06 13:11:36 +00:00
|
|
|
return err
|
2022-02-21 12:22:20 +00:00
|
|
|
}
|
2024-09-06 13:11:36 +00:00
|
|
|
config.Details = writeModelToObjectDetails(&smsConfigWriteModel.WriteModel)
|
|
|
|
return nil
|
2022-02-21 12:22:20 +00:00
|
|
|
}
|
|
|
|
|
2024-09-06 13:11:36 +00:00
|
|
|
func (c *Commands) ActivateSMSConfig(ctx context.Context, resourceOwner, id string) (*domain.ObjectDetails, error) {
|
|
|
|
if resourceOwner == "" {
|
|
|
|
return nil, zerrors.ThrowInvalidArgument(nil, "COMMAND-EFgoOg997V", "Errors.ResourceOwnerMissing")
|
|
|
|
}
|
2022-02-21 12:22:20 +00:00
|
|
|
if id == "" {
|
2024-09-06 13:11:36 +00:00
|
|
|
return nil, zerrors.ThrowInvalidArgument(nil, "COMMAND-jJ6TVqzvjp", "Errors.IDMissing")
|
2022-02-21 12:22:20 +00:00
|
|
|
}
|
2024-09-06 13:11:36 +00:00
|
|
|
|
|
|
|
smsConfigWriteModel, err := c.getSMSConfig(ctx, resourceOwner, id)
|
2022-02-21 12:22:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-09-06 13:11:36 +00:00
|
|
|
|
2022-06-13 06:34:11 +00:00
|
|
|
if !smsConfigWriteModel.State.Exists() {
|
2024-09-06 13:11:36 +00:00
|
|
|
return nil, zerrors.ThrowNotFound(nil, "COMMAND-9ULtp9PH5E", "Errors.SMSConfig.NotFound")
|
2022-02-21 12:22:20 +00:00
|
|
|
}
|
2024-09-06 13:11:36 +00:00
|
|
|
if smsConfigWriteModel.State == domain.SMSConfigStateActive {
|
|
|
|
return nil, zerrors.ThrowPreconditionFailed(nil, "COMMAND-B25GFeIvRi", "Errors.SMSConfig.AlreadyActive")
|
|
|
|
}
|
|
|
|
err = c.pushAppendAndReduce(ctx, smsConfigWriteModel,
|
|
|
|
instance.NewSMSConfigActivatedEvent(
|
|
|
|
ctx,
|
|
|
|
InstanceAggregateFromWriteModel(&smsConfigWriteModel.WriteModel),
|
|
|
|
id,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2022-02-21 12:22:20 +00:00
|
|
|
}
|
2024-09-06 13:11:36 +00:00
|
|
|
return writeModelToObjectDetails(&smsConfigWriteModel.WriteModel), nil
|
|
|
|
}
|
2022-02-21 12:22:20 +00:00
|
|
|
|
2024-09-06 13:11:36 +00:00
|
|
|
func (c *Commands) DeactivateSMSConfig(ctx context.Context, resourceOwner, id string) (*domain.ObjectDetails, error) {
|
|
|
|
if resourceOwner == "" {
|
|
|
|
return nil, zerrors.ThrowInvalidArgument(nil, "COMMAND-V9NWOZj8Gi", "Errors.ResourceOwnerMissing")
|
|
|
|
}
|
|
|
|
if id == "" {
|
|
|
|
return nil, zerrors.ThrowInvalidArgument(nil, "COMMAND-xs1ah1v1CL", "Errors.IDMissing")
|
|
|
|
}
|
|
|
|
smsConfigWriteModel, err := c.getSMSConfig(ctx, resourceOwner, id)
|
2022-02-21 12:22:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-09-06 13:11:36 +00:00
|
|
|
if !smsConfigWriteModel.State.Exists() {
|
|
|
|
return nil, zerrors.ThrowNotFound(nil, "COMMAND-La91dGNhbM", "Errors.SMSConfig.NotFound")
|
|
|
|
}
|
|
|
|
if smsConfigWriteModel.State == domain.SMSConfigStateInactive {
|
|
|
|
return nil, zerrors.ThrowPreconditionFailed(nil, "COMMAND-OSZAEkYvk7", "Errors.SMSConfig.AlreadyDeactivated")
|
|
|
|
}
|
|
|
|
err = c.pushAppendAndReduce(ctx,
|
|
|
|
smsConfigWriteModel,
|
|
|
|
instance.NewSMSConfigDeactivatedEvent(
|
|
|
|
ctx,
|
|
|
|
InstanceAggregateFromWriteModel(&smsConfigWriteModel.WriteModel),
|
|
|
|
id,
|
|
|
|
),
|
|
|
|
)
|
2022-02-21 12:22:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return writeModelToObjectDetails(&smsConfigWriteModel.WriteModel), nil
|
|
|
|
}
|
|
|
|
|
2024-09-06 13:11:36 +00:00
|
|
|
func (c *Commands) RemoveSMSConfig(ctx context.Context, resourceOwner, id string) (*domain.ObjectDetails, error) {
|
|
|
|
if resourceOwner == "" {
|
|
|
|
return nil, zerrors.ThrowInvalidArgument(nil, "COMMAND-cw0NSJsn1v", "Errors.ResourceOwnerMissing")
|
|
|
|
}
|
2022-02-21 12:22:20 +00:00
|
|
|
if id == "" {
|
2024-09-06 13:11:36 +00:00
|
|
|
return nil, zerrors.ThrowInvalidArgument(nil, "COMMAND-Qrz7lvdC4c", "Errors.IDMissing")
|
2022-02-21 12:22:20 +00:00
|
|
|
}
|
2024-09-06 13:11:36 +00:00
|
|
|
smsConfigWriteModel, err := c.getSMSConfig(ctx, resourceOwner, 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() {
|
2024-09-06 13:11:36 +00:00
|
|
|
return nil, zerrors.ThrowNotFound(nil, "COMMAND-povEVHPCkV", "Errors.SMSConfig.NotFound")
|
2022-02-21 12:22:20 +00:00
|
|
|
}
|
|
|
|
|
2024-09-06 13:11:36 +00:00
|
|
|
err = c.pushAppendAndReduce(ctx,
|
|
|
|
smsConfigWriteModel,
|
|
|
|
instance.NewSMSConfigRemovedEvent(
|
|
|
|
ctx,
|
|
|
|
InstanceAggregateFromWriteModel(&smsConfigWriteModel.WriteModel),
|
|
|
|
id,
|
|
|
|
),
|
|
|
|
)
|
2022-02-21 12:22:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return writeModelToObjectDetails(&smsConfigWriteModel.WriteModel), nil
|
|
|
|
}
|
2024-09-06 13:11:36 +00:00
|
|
|
|
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
|
|
|
|
}
|
2024-09-26 07:14:33 +00:00
|
|
|
|
|
|
|
// getActiveSMSConfig returns the last activated SMS configuration
|
|
|
|
func (c *Commands) getActiveSMSConfig(ctx context.Context, instanceID string) (_ *IAMSMSConfigWriteModel, err error) {
|
|
|
|
writeModel := NewIAMSMSLastActivatedConfigWriteModel(instanceID)
|
|
|
|
err = c.eventstore.FilterToQueryReducer(ctx, writeModel)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return c.getSMSConfig(ctx, instanceID, writeModel.activeID)
|
|
|
|
}
|