-
Notifications
You must be signed in to change notification settings - Fork 177
/
dao.go
107 lines (95 loc) · 2.61 KB
/
dao.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
package sessions
import (
"context"
"net/http"
"net/url"
"sync"
"github.com/gorilla/sessions"
"github.com/pydio/cells/v4/common/config"
"github.com/pydio/cells/v4/common/dao"
"github.com/pydio/cells/v4/common/log"
"github.com/pydio/cells/v4/common/service/frontend/sessions/securecookie"
"github.com/pydio/cells/v4/common/service/frontend/sessions/sqlsessions"
"github.com/pydio/cells/v4/common/service/frontend/sessions/utils"
"github.com/pydio/cells/v4/common/sql"
"github.com/pydio/cells/v4/common/utils/configx"
)
func NewDAO(ctx context.Context, o dao.DAO) (dao.DAO, error) {
timeout := config.Get("frontend", "plugin", "gui.ajax", "SESSION_TIMEOUT").Default(60).Int()
defaultOptions := &sessions.Options{
Path: "/a/frontend",
MaxAge: 60 * timeout,
HttpOnly: true,
}
switch v := o.(type) {
case securecookie.DAO:
ci := &cookiesImpl{}
ci.DAO = v
ci.storeFactory = func(u *url.URL, keyPairs ...[]byte) (sessions.Store, error) {
cs := sessions.NewCookieStore(keyPairs...)
cs.Options = &sessions.Options{
Path: defaultOptions.Path,
MaxAge: defaultOptions.MaxAge,
HttpOnly: defaultOptions.HttpOnly,
}
if u.Scheme == "https" {
cs.Options.Secure = true
}
cs.Options.Domain = u.Hostname()
return cs, nil
}
return ci, nil
case sql.DAO:
return &sqlsessions.Impl{
DAO: v,
Options: defaultOptions,
}, nil
default:
return nil, dao.UnsupportedDriver(o)
}
}
type DAO interface {
dao.DAO
GetSession(r *http.Request) (*sessions.Session, error)
DeleteExpired(ctx context.Context, logger log.ZapLogger)
}
type cookiesImpl struct {
dao.DAO
sync.Mutex
secureKeyPairs []byte
sessionStores map[string]sessions.Store
storeFactory func(u *url.URL, keyPairs ...[]byte) (sessions.Store, error)
}
func (s *cookiesImpl) Init(ctx context.Context, values configx.Values) error {
s.sessionStores = make(map[string]sessions.Store)
if k, e := utils.LoadKey(); e != nil {
return e
} else {
s.secureKeyPairs = k
return s.DAO.Init(ctx, values)
}
}
func (s *cookiesImpl) GetSession(r *http.Request) (*sessions.Session, error) {
store, er := s.storeForUrl(utils.RequestURL(r))
if er != nil {
return nil, er
}
return store.Get(r, utils.SessionName(r))
}
func (s *cookiesImpl) DeleteExpired(ctx context.Context, logger log.ZapLogger) {
return
}
func (s *cookiesImpl) storeForUrl(u *url.URL) (sessions.Store, error) {
key := u.Scheme + "://" + u.Hostname()
s.Lock()
defer s.Unlock()
if ss, o := s.sessionStores[key]; o {
return ss, nil
}
ss, e := s.storeFactory(u, s.secureKeyPairs)
if e != nil {
return nil, e
}
s.sessionStores[key] = ss
return ss, nil
}