-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutil.go
135 lines (118 loc) · 2.92 KB
/
util.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
package main
import (
cryptorand "crypto/rand"
"errors"
"io/ioutil"
"math/rand"
"net/http"
"strings"
"github.com/gorilla/securecookie"
)
// I was going to call it cookieData, but data about a cookie is just the
// nutrition facts #HowDidIGetAProfessionalCodingJob
type nutritionFacts struct {
MagicRandomness string
AccessToken string
}
func withLogin(handler func(c context)) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
c := newContext(w, r)
// Do some best-effort context-filling
if nutFact, err := loadCookie(r); err == nil {
c.magicToken = nutFact.MagicRandomness
if nutFact.AccessToken != "" {
if p, err := db.loadUser(userID(nutFact.AccessToken)); err == nil {
c.p = p
}
}
} else {
// They don't have a cookie from us yet. Let's fix that
r := genName(128)
val := nutritionFacts{
MagicRandomness: r,
}
if encoded, err := s.Encode("info", val); err == nil {
cookie := &http.Cookie{
Name: "info",
Value: encoded,
Path: "/",
}
http.SetCookie(w, cookie)
c.magicToken = r
}
}
handler(c)
}
}
var letters = []byte("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
func genName(n int) string {
b := make([]byte, n)
r := rand.New(cryptoRandSource{})
for i := range b {
b[i] = letters[r.Intn(len(letters))]
}
return string(b)
}
func initSecretz() (string, error) {
if dat, err := ioutil.ReadFile("secretz"); err == nil {
return strings.TrimSpace(string(dat)), nil
} else {
return "", err
}
}
func initKeys() (*securecookie.SecureCookie, error) {
var hashKey []byte
var blockKey []byte
if dat, err := loadOrGenKey("hashKey"); err != nil {
return nil, err
} else {
hashKey = dat
}
if dat, err := loadOrGenKey("blockKey"); err != nil {
return nil, err
} else {
blockKey = dat
}
return securecookie.New(hashKey, blockKey), nil
}
func loadOrGenKey(name string) ([]byte, error) {
if f, err := ioutil.ReadFile(name); err != nil {
if dat := securecookie.GenerateRandomKey(32); dat != nil {
if err := ioutil.WriteFile(name, dat, 0777); err == nil {
return dat, nil
}
return nil, errors.New("Error writing file")
}
return nil, errors.New("Failed to generate key")
} else {
return f, nil
}
}
func loadCookie(r *http.Request) (nutritionFacts, error) {
if cookie, err := r.Cookie("info"); err == nil {
value := nutritionFacts{}
if err = s.Decode("info", cookie.Value, &value); err != nil {
return nutritionFacts{}, err
}
return value, nil
} else {
return nutritionFacts{}, err
}
}
type cryptoRandSource struct{}
func (cryptoRandSource) Int63() int64 {
var buf [8]byte
_, err := cryptorand.Read(buf[:])
if err != nil {
panic(err)
}
return int64(buf[0]) |
int64(buf[1])<<8 |
int64(buf[2])<<16 |
int64(buf[3])<<24 |
int64(buf[4])<<32 |
int64(buf[5])<<40 |
int64(buf[6])<<48 |
int64(buf[7]&0x7f)<<56
}
func (cryptoRandSource) Seed(int64) {}