This repository has been archived by the owner on Jul 12, 2023. It is now read-only.
/
session.go
312 lines (265 loc) · 8.31 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
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
// Copyright 2020 the Exposure Notifications Verification Server authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package controller
import (
"time"
"github.com/google/exposure-notifications-verification-server/pkg/database"
"github.com/gorilla/sessions"
)
// sessionKey is a unique type to avoid overwriting over values in the session
// map. It also makes it much more difficult to modify values in the session map
// unintentionally.
type sessionKey string
const (
emailVerificationPrompted = sessionKey("emailVerificationPrompted")
mfaPrompted = sessionKey("mfaPrompted")
passwordExpireWarned = sessionKey("passwordExpireWarned")
sessionKeyCSRFToken = sessionKey("csrfToken")
sessionKeyLastActivity = sessionKey("lastActivity")
sessionKeyRealmID = sessionKey("realmID")
sessionKeyWelcomeMessageDisplayed = sessionKey("welcomeMessageDisplayed")
nonceKey = sessionKey("nonce")
regionKey = sessionKey("region")
)
// StoreSessionNonce stores this sessions current nonce value for
// processing.
func StoreSessionNonce(session *sessions.Session, nonce string) {
if session == nil {
return
}
session.Values[nonceKey] = nonce
}
// NonceFromSession returns the current nonce from the session or
// empty string if there isn't one, or the value is malformed.
func NonceFromSession(session *sessions.Session) string {
v := sessionGet(session, nonceKey)
if v == nil {
return ""
}
strVal, ok := v.(string)
if !ok {
return ""
}
return strVal
}
// ClearNonceFromSession removes the nonce key from the current session.
func ClearNonceFromSession(session *sessions.Session) {
sessionClear(session, nonceKey)
}
// StoreSessionRegion stores the current operating region in the session.
func StoreSessionRegion(session *sessions.Session, region string) {
if session == nil {
return
}
session.Values[regionKey] = region
}
// RegionFromSession returns the current region from the session or
// empty string if there isn't one, or the value is malformed.
func RegionFromSession(session *sessions.Session) string {
v := sessionGet(session, regionKey)
if v == nil {
return ""
}
strVal, ok := v.(string)
if !ok {
return ""
}
return strVal
}
// StoreSessionCSRFToken stores the CSRF token on the session.
func StoreSessionCSRFToken(session *sessions.Session, token []byte) {
if session == nil || len(token) == 0 {
return
}
session.Values[sessionKeyCSRFToken] = token
}
// ClearSessionCSRFToken clears the CSRF token from the session.
func ClearSessionCSRFToken(session *sessions.Session) {
sessionClear(session, sessionKeyCSRFToken)
}
// CSRFTokenFromSession extracts the CSRF token from the session.
func CSRFTokenFromSession(session *sessions.Session) []byte {
v := sessionGet(session, sessionKeyCSRFToken)
if v == nil {
return nil
}
t, ok := v.([]byte)
if !ok {
delete(session.Values, sessionKeyCSRFToken)
return nil
}
return t
}
// StoreSessionRealm stores the realm's ID in the session.
func StoreSessionRealm(session *sessions.Session, realm *database.Realm) {
if session == nil || realm == nil {
return
}
ClearWelcomeMessageDisplayed(session)
session.Values[sessionKeyRealmID] = realm.ID
}
// ClearSessionRealm clears the realm from the session.
func ClearSessionRealm(session *sessions.Session) {
sessionClear(session, sessionKeyRealmID)
}
// RealmIDFromSession extracts the realm from the session.
func RealmIDFromSession(session *sessions.Session) uint {
v := sessionGet(session, sessionKeyRealmID)
if v == nil {
return 0
}
t, ok := v.(uint)
if !ok {
delete(session.Values, sessionKeyRealmID)
return 0
}
return t
}
// StoreSessionMFAPrompted stores if the user was prompted for MFA.
func StoreSessionMFAPrompted(session *sessions.Session, prompted bool) {
if session == nil {
return
}
session.Values[mfaPrompted] = prompted
}
// ClearMFAPrompted clears the MFA prompt bit.
func ClearMFAPrompted(session *sessions.Session) {
sessionClear(session, mfaPrompted)
}
// MFAPromptedFromSession extracts if the user was prompted for MFA.
func MFAPromptedFromSession(session *sessions.Session) bool {
v := sessionGet(session, mfaPrompted)
if v == nil {
return false
}
f, ok := v.(bool)
if !ok {
delete(session.Values, mfaPrompted)
return false
}
return f
}
// StoreSessionLastActivity stores the last time the user did something. This is
// used to track idle session timeouts.
func StoreSessionLastActivity(session *sessions.Session, t time.Time) {
if session == nil {
return
}
session.Values[sessionKeyLastActivity] = t.Unix()
}
// ClearLastActivity clears the session last activity time.
func ClearLastActivity(session *sessions.Session) {
sessionClear(session, sessionKeyLastActivity)
}
// LastActivityFromSession extracts the last time the user did something.
func LastActivityFromSession(session *sessions.Session) time.Time {
v := sessionGet(session, sessionKeyLastActivity)
if v == nil {
return time.Time{}
}
i, ok := v.(int64)
if !ok || i == 0 {
delete(session.Values, sessionKeyLastActivity)
return time.Time{}
}
return time.Unix(i, 0)
}
// StoreSessionEmailVerificationPrompted stores if the user was prompted for email verification.
func StoreSessionEmailVerificationPrompted(session *sessions.Session, prompted bool) {
if session == nil {
return
}
session.Values[emailVerificationPrompted] = prompted
}
// ClearEmailVerificationPrompted clears the MFA prompt bit.
func ClearEmailVerificationPrompted(session *sessions.Session) {
sessionClear(session, emailVerificationPrompted)
}
// EmailVerificationPromptedFromSession extracts if the user was prompted for email verification.
func EmailVerificationPromptedFromSession(session *sessions.Session) bool {
v := sessionGet(session, emailVerificationPrompted)
if v == nil {
return false
}
f, ok := v.(bool)
if !ok {
delete(session.Values, emailVerificationPrompted)
return false
}
return f
}
// StoreSessionWelcomeMessageDisplayed stores if the user was displayed the
// realm welcome message.
func StoreSessionWelcomeMessageDisplayed(session *sessions.Session, prompted bool) {
if session == nil {
return
}
session.Values[sessionKeyWelcomeMessageDisplayed] = prompted
}
// ClearWelcomeMessageDisplayed clears the welcome message prompt bit.
func ClearWelcomeMessageDisplayed(session *sessions.Session) {
sessionClear(session, sessionKeyWelcomeMessageDisplayed)
}
// WelcomeMessageDisplayedFromSession extracts if the user was displayed the
// realm welcome message.
func WelcomeMessageDisplayedFromSession(session *sessions.Session) bool {
v := sessionGet(session, sessionKeyWelcomeMessageDisplayed)
if v == nil {
return false
}
f, ok := v.(bool)
if !ok {
delete(session.Values, sessionKeyWelcomeMessageDisplayed)
return false
}
return f
}
// StorePasswordExpireWarned stores if the user was displayed the
// realm welcome message.
func StorePasswordExpireWarned(session *sessions.Session, prompted bool) {
if session == nil {
return
}
session.Values[passwordExpireWarned] = prompted
}
// ClearPasswordExpireWarned clears the welcome message prompt bit.
func ClearPasswordExpireWarned(session *sessions.Session) {
sessionClear(session, passwordExpireWarned)
}
// PasswordExpireWarnedFromSession extracts if the user was displayed the
// realm welcome message.
func PasswordExpireWarnedFromSession(session *sessions.Session) bool {
v := sessionGet(session, passwordExpireWarned)
if v == nil {
return false
}
f, ok := v.(bool)
if !ok {
delete(session.Values, passwordExpireWarned)
return false
}
return f
}
func sessionGet(session *sessions.Session, key sessionKey) interface{} {
if session == nil || session.Values == nil {
return nil
}
return session.Values[key]
}
func sessionClear(session *sessions.Session, key sessionKey) {
if session == nil {
return
}
delete(session.Values, key)
}