-
Notifications
You must be signed in to change notification settings - Fork 55
/
api.go
183 lines (153 loc) · 4.36 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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strings"
"github.com/gorilla/mux"
)
type Response struct {
Ok bool `json:"ok"`
Message string `json:"message"`
Data interface{} `json:"data"`
}
type SuccessClaim struct {
Name string `json:"name"`
Domain string `json:"domain"`
PIN string `json:"pin"`
Invoice string `json:"invoice"`
}
// not authenticated, if correct pin is provided call returns the SuccessClaim
func ClaimAddress(w http.ResponseWriter, r *http.Request) {
params := parseParams(r)
pin, inv, err := SaveName(params.Name, params.Domain, params, params.Pin)
if err != nil {
sendError(w, 400, "could not register name: %s", err.Error())
return
}
response := Response{
Ok: true,
Message: fmt.Sprintf("claimed %v@%v", params.Name, params.Domain),
Data: SuccessClaim{params.Name, params.Domain, pin, inv},
}
// TODO: middleware for responses that adds this header
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusCreated)
json.NewEncoder(w).Encode(response)
}
func GetUser(w http.ResponseWriter, r *http.Request) {
name := mux.Vars(r)["name"]
domain := mux.Vars(r)["domain"]
params, err := GetName(name, domain)
if err != nil {
sendError(w, 400, err.Error())
return
}
// add pin to response because sometimes not saved in database; after first call to /api/v1/claim
params.Pin = ComputePIN(name, domain)
response := Response{
Ok: true,
Message: fmt.Sprintf("%v@%v found", params.Name, domain),
Data: params,
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(response)
}
func UpdateUser(w http.ResponseWriter, r *http.Request) {
params := parseParams(r)
name := mux.Vars(r)["name"]
domain := mux.Vars(r)["domain"]
// if pin not in json request body get it from header
if params.Pin == "" {
// TODO: work with Context()?
params.Pin = r.Header.Get("X-Pin")
}
if _, _, err := SaveName(name, domain, params, params.Pin); err != nil {
sendError(w, 500, err.Error())
return
}
updatedParams, err := GetName(name, domain)
if err != nil {
sendError(w, 500, err.Error())
return
}
// return the updated values or just http.StatusCreated?
response := Response{
Ok: true,
Message: fmt.Sprintf("updated %v@%v parameters", params.Name, domain),
Data: updatedParams,
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusCreated)
json.NewEncoder(w).Encode(response)
}
func DeleteUser(w http.ResponseWriter, r *http.Request) {
name := mux.Vars(r)["name"]
domain := mux.Vars(r)["domain"]
if err := DeleteName(name, domain); err != nil {
sendError(w, 500, err.Error())
return
}
response := Response{
Ok: true,
Message: fmt.Sprintf("deleted %v@%v", name, domain),
Data: nil,
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(response)
}
// authentication middleware
func authenticate(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// check domain
domain := mux.Vars(r)["domain"]
available := getDomains(s.Domain)
found := false
for _, one := range available {
if one == domain {
found = true
}
}
if !found {
sendError(w, 400, "could not use domain: %s", domain)
return
}
// exempt /claim from authentication check;
if strings.HasPrefix(r.URL.Path, "/api/v1/claim") {
next.ServeHTTP(w, r)
return
}
name := mux.Vars(r)["name"]
providedPin := r.Header.Get("X-Pin")
var err error
if providedPin == "" {
err = fmt.Errorf("X-Pin header not provided")
// pin should always be passed in header but search in json request body anyways
providedPin = parseParams(r).Pin
}
if providedPin != ComputePIN(name, domain) {
err = fmt.Errorf("wrong pin")
}
if err != nil {
sendError(w, 401, "error fetching user: %s", err.Error())
return
}
next.ServeHTTP(w, r)
})
}
// helpers
func sendError(w http.ResponseWriter, code int, msg string, args ...interface{}) {
b, _ := json.Marshal(Response{false, fmt.Sprintf(msg, args...), nil})
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(code)
w.Write(b)
}
func parseParams(r *http.Request) *Params {
reqBody, _ := ioutil.ReadAll(r.Body)
var params Params
json.Unmarshal(reqBody, ¶ms)
return ¶ms
}