-
Notifications
You must be signed in to change notification settings - Fork 2
/
sesh.go
202 lines (169 loc) · 4.63 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
package sesh
import (
"encoding/gob"
"fmt"
"net/http"
"time"
"github.com/gorilla/sessions"
)
type Store = sessions.Store
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))
}
// 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 (
cookieSignIn = "params"
cookieSession = "session"
cookiePay = "pay"
)
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{})
}
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 OneLogin(store sessions.Store, r *http.Request) (*OneLoginSession, error) {
params, err := store.Get(r, cookieSignIn)
if err != nil {
return nil, err
}
session, ok := params.Values["one-login"]
if !ok {
return nil, MissingSessionError("one-login")
}
oneLoginSession, ok := session.(*OneLoginSession)
if !ok {
return nil, MissingSessionError("one-login")
}
if !oneLoginSession.Valid() {
return nil, InvalidSessionError("one-login")
}
return oneLoginSession, nil
}
func SetOneLogin(store sessions.Store, r *http.Request, w http.ResponseWriter, oneLoginSession *OneLoginSession) error {
params := sessions.NewSession(store, cookieSignIn)
params.Values = map[any]any{"one-login": oneLoginSession}
params.Options = oneLoginCookieOptions
return store.Save(r, w, params)
}
type LoginSession struct {
IDToken string
Sub string
Email string
}
func (s LoginSession) Valid() bool {
return s.Sub != ""
}
func Login(store sessions.Store, r *http.Request) (*LoginSession, error) {
params, err := store.Get(r, cookieSession)
if err != nil {
return nil, err
}
session, ok := params.Values["session"]
if !ok {
return nil, MissingSessionError("session")
}
loginSession, ok := session.(*LoginSession)
if !ok {
return nil, MissingSessionError("session")
}
if !loginSession.Valid() {
return nil, InvalidSessionError("session")
}
return loginSession, nil
}
func SetLoginSession(store sessions.Store, r *http.Request, w http.ResponseWriter, donorSession *LoginSession) error {
session := sessions.NewSession(store, cookieSession)
session.Values = map[any]any{"session": donorSession}
session.Options = sessionCookieOptions
return store.Save(r, w, session)
}
func ClearLoginSession(store Store, r *http.Request, w http.ResponseWriter) error {
session, err := store.Get(r, cookieSession)
if err != nil {
return err
}
session.Values = map[any]any{}
session.Options.MaxAge = -1
return store.Save(r, w, session)
}
type PaymentSession struct {
PaymentID string
}
func (s PaymentSession) Valid() bool {
return true
}
func Payment(store sessions.Store, r *http.Request) (*PaymentSession, error) {
params, err := store.Get(r, cookiePay)
if err != nil {
return nil, err
}
session, ok := params.Values["payment"]
if !ok {
return nil, MissingSessionError("payment")
}
paymentSession, ok := session.(*PaymentSession)
if !ok {
return nil, MissingSessionError("payment")
}
if !paymentSession.Valid() {
return nil, InvalidSessionError("payment")
}
return paymentSession, nil
}
func SetPayment(store sessions.Store, r *http.Request, w http.ResponseWriter, paymentSession *PaymentSession) error {
session := sessions.NewSession(store, cookiePay)
session.Values = map[any]any{"payment": paymentSession}
session.Options = paymentCookieOptions
return store.Save(r, w, session)
}
func ClearPayment(store Store, r *http.Request, w http.ResponseWriter) error {
session, err := store.Get(r, cookiePay)
if err != nil {
return err
}
session.Values = map[any]any{}
session.Options.MaxAge = -1
return store.Save(r, w, session)
}