-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsessionManager.go
186 lines (174 loc) · 4.01 KB
/
sessionManager.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
package gosecuresessions
import (
"bytes"
"encoding/base64"
"encoding/gob"
"errors"
"net/http"
cok "github.com/GolangToolKits/go-secure-cookies"
)
const (
cookieSession int = 0
)
// SessionManager SessionManager
type SessionManager interface {
NewSession(r *http.Request, name string) Session
}
// Manager Manager
type Manager struct {
cookies cok.Cookies
config ConfigOptions
}
// NewSessionManager securekey must be at least 16 char long
// The key argument should be the AES key,
// either 16, 24, or 32 bytes to select
// AES-128, AES-192, or AES-256.
func NewSessionManager(secretKey string, config ConfigOptions) (SessionManager, error) {
var rtn SessionManager
var rtnErr error
cookies, err := cok.NewCookies(secretKey)
if err == nil {
var m Manager
m.cookies = cookies
m.config = config
rtn = &m
} else {
rtnErr = err
}
return rtn, rtnErr
}
// NewSession NewSession
// Gets session from request if one exists
// Creates a new session if one doesn't exist in request
func (m *Manager) NewSession(r *http.Request, name string) Session {
var rtn Session
if m.config.SessionType == cookieSession {
eses, err := m.getSession(r, name)
if eses != nil && err == nil {
rtn = eses
} else {
var ses CookieSession
ses.manager = m
ses.name = name
if m.config.Path == "" {
ses.path = "/"
} else {
ses.path = m.config.Path
}
ses.maxAge = m.config.MaxAge
ses.values = make(map[any]any)
rtn = &ses
}
}
return rtn
}
// SaveSession Saves the Session to a secure cookie
func (m *Manager) saveSession(w http.ResponseWriter, s Session) bool {
var rtn bool
if s != nil {
cs := s.(*CookieSession)
ss, err := m.serializeSession(s)
if err == nil {
var ck http.Cookie
ck.Name = cs.name
ck.Value = ss
ck.Path = cs.path
ck.MaxAge = cs.maxAge
ck.Domain = cs.domain
err := m.cookies.Write(w, ck)
if err == nil {
rtn = true
}
}
}
return rtn
}
// GetSession GetSession
func (m *Manager) getSession(r *http.Request, name string) (Session, error) {
var rtn Session
var rtnErr error
sc, err := m.cookies.Read(r, name)
if err == nil && sc != "" {
ses, err := m.deserializeSession(sc)
if err == nil {
rtn = ses
} else {
rtnErr = err
}
} else {
rtnErr = errors.New("Could not read session cookie named " + name)
}
return rtn, rtnErr
}
func (m *Manager) serializeSession(s Session) (string, error) {
var rtn string
var rtnErr error
var esus = false
if s != nil {
cs := s.(*CookieSession)
b := new(bytes.Buffer)
encoder := gob.NewEncoder(b)
err := encoder.Encode(cs.id)
if err == nil {
err := encoder.Encode(cs.maxAge)
if err == nil {
err := encoder.Encode(cs.domain)
if err == nil {
err := encoder.Encode(cs.name)
if err == nil {
err := encoder.Encode(cs.path)
if err == nil {
err := encoder.Encode(cs.values)
if err == nil {
esus = true
rtn = base64.StdEncoding.EncodeToString(b.Bytes())
}
}
}
}
}
}
}
if !esus {
rtnErr = errors.New("Failure: There was a problem in serializing the secure session")
}
return rtn, rtnErr
}
func (m *Manager) deserializeSession(ss string) (Session, error) {
var rtn Session
var rtnErr error
var cses CookieSession
var esus = false
bs, err := base64.StdEncoding.DecodeString(ss)
if err == nil {
b := bytes.NewBuffer(bs)
decoder := gob.NewDecoder(b)
err := decoder.Decode(&cses.id)
if err == nil {
err := decoder.Decode(&cses.maxAge)
if err == nil {
err := decoder.Decode(&cses.domain)
if err == nil {
err := decoder.Decode(&cses.name)
if err == nil {
err := decoder.Decode(&cses.path)
if err == nil {
err := decoder.Decode(&cses.values)
if err == nil {
//cookies, err := cok.NewCookies(secretKey)
//cses.cookies = m.cookies
cses.manager = m
esus = true
rtn = &cses
}
}
}
}
}
}
}
if !esus {
rtnErr = errors.New("Failure: There was a problem in deserializing the secure session")
}
return rtn, rtnErr
}