alps/session.go

249 lines
5.1 KiB
Go
Raw Normal View History

2019-12-02 16:24:19 +00:00
package koushin
import (
"crypto/rand"
"encoding/base64"
"errors"
"fmt"
2019-12-02 16:40:53 +00:00
"sync"
"time"
2019-12-02 16:24:19 +00:00
imapclient "github.com/emersion/go-imap/client"
"github.com/emersion/go-sasl"
2019-12-16 13:53:56 +00:00
"github.com/emersion/go-smtp"
2019-12-02 16:24:19 +00:00
)
// TODO: make this configurable
const sessionDuration = 30 * time.Minute
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
}
2019-12-11 14:24:39 +00:00
var errSessionExpired = errors.New("session expired")
2019-12-02 16:24:19 +00:00
2019-12-11 14:24:39 +00:00
// AuthError wraps an authentication error.
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.
//
// The session's password is not available to plugins. Plugins should use the
// session helpers to authenticate outgoing connections, for instance DoSMTP.
type Session struct {
manager *SessionManager
username, password string
token string
closed chan struct{}
pings chan struct{}
timer *time.Timer
locker sync.Mutex
imapConn *imapclient.Client // protected by locker, can be nil
}
func (s *Session) ping() {
s.pings <- struct{}{}
}
// 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 {
s.locker.Lock()
defer s.locker.Unlock()
if s.imapConn == nil {
var err error
s.imapConn, err = s.manager.connect(s.username, s.password)
if err != nil {
s.Close()
return fmt.Errorf("failed to re-connect to IMAP server: %v", err)
}
}
return f(s.imapConn)
}
// 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 {
c, err := s.manager.dialSMTP()
if err != nil {
return err
}
defer c.Close()
auth := sasl.NewPlainClient("", s.username, s.password)
if err := c.Auth(auth); err != nil {
return AuthError{err}
}
if err := f(c); err != nil {
return err
}
if err := c.Quit(); err != nil {
return fmt.Errorf("QUIT failed: %v", err)
}
return nil
}
2019-12-11 14:24:39 +00:00
// Close destroys the session. This can be used to log the user out.
func (s *Session) Close() {
select {
case <-s.closed:
// This space is intentionally left blank
default:
close(s.closed)
}
}
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 {
dialIMAP DialIMAPFunc
dialSMTP DialSMTPFunc
locker sync.Mutex
sessions map[string]*Session // protected by locker
2019-12-02 16:24:19 +00:00
}
func newSessionManager(dialIMAP DialIMAPFunc, dialSMTP DialSMTPFunc) *SessionManager {
2019-12-09 17:16:27 +00:00
return &SessionManager{
2019-12-16 13:53:56 +00:00
sessions: make(map[string]*Session),
dialIMAP: dialIMAP,
dialSMTP: dialSMTP,
2019-12-02 16:24:19 +00:00
}
}
func (sm *SessionManager) connect(username, password string) (*imapclient.Client, error) {
c, err := sm.dialIMAP()
if err != nil {
return nil, err
}
if err := c.Login(username, password); err != nil {
c.Logout()
return nil, AuthError{err}
}
return c, nil
}
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 {
return nil, errSessionExpired
2019-12-02 16:24:19 +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.
func (sm *SessionManager) Put(username, password string) (*Session, error) {
c, err := sm.connect(username, password)
if err != nil {
return nil, err
}
2019-12-09 17:16:27 +00:00
sm.locker.Lock()
defer sm.locker.Unlock()
2019-12-02 16:40:53 +00:00
var token string
2019-12-02 16:24:19 +00:00
for {
var err error
token, err = generateToken()
if err != nil {
c.Logout()
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
}
}
s := &Session{
manager: sm,
closed: make(chan struct{}),
pings: make(chan struct{}, 5),
imapConn: c,
username: username,
password: password,
token: token,
}
sm.sessions[token] = s
2019-12-02 16:36:43 +00:00
go func() {
timer := time.NewTimer(sessionDuration)
alive := true
for alive {
var loggedOut <-chan struct{}
s.locker.Lock()
if s.imapConn != nil {
loggedOut = s.imapConn.LoggedOut()
}
s.locker.Unlock()
select {
case <-loggedOut:
s.locker.Lock()
s.imapConn = nil
s.locker.Unlock()
case <-s.pings:
if !timer.Stop() {
<-timer.C
}
timer.Reset(sessionDuration)
case <-timer.C:
alive = false
case <-s.closed:
alive = false
}
}
if !timer.Stop() {
<-timer.C
}
s.locker.Lock()
if s.imapConn != nil {
s.imapConn.Logout()
}
s.locker.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
}()
return s, nil
2019-12-02 16:24:19 +00:00
}