mirror of
https://github.com/andwati/zitadel.git
synced 2026-12-01 00:18:51 +00:00
# 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]>
65 lines
1.6 KiB
Go
65 lines
1.6 KiB
Go
package smtp
|
|
|
|
import (
|
|
"bytes"
|
|
"net/smtp"
|
|
"slices"
|
|
|
|
"github.com/zitadel/zitadel/internal/zerrors"
|
|
)
|
|
|
|
// 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(config PlainAuthConfig, host string) smtp.Auth {
|
|
return &plainOrLoginAuth{
|
|
host: host,
|
|
config: config,
|
|
}
|
|
}
|
|
|
|
type plainOrLoginAuth struct {
|
|
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")
|
|
}
|
|
if !slices.Contains(server.Auth, "PLAIN") {
|
|
a.authMethod = "LOGIN"
|
|
return a.authMethod, nil, nil
|
|
} else {
|
|
a.authMethod = "PLAIN"
|
|
resp := []byte("\x00" + a.config.User + "\x00" + a.config.Password)
|
|
return a.authMethod, resp, nil
|
|
}
|
|
}
|
|
|
|
func (a *plainOrLoginAuth) Next(fromServer []byte, more bool) ([]byte, error) {
|
|
if !more {
|
|
return nil, nil
|
|
}
|
|
|
|
if a.authMethod == "PLAIN" {
|
|
// We've already sent everything.
|
|
return nil, zerrors.ThrowInternal(nil, "SMTP-AAf43", "unexpected server challenge for PLAIN auth method")
|
|
}
|
|
|
|
switch {
|
|
case bytes.Equal(fromServer, []byte("Username:")):
|
|
return []byte(a.config.User), nil
|
|
case bytes.Equal(fromServer, []byte("Password:")):
|
|
return []byte(a.config.Password), nil
|
|
default:
|
|
return nil, zerrors.ThrowInternal(nil, "SMTP-HjW21", "unexpected server challenge")
|
|
}
|
|
}
|