-
Notifications
You must be signed in to change notification settings - Fork 2
/
sesh.go
226 lines (184 loc) · 5.65 KB
/
sesh.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package sesh
import (
"encoding/base64"
"encoding/gob"
"fmt"
"net/http"
"time"
"github.com/gorilla/sessions"
)
// These are the cookie names in use. We need some to be able to overlap
// (e.g. session+pay, so you can be signed in and pay for something), but others
// shouldn't (i.e. the reuse of session as you can't be signed in twice).
const (
cookieCsrf = "csrf"
cookieLPAData = "lpa-data"
cookiePayment = "pay"
cookieSession = "session"
cookieSignIn = "params"
)
var (
sessionCookieOptions = &sessions.Options{
Path: "/",
MaxAge: 24 * 60 * 60,
SameSite: http.SameSiteLaxMode,
HttpOnly: true,
Secure: true,
}
oneLoginCookieOptions = &sessions.Options{
Path: "/",
MaxAge: 60 * 60,
SameSite: http.SameSiteLaxMode,
HttpOnly: true,
Secure: true,
}
paymentCookieOptions = &sessions.Options{
Path: "/",
// A payment can be resumed up to 90 minutes after creation
MaxAge: int(time.Minute * 90 / time.Second),
SameSite: http.SameSiteLaxMode,
HttpOnly: true,
Secure: true,
}
)
func init() {
gob.Register(&OneLoginSession{})
gob.Register(&LoginSession{})
gob.Register(&PaymentSession{})
gob.Register(&CsrfSession{})
gob.Register(&LpaDataSession{})
}
type cookieStore interface {
Get(r *http.Request, name string) (*sessions.Session, error)
New(r *http.Request, name string) (*sessions.Session, error)
Save(r *http.Request, w http.ResponseWriter, s *sessions.Session) error
}
type MissingSessionError string
func (e MissingSessionError) Error() string {
return fmt.Sprintf("missing %s session", string(e))
}
type InvalidSessionError string
func (e InvalidSessionError) Error() string {
return fmt.Sprintf("%s session invalid", string(e))
}
type Store struct {
s sessions.Store
}
func NewStore(keyPairs [][]byte) *Store {
return &Store{s: sessions.NewCookieStore(keyPairs...)}
}
type isValid interface {
Valid() bool
}
func getSession[T isValid](s sessions.Store, cookieName, paramKey string, r *http.Request) (T, error) {
params, err := s.Get(r, cookieName)
if err != nil {
return *new(T), err
}
value, ok := params.Values[paramKey]
if !ok {
return *new(T), MissingSessionError(paramKey)
}
session, ok := value.(T)
if !ok {
return *new(T), MissingSessionError(paramKey)
}
if !session.Valid() {
return *new(T), InvalidSessionError(paramKey)
}
return session, nil
}
func setSession[T any](s sessions.Store, cookieName, paramKey string, r *http.Request, w http.ResponseWriter, values T, options *sessions.Options) error {
session := sessions.NewSession(s, cookieName)
session.Values = map[any]any{paramKey: values}
session.Options = options
return s.Save(r, w, session)
}
func clearSession(s sessions.Store, cookieName string, r *http.Request, w http.ResponseWriter) error {
session, err := s.Get(r, cookieName)
if err != nil {
return err
}
session.Values = map[any]any{}
session.Options.MaxAge = -1
return s.Save(r, w, session)
}
type OneLoginSession struct {
State string
Nonce string
Locale string
Redirect string
SessionID string
LpaID string
}
func (s OneLoginSession) Valid() bool {
return s.State != "" && s.Nonce != "" && s.Redirect != ""
}
func (s *Store) OneLogin(r *http.Request) (*OneLoginSession, error) {
return getSession[*OneLoginSession](s.s, cookieSignIn, "one-login", r)
}
func (s *Store) SetOneLogin(r *http.Request, w http.ResponseWriter, oneLoginSession *OneLoginSession) error {
return setSession(s.s, cookieSignIn, "one-login", r, w, oneLoginSession, oneLoginCookieOptions)
}
type LoginSession struct {
IDToken string
Sub string
Email string
OrganisationID string
OrganisationName string
}
func (s LoginSession) SessionID() string {
return base64.StdEncoding.EncodeToString([]byte(s.Sub))
}
func (s LoginSession) Valid() bool {
return s.Sub != ""
}
func (s *Store) Login(r *http.Request) (*LoginSession, error) {
return getSession[*LoginSession](s.s, cookieSession, "session", r)
}
func (s *Store) SetLogin(r *http.Request, w http.ResponseWriter, donorSession *LoginSession) error {
return setSession(s.s, cookieSession, "session", r, w, donorSession, sessionCookieOptions)
}
func (s *Store) ClearLogin(r *http.Request, w http.ResponseWriter) error {
return clearSession(s.s, cookieSession, r, w)
}
type PaymentSession struct {
PaymentID string
}
func (s PaymentSession) Valid() bool {
return true
}
func (s *Store) Payment(r *http.Request) (*PaymentSession, error) {
return getSession[*PaymentSession](s.s, cookiePayment, "payment", r)
}
func (s *Store) SetPayment(r *http.Request, w http.ResponseWriter, paymentSession *PaymentSession) error {
return setSession(s.s, cookiePayment, "payment", r, w, paymentSession, paymentCookieOptions)
}
func (s *Store) ClearPayment(r *http.Request, w http.ResponseWriter) error {
return clearSession(s.s, cookiePayment, r, w)
}
type CsrfSession struct {
IsNew bool
Token string
}
func (s CsrfSession) Valid() bool {
return true
}
func (s *Store) Csrf(r *http.Request) (*CsrfSession, error) {
return getSession[*CsrfSession](s.s, cookieCsrf, "csrf", r)
}
func (s *Store) SetCsrf(r *http.Request, w http.ResponseWriter, csrfSession *CsrfSession) error {
return setSession(s.s, cookieCsrf, "csrf", r, w, csrfSession, sessionCookieOptions)
}
type LpaDataSession struct {
LpaID string
}
func (s LpaDataSession) Valid() bool {
return s.LpaID != ""
}
func (s *Store) LpaData(r *http.Request) (*LpaDataSession, error) {
return getSession[*LpaDataSession](s.s, cookieLPAData, "lpa-data", r)
}
func (s *Store) SetLpaData(r *http.Request, w http.ResponseWriter, lpaDataSession *LpaDataSession) error {
return setSession(s.s, cookieLPAData, "lpa-data", r, w, lpaDataSession, sessionCookieOptions)
}