-
Notifications
You must be signed in to change notification settings - Fork 0
/
cookie.go
70 lines (63 loc) · 1.89 KB
/
cookie.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
package web
import (
"encoding/json"
"github.com/gorilla/sessions"
"github.com/labstack/echo-contrib/session"
"github.com/labstack/echo/v4"
"github.com/vshn/odootools/pkg/odoo"
"github.com/vshn/odootools/pkg/web/controller"
)
const (
// DataCookieID is the cookie identifier for storing additional data.
DataCookieID = "odootools-data"
)
// GetOdooSession returns the Odoo session from the session cookie.
// Returns nil if there is no active session.
func (s *Server) GetOdooSession(e echo.Context) *odoo.Session {
sess, _ := session.Get(SessionCookieID, e)
odooId := sess.Values["odoo_id"]
odooUid := sess.Values["odoo_uid"]
if odooId == nil && odooUid == nil {
return nil
}
odooSess := odoo.RestoreSession(s.odooClient, odooId.(string), odooUid.(int))
return odooSess
}
func (s *Server) GetSessionData(e echo.Context) controller.SessionData {
sess, _ := session.Get(DataCookieID, e)
data := controller.SessionData{}
if raw, found := sess.Values["data"]; found {
err := json.Unmarshal([]byte(raw.(string)), &data)
if err != nil {
e.Logger().Errorf("no session data found: %v", err)
}
}
return data
}
func (s *Server) SaveOdooSession(e echo.Context, odooSession *odoo.Session) error {
sess := sessions.NewSession(s.cookieStore, SessionCookieID)
sess.Options = &sessions.Options{
Path: "/",
MaxAge: 86400 * 7,
HttpOnly: true,
Secure: true,
}
sess.Values["odoo_id"] = odooSession.SessionID
sess.Values["odoo_uid"] = odooSession.UID
return sess.Save(e.Request(), e.Response())
}
func (s *Server) SaveSessionData(e echo.Context, data controller.SessionData) error {
sess := sessions.NewSession(s.cookieStore, DataCookieID)
sess.Options = &sessions.Options{
Path: "/",
MaxAge: 86400 * 7,
HttpOnly: true,
Secure: true,
}
b, err := json.Marshal(data)
if err != nil {
return err
}
sess.Values["data"] = string(b)
return sess.Save(e.Request(), e.Response())
}