feat: added xoauth for smtp (#11239)

# Which Problems Are Solved

- Some mail providers (e.g.: MS Exchange) have deprecated plain auth

# How the Problems Are Solved

Adds XOauth2 auth option to SMPT
- SMTP config now can contain XOAuth2 auth config
- Proto files are updated to have a oneof for configuration selection.
Old fields for configuring plain auth are still available not to create
a breaking change. These can be removed in the future.
- Columns are added to the database to persist the config

# Additional Changes

none

# Additional Context

Replace this example with links to related issues, discussions, discord
threads, or other sources with more context.
Use the Closing #issue syntax for issues that are resolved with this PR.
- Closes #8042

---------

Co-authored-by: Copilot <[email protected]>
Co-authored-by: Tim Möhlmann <[email protected]>
This commit is contained in:
Wim Van Laer
2026-01-27 15:11:56 +01:00
committed by GitHub
co-authored by Copilot Tim Möhlmann
parent 96bbd6a65c
commit 3fc0ef2506
31 changed files with 2990 additions and 1059 deletions
+140 -36
View File
@@ -23,11 +23,25 @@ type AddSMTPConfig struct {
Description string
Host string
User string
Password string
Tls bool
From string
FromName string
ReplyToAddress string
PlainAuth *PlainAuth
XOAuth2Auth *XOAuth2Auth
}
type PlainAuth struct {
Password string
}
type XOAuth2Auth struct {
TokenEndpoint string
Scopes []string
ClientCredentialsAuth *OAuth2ClientCredentials
}
type OAuth2ClientCredentials struct {
ClientId string
ClientSecret string
}
func (c *Commands) AddSMTPConfig(ctx context.Context, config *AddSMTPConfig) (err error) {
@@ -55,14 +69,6 @@ func (c *Commands) AddSMTPConfig(ctx context.Context, config *AddSMTPConfig) (er
return zerrors.ThrowInvalidArgument(nil, "COMMAND-EvAtufIinh", "Errors.Invalid.Argument")
}
var smtpPassword *crypto.CryptoValue
if config.Password != "" {
smtpPassword, err = crypto.Encrypt([]byte(config.Password), c.smtpEncryption)
if err != nil {
return err
}
}
smtpConfigWriteModel, err := c.getSMTPConfig(ctx, config.ResourceOwner, config.ID, senderDomain)
if err != nil {
return err
@@ -73,6 +79,36 @@ func (c *Commands) AddSMTPConfig(ctx context.Context, config *AddSMTPConfig) (er
return err
}
var plainAuth *instance.PlainAuth
var xoauth2Auth *instance.XOAuth2Auth
if config.XOAuth2Auth != nil {
xoauth2Auth = &instance.XOAuth2Auth{
TokenEndpoint: config.XOAuth2Auth.TokenEndpoint,
Scopes: config.XOAuth2Auth.Scopes,
}
if config.XOAuth2Auth.ClientCredentialsAuth != nil {
clientSecret, err := crypto.Encrypt([]byte(config.XOAuth2Auth.ClientCredentialsAuth.ClientSecret), c.smtpEncryption)
if err != nil {
return err
}
xoauth2Auth.ClientCredentials = &instance.XOAuth2ClientCredentials{
ClientId: config.XOAuth2Auth.ClientCredentialsAuth.ClientId,
ClientSecret: clientSecret,
}
}
}
if config.PlainAuth != nil {
plainAuth = &instance.PlainAuth{}
if config.PlainAuth.Password != "" {
plainAuth.Password, err = crypto.Encrypt([]byte(config.PlainAuth.Password), c.smtpEncryption)
if err != nil {
return err
}
}
}
err = c.pushAppendAndReduce(ctx,
smtpConfigWriteModel,
instance.NewSMTPConfigAddedEvent(
@@ -86,12 +122,14 @@ func (c *Commands) AddSMTPConfig(ctx context.Context, config *AddSMTPConfig) (er
replyTo,
hostAndPort,
config.User,
smtpPassword,
plainAuth,
xoauth2Auth,
),
)
if err != nil {
return err
}
config.Details = writeModelToObjectDetails(&smtpConfigWriteModel.WriteModel)
return nil
}
@@ -104,11 +142,12 @@ type ChangeSMTPConfig struct {
Description string
Host string
User string
Password string
Tls bool
From string
FromName string
ReplyToAddress string
PlainAuth *PlainAuth
XOAuth2Auth *XOAuth2Auth
}
func (c *Commands) ChangeSMTPConfig(ctx context.Context, config *ChangeSMTPConfig) error {
@@ -132,15 +171,6 @@ func (c *Commands) ChangeSMTPConfig(ctx context.Context, config *ChangeSMTPConfi
return zerrors.ThrowInvalidArgument(nil, "COMMAND-kZNVkuL32L", "Errors.Invalid.Argument")
}
var smtpPassword *crypto.CryptoValue
var err error
if config.Password != "" {
smtpPassword, err = crypto.Encrypt([]byte(config.Password), c.smtpEncryption)
if err != nil {
return err
}
}
smtpConfigWriteModel, err := c.getSMTPConfig(ctx, config.ResourceOwner, config.ID, senderDomain)
if err != nil {
return err
@@ -155,6 +185,35 @@ func (c *Commands) ChangeSMTPConfig(ctx context.Context, config *ChangeSMTPConfi
return err
}
var plainAuth *instance.PlainAuth
var xoauth2Auth *instance.XOAuth2Auth
if config.PlainAuth != nil {
plainAuth = &instance.PlainAuth{}
if config.PlainAuth.Password != "" {
plainAuth.Password, err = crypto.Encrypt([]byte(config.PlainAuth.Password), c.smtpEncryption)
if err != nil {
return err
}
}
}
if config.XOAuth2Auth != nil {
xoauth2Auth = &instance.XOAuth2Auth{
TokenEndpoint: config.XOAuth2Auth.TokenEndpoint,
Scopes: config.XOAuth2Auth.Scopes,
}
if config.XOAuth2Auth.ClientCredentialsAuth != nil {
xoauth2Auth.ClientCredentials = &instance.XOAuth2ClientCredentials{
ClientId: config.XOAuth2Auth.ClientCredentialsAuth.ClientId,
}
if config.XOAuth2Auth.ClientCredentialsAuth.ClientSecret != "" {
xoauth2Auth.ClientCredentials.ClientSecret, err = crypto.Encrypt([]byte(config.XOAuth2Auth.ClientCredentialsAuth.ClientSecret), c.smtpEncryption)
if err != nil {
return err
}
}
}
}
changedEvent, hasChanged, err := smtpConfigWriteModel.NewChangedEvent(
ctx,
InstanceAggregateFromWriteModel(&smtpConfigWriteModel.WriteModel),
@@ -166,7 +225,8 @@ func (c *Commands) ChangeSMTPConfig(ctx context.Context, config *ChangeSMTPConfi
replyTo,
hostAndPort,
config.User,
smtpPassword,
plainAuth,
xoauth2Auth,
)
if err != nil {
return err
@@ -430,34 +490,50 @@ func (c *Commands) RemoveSMTPConfig(ctx context.Context, resourceOwner, id strin
}
func (c *Commands) TestSMTPConfig(ctx context.Context, instanceID, id, email string, config *smtp.Config) error {
password := config.SMTP.Password
if email == "" {
return zerrors.ThrowInvalidArgument(nil, "SMTP-p9uy", "Errors.SMTPConfig.TestEmailNotFound")
}
if id == "" && password == "" {
if id == "" && config.SMTP.PlainAuth != nil && config.SMTP.PlainAuth.Password == "" {
return zerrors.ThrowInvalidArgument(nil, "SMTP-p9kj", "Errors.SMTPConfig.TestPassword")
}
// If the password is not sent it'd mean that the password hasn't been changed for
// the stored configuration identified by its id so we can try to retrieve it
if id != "" && password == "" {
if id == "" && config.SMTP.XOAuth2Auth != nil &&
config.SMTP.XOAuth2Auth.ClientCredentialsAuth != nil &&
config.SMTP.XOAuth2Auth.ClientCredentialsAuth.ClientSecret == "" {
return zerrors.ThrowInvalidArgument(nil, "SMTP-9OP96s", "Errors.SMTPConfig.TestClientSecret")
}
if config.SMTP.PlainAuth != nil && config.SMTP.PlainAuth.Password == "" {
smtpConfigWriteModel, err := c.getSMTPConfig(ctx, instanceID, id, "")
if err != nil {
return err
}
if !smtpConfigWriteModel.State.Exists() || smtpConfigWriteModel.SMTPConfig == nil {
if !smtpConfigWriteModel.State.Exists() || smtpConfigWriteModel.SMTPConfig == nil || smtpConfigWriteModel.SMTPConfig.PlainAuth == nil {
return zerrors.ThrowNotFound(nil, "SMTP-p9cc", "Errors.SMTPConfig.NotFound")
}
password, err = crypto.DecryptString(smtpConfigWriteModel.SMTPConfig.Password, c.smtpEncryption)
config.SMTP.PlainAuth.Password, err = crypto.DecryptString(smtpConfigWriteModel.SMTPConfig.PlainAuth.Password, c.smtpEncryption)
if err != nil {
return err
}
}
config.SMTP.Password = password
if config.SMTP.XOAuth2Auth != nil && config.SMTP.XOAuth2Auth.ClientCredentialsAuth != nil && config.SMTP.XOAuth2Auth.ClientCredentialsAuth.ClientSecret == "" {
smtpConfigWriteModel, err := c.getSMTPConfig(ctx, instanceID, id, "")
if err != nil {
return err
}
if !smtpConfigWriteModel.State.Exists() || smtpConfigWriteModel.SMTPConfig == nil || smtpConfigWriteModel.SMTPConfig.XOAuth2Auth == nil {
return zerrors.ThrowNotFound(nil, "SMTP-p9c2", "Errors.SMTPConfig.NotFound")
}
config.SMTP.XOAuth2Auth.ClientCredentialsAuth.ClientSecret, err = crypto.DecryptString(smtpConfigWriteModel.SMTPConfig.XOAuth2Auth.ClientCredentials.ClientSecret, c.smtpEncryption)
if err != nil {
return err
}
}
// Try to send an email
err := smtp.TestConfiguration(config, email)
@@ -486,9 +562,36 @@ func (c *Commands) TestSMTPConfigById(ctx context.Context, instanceID, id, email
return zerrors.ThrowNotFound(nil, "SMTP-99klw", "Errors.SMTPConfig.NotFound")
}
password, err := crypto.DecryptString(smtpConfigWriteModel.SMTPConfig.Password, c.smtpEncryption)
if err != nil {
return err
var plainAuth *smtp.PlainAuthConfig
var xoauth2Auth *smtp.XOAuth2AuthConfig
if smtpConfigWriteModel.SMTPConfig.PlainAuth != nil {
password, err := crypto.DecryptString(smtpConfigWriteModel.SMTPConfig.PlainAuth.Password, c.smtpEncryption)
if err != nil {
return err
}
plainAuth = &smtp.PlainAuthConfig{
User: smtpConfigWriteModel.SMTPConfig.User,
Password: password,
}
}
if smtpConfigWriteModel.SMTPConfig.XOAuth2Auth != nil {
xoauth2Auth = &smtp.XOAuth2AuthConfig{
User: smtpConfigWriteModel.SMTPConfig.User,
TokenEndpoint: smtpConfigWriteModel.SMTPConfig.XOAuth2Auth.TokenEndpoint,
Scopes: smtpConfigWriteModel.SMTPConfig.XOAuth2Auth.Scopes,
}
if smtpConfigWriteModel.SMTPConfig.XOAuth2Auth.ClientCredentials != nil {
clientSecret, err := crypto.DecryptString(smtpConfigWriteModel.SMTPConfig.XOAuth2Auth.ClientCredentials.ClientSecret, c.smtpEncryption)
if err != nil {
return err
}
xoauth2Auth.ClientCredentialsAuth = &smtp.OAuth2ClientCredentials{
ClientId: smtpConfigWriteModel.SMTPConfig.XOAuth2Auth.ClientCredentials.ClientId,
ClientSecret: clientSecret,
}
}
}
smtpConfig := &smtp.Config{
@@ -496,9 +599,9 @@ func (c *Commands) TestSMTPConfigById(ctx context.Context, instanceID, id, email
From: smtpConfigWriteModel.SMTPConfig.SenderAddress,
FromName: smtpConfigWriteModel.SMTPConfig.SenderName,
SMTP: smtp.SMTP{
Host: smtpConfigWriteModel.SMTPConfig.Host,
User: smtpConfigWriteModel.SMTPConfig.User,
Password: password,
Host: smtpConfigWriteModel.SMTPConfig.Host,
PlainAuth: plainAuth,
XOAuth2Auth: xoauth2Auth,
},
}
@@ -582,7 +685,8 @@ func (c *Commands) prepareAddAndActivateSMTPConfig(a *instance.Aggregate, descri
replyTo,
hostAndPort,
user,
smtpPassword,
&instance.PlainAuth{Password: smtpPassword},
nil,
),
instance.NewSMTPConfigActivatedEvent(
ctx,