-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsecurity.go
354 lines (288 loc) · 8.73 KB
/
security.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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
// gozilla.go
package main
import (
"bytes"
"crypto/sha256"
"encoding/binary"
"github.com/gorilla/securecookie"
"net/http"
"net/url"
"strconv"
"time"
)
type int256 [4]int64
type UserData struct {
Email string
Username string
Name string
Admin bool
}
var (
cookieCypher *securecookie.SecureCookie
)
const (
kUserId = "UserId"
)
func packInt256(buffer []byte) (q int256) {
err := binary.Read(bytes.NewBuffer(buffer[:]), binary.LittleEndian, &q)
check(err)
return
}
// Hashes password with salt into [4]int64 and returns result.
func GetPasswordHash256(password string) int256 {
passwordHash := sha256.Sum256([]byte(password + flags.dbSalt))
return packInt256(passwordHash[:])
}
// Sets a secure browser cookie.
func setCookie(w http.ResponseWriter, r *http.Request, name string, value string, expiration time.Time, encrypt bool) {
if encrypt {
encrypted, err := cookieCypher.Encode(name, value)
value = encrypted
check(err)
}
cookie := http.Cookie {
Name : name,
Value : value,
Expires : expiration,
Domain : r.URL.Host,
Secure : false, // TODO: set cookie.Secure to true once SSL is enabled.
HttpOnly: true, // Prevents XSFR attacks, but does not allow server-side cookies to be read on the client side via JS.
Path : "/",
SameSite: http.SameSiteLaxMode,
}
prVal("setCookie", cookie)
prVal(" expiration", expiration.Format(time.UnixDate))
http.SetCookie(w, &cookie)
}
// Gets a secure cookie value.
func getCookie(r *http.Request, name string, encrypted bool) (string, error) {
//prVal("getCookie ", name)
cookie, err := r.Cookie(name)
//prVal(" r.Cookie", cookie)
if err != nil { // likely ErrNoCookie
return "", err
}
if encrypted {
var decoded string
err := cookieCypher.Decode(name, cookie.Value, &decoded)
if err != nil {
return "", err
}
return decoded, err
} else {
return cookie.Value, nil
}
}
// Sets a cookie the easy way.
func SetCookie(w http.ResponseWriter, r *http.Request, name string, value string) {
setCookie(w, r, name, value, longExpiration(), false)
}
// Gets a cookie the easy way.
func GetCookie(r *http.Request, name string, defaultValue string) string {
value, err := getCookie(r, name, false)
if err != nil {
return defaultValue // likely ErrNoCookie
} else {
return value
}
}
func GetAndDecodeCookie(r *http.Request, name string, defaultValue string) string {
cookieVal := GetCookie(r, name, defaultValue)
prVal("encoded cookieVal", cookieVal)
// decode return address
decodedCookieVal, err := url.QueryUnescape(cookieVal)
check(err)
prVal("decoded cookieVal", cookieVal)
return decodedCookieVal
}
// Refreshes a cookie by potentially extending its expiration.
func refreshCookie(w http.ResponseWriter, r *http.Request, name string, expiration time.Time) {
/* prVal("RefreshCookie", name)
cookieVal, err := getCookie(r, name, false)
prVal(" cookieVal", cookieVal)
prVal(" err", err)
if err != nil { // likely ErrNoCookie
pr(" No cookie found to refresh")
setCookie(w, r, name, cookieVal, expiration, false)
}
prVal(" Setting expiration to", expiration.Format(time.UnixDate))
cookie, err := r.Cookie(name)
if err != nil { // likely ErrNoCookie
return
}
cookie.Expires = expiration
http.SetCookie(w, cookie)
*/
}
func longExpiration() time.Time { return time.Now().Add(365 * 24 * time.Hour) }
func shortExpiration() time.Time { return time.Now().Add(10 * time.Minute) }
// Creates a secure session for Username.
// If RememberMe, remember cookie for one year. Otherwise, terminate cookie when browser closes.
func CreateSession(w http.ResponseWriter, r *http.Request, userId int64) {
setCookie(w, r, kUserId, strconv.FormatInt(userId, 10), longExpiration(), true)
}
func DestroySession(w http.ResponseWriter, r *http.Request) {
pr("DestroySession")
setCookie(w, r, kUserId, "", time.Now(), false)
//setCookie(w, r, kRememberMe, "false", time.Now(), false)
pr("Test DestroySession:")
prVal(" getCookie(kUserId)", GetCookie(r, kUserId, "not found"))
}
// Returns the User.Id if the secure cookie exists, ok=false otherwise.
func GetSession(w http.ResponseWriter, r *http.Request) (userId int64) {
pr("GetSession")
// Friction-free login from email if url has ?autoLoginEmail=[eml]
escapedEmail := parseUrlParam(r, "autoLoginEmail")
prVal(" escapedEmail", escapedEmail)
if escapedEmail != "" {
autoLoginEmail, err := url.QueryUnescape(escapedEmail)
check(err)
prVal(" autoLoginEmail", autoLoginEmail)
userId := int64(-1)
rows := DbQuery("SELECT Id FROM $$User WHERE Email = $1", autoLoginEmail)
if rows.Next() {
err := rows.Scan(&userId)
check(err)
}
check(rows.Err())
rows.Close()
if userId >= 0 {
prf(" XXX Auto-Login from email successful! UserId = %d", userId)
CreateSession(w, r, userId)
return userId
}
}
// Get userId from the session cookie.
cookie, err := getCookie(r, kUserId, true)
if err != nil { // Missing or forged cookie
if flags.testUserId != "" {
cookie = flags.testUserId
pr(" userId set to testUserId")
} else {
return -1
}
}
userId, err = strconv.ParseInt(cookie, 10, 64)
if err != nil { // Cannot parse cookie
pr("Cannot parse cookie")
return -1
}
prVal(" userId", userId)
return userId
}
// Get userId, username from the secure cookie.
func GetSessionInfo(w http.ResponseWriter, r *http.Request) (userId int64, username string) {
pr("GetSessionInfo")
userId = GetSession(w, r)
prVal(" userId", userId)
if userId == -1 {
pr(`GetSessionInfo: -1, ""`)
return -1, ""
}
username = ""
rows := DbQuery("SELECT Username FROM $$User WHERE Id = $1::bigint;", userId)
if rows.Next() {
err := rows.Scan(&username)
check(err)
} else { // User was deleted from db; destroy session to maintain consistency.
DestroySession(w, r)
return -1, ""
}
check(rows.Err())
rows.Close()
prf("GetSessionInfo %d, %s", userId, username)
return
}
func GetUserData(userId int64) (userData UserData) {
rows := DbQuery("SELECT Email, Username, COALESCE(Name, ''), Admin FROM $$User WHERE Id = $1::bigint;", userId)
if rows.Next() {
err := rows.Scan(&userData.Email, &userData.Username, &userData.Name, &userData.Admin)
check(err)
}
check(rows.Err())
rows.Close()
prVal("userData", userData)
return userData
}
func InvalidateCache(userId int64) {
DbExec(`UPDATE $$User
SET LastModTime = Now()
WHERE Id = $1::bigint;`,
userId)
}
// Get userId, username, isCacheValid from the secure cookie & database lookup.
func GetSessionInfo2(w http.ResponseWriter, r *http.Request) (userId int64, username string, isCacheValid bool) {
pr("GetSessionInfo2")
userId = GetSession(w, r)
prVal(" userId", userId)
if userId == -1 {
pr(`GetSessionInfo: -1, ""`)
return -1, "", true
}
username = ""
isCacheValid = false
rows := DbQuery(`SELECT Username,
Now() > LastModtime + interval '3 minutes'
FROM $$User
WHERE Id = $1::bigint;`,
userId)
if rows.Next() {
err := rows.Scan(&username, &isCacheValid)
prf(" username=%s isCacheValid=%s", username, bool_to_str(isCacheValid))
check(err)
} else { // User was deleted from db; destroy session to maintain consistency.
pr(" DestroySession")
DestroySession(w, r)
return -1, "", true
}
check(rows.Err())
rows.Close()
prf(" GetSessionInfo2 %d, %s, %s", userId, username, bool_to_str(isCacheValid))
return userId, username, isCacheValid
}
func GetUsername(userId int64) (username string) {
rows := DbQuery("SELECT Username FROM $$User WHERE Id = $1::bigint;", userId)
if rows.Next() {
err := rows.Scan(&username)
check(err)
} else { // User was deleted from db; destroy session to maintain consistency.
panic("userId not found")
return ""
}
check(rows.Err())
rows.Close()
return
}
// Return -1 if not found or error.
func UsernameToUserId(username string) int64 {
prVal("UsernameToUserId... username", username)
rows := DbQuery("SELECT Id FROM $$User WHERE Username = $1;", username)
if rows.Next() {
var userId int64
err := rows.Scan(&userId)
check(err)
return userId
} else { // User was deleted from db; destroy session to maintain consistency.
panic("userId not found")
return -1
}
check(rows.Err())
rows.Close()
return -1
}
func RefreshSession(w http.ResponseWriter, r *http.Request) {
/* pr("RefreshSession")
rememberMeCookie, _ := getCookie(r, kRememberMe, false)
prVal(" rememberMeCookie", rememberMeCookie)
prVal("rememberMeCookie", rememberMeCookie)
if rememberMeCookie == "true" {
pr(" RememberMe = true!")
return // Don't refresh the cookie if rememberMe is set - it lasts for a year.
}
// Refresh by extending the expiration by 10 minutes.
refreshCookie(w, r, kUserId, shortExpiration())*/
}
// Should be called from init()
func InitSecurity() {
cookieCypher = securecookie.New([]byte(flags.secureCookieHashKey), []byte(flags.secureCookieBlockKey))
}