forked from Silverfish/proton-bridge
chore(GODT-2799): Separate account states for SMTP Backend
Rather than accessing the Bridge user list, each user register their individual SMTP service with the server manager. Note that some dependencies on the user data are hidden behind the `UserInterface`. These will be removed in a future patch.
This commit is contained in:
86
internal/services/smtp/accounts.go
Normal file
86
internal/services/smtp/accounts.go
Normal file
@ -0,0 +1,86 @@
|
||||
// Copyright (c) 2023 Proton AG
|
||||
//
|
||||
// This file is part of Proton Mail Bridge.
|
||||
//
|
||||
// Proton Mail Bridge is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// Proton Mail Bridge is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Proton Mail Bridge. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
package smtp
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"sync"
|
||||
)
|
||||
|
||||
type Accounts struct {
|
||||
accountsLock sync.RWMutex
|
||||
accounts map[string]*Service
|
||||
}
|
||||
|
||||
func NewAccounts() *Accounts {
|
||||
return &Accounts{
|
||||
accounts: make(map[string]*Service),
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Accounts) AddAccount(account *Service) {
|
||||
s.accountsLock.Lock()
|
||||
defer s.accountsLock.Unlock()
|
||||
|
||||
s.accounts[account.UserID()] = account
|
||||
}
|
||||
|
||||
func (s *Accounts) RemoveAccount(account *Service) {
|
||||
s.accountsLock.Lock()
|
||||
defer s.accountsLock.Unlock()
|
||||
|
||||
delete(s.accounts, account.UserID())
|
||||
}
|
||||
|
||||
func (s *Accounts) CheckAuth(user string, password []byte) (string, string, error) {
|
||||
s.accountsLock.RLock()
|
||||
defer s.accountsLock.RUnlock()
|
||||
|
||||
for id, service := range s.accounts {
|
||||
addrID, err := service.user.CheckAuth(user, password)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
service.user.ReportSMTPAuthSuccess(context.Background())
|
||||
return id, addrID, nil
|
||||
}
|
||||
|
||||
for _, service := range s.accounts {
|
||||
service.user.ReportSMTPAuthFailed(user)
|
||||
}
|
||||
|
||||
return "", "", ErrNoSuchUser
|
||||
}
|
||||
|
||||
func (s *Accounts) SendMail(ctx context.Context, userID, addrID, from string, to []string, r io.Reader) error {
|
||||
if len(to) == 0 {
|
||||
return ErrInvalidRecipient
|
||||
}
|
||||
|
||||
s.accountsLock.RLock()
|
||||
defer s.accountsLock.RUnlock()
|
||||
|
||||
service, ok := s.accounts[userID]
|
||||
if !ok {
|
||||
return ErrNoSuchUser
|
||||
}
|
||||
|
||||
return service.SendMail(ctx, addrID, from, to, r)
|
||||
}
|
||||
@ -21,3 +21,4 @@ import "errors"
|
||||
|
||||
var ErrInvalidRecipient = errors.New("invalid recipient")
|
||||
var ErrInvalidReturnPath = errors.New("invalid return path")
|
||||
var ErrNoSuchUser = errors.New("no such user")
|
||||
|
||||
@ -19,6 +19,7 @@ package smtp
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"io"
|
||||
|
||||
"github.com/ProtonMail/gluon/async"
|
||||
@ -34,7 +35,10 @@ import (
|
||||
// UserInterface is just wrapper to avoid recursive go module imports. To be removed when the identity service is ready.
|
||||
type UserInterface interface {
|
||||
ID() string
|
||||
CheckAuth(string, []byte) (string, error)
|
||||
WithSMTPData(context.Context, func(context.Context, map[string]proton.Address, proton.User, *vault.User) error) error
|
||||
ReportSMTPAuthSuccess(context.Context)
|
||||
ReportSMTPAuthFailed(username string)
|
||||
}
|
||||
|
||||
type Service struct {
|
||||
@ -91,6 +95,10 @@ func (s *Service) Start(group *async.Group) {
|
||||
})
|
||||
}
|
||||
|
||||
func (s *Service) UserID() string {
|
||||
return s.user.ID()
|
||||
}
|
||||
|
||||
func (s *Service) run(ctx context.Context) {
|
||||
s.log.Debug("Starting service main loop")
|
||||
defer s.log.Debug("Exiting service main loop")
|
||||
@ -128,5 +136,13 @@ type sendMailReq struct {
|
||||
|
||||
func (s *Service) sendMail(ctx context.Context, req *sendMailReq) error {
|
||||
defer async.HandlePanic(s.panicHandler)
|
||||
return s.smtpSendMail(ctx, req.authID, req.from, req.to, req.r)
|
||||
if err := s.smtpSendMail(ctx, req.authID, req.from, req.to, req.r); err != nil {
|
||||
if apiErr := new(proton.APIError); errors.As(err, &apiErr) {
|
||||
s.log.WithError(apiErr).WithField("Details", apiErr.DetailsToString()).Error("failed to send message")
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user