forked from privacybydesign/irmago
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sessions.go
196 lines (165 loc) · 4.58 KB
/
sessions.go
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package servercore
import (
"crypto/rand"
"sync"
"time"
"github.com/privacybydesign/gabi"
"github.com/privacybydesign/gabi/big"
"github.com/privacybydesign/irmago"
"github.com/privacybydesign/irmago/server"
"github.com/sirupsen/logrus"
"gopkg.in/antage/eventsource.v1"
)
type session struct {
sync.Mutex
action irma.Action
token string
clientToken string
version *irma.ProtocolVersion
rrequest irma.RequestorRequest
request irma.SessionRequest
legacyCompatible bool // if the request is convertible to pre-condiscon format
status server.Status
prevStatus server.Status
evtSource eventsource.EventSource
responseCache responseCache
lastActive time.Time
result *server.SessionResult
kssProofs map[irma.SchemeManagerIdentifier]*gabi.ProofP
conf *server.Configuration
sessions sessionStore
}
type responseCache struct {
message []byte
response []byte
status int
sessionStatus server.Status
}
type sessionStore interface {
get(token string) *session
clientGet(token string) *session
add(session *session)
update(session *session)
deleteExpired()
stop()
}
type memorySessionStore struct {
sync.RWMutex
conf *server.Configuration
requestor map[string]*session
client map[string]*session
}
const (
maxSessionLifetime = 5 * time.Minute // After this a session is cancelled
sessionChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
)
var (
minProtocolVersion = irma.NewVersion(2, 4)
maxProtocolVersion = irma.NewVersion(2, 5)
)
func (s *memorySessionStore) get(t string) *session {
s.RLock()
defer s.RUnlock()
return s.requestor[t]
}
func (s *memorySessionStore) clientGet(t string) *session {
s.RLock()
defer s.RUnlock()
return s.client[t]
}
func (s *memorySessionStore) add(session *session) {
s.Lock()
defer s.Unlock()
s.requestor[session.token] = session
s.client[session.clientToken] = session
}
func (s *memorySessionStore) update(session *session) {
session.onUpdate()
}
func (s *memorySessionStore) stop() {
s.Lock()
defer s.Unlock()
for _, session := range s.requestor {
if session.evtSource != nil {
session.evtSource.Close()
}
}
}
func (s *memorySessionStore) deleteExpired() {
// First check which sessions have expired
// We don't need a write lock for this yet, so postpone that for actual deleting
s.RLock()
expired := make([]string, 0, len(s.requestor))
for token, session := range s.requestor {
session.Lock()
timeout := maxSessionLifetime
if session.status == server.StatusInitialized && session.rrequest.Base().ClientTimeout != 0 {
timeout = time.Duration(session.rrequest.Base().ClientTimeout) * time.Second
}
if session.lastActive.Add(timeout).Before(time.Now()) {
if !session.status.Finished() {
s.conf.Logger.WithFields(logrus.Fields{"session": session.token}).Infof("Session expired")
session.markAlive()
session.setStatus(server.StatusTimeout)
} else {
s.conf.Logger.WithFields(logrus.Fields{"session": session.token}).Infof("Deleting session")
expired = append(expired, token)
}
}
session.Unlock()
}
s.RUnlock()
// Using a write lock, delete the expired sessions
s.Lock()
for _, token := range expired {
session := s.requestor[token]
if session.evtSource != nil {
session.evtSource.Close()
}
delete(s.client, session.clientToken)
delete(s.requestor, token)
}
s.Unlock()
}
var one *big.Int = big.NewInt(1)
func (s *Server) newSession(action irma.Action, request irma.RequestorRequest) *session {
token := newSessionToken()
clientToken := newSessionToken()
ses := &session{
action: action,
rrequest: request,
request: request.SessionRequest(),
lastActive: time.Now(),
token: token,
clientToken: clientToken,
status: server.StatusInitialized,
prevStatus: server.StatusInitialized,
conf: s.conf,
sessions: s.sessions,
result: &server.SessionResult{
LegacySession: request.SessionRequest().Base().Legacy(),
Token: token,
Type: action,
Status: server.StatusInitialized,
},
}
s.conf.Logger.WithFields(logrus.Fields{"session": ses.token}).Debug("New session started")
nonce, _ := gabi.RandomBigInt(gabi.DefaultSystemParameters[2048].Lstatzk)
ses.request.Base().Nonce = nonce
ses.request.Base().Context = one
s.sessions.add(ses)
return ses
}
func newSessionToken() string {
count := 20
r := make([]byte, count)
_, err := rand.Read(r)
if err != nil {
panic(err)
}
b := make([]byte, count)
for i := range b {
b[i] = sessionChars[r[i]%byte(len(sessionChars))]
}
return string(b)
}