forked from dcposch/scramble
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandlers.go
247 lines (214 loc) · 6.2 KB
/
handlers.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
package main
import (
"encoding/json"
"io/ioutil"
"log"
"net/http"
"strings"
"time"
)
//
// SERVE HTML, CSS, JS
//
func staticHandler(w http.ResponseWriter, r *http.Request) {
var path string
if strings.HasSuffix(r.URL.Path, "/") {
path = r.URL.Path + "index.html"
} else {
path = r.URL.Path
}
http.ServeFile(w, r, "static/"+path)
}
//
// USER ROUTE
//
func userHandler(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
publicKeyHandler(w, r)
} else if r.Method == "POST" {
createHandler(w, r)
}
}
// GET /user/<public key hash> for public key lookup
// The server is untrusted, so the client will verify in Javascript
// that the public key we send here matches the hash they requested
func publicKeyHandler(w http.ResponseWriter, r *http.Request) {
userPubHash := validateHash(r.URL.Path[len("/user/"):])
userPub := LoadPubKey(userPubHash)
if userPub == "" {
http.Error(w, "Not found", http.StatusNotFound)
} else {
w.Write([]byte(userPub))
}
}
// POST /user to create a new account
// Remember that public and private key generation happens
// on the client. Public key, encrypted private key posted here.
func createHandler(w http.ResponseWriter, r *http.Request) {
user := new(User)
user.Token = validateToken(r.FormValue("token"))
user.PasswordHash = validatePassHash(r.FormValue("passHash"))
user.PublicKey = validatePublicKey(r.FormValue("publicKey"))
user.PublicHash = computePublicHash(user.PublicKey)
user.CipherPrivateKey = validateHex(r.FormValue("cipherPrivateKey"))
log.Printf("Woot! New user %s %s\n", user.Token, user.PublicHash)
if !SaveUser(user) {
http.Error(w, "That username is taken", http.StatusBadRequest)
}
}
// GET /user/me/contacts for the logged-in user's encrypted address book
// POST /user/me/contacts to update logged-in user's encrypted address book
// The entire address book is a single blob.
// Because the server never knows the plaintext, it is also
// unable to update individual keys in address book -- whenever
// the user makes changes, the client encrypts and posts all contacts
func contactsHandler(w http.ResponseWriter, r *http.Request) {
userId := authenticate(r)
if r.Method == "GET" {
cipherContactsHex := LoadContacts(userId.Token)
if cipherContactsHex == nil {
http.Error(w, "Not found", http.StatusNotFound)
} else {
w.Write([]byte(*cipherContactsHex))
}
} else if r.Method == "POST" {
cipherContactsHex, err := ioutil.ReadAll(r.Body)
if err != nil {
panic(err)
}
SaveContacts(userId.Token, string(cipherContactsHex))
}
}
// GET /user/me/key for the logged-in user's encrypted private key
func privateKeyHandler(w http.ResponseWriter, r *http.Request) {
userId := authenticate(r)
user := LoadUser(userId.Token)
if user == nil {
http.Error(w, "Not found", http.StatusNotFound)
return
}
w.Write([]byte(user.CipherPrivateKey))
}
//
// AUTHENTICATION
//
// Checks cookies, returns the logged-in user token or empty string
func authenticate(r *http.Request) *UserID {
token, err := r.Cookie("token")
if err != nil {
return nil
}
passHash, err := r.Cookie("passHash")
if err != nil {
return nil
}
passHashOld, _ := r.Cookie("passHashOld")
var passHashOldVal string
if err != nil {
passHashOldVal = ""
} else {
passHashOldVal = passHashOld.Value
}
return authenticateUserPass(token.Value, passHash.Value, passHashOldVal)
}
func authenticateUserPass(token string, passHash string, passHashOld string) *UserID {
userId := LoadUserID(token)
if userId == nil {
return nil
}
if passHash == userId.PasswordHash && passHash != "" {
return userId
}
if passHashOld == userId.PasswordHashOld && passHashOld != "" {
return userId
}
return nil
}
//
// INBOX ROUTE
//
// Takes no arguments, returns all the metadata about a user's inbox.
// Encrypted subjects are returned, but no message bodies.
// The caller must have auth cookies set.
func inboxHandler(w http.ResponseWriter, r *http.Request) {
userId := authenticate(r)
if userId == nil {
http.Error(w, "Not logged in", http.StatusUnauthorized)
return
}
box := r.URL.Path[len("/box/"):]
var emailHeaders []EmailHeader
if box == "inbox" || box == "archive" {
emailHeaders = LoadBox(userId.PublicHash, box)
} else if box == "sent" {
emailHeaders = LoadSent(userId.PublicHash)
} else {
http.Error(w, "Unknown box. "+
"Expected 'inbox','sent', etc, got "+box,
http.StatusBadRequest)
return
}
var inbox InboxSummary
inbox.Token = userId.Token
inbox.PublicHash = userId.PublicHash
inbox.EmailHeaders = emailHeaders
inboxJson, err := json.Marshal(inbox)
if err != nil {
panic(err)
}
w.Write(inboxJson)
}
//
// EMAIL ROUTE
//
func emailHandler(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
emailFetchHandler(w, r)
} else if r.Method == "PUT" {
emailBoxHandler(w, r)
} else if r.Method == "POST" {
emailSendHandler(w, r)
}
}
// GET /email/id fetches the body
func emailFetchHandler(w http.ResponseWriter, r *http.Request) {
userId := authenticate(r)
if userId == nil {
http.Error(w, "Not logged in", http.StatusUnauthorized)
return
}
id := r.URL.Path[len("/email/"):]
validateMessageID(id)
message := LoadMessage(id, userId.PublicHash)
w.Write([]byte(message.CipherBody))
}
// PUT /email/id can change things about an email, eg what box it's in
func emailBoxHandler(w http.ResponseWriter, r *http.Request) {
userId := authenticate(r)
if userId == nil {
http.Error(w, "Not logged in", http.StatusUnauthorized)
return
}
id := r.URL.Path[len("/email/"):]
validateMessageID(id)
newBox := validateBox(r.FormValue("box"))
UpdateEmail(id, userId.PublicHash, newBox)
}
func emailSendHandler(w http.ResponseWriter, r *http.Request) {
userId := authenticate(r)
if userId == nil {
http.Error(w, "Not logged in", http.StatusUnauthorized)
return
}
email := new(Email)
email.MessageID = validateMessageID(r.FormValue("msgId"))
email.UnixTime = time.Now().Unix()
email.From = userId.PublicHash + "@" + r.Host
email.To = r.FormValue("to")
email.PubHashFrom = userId.PublicHash
email.PubHashTo = validateHash(r.FormValue("pubHashTo"))
email.Box = validateBox(r.FormValue("box"))
email.CipherSubject = validateHex(r.FormValue("cipherSubject"))
email.CipherBody = validateHex(r.FormValue("cipherBody"))
SaveMessage(email)
}