/
api.go
222 lines (209 loc) · 5.97 KB
/
api.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
package ufo
import (
"encoding/json"
"io/ioutil"
"net/http"
)
var (
regin = make(chan RegisterIn)
proofin = make(chan proof)
regout chan error
proofout chan error
chalin = make(chan ChallengeIn)
verifyin = make(chan SignedFingerPrint)
chalout chan ChallengeOut
verifyout chan error
readin = make(chan ReadIn)
writein = make(chan WriteIn)
readout chan ReadOut
writeout chan error
groupin = make(chan Group)
listin = make(chan ListIn)
groupout chan GroupOut
listout chan ListOut
login = make(chan Event)
)
func init() {
regout, proofout = registerProc(regin, proofin)
readout, writeout = msgProc(readin, writein)
groupout, listout = convoProc(groupin, listin)
chalout, verifyout = challengeProc(chalin, verifyin)
logger(login)
login <- Event{"started", nil}
}
//RegisterInHandler is the endpoint for registration requests
//it accepts a marshalled RegisterIn struct and returns
//a 200 status code on success.
func RegisterInHandler(w http.ResponseWriter, r *http.Request) {
var in RegisterIn
b, err := ioutil.ReadAll(r.Body)
if err != nil {
login <- Event{"Reading POST", err}
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
}
err = json.Unmarshal(b, &in)
if err != nil {
login <- Event{"Parsing JSON", err}
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
}
regin <- in
if err = <-regout; err != nil {
login <- Event{"Registration", err}
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
}
w.Write([]byte("OK"))
}
//ChallengeHandler is the endpoint for challenge requests
//it accepts a json marshalled ChallengeIn struct and
//returns a marshalled ChallengeOut on success.
func ChallengeHandler(w http.ResponseWriter, r *http.Request) {
var in ChallengeIn
b, err := ioutil.ReadAll(r.Body)
if err != nil {
login <- Event{"Reading POST", err}
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
}
err = json.Unmarshal(b, &in)
if err != nil {
login <- Event{"Parsing JSON", err}
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
}
chalin <- in
out := <-chalout
if out.UUID == "" {
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
}
b, _ = json.Marshal(&out)
w.Write(b)
}
//MakeConvoHandler is the endpoint for creation of
//conversations, it accepts a json marshalled GroupIn
//struct and returns a marshalled GroupOut struct on success.
func MakeConvoHandler(w http.ResponseWriter, r *http.Request) {
var in GroupIn
b, err := ioutil.ReadAll(r.Body)
if err != nil {
login <- Event{"Reading POST", err}
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
}
err = json.Unmarshal(b, &in)
if err != nil {
login <- Event{"Parsing JSON", err}
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
}
verifyin <- in.SignedFingerPrint
err = <-verifyout
if err != nil {
login <- Event{"Verification", err}
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
}
groupin <- in.Group
out := <-groupout
b, _ = json.Marshal(&out)
w.Write(b)
}
//ReadHandler is the endpoint for requesting messages from
//the server. It accepts a marshalled ReadIn struct and
//returns a marshalled ReadOut struct on success.
func ReadHandler(w http.ResponseWriter, r *http.Request) {
var in ReadIn
b, err := ioutil.ReadAll(r.Body)
if err != nil {
login <- Event{"Reading POST", err}
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
}
err = json.Unmarshal(b, &in)
if err != nil {
login <- Event{"Parsing JSON", err}
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
}
verifyin <- in.SignedFingerPrint
err = <-verifyout
if err != nil {
login <- Event{"Verification", err}
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
}
readin <- in
out := <-readout
if out.Err != nil {
login <- Event{"Read", out.Err}
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
}
b, _ = json.Marshal(&out)
w.Write(b)
}
//WriteHandler is the endpoint for writing messages
//to a group. It accepts a WriteIn struct and returns a
//200 status code on success with a body of "OK"
func WriteHandler(w http.ResponseWriter, r *http.Request) {
var in WriteIn
b, err := ioutil.ReadAll(r.Body)
if err != nil {
login <- Event{"Reading POST", err}
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
}
err = json.Unmarshal(b, &in)
if err != nil {
login <- Event{"Parsing JSON", err}
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
}
verifyin <- in.SignedFingerPrint
err = <-verifyout
if err != nil {
login <- Event{"Verification", err}
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
}
writein <- in
out := <-writeout
if out != nil {
login <- Event{"Write", out}
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
}
w.Write([]byte("OK"))
}
//ListHandler is the endpoint for users to query what
//groups they are a part of. It accepts a ListIn struct
//and returns a ListOut struct.
func ListHandler(w http.ResponseWriter, r *http.Request) {
var in ListIn
b, err := ioutil.ReadAll(r.Body)
if err != nil {
login <- Event{"Reading POST", err}
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
}
err = json.Unmarshal(b, &in)
if err != nil {
login <- Event{"Parsing JSON", err}
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
}
verifyin <- in.SignedFingerPrint
err = <-verifyout
if err != nil {
login <- Event{"Verification", err}
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
}
listin <- in
out := <-listout
b, _ = json.Marshal(&out)
w.Write(b)
}