forked from Silverfish/proton-bridge
GODT-1650: Send extras
This commit is contained in:
@ -2,30 +2,36 @@ package bridge
|
||||
|
||||
import (
|
||||
"crypto/subtle"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/ProtonMail/proton-bridge/v2/internal/user"
|
||||
"github.com/bradenaw/juniper/xslices"
|
||||
"github.com/emersion/go-smtp"
|
||||
"golang.org/x/exp/slices"
|
||||
)
|
||||
|
||||
type smtpBackend struct {
|
||||
users []*user.User
|
||||
users map[string]*user.User
|
||||
usersLock sync.RWMutex
|
||||
}
|
||||
|
||||
func newSMTPBackend() (*smtpBackend, error) {
|
||||
return &smtpBackend{}, nil
|
||||
return &smtpBackend{
|
||||
users: make(map[string]*user.User),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (backend *smtpBackend) Login(state *smtp.ConnectionState, username, password string) (smtp.Session, error) {
|
||||
func (backend *smtpBackend) Login(state *smtp.ConnectionState, email, password string) (smtp.Session, error) {
|
||||
backend.usersLock.RLock()
|
||||
defer backend.usersLock.RUnlock()
|
||||
|
||||
for _, user := range backend.users {
|
||||
if slices.Contains(user.Emails(), username) && subtle.ConstantTimeCompare(user.BridgePass(), []byte(password)) == 1 {
|
||||
return user.NewSMTPSession(username), nil
|
||||
if subtle.ConstantTimeCompare(user.BridgePass(), []byte(password)) != 1 {
|
||||
continue
|
||||
}
|
||||
|
||||
if email := strings.ToLower(email); slices.Contains(user.Emails(), email) {
|
||||
return user.NewSMTPSession(email)
|
||||
}
|
||||
}
|
||||
|
||||
@ -38,17 +44,15 @@ func (backend *smtpBackend) AnonymousLogin(state *smtp.ConnectionState) (smtp.Se
|
||||
|
||||
// addUser adds the given user to the backend.
|
||||
// It returns an error if a user with the same ID already exists.
|
||||
func (backend *smtpBackend) addUser(user *user.User) error {
|
||||
func (backend *smtpBackend) addUser(newUser *user.User) error {
|
||||
backend.usersLock.Lock()
|
||||
defer backend.usersLock.Unlock()
|
||||
|
||||
for _, u := range backend.users {
|
||||
if u.ID() == user.ID() {
|
||||
return ErrUserAlreadyExists
|
||||
}
|
||||
if _, ok := backend.users[newUser.ID()]; ok {
|
||||
return ErrUserAlreadyExists
|
||||
}
|
||||
|
||||
backend.users = append(backend.users, user)
|
||||
backend.users[newUser.ID()] = newUser
|
||||
|
||||
return nil
|
||||
}
|
||||
@ -59,13 +63,11 @@ func (backend *smtpBackend) removeUser(user *user.User) error {
|
||||
backend.usersLock.Lock()
|
||||
defer backend.usersLock.Unlock()
|
||||
|
||||
idx := xslices.Index(backend.users, user)
|
||||
|
||||
if idx < 0 {
|
||||
if _, ok := backend.users[user.ID()]; !ok {
|
||||
return ErrNoSuchUser
|
||||
}
|
||||
|
||||
backend.users = append(backend.users[:idx], backend.users[idx+1:]...)
|
||||
delete(backend.users, user.ID())
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user