aboutsummaryrefslogtreecommitdiffstats
path: root/conn_pool.go
diff options
context:
space:
mode:
authorLibravatarLibravatar Simon Ser <[email protected]> 2019-12-03 15:44:20 +0100
committerLibravatarLibravatar Simon Ser <[email protected]> 2019-12-03 15:44:24 +0100
commit702719c072dea73ed5ec046fa640b3151ebcbc55 (patch)
treee25ed8a538eead61001906f1af326897f61b8fb3 /conn_pool.go
parentae79f998769cdd62dbf856db0b3b53528b532438 (diff)
downloadalps-702719c072dea73ed5ec046fa640b3151ebcbc55.tar.gz
alps-702719c072dea73ed5ec046fa640b3151ebcbc55.tar.bz2
alps-702719c072dea73ed5ec046fa640b3151ebcbc55.zip
Save username/password in session
This is required for authenticating with the SMTP server when composing a new message.
Diffstat (limited to 'conn_pool.go')
-rw-r--r--conn_pool.go31
1 files changed, 20 insertions, 11 deletions
diff --git a/conn_pool.go b/conn_pool.go
index fb530b9..6e244b0 100644
--- a/conn_pool.go
+++ b/conn_pool.go
@@ -20,30 +20,35 @@ func generateToken() (string, error) {
var ErrSessionExpired = errors.New("session expired")
+type Session struct {
+ imapConn *imapclient.Client
+ username, password string
+}
+
// TODO: expiration timer
type ConnPool struct {
locker sync.Mutex
- conns map[string]*imapclient.Client
+ sessions map[string]*Session
}
func NewConnPool() *ConnPool {
return &ConnPool{
- conns: make(map[string]*imapclient.Client),
+ sessions: make(map[string]*Session),
}
}
-func (pool *ConnPool) Get(token string) (*imapclient.Client, error) {
+func (pool *ConnPool) Get(token string) (*Session, error) {
pool.locker.Lock()
defer pool.locker.Unlock()
- conn, ok := pool.conns[token]
+ session, ok := pool.sessions[token]
if !ok {
return nil, ErrSessionExpired
}
- return conn, nil
+ return session, nil
}
-func (pool *ConnPool) Put(conn *imapclient.Client) (token string, err error) {
+func (pool *ConnPool) Put(imapConn *imapclient.Client, username, password string) (token string, err error) {
pool.locker.Lock()
defer pool.locker.Unlock()
@@ -51,22 +56,26 @@ func (pool *ConnPool) Put(conn *imapclient.Client) (token string, err error) {
var err error
token, err = generateToken()
if err != nil {
- conn.Logout()
+ imapConn.Logout()
return "", err
}
- if _, ok := pool.conns[token]; !ok {
+ if _, ok := pool.sessions[token]; !ok {
break
}
}
- pool.conns[token] = conn
+ pool.sessions[token] = &Session{
+ imapConn: imapConn,
+ username: username,
+ password: password,
+ }
go func() {
- <-conn.LoggedOut()
+ <-imapConn.LoggedOut()
pool.locker.Lock()
- delete(pool.conns, token)
+ delete(pool.sessions, token)
pool.locker.Unlock()
}()