2020-05-13 12:07:44 +00:00
|
|
|
package alps
|
2019-12-02 16:24:19 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/rand"
|
|
|
|
"encoding/base64"
|
|
|
|
"errors"
|
2019-12-09 17:35:51 +00:00
|
|
|
"fmt"
|
2020-10-29 19:18:36 +00:00
|
|
|
"mime/multipart"
|
2020-01-20 12:22:18 +00:00
|
|
|
"net/http"
|
2020-02-11 17:39:52 +00:00
|
|
|
"os"
|
2019-12-02 16:40:53 +00:00
|
|
|
"sync"
|
2019-12-09 18:25:45 +00:00
|
|
|
"time"
|
2019-12-02 16:24:19 +00:00
|
|
|
|
|
|
|
imapclient "github.com/emersion/go-imap/client"
|
2019-12-16 11:51:42 +00:00
|
|
|
"github.com/emersion/go-sasl"
|
2019-12-16 13:53:56 +00:00
|
|
|
"github.com/emersion/go-smtp"
|
2020-10-29 19:18:36 +00:00
|
|
|
"github.com/google/uuid"
|
2020-01-28 19:04:18 +00:00
|
|
|
"github.com/labstack/echo/v4"
|
2019-12-02 16:24:19 +00:00
|
|
|
)
|
|
|
|
|
2019-12-09 18:25:45 +00:00
|
|
|
// TODO: make this configurable
|
|
|
|
const sessionDuration = 30 * time.Minute
|
2020-11-19 14:27:59 +00:00
|
|
|
const maxAttachmentSize = 32 << 20 // 32 MiB
|
2019-12-09 18:25:45 +00:00
|
|
|
|
2019-12-02 16:24:19 +00:00
|
|
|
func generateToken() (string, error) {
|
|
|
|
b := make([]byte, 32)
|
|
|
|
_, err := rand.Read(b)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return base64.URLEncoding.EncodeToString(b), nil
|
|
|
|
}
|
|
|
|
|
2020-11-19 14:27:59 +00:00
|
|
|
var (
|
|
|
|
ErrSessionExpired = errors.New("session expired")
|
|
|
|
ErrAttachmentCacheSize = errors.New("Attachments on session exceed maximum file size")
|
|
|
|
)
|
2019-12-02 16:24:19 +00:00
|
|
|
|
2019-12-11 14:24:39 +00:00
|
|
|
// AuthError wraps an authentication error.
|
2019-12-09 17:35:51 +00:00
|
|
|
type AuthError struct {
|
|
|
|
cause error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (err AuthError) Error() string {
|
|
|
|
return fmt.Sprintf("authentication failed: %v", err.cause)
|
|
|
|
}
|
|
|
|
|
2019-12-11 14:24:39 +00:00
|
|
|
// Session is an active user session. It may also hold an IMAP connection.
|
2019-12-17 14:55:52 +00:00
|
|
|
//
|
|
|
|
// The session's password is not available to plugins. Plugins should use the
|
|
|
|
// session helpers to authenticate outgoing connections, for instance DoSMTP.
|
2019-12-03 14:44:20 +00:00
|
|
|
type Session struct {
|
2019-12-09 18:25:45 +00:00
|
|
|
manager *SessionManager
|
2019-12-03 14:44:20 +00:00
|
|
|
username, password string
|
2019-12-11 11:54:00 +00:00
|
|
|
token string
|
2019-12-09 18:25:45 +00:00
|
|
|
closed chan struct{}
|
|
|
|
pings chan struct{}
|
|
|
|
timer *time.Timer
|
2020-01-28 19:04:18 +00:00
|
|
|
store Store
|
2019-12-09 18:25:45 +00:00
|
|
|
|
2020-01-28 14:40:52 +00:00
|
|
|
imapLocker sync.Mutex
|
|
|
|
imapConn *imapclient.Client // protected by locker, can be nil
|
2020-10-29 19:18:36 +00:00
|
|
|
|
|
|
|
attachmentsLocker sync.Mutex
|
|
|
|
attachments map[string]*Attachment // protected by attachmentsLocker
|
|
|
|
}
|
|
|
|
|
|
|
|
type Attachment struct {
|
2020-11-13 15:33:19 +00:00
|
|
|
File *multipart.FileHeader
|
|
|
|
Form *multipart.Form
|
2019-12-09 18:25:45 +00:00
|
|
|
}
|
|
|
|
|
2019-12-11 12:03:31 +00:00
|
|
|
func (s *Session) ping() {
|
2019-12-09 18:25:45 +00:00
|
|
|
s.pings <- struct{}{}
|
2019-12-03 14:44:20 +00:00
|
|
|
}
|
|
|
|
|
2019-12-16 11:51:42 +00:00
|
|
|
// Username returns the session's username.
|
|
|
|
func (s *Session) Username() string {
|
|
|
|
return s.username
|
|
|
|
}
|
|
|
|
|
2019-12-16 12:01:33 +00:00
|
|
|
// DoIMAP executes an IMAP operation on this session. The IMAP client can only
|
|
|
|
// be used from inside f.
|
|
|
|
func (s *Session) DoIMAP(f func(*imapclient.Client) error) error {
|
2020-01-28 14:40:52 +00:00
|
|
|
s.imapLocker.Lock()
|
|
|
|
defer s.imapLocker.Unlock()
|
2019-12-03 16:18:17 +00:00
|
|
|
|
2019-12-09 18:25:45 +00:00
|
|
|
if s.imapConn == nil {
|
|
|
|
var err error
|
2020-02-10 16:21:34 +00:00
|
|
|
s.imapConn, err = s.manager.connectIMAP(s.username, s.password)
|
2019-12-09 18:25:45 +00:00
|
|
|
if err != nil {
|
|
|
|
s.Close()
|
|
|
|
return fmt.Errorf("failed to re-connect to IMAP server: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-03 16:18:17 +00:00
|
|
|
return f(s.imapConn)
|
|
|
|
}
|
|
|
|
|
2019-12-16 12:07:35 +00:00
|
|
|
// DoSMTP executes an SMTP operation on this session. The SMTP client can only
|
|
|
|
// be used from inside f.
|
|
|
|
func (s *Session) DoSMTP(f func(*smtp.Client) error) error {
|
2019-12-16 11:51:42 +00:00
|
|
|
c, err := s.manager.dialSMTP()
|
|
|
|
if err != nil {
|
2019-12-16 12:07:35 +00:00
|
|
|
return err
|
2019-12-16 11:51:42 +00:00
|
|
|
}
|
2019-12-16 12:07:35 +00:00
|
|
|
defer c.Close()
|
2019-12-16 11:51:42 +00:00
|
|
|
|
|
|
|
auth := sasl.NewPlainClient("", s.username, s.password)
|
|
|
|
if err := c.Auth(auth); err != nil {
|
2019-12-16 12:07:35 +00:00
|
|
|
return AuthError{err}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := f(c); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := c.Quit(); err != nil {
|
|
|
|
return fmt.Errorf("QUIT failed: %v", err)
|
2019-12-16 11:51:42 +00:00
|
|
|
}
|
|
|
|
|
2019-12-16 12:07:35 +00:00
|
|
|
return nil
|
2019-12-16 11:51:42 +00:00
|
|
|
}
|
|
|
|
|
2020-01-10 18:35:55 +00:00
|
|
|
// SetHTTPBasicAuth adds an Authorization header field to the request with
|
|
|
|
// this session's credentials.
|
|
|
|
func (s *Session) SetHTTPBasicAuth(req *http.Request) {
|
|
|
|
// TODO: find a way to make it harder for plugins to steal credentials
|
|
|
|
req.SetBasicAuth(s.username, s.password)
|
|
|
|
}
|
|
|
|
|
2019-12-11 14:24:39 +00:00
|
|
|
// Close destroys the session. This can be used to log the user out.
|
2019-12-09 18:25:45 +00:00
|
|
|
func (s *Session) Close() {
|
2020-10-29 19:18:36 +00:00
|
|
|
s.attachmentsLocker.Lock()
|
|
|
|
defer s.attachmentsLocker.Unlock()
|
|
|
|
|
|
|
|
for _, f := range s.attachments {
|
|
|
|
f.Form.RemoveAll()
|
|
|
|
}
|
|
|
|
|
2019-12-09 18:25:45 +00:00
|
|
|
select {
|
|
|
|
case <-s.closed:
|
|
|
|
// This space is intentionally left blank
|
|
|
|
default:
|
|
|
|
close(s.closed)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-29 19:18:36 +00:00
|
|
|
// Puts an attachment and returns a generated UUID
|
|
|
|
func (s *Session) PutAttachment(in *multipart.FileHeader,
|
|
|
|
form *multipart.Form) (string, error) {
|
|
|
|
id := uuid.New()
|
|
|
|
s.attachmentsLocker.Lock()
|
2020-11-19 14:27:59 +00:00
|
|
|
|
|
|
|
var size int64
|
|
|
|
for _, a := range s.attachments {
|
|
|
|
size += a.File.Size
|
|
|
|
}
|
|
|
|
if size + in.Size > maxAttachmentSize {
|
|
|
|
return "", ErrAttachmentCacheSize
|
|
|
|
}
|
|
|
|
|
2020-10-29 19:18:36 +00:00
|
|
|
s.attachments[id.String()] = &Attachment{
|
|
|
|
File: in,
|
|
|
|
Form: form,
|
|
|
|
}
|
|
|
|
s.attachmentsLocker.Unlock()
|
|
|
|
return id.String(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Removes an attachment from the session. Returns nil if there was no such
|
|
|
|
// attachment.
|
|
|
|
func (s *Session) PopAttachment(uuid string) *Attachment {
|
|
|
|
s.attachmentsLocker.Lock()
|
|
|
|
defer s.attachmentsLocker.Unlock()
|
|
|
|
|
|
|
|
a, ok := s.attachments[uuid]
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
delete(s.attachments, uuid)
|
|
|
|
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
2020-01-28 19:04:18 +00:00
|
|
|
// Store returns a store suitable for storing persistent user data.
|
|
|
|
func (s *Session) Store() Store {
|
|
|
|
return s.store
|
|
|
|
}
|
|
|
|
|
2019-12-16 11:51:42 +00:00
|
|
|
type (
|
|
|
|
// DialIMAPFunc connects to the upstream IMAP server.
|
|
|
|
DialIMAPFunc func() (*imapclient.Client, error)
|
|
|
|
// DialSMTPFunc connects to the upstream SMTP server.
|
|
|
|
DialSMTPFunc func() (*smtp.Client, error)
|
|
|
|
)
|
|
|
|
|
2019-12-11 14:24:39 +00:00
|
|
|
// SessionManager keeps track of active sessions. It connects and re-connects
|
|
|
|
// to the upstream IMAP server as necessary. It prunes expired sessions.
|
2019-12-09 17:16:27 +00:00
|
|
|
type SessionManager struct {
|
2019-12-16 11:51:42 +00:00
|
|
|
dialIMAP DialIMAPFunc
|
|
|
|
dialSMTP DialSMTPFunc
|
2020-01-28 19:04:18 +00:00
|
|
|
logger echo.Logger
|
2020-02-11 17:39:52 +00:00
|
|
|
debug bool
|
2019-12-09 18:25:45 +00:00
|
|
|
|
|
|
|
locker sync.Mutex
|
|
|
|
sessions map[string]*Session // protected by locker
|
2019-12-02 16:24:19 +00:00
|
|
|
}
|
|
|
|
|
2020-02-11 17:39:52 +00:00
|
|
|
func newSessionManager(dialIMAP DialIMAPFunc, dialSMTP DialSMTPFunc, logger echo.Logger, debug bool) *SessionManager {
|
2019-12-09 17:16:27 +00:00
|
|
|
return &SessionManager{
|
2019-12-16 13:53:56 +00:00
|
|
|
sessions: make(map[string]*Session),
|
2019-12-16 11:51:42 +00:00
|
|
|
dialIMAP: dialIMAP,
|
|
|
|
dialSMTP: dialSMTP,
|
2020-01-28 19:04:18 +00:00
|
|
|
logger: logger,
|
2020-02-11 17:39:52 +00:00
|
|
|
debug: debug,
|
2019-12-02 16:24:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-29 19:18:36 +00:00
|
|
|
func (sm *SessionManager) Close() {
|
|
|
|
for _, s := range sm.sessions {
|
|
|
|
s.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-10 16:21:34 +00:00
|
|
|
func (sm *SessionManager) connectIMAP(username, password string) (*imapclient.Client, error) {
|
2019-12-16 11:51:42 +00:00
|
|
|
c, err := sm.dialIMAP()
|
2019-12-09 17:35:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := c.Login(username, password); err != nil {
|
|
|
|
c.Logout()
|
|
|
|
return nil, AuthError{err}
|
|
|
|
}
|
|
|
|
|
2020-02-11 17:39:52 +00:00
|
|
|
if sm.debug {
|
|
|
|
c.SetDebug(os.Stderr)
|
|
|
|
}
|
|
|
|
|
2019-12-09 17:35:51 +00:00
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
2019-12-11 12:03:31 +00:00
|
|
|
func (sm *SessionManager) get(token string) (*Session, error) {
|
2019-12-09 17:16:27 +00:00
|
|
|
sm.locker.Lock()
|
|
|
|
defer sm.locker.Unlock()
|
2019-12-02 16:40:53 +00:00
|
|
|
|
2019-12-09 17:16:27 +00:00
|
|
|
session, ok := sm.sessions[token]
|
2019-12-02 16:24:19 +00:00
|
|
|
if !ok {
|
2020-11-19 14:27:59 +00:00
|
|
|
return nil, ErrSessionExpired
|
2019-12-02 16:24:19 +00:00
|
|
|
}
|
2019-12-03 14:44:20 +00:00
|
|
|
return session, nil
|
2019-12-02 16:24:19 +00:00
|
|
|
}
|
|
|
|
|
2019-12-11 14:24:39 +00:00
|
|
|
// Put connects to the IMAP server and creates a new session. If authentication
|
|
|
|
// fails, the error will be of type AuthError.
|
2019-12-09 18:25:45 +00:00
|
|
|
func (sm *SessionManager) Put(username, password string) (*Session, error) {
|
2020-02-10 16:21:34 +00:00
|
|
|
c, err := sm.connectIMAP(username, password)
|
2019-12-09 17:35:51 +00:00
|
|
|
if err != nil {
|
2019-12-09 18:25:45 +00:00
|
|
|
return nil, err
|
2019-12-09 17:35:51 +00:00
|
|
|
}
|
|
|
|
|
2019-12-09 17:16:27 +00:00
|
|
|
sm.locker.Lock()
|
|
|
|
defer sm.locker.Unlock()
|
2019-12-02 16:40:53 +00:00
|
|
|
|
2019-12-09 18:25:45 +00:00
|
|
|
var token string
|
2019-12-02 16:24:19 +00:00
|
|
|
for {
|
|
|
|
token, err = generateToken()
|
|
|
|
if err != nil {
|
2019-12-09 17:35:51 +00:00
|
|
|
c.Logout()
|
2019-12-09 18:25:45 +00:00
|
|
|
return nil, err
|
2019-12-02 16:24:19 +00:00
|
|
|
}
|
|
|
|
|
2019-12-09 17:16:27 +00:00
|
|
|
if _, ok := sm.sessions[token]; !ok {
|
2019-12-02 16:24:19 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-09 18:25:45 +00:00
|
|
|
s := &Session{
|
2020-10-29 19:18:36 +00:00
|
|
|
manager: sm,
|
|
|
|
closed: make(chan struct{}),
|
|
|
|
pings: make(chan struct{}, 5),
|
|
|
|
imapConn: c,
|
|
|
|
username: username,
|
|
|
|
password: password,
|
|
|
|
token: token,
|
|
|
|
attachments: make(map[string]*Attachment),
|
2019-12-03 14:44:20 +00:00
|
|
|
}
|
2020-01-28 19:04:18 +00:00
|
|
|
|
|
|
|
s.store, err = newStore(s, sm.logger)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-12-09 18:25:45 +00:00
|
|
|
sm.sessions[token] = s
|
2019-12-02 16:36:43 +00:00
|
|
|
|
|
|
|
go func() {
|
2019-12-09 18:25:45 +00:00
|
|
|
timer := time.NewTimer(sessionDuration)
|
|
|
|
|
|
|
|
alive := true
|
|
|
|
for alive {
|
|
|
|
var loggedOut <-chan struct{}
|
2020-01-28 14:40:52 +00:00
|
|
|
s.imapLocker.Lock()
|
2019-12-09 18:25:45 +00:00
|
|
|
if s.imapConn != nil {
|
|
|
|
loggedOut = s.imapConn.LoggedOut()
|
|
|
|
}
|
2020-01-28 14:40:52 +00:00
|
|
|
s.imapLocker.Unlock()
|
2019-12-09 18:25:45 +00:00
|
|
|
|
|
|
|
select {
|
|
|
|
case <-loggedOut:
|
2020-01-28 14:40:52 +00:00
|
|
|
s.imapLocker.Lock()
|
2019-12-09 18:25:45 +00:00
|
|
|
s.imapConn = nil
|
2020-01-28 14:40:52 +00:00
|
|
|
s.imapLocker.Unlock()
|
2019-12-09 18:25:45 +00:00
|
|
|
case <-s.pings:
|
|
|
|
if !timer.Stop() {
|
|
|
|
<-timer.C
|
|
|
|
}
|
|
|
|
timer.Reset(sessionDuration)
|
|
|
|
case <-timer.C:
|
|
|
|
alive = false
|
|
|
|
case <-s.closed:
|
|
|
|
alive = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-20 14:25:46 +00:00
|
|
|
timer.Stop()
|
2019-12-09 18:25:45 +00:00
|
|
|
|
2020-01-28 14:40:52 +00:00
|
|
|
s.imapLocker.Lock()
|
2019-12-09 18:25:45 +00:00
|
|
|
if s.imapConn != nil {
|
|
|
|
s.imapConn.Logout()
|
|
|
|
}
|
2020-01-28 14:40:52 +00:00
|
|
|
s.imapLocker.Unlock()
|
2019-12-02 16:40:53 +00:00
|
|
|
|
2019-12-09 17:16:27 +00:00
|
|
|
sm.locker.Lock()
|
|
|
|
delete(sm.sessions, token)
|
|
|
|
sm.locker.Unlock()
|
2019-12-02 16:36:43 +00:00
|
|
|
}()
|
|
|
|
|
2019-12-09 18:25:45 +00:00
|
|
|
return s, nil
|
2019-12-02 16:24:19 +00:00
|
|
|
}
|