mirror of
https://github.com/andwati/zitadel.git
synced 2026-11-29 07:28:52 +00:00
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:
co-authored by
Copilot
Tim Möhlmann
parent
96bbd6a65c
commit
3fc0ef2506
@@ -148,14 +148,22 @@ func (smtpConfig SMTP) getSMTPClientWithStartTls(host string) (*smtp.Client, err
|
||||
}
|
||||
|
||||
func (smtpConfig SMTP) smtpAuth(client *smtp.Client, host string) error {
|
||||
if !smtpConfig.HasAuth() {
|
||||
if smtpConfig.XOAuth2Auth != nil {
|
||||
err := client.Auth(XOAuth2Auth(*smtpConfig.XOAuth2Auth, host))
|
||||
if err != nil {
|
||||
return zerrors.ThrowInternal(err, "EMAIL-av88SB", "Errors.SMTP.CouldNotXOAuth2")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
// Auth
|
||||
err := client.Auth(PlainOrLoginAuth(smtpConfig.User, smtpConfig.Password, host))
|
||||
if err != nil {
|
||||
return zerrors.ThrowInternal(err, "EMAIL-s9kfs", "Errors.SMTP.CouldNotAuth")
|
||||
|
||||
if smtpConfig.PlainAuth != nil {
|
||||
err := client.Auth(PlainOrLoginAuth(*smtpConfig.PlainAuth, host))
|
||||
if err != nil {
|
||||
return zerrors.ThrowInternal(err, "EMAIL-s9kfs", "Errors.SMTP.CouldNotAuth")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -176,11 +184,11 @@ func TestConfiguration(cfg *Config, testEmail string) error {
|
||||
}
|
||||
|
||||
if err := client.Mail(cfg.From); err != nil {
|
||||
return zerrors.ThrowInternal(err, "EMAIL-s3is3", "Errors.SMTP.CouldNotSetSender")
|
||||
return zerrors.ThrowInternal(err, "EMAIL-s5is5", "Errors.SMTP.CouldNotSetSender")
|
||||
}
|
||||
|
||||
if err := client.Rcpt(testEmail); err != nil {
|
||||
return zerrors.ThrowInternal(err, "EMAIL-s4is4", "Errors.SMTP.CouldNotSetRecipient")
|
||||
return zerrors.ThrowInternal(err, "EMAIL-s6is6", "Errors.SMTP.CouldNotSetRecipient")
|
||||
}
|
||||
|
||||
// Open data connection
|
||||
|
||||
@@ -9,19 +9,9 @@ type Config struct {
|
||||
}
|
||||
|
||||
type SMTP struct {
|
||||
Host string
|
||||
User string
|
||||
Password string
|
||||
}
|
||||
|
||||
// HasAuth reports whether SMTP authentication should be attempted.
|
||||
//
|
||||
// It returns true when a User is configured, regardless of whether Password
|
||||
// is set. This behavior is intentional to support SMTP servers that allow
|
||||
// authentication without a password. For servers that require a password,
|
||||
// this means authentication may still be attempted with an empty password.
|
||||
func (smtp *SMTP) HasAuth() bool {
|
||||
return smtp.User != ""
|
||||
Host string
|
||||
PlainAuth *PlainAuthConfig
|
||||
XOAuth2Auth *XOAuth2AuthConfig
|
||||
}
|
||||
|
||||
type ConfigHTTP struct {
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
package smtp
|
||||
|
||||
import (
|
||||
"net/smtp"
|
||||
"slices"
|
||||
|
||||
"github.com/zitadel/zitadel/internal/zerrors"
|
||||
)
|
||||
|
||||
type GenericAuth struct {
|
||||
Host string
|
||||
PlainOrLogin *plainOrLoginAuth
|
||||
XOAuth2 *xoauth2Auth
|
||||
selectedMethod smtp.Auth
|
||||
}
|
||||
|
||||
func (a *GenericAuth) Start(server *smtp.ServerInfo) (string, []byte, error) {
|
||||
if server.Name != a.Host {
|
||||
return "", nil, zerrors.ThrowInternal(nil, "SMTP-BtXgkX", "wrong host name")
|
||||
}
|
||||
|
||||
switch {
|
||||
case slices.Contains(server.Auth, "XOAUTH2") && a.XOAuth2 != nil:
|
||||
a.selectedMethod = a.XOAuth2
|
||||
case slices.Contains(server.Auth, "PLAIN") && a.PlainOrLogin != nil:
|
||||
a.selectedMethod = a.PlainOrLogin
|
||||
case slices.Contains(server.Auth, "LOGIN") && a.PlainOrLogin != nil:
|
||||
a.selectedMethod = a.PlainOrLogin
|
||||
default:
|
||||
return "", nil, zerrors.ThrowInternalf(nil, "SMTP-3mBHTz", "no supported auth method found (server supports: %v)", server.Auth)
|
||||
}
|
||||
return a.selectedMethod.Start(server)
|
||||
}
|
||||
|
||||
func (a *GenericAuth) Next(fromServer []byte, more bool) ([]byte, error) {
|
||||
if a.selectedMethod == nil {
|
||||
return nil, zerrors.ThrowInternal(nil, "SMTP-G99DUr", "no auth method selected")
|
||||
}
|
||||
|
||||
return a.selectedMethod.Next(fromServer, more)
|
||||
}
|
||||
@@ -11,17 +11,24 @@ import (
|
||||
// golang net/smtp SMTP AUTH LOGIN or PLAIN Auth Handler
|
||||
// Reference: https://gist.github.com/andelf/5118732?permalink_comment_id=4825669#gistcomment-4825669
|
||||
|
||||
func PlainOrLoginAuth(username, password, host string) smtp.Auth {
|
||||
return &plainOrLoginAuth{username: username, password: password, host: host}
|
||||
func PlainOrLoginAuth(config PlainAuthConfig, host string) smtp.Auth {
|
||||
return &plainOrLoginAuth{
|
||||
host: host,
|
||||
config: config,
|
||||
}
|
||||
}
|
||||
|
||||
type plainOrLoginAuth struct {
|
||||
username string
|
||||
password string
|
||||
host string
|
||||
config PlainAuthConfig
|
||||
authMethod string
|
||||
}
|
||||
|
||||
type PlainAuthConfig struct {
|
||||
User string
|
||||
Password string
|
||||
}
|
||||
|
||||
func (a *plainOrLoginAuth) Start(server *smtp.ServerInfo) (string, []byte, error) {
|
||||
if server.Name != a.host {
|
||||
return "", nil, zerrors.ThrowInternal(nil, "SMTP-RRi75", "wrong host name")
|
||||
@@ -31,7 +38,7 @@ func (a *plainOrLoginAuth) Start(server *smtp.ServerInfo) (string, []byte, error
|
||||
return a.authMethod, nil, nil
|
||||
} else {
|
||||
a.authMethod = "PLAIN"
|
||||
resp := []byte("\x00" + a.username + "\x00" + a.password)
|
||||
resp := []byte("\x00" + a.config.User + "\x00" + a.config.Password)
|
||||
return a.authMethod, resp, nil
|
||||
}
|
||||
}
|
||||
@@ -48,9 +55,9 @@ func (a *plainOrLoginAuth) Next(fromServer []byte, more bool) ([]byte, error) {
|
||||
|
||||
switch {
|
||||
case bytes.Equal(fromServer, []byte("Username:")):
|
||||
return []byte(a.username), nil
|
||||
return []byte(a.config.User), nil
|
||||
case bytes.Equal(fromServer, []byte("Password:")):
|
||||
return []byte(a.password), nil
|
||||
return []byte(a.config.Password), nil
|
||||
default:
|
||||
return nil, zerrors.ThrowInternal(nil, "SMTP-HjW21", "unexpected server challenge")
|
||||
}
|
||||
|
||||
@@ -0,0 +1,96 @@
|
||||
package smtp
|
||||
|
||||
import (
|
||||
"context"
|
||||
"hash/fnv"
|
||||
"net/smtp"
|
||||
|
||||
lru "github.com/hashicorp/golang-lru/v2"
|
||||
"golang.org/x/oauth2"
|
||||
"golang.org/x/oauth2/clientcredentials"
|
||||
|
||||
"github.com/zitadel/zitadel/internal/zerrors"
|
||||
)
|
||||
|
||||
var tokenSourceCache *lru.TwoQueueCache[string, oauth2.TokenSource]
|
||||
|
||||
func init() {
|
||||
var err error
|
||||
tokenSourceCache, err = lru.New2Q[string, oauth2.TokenSource](100)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
type XOAuth2AuthConfig struct {
|
||||
User string
|
||||
TokenEndpoint string
|
||||
Scopes []string
|
||||
ClientCredentialsAuth *OAuth2ClientCredentials
|
||||
}
|
||||
|
||||
type OAuth2ClientCredentials struct {
|
||||
ClientId string
|
||||
ClientSecret string
|
||||
}
|
||||
|
||||
func (cfg XOAuth2AuthConfig) Hash() string {
|
||||
hash := fnv.New128()
|
||||
hash.Write([]byte(cfg.User))
|
||||
hash.Write([]byte(cfg.TokenEndpoint))
|
||||
for _, s := range cfg.Scopes {
|
||||
hash.Write([]byte(s))
|
||||
}
|
||||
if cfg.ClientCredentialsAuth != nil {
|
||||
hash.Write([]byte(cfg.ClientCredentialsAuth.ClientId))
|
||||
hash.Write([]byte(cfg.ClientCredentialsAuth.ClientSecret))
|
||||
}
|
||||
return string(hash.Sum(nil))
|
||||
}
|
||||
|
||||
type xoauth2Auth struct {
|
||||
host string
|
||||
config XOAuth2AuthConfig
|
||||
}
|
||||
|
||||
func XOAuth2Auth(config XOAuth2AuthConfig, host string) smtp.Auth {
|
||||
return &xoauth2Auth{
|
||||
host: host,
|
||||
config: config,
|
||||
}
|
||||
}
|
||||
func (a *xoauth2Auth) Start(server *smtp.ServerInfo) (string, []byte, error) {
|
||||
if server.Name != a.host {
|
||||
return "", nil, zerrors.ThrowInternal(nil, "SMTP-eRJLyi", "wrong host name")
|
||||
}
|
||||
|
||||
hash := a.config.Hash()
|
||||
tokenSource, ok := tokenSourceCache.Get(hash)
|
||||
if !ok {
|
||||
if a.config.ClientCredentialsAuth != nil {
|
||||
config := &clientcredentials.Config{
|
||||
ClientID: a.config.ClientCredentialsAuth.ClientId,
|
||||
ClientSecret: a.config.ClientCredentialsAuth.ClientSecret,
|
||||
Scopes: a.config.Scopes,
|
||||
TokenURL: a.config.TokenEndpoint,
|
||||
}
|
||||
tokenSource = config.TokenSource(context.Background())
|
||||
tokenSourceCache.Add(hash, tokenSource)
|
||||
}
|
||||
}
|
||||
|
||||
token, err := tokenSource.Token()
|
||||
if err != nil {
|
||||
return "", nil, zerrors.ThrowInternal(err, "SMTP-fjHcJu", "Failed to get token to connect with smtp server")
|
||||
}
|
||||
|
||||
resp := []byte("user=" + a.config.User + "\x01" + "auth=Bearer " + token.AccessToken + "\x01\x01")
|
||||
return "XOAUTH2", resp, nil
|
||||
}
|
||||
|
||||
func (a *xoauth2Auth) Next(_ []byte, more bool) ([]byte, error) {
|
||||
if !more {
|
||||
return nil, nil
|
||||
}
|
||||
return nil, zerrors.ThrowInternal(nil, "SMTP-Pqsrj9", "unexpected server challenge for XOAUTH2 auth method")
|
||||
}
|
||||
Reference in New Issue
Block a user