Accept usernames as email format

This commit is contained in:
Neil Alexander
2021-07-08 23:12:20 +01:00
parent 2093fa901c
commit 8411468451
7 changed files with 29 additions and 11 deletions

View File

@@ -10,6 +10,7 @@ import (
"github.com/neilalexander/yggmail/internal/config"
"github.com/neilalexander/yggmail/internal/smtpsender"
"github.com/neilalexander/yggmail/internal/storage"
"github.com/neilalexander/yggmail/internal/utils"
)
type BackendMode int
@@ -30,6 +31,14 @@ type Backend struct {
func (b *Backend) Login(state *smtp.ConnectionState, username, password string) (smtp.Session, error) {
switch b.Mode {
case BackendModeInternal:
// If our username is email-like, then take just the localpart
if localpart, host, err := utils.ParseAddress(username); err == nil {
if host != base62.EncodeToString(b.Config.PublicKey) {
return nil, fmt.Errorf("failed to authenticate: wrong domain in username")
}
username = localpart
}
// The connection came from our local listener
if authed, err := b.Storage.TryAuthenticate(username, password); err != nil {
b.Log.Printf("Failed to authenticate SMTP user %q due to error: %s", username, err)

View File

@@ -1,19 +0,0 @@
package smtpserver
import (
"fmt"
"strings"
)
const TLD = ".yggmail"
func parseAddress(email string) (string, string, error) {
if !strings.HasSuffix(email, TLD) {
return "", "", fmt.Errorf("invalid TLD")
}
at := strings.LastIndex(email, "@")
if at == 0 {
return "", "", fmt.Errorf("invalid email address")
}
return email[:at], strings.TrimSuffix(email[at+1:], TLD), nil
}

View File

@@ -10,6 +10,7 @@ import (
"github.com/emersion/go-smtp"
"github.com/jxskiss/base62"
"github.com/neilalexander/yggmail/internal/smtpsender"
"github.com/neilalexander/yggmail/internal/utils"
)
type SessionLocal struct {
@@ -20,7 +21,7 @@ type SessionLocal struct {
}
func (s *SessionLocal) Mail(from string, opts smtp.MailOptions) error {
_, host, err := parseAddress(from)
_, host, err := utils.ParseAddress(from)
if err != nil {
return fmt.Errorf("parseAddress: %w", err)
}
@@ -55,7 +56,7 @@ func (s *SessionLocal) Data(r io.Reader) error {
servers := make(map[string]struct{})
for _, rcpt := range s.rcpt {
localpart, host, err := parseAddress(rcpt)
localpart, host, err := utils.ParseAddress(rcpt)
if err != nil {
return fmt.Errorf("parseAddress: %w", err)
}

View File

@@ -11,6 +11,7 @@ import (
"github.com/emersion/go-message"
"github.com/emersion/go-smtp"
"github.com/jxskiss/base62"
"github.com/neilalexander/yggmail/internal/utils"
)
type SessionRemote struct {
@@ -22,7 +23,7 @@ type SessionRemote struct {
}
func (s *SessionRemote) Mail(from string, opts smtp.MailOptions) error {
_, host, err := parseAddress(from)
_, host, err := utils.ParseAddress(from)
if err != nil {
return fmt.Errorf("mail.ParseAddress: %w", err)
}
@@ -41,7 +42,7 @@ func (s *SessionRemote) Mail(from string, opts smtp.MailOptions) error {
}
func (s *SessionRemote) Rcpt(to string) error {
user, host, err := parseAddress(to)
user, host, err := utils.ParseAddress(to)
if err != nil {
return fmt.Errorf("mail.ParseAddress: %w", err)
}