1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
|
package koushin
import (
"crypto/rand"
"encoding/base64"
"errors"
"fmt"
"sync"
imapclient "github.com/emersion/go-imap/client"
)
func generateToken() (string, error) {
b := make([]byte, 32)
_, err := rand.Read(b)
if err != nil {
return "", err
}
return base64.URLEncoding.EncodeToString(b), nil
}
var ErrSessionExpired = errors.New("session expired")
type AuthError struct {
cause error
}
func (err AuthError) Error() string {
return fmt.Sprintf("authentication failed: %v", err.cause)
}
type Session struct {
locker sync.Mutex
imapConn *imapclient.Client
username, password string
}
func (s *Session) Do(f func(*imapclient.Client) error) error {
s.locker.Lock()
defer s.locker.Unlock()
return f(s.imapConn)
}
// TODO: expiration timer
type SessionManager struct {
locker sync.Mutex
sessions map[string]*Session
newIMAPClient func() (*imapclient.Client, error)
}
func NewSessionManager(newIMAPClient func() (*imapclient.Client, error)) *SessionManager {
return &SessionManager{
sessions: make(map[string]*Session),
newIMAPClient: newIMAPClient,
}
}
func (sm *SessionManager) connect(username, password string) (*imapclient.Client, error) {
c, err := sm.newIMAPClient()
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) {
sm.locker.Lock()
defer sm.locker.Unlock()
session, ok := sm.sessions[token]
if !ok {
return nil, ErrSessionExpired
}
return session, nil
}
func (sm *SessionManager) Put(username, password string) (token string, err error) {
c, err := sm.connect(username, password)
if err != nil {
return "", err
}
sm.locker.Lock()
defer sm.locker.Unlock()
for {
var err error
token, err = generateToken()
if err != nil {
c.Logout()
return "", err
}
if _, ok := sm.sessions[token]; !ok {
break
}
}
sm.sessions[token] = &Session{
imapConn: c,
username: username,
password: password,
}
go func() {
<-c.LoggedOut()
sm.locker.Lock()
delete(sm.sessions, token)
sm.locker.Unlock()
}()
return token, nil
}
|