This repository has been archived by the owner on Apr 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
session.go
265 lines (226 loc) · 6.88 KB
/
session.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
package main
import (
"crypto/rand"
"database/sql"
"encoding/json"
"errors"
log "github.com/sirupsen/logrus"
"github.com/crewjam/saml"
"github.com/crewjam/saml/samlsp"
)
const (
passwordProtectedTransport = "urn:oasis:names:tc:SAML:2.0:ac:classes:PasswordProtectedTransport"
mobileTwoFactorContract = "urn:oasis:names:tc:SAML:2.0:ac:classes:MobileTwoFactorContract"
smartcard = "urn:oasis:names:tc:SAML:2.0:ac:classes:Smartcard"
smartcardPKI = "urn:oasis:names:tc:SAML:2.0:ac:classes:SmartcardPKI"
)
var digidAuthnContextClasses = map[string]string{
"Basis": passwordProtectedTransport,
"Midden": mobileTwoFactorContract,
"Substantieel": smartcard,
"Hoog": smartcardPKI,
}
var authnContextClasses = map[string]int{
passwordProtectedTransport: 1,
mobileTwoFactorContract: 2,
smartcard: 3,
smartcardPKI: 4,
}
func CompareAuthnContextClass(minimum string, acc string) bool {
return authnContextClasses[minimum] <= authnContextClasses[acc]
}
var chars = []rune("0123456789abcdefghkmnpqrstuvwxyz")
const ID_LENGTH = 20
func GenerateID() string {
bytes := make([]byte, ID_LENGTH)
_, err := rand.Read(bytes)
if err != nil {
log.Fatal(err)
}
result := make([]rune, ID_LENGTH)
for i := 0; i < ID_LENGTH; i++ {
result[i] = chars[int(bytes[i]&0x1f)]
}
return string(result)
}
type SamlSessionEncoder struct {
db *sql.DB
timeout int // timeout time in minutes
}
type SamlSession struct {
id string
attributes samlsp.Attributes
}
func (s *SamlSessionEncoder) New(assertion *saml.Assertion) (samlsp.Session, error) {
// Setup data
id := GenerateID()
attributes := make(samlsp.Attributes)
for _, statement := range assertion.AttributeStatements {
for _, attribute := range statement.Attributes {
for _, value := range attribute.Values {
attributes[attribute.Name] = append(attributes[attribute.Name], value.Value)
}
}
}
if assertion.Subject != nil {
if assertion.Subject.NameID != nil {
attributes["NameID"] = append(attributes["NameID"], assertion.Subject.NameID.Value)
}
}
for _, authnStatement := range assertion.AuthnStatements {
if authnStatement.AuthnContext.AuthnContextClassRef != nil {
attributes["AuthnContextClassRef"] = append(attributes["AuthnContextClassRef"], authnStatement.AuthnContext.AuthnContextClassRef.Value)
}
}
encodedAttributes, err := json.Marshal(attributes)
if err != nil {
log.Error(err)
return nil, err
}
_, err = s.db.Exec("INSERT INTO saml_session (sessionid, attributes, expiry) VALUES ($1, $2, NOW() + ($3 * Interval '1 minute'))", id, string(encodedAttributes), s.timeout)
if err != nil {
log.Error(err)
return nil, err
}
return &SamlSession{
id: id,
attributes: attributes,
}, nil
}
func (s *SamlSessionEncoder) Encode(session samlsp.Session) (string, error) {
return session.(*SamlSession).id, nil
}
func (s *SamlSessionEncoder) Decode(id string) (samlsp.Session, error) {
rows, err := s.db.Query("SELECT attributes FROM saml_session WHERE sessionid = $1 AND expiry > NOW()", id)
if err != nil {
log.Error(err)
return nil, err
}
defer rows.Close()
if !rows.Next() {
return nil, samlsp.ErrNoSession
}
var encodedAttributes string
err = rows.Scan(&encodedAttributes)
if err != nil {
log.Error(err)
return nil, err
}
var attributes samlsp.Attributes
err = json.Unmarshal([]byte(encodedAttributes), &attributes)
if err != nil {
log.Error(err)
return nil, err
}
return &SamlSession{
id: id,
attributes: attributes,
}, nil
}
func (s *SamlSessionEncoder) SetVerderHelpenSession(session *SamlSession, verder_helpen_session string, session_attributes string) error {
result, err := s.db.Exec("UPDATE saml_session SET verderhelpen_session_id = (SELECT id FROM verderhelpen_session WHERE sessionid=$2), session_attributes = $3 WHERE sessionid = $1", session.id, verder_helpen_session, session_attributes)
if err != nil {
log.Error(err)
return err
}
aff, err := result.RowsAffected()
if err != nil {
log.Error(err)
return err
}
if aff != 1 {
return samlsp.ErrNoSession
}
return nil
}
func (s *SamlSessionEncoder) GetVerderHelpenSession(session *SamlSession) (string, string, error) {
rows, err := s.db.Query("SELECT verderhelpen_session.sessionid, session_attributes FROM saml_session INNER JOIN verderhelpen_session ON saml_session.verderhelpen_session_id = verderhelpen_session.id")
if err != nil {
log.Error(err)
return "", "", err
}
defer rows.Close()
if !rows.Next() {
return "", "", samlsp.ErrNoSession
}
var sessionid, session_attributes string
err = rows.Scan(&sessionid, &session_attributes)
if err != nil {
log.Error(err)
return "", "", err
}
return sessionid, session_attributes, nil
}
func (s *SamlSessionEncoder) Logout(sessionid string) error {
result, err := s.db.Exec("DELETE FROM saml_session WHERE sessionid = $1", sessionid)
if err != nil {
log.Error(err)
return err
}
aff, err := result.RowsAffected()
if err != nil {
log.Error(err)
return err
}
if aff != 1 {
return samlsp.ErrNoSession
}
return nil
}
func (s *SamlSessionEncoder) Cleanup() error {
_, err := s.db.Exec("DELETE FROM saml_session WHERE expiry < NOW() - Interval '1 minute'")
return err
}
func (s *SamlSession) GetAttributes() samlsp.Attributes {
return s.attributes
}
type VerderHelpenSessionManager struct {
db *sql.DB
timeout int // session timeout in minutes
}
type VerderHelpenSession struct {
id string
attributes string
continuation string
attributeURL *string
}
func (m *VerderHelpenSessionManager) NewSession(attributes, continuation string, attributeURL *string) (*VerderHelpenSession, error) {
id := GenerateID()
_, err := m.db.Exec("INSERT INTO verderhelpen_session (sessionid, attributes, continuation, attr_url, expiry) VALUES ($1, $2, $3, $4, NOW() + ($5 * Interval '1 minute'))", id, attributes, continuation, attributeURL, m.timeout)
if err != nil {
return nil, err
}
return &VerderHelpenSession{
id: id,
attributes: attributes,
continuation: continuation,
attributeURL: attributeURL,
}, nil
}
func (m *VerderHelpenSessionManager) GetSession(id string) (*VerderHelpenSession, error) {
rows, err := m.db.Query("SELECT attributes, continuation, attr_url FROM verderhelpen_session WHERE sessionid = $1 AND expiry > NOW()", id)
if err != nil {
return nil, err
}
defer rows.Close()
if !rows.Next() {
return nil, errors.New("No Session")
}
var attributes string
var continuation string
var attributeURL *string
err = rows.Scan(&attributes, &continuation, &attributeURL)
if err != nil {
return nil, err
}
return &VerderHelpenSession{
id: id,
attributes: attributes,
continuation: continuation,
attributeURL: attributeURL,
}, nil
}
func (m *VerderHelpenSessionManager) Cleanup() error {
_, err := m.db.Exec("DELETE FROM verderhelpen_session WHERE expiry < NOW() - Interval '1 minute'")
return err
}