-
Notifications
You must be signed in to change notification settings - Fork 3
/
keys.go
81 lines (67 loc) · 1.5 KB
/
keys.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
package main
import (
"encoding/json"
"io/ioutil"
"net/http"
)
type Key struct {
KeyID int64 `json:"keyId"`
PublicKey string `json:"publicKey"`
}
type SignedKey struct {
KeyID int64 `json:"keyId"`
PublicKey string `json:"publicKey"`
Signature string `json:"signature"`
}
type PreKeys struct {
SignedPreKey SignedKey `json:"signedKey"`
IdentityKey string `json:"identityKey`
LastResortKey Key `json:"lastResortKey"`
PreKeys []Key `json:"preKeys"`
}
func registerKeys(w http.ResponseWriter, req *http.Request) {
uname, _, ok := req.BasicAuth()
if !ok {
http.Error(w, "auth fail", 401)
return
}
log.Infof("number: %s", uname)
body, err := ioutil.ReadAll(req.Body)
if err != nil {
http.Error(w, "server error", 500)
return
}
var preKeys PreKeys
err = json.Unmarshal(body, &preKeys)
if err != nil {
http.Error(w, "server error", 500)
return
}
log.Infof("%+v\n", preKeys)
log.Infof("%s\n", body)
id := encodeNumber(uname)
writeDB(id[:], []byte("k"), body)
w.WriteHeader(200)
}
func recipientKeys(w http.ResponseWriter, req *http.Request) {
uname, _, ok := req.BasicAuth()
if !ok {
http.Error(w, "auth fail", 401)
return
}
log.Infof("number: %s", uname)
id := encodeNumber(uname)
value, err := readDB(id[:], []byte("k"))
if err != nil {
http.Error(w, "server error", 500)
return
}
var preKeys PreKeys
err = json.Unmarshal(value, &preKeys)
if err != nil {
http.Error(w, "server error", 500)
return
}
w.WriteHeader(200)
w.Write(value)
}