-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintweb.go
285 lines (250 loc) · 7.26 KB
/
intweb.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
package intweb
// This package is a partial implementation of
// https://wiki.hive13.org/view/Access_Protocol. As this protocol is
// considered sort of legacy at this point, it does not put
// extraordinary effort into correctness - so some things are ignored,
// such as verifying the checksums on communications with the server.
import (
"bytes"
"fmt"
"log"
"crypto/sha512"
"encoding/json"
"io/ioutil"
"math/rand"
"net/http"
)
// Session contains parameters for intweb communications.
//
// "Session" is something of a misnomer as this struct contains no
// state, though conceivably it could present a nicer interface by
// doing so (e.g. for things like the nonce the server uses).
type Session struct {
// The name of the device
Device string
// The device key
DeviceKey []byte
// The URL of the intweb server, including /api/access.
URL string
// Set true for more verbose logging
Verbose bool
// The HTTP client (if a custom one is needed)
Client *http.Client
}
// PostIntweb POSTs a message to the intweb server, returning the reply.
//
// Having a proper message format, including nonces and checksums, is
// up to the caller. This call does not do it.
func (s *Session) PostIntweb(data interface {}) ([]byte, error) {
msg_json, err := json.Marshal(data)
if err != nil {
return nil, err
}
if s.Verbose {
log.Printf("Request: POST to %s: %s", s.URL, msg_json)
}
resp, err := s.Client.Post(s.URL, "application/json", bytes.NewBuffer(msg_json))
if err != nil {
return nil, err
}
body, err := ioutil.ReadAll(resp.Body)
defer resp.Body.Close()
if s.Verbose {
log.Printf("Response: HTTP %d, %s", resp.StatusCode, body)
}
if resp.StatusCode != 200 {
return nil, &Error{
Msg: fmt.Sprintf("HTTP code %d", resp.StatusCode),
Resp: nil,
}
}
if err != nil {
return nil, err
}
return body, nil
}
// GetNonce requests a new nonce from the server.
//
// This is a necessary first step for many other requests.
func (s *Session) GetNonce() (string, error) {
d := MessageData{
Operation: "get_nonce",
Version: 2,
RandomResponse: randomResponse(),
}
cs, err := checksum(s.DeviceKey, d)
if err != nil {
return "", err
}
msg := map[string](interface {}){
"data": d,
"device": s.Device,
"checksum": fmt.Sprintf("%X", cs),
}
resp_bytes, err := s.PostIntweb(msg)
if err != nil {
return "", err
}
var resp Response
err = json.Unmarshal(resp_bytes, &resp)
if err != nil {
return "", err
}
data, err := DecodeAndCheck(&resp)
if err != nil {
return "", err
}
return data.NewNonce, nil
}
// Access requests access to some item for some badge number.
//
// Item and badge number must be in exactly the same format as in the
// intweb database. Nonce must also be supplied, e.g. from a previous
// GetNonce() call.
func (s *Session) Access(nonce string, item string, badge uint64) (bool, string, error) {
d := AccessReqData{
Operation: "access",
Version: 2,
RandomResponse: randomResponse(),
Nonce: nonce,
Item: item,
Badge: badge,
}
cs, err := checksum(s.DeviceKey, d)
if err != nil {
return false, "", err
}
msg := map[string](interface {}){
"data": d,
"device": s.Device,
"checksum": fmt.Sprintf("%X", cs),
}
resp_bytes, err := s.PostIntweb(msg)
if err != nil {
return false, "", err
}
var resp Response
err = json.Unmarshal(resp_bytes, &resp)
if err != nil {
return false, "", err
}
data, err := DecodeAndCheck(&resp)
if err != nil {
return false, "", err
}
return data.Access, data.Error, nil
}
// MessageData contains the data for a generic message that is sent to
// intweb, e.g. to request a new nonce.
type MessageData struct {
Operation string `json:"operation"`
RandomResponse []int `json:"random_response"`
Version int `json:"version"`
// These fields must remain in sorted order for the checksum.
}
// AccessReqData contains the data for an access request message that
// is sent to intweb.
type AccessReqData struct {
Badge uint64 `json:"badge"`
Item string `json:"item"`
Nonce string `json:"nonce"`
Operation string `json:"operation"`
RandomResponse []int `json:"random_response"`
Version int `json:"version"`
// These fields must remain in sorted order for the checksum.
// Ordinarily I would have just embedded MessageData.
}
// Response is a catch-all structure for a response from intweb.
//
// In theory, we could parse this in different ways depending on which
// message type it is. In practice, the documentation is hairy and
// the server-side implementation is even hairier, so I really don't
// care.
type Response struct {
Data json.RawMessage `json:"data"`
Response *bool `json:"response"`
Version string `json:"version"`
}
// RespData contains the data from a few intweb response types.
//
// This includes a generic error reply, a reply to requesting a new
// nonce, and a reply to an access request. Not all fields will
// always be used.
//
// See the DecodeAndCheck function for both producing an instance of
// this, and checking some of the common errors
type RespData struct {
NewNonce string `json:"new_nonce"`
NonceValid bool `json:"nonce_valid"`
Random []int `json:"random"`
RandomResponse []int `json:"random_response"`
Response bool `json:"response"`
// Disabled to work around a server work-around:
//Version string `json:"version"`
Data string `json:"data"`
Access bool `json:"access"`
Error string `json:"error"`
}
// An error reported by the intweb server.
type Error struct {
// The message text (which may be directly from the server, or may
// be a summary from some flag that is set):
Msg string
// The actual response that produced this error:
Resp *RespData
}
func (err *Error) Error() string {
return fmt.Sprintf("intweb reported error: %s", err.Msg)
}
// DecodeAndCheck attempts to parse a Response into RespData.
//
// Returns an error if this fails. Errors may be from JSON
// unmarshaling, or may be an intweb.Error.
func DecodeAndCheck(r *Response) (*RespData, error) {
if r.Response != nil && !(*r.Response) {
var s string
if err := json.Unmarshal(r.Data, &s); err != nil {
return nil, err
}
return nil, &Error{ Msg: s, Resp: nil }
}
var data RespData
err := json.Unmarshal(r.Data, &data)
if err != nil {
return nil, err
}
if !data.Response {
return nil, &Error{ Msg: data.Data, Resp: &data }
}
if !data.NonceValid {
return nil, &Error{ Msg: "Nonce invalid", Resp: &data }
}
// TODO: Check checksums and random response?
return &data, nil
}
// randomResponse returns an array with 16 random values (0-255).
func randomResponse() []int {
resp := make([]int, 16)
for i, _ := range resp {
resp[i] = rand.Intn(256)
}
return resp
}
// checksumRaw returns the SHA-512 checksum for a given device key and data.
func checksumRaw(key []byte, data []byte) []byte {
h := sha512.New()
h.Write(key)
h.Write(data)
return h.Sum(nil)
}
// checksum returns the SHA-512 checksum for some device key, and JSON data.
//
// This attempts to turn 'data' to JSON, and then computes the
// checksum over the device key and this data.
func checksum(key []byte, data interface{}) ([]byte, error) {
data_json, err := json.Marshal(data)
if err != nil {
return nil, err
}
return checksumRaw(key, data_json), nil
}