-
Notifications
You must be signed in to change notification settings - Fork 11
/
session.go
169 lines (147 loc) · 3.32 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
// Session implements middleware for easily using github.com/gorilla/sessions
// within echo. This package was originally inspired from the
// https://github.com/ipfans/echo-session package, and modified to provide more
// functionality
package engine
import (
"errors"
"fmt"
"log"
"github.com/admpub/sessions"
"github.com/webx-top/com"
"github.com/webx-top/echo"
)
const (
errorFormat = "[sessions] ERROR! %s\n"
)
var ErrInvalidSessionID = errors.New("invalid session ID")
type Session struct {
name string
context echo.Context
store sessions.Store
session *sessions.Session
written bool
preSave []func(echo.Context) error
}
func (s *Session) AddPreSaveHook(hook func(echo.Context) error) {
s.preSave = append(s.preSave, hook)
}
func (s *Session) SetPreSaveHook(hooks ...func(echo.Context) error) {
s.preSave = hooks
}
func (s *Session) Get(key string) interface{} {
return s.Session().Values[key]
}
func (s *Session) Set(key string, val interface{}) echo.Sessioner {
s.Session().Values[key] = val
s.setWritten()
return s
}
func (s *Session) Delete(key string) echo.Sessioner {
delete(s.Session().Values, key)
s.setWritten()
return s
}
func (s *Session) Clear() echo.Sessioner {
for key := range s.Session().Values {
if k, ok := key.(string); ok {
s.Delete(k)
}
}
s.setWritten()
return s
}
func (s *Session) AddFlash(value interface{}, vars ...string) echo.Sessioner {
s.Session().AddFlash(value, vars...)
s.setWritten()
return s
}
func (s *Session) Flashes(vars ...string) []interface{} {
flashes := s.Session().Flashes(vars...)
if len(flashes) > 0 {
s.setWritten()
}
return flashes
}
func (s *Session) SetID(id string, notReload ...bool) error {
if s.Session().ID == id {
return nil
}
if !com.StrIsAlphaNumeric(id) {
return ErrInvalidSessionID
}
s.Session().ID = id
if len(notReload) == 0 || !notReload[0] {
if err := s.Session().Reload(s.context); err != nil {
return err
}
}
s.setWritten()
return nil
}
func (s *Session) ID() string {
return s.Session().ID
}
func (s *Session) MustID() string {
if len(s.Session().ID) > 0 {
return s.Session().ID
}
if idGen, ok := s.Session().Store().(sessions.IDGenerator); ok {
var err error
s.Session().ID, err = idGen.GenerateID(s.context, s.Session())
if err != nil {
err = fmt.Errorf(`Session ID generation failed: %w`, err)
panic(err)
}
s.setWritten()
return s.Session().ID
}
s.Session().ID = GenerateSessionID()
s.setWritten()
return s.Session().ID
}
func (s *Session) RemoveID(sessionID string) error {
if !com.StrIsAlphaNumeric(sessionID) {
return ErrInvalidSessionID
}
return s.store.Remove(sessionID)
}
func (s *Session) Save() error {
if !s.Written() {
return nil
}
for _, hook := range s.preSave {
if err := hook(s.context); err != nil {
return err
}
}
err := s.Session().Save(s.context)
if err == nil {
s.written = false
} else {
log.Printf(errorFormat, err)
}
return err
}
func (s *Session) Session() *sessions.Session {
if s.session == nil {
var err error
s.session, err = s.store.Get(s.context, s.name)
if err != nil {
if s.session == nil {
panic(fmt.Sprintf(errorFormat, err))
}
log.Printf(errorFormat, err)
}
}
return s.session
}
func (s *Session) Written() bool {
return s.written
}
func (s *Session) setWritten() *Session {
if !s.written {
s.written = true
}
return s
}