forked from cloudfoundry/stratos
/
session.go
121 lines (93 loc) · 2.95 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
package main
import (
"fmt"
"time"
log "github.com/Sirupsen/logrus"
"github.com/gorilla/sessions"
"github.com/labstack/echo"
"github.com/labstack/echo/engine/standard"
)
const (
portalSessionName = "console-session"
)
// SessionValueNotFound - Error returned when a requested key was not found in the session
type SessionValueNotFound struct {
msg string
}
func (e *SessionValueNotFound) Error() string {
return fmt.Sprintf("Session value not found %s", e.msg)
}
func (p *portalProxy) GetSession(c echo.Context) (*sessions.Session, error) {
log.Debug("getSession")
req := c.Request().(*standard.Request).Request
return p.SessionStore.Get(req, portalSessionName)
}
func (p *portalProxy) GetSessionValue(c echo.Context, key string) (interface{}, error) {
log.Debug("getSessionValue")
session, err := p.GetSession(c)
if err != nil {
return nil, err
}
// transfering this session value to echo.Context to keep our API surface
// low inside our handlers. This allows us to rip out gorilla handlers later
if intf, ok := session.Values[key]; ok {
return intf, nil
}
return nil, &SessionValueNotFound{key}
}
func (p *portalProxy) GetSessionInt64Value(c echo.Context, key string) (int64, error) {
log.Debug("GetSessionInt64Value")
intf, err := p.GetSessionValue(c, key)
if err != nil {
return 0, err
}
return intf.(int64), nil
}
func (p *portalProxy) GetSessionStringValue(c echo.Context, key string) (string, error) {
log.Debug("GetSessionStringValue")
intf, err := p.GetSessionValue(c, key)
if err != nil {
return "", err
}
return intf.(string), nil
}
func (p *portalProxy) SaveSession(c echo.Context, session *sessions.Session) error {
req := c.Request().(*standard.Request).Request
res := c.Response().(*standard.Response).ResponseWriter
expiresOn := time.Now().Add(time.Second * time.Duration(session.Options.MaxAge))
session.Values["expires_on"] = expiresOn
return p.SessionStore.Save(req, res, session)
}
func (p *portalProxy) setSessionValues(c echo.Context, values map[string]interface{}) error {
log.Debug("setSessionValues")
req := c.Request().(*standard.Request).Request
session, err := p.SessionStore.Get(req, portalSessionName)
if err != nil {
return err
}
for k, v := range values {
session.Values[k] = v
}
return p.SaveSession(c, session)
}
func (p *portalProxy) unsetSessionValue(c echo.Context, sessionKey string) error {
log.Debug("unsetSessionValues")
req := c.Request().(*standard.Request).Request
session, err := p.SessionStore.Get(req, portalSessionName)
if err != nil {
return err
}
delete(session.Values, sessionKey)
return p.SaveSession(c, session)
}
func (p *portalProxy) clearSession(c echo.Context) error {
log.Debug("clearSession")
req := c.Request().(*standard.Request).Request
res := c.Response().(*standard.Response).ResponseWriter
session, err := p.SessionStore.Get(req, portalSessionName)
if err != nil {
return err
}
session.Options.MaxAge = -1
return p.SessionStore.Save(req, res, session)
}