/
http.go
328 lines (277 loc) · 7.64 KB
/
http.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
package client
import (
"bytes"
"context"
"crypto/tls"
"encoding/json"
"errors"
"io"
"net/http"
"net/http/cookiejar"
"net/url"
log "github.com/sirupsen/logrus"
"github.com/agodlevskii/goph-keeper/internal/app/goph-keeper/client/config"
"github.com/agodlevskii/goph-keeper/internal/app/goph-keeper/models"
)
type HTTPKeeperClient struct {
http *http.Client
apiURL *url.URL
}
const (
SBinary string = "/storage/binary/"
SCard string = "/storage/card/"
SPassword string = "/storage/password/"
SText string = "/storage/text/"
)
var ErrUnauthorized = errors.New("incorrect username or password")
func NewHTTPClient(cfg *config.ClientConfig) (HTTPKeeperClient, error) {
caCertPool, err := cfg.GetCACertPool()
if err != nil {
return HTTPKeeperClient{}, err
}
c, err := cfg.GetCertificate()
if err != nil {
return HTTPKeeperClient{}, err
}
jar, err := cookiejar.New(nil)
if err != nil {
return HTTPKeeperClient{}, err
}
uri, err := url.Parse(cfg.GetAPIAddress())
if err != nil {
return HTTPKeeperClient{}, err
}
return HTTPKeeperClient{
http: &http.Client{
Jar: jar,
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
RootCAs: caCertPool,
Certificates: []tls.Certificate{c},
MinVersion: tls.VersionTLS12,
},
},
},
apiURL: uri,
}, nil
}
func (c HTTPKeeperClient) Login(ctx context.Context, user, password string) error {
res, err := c.makeRequest(ctx, http.MethodPost, "/auth/login", models.UserRequest{
Name: user,
Password: password,
})
if err != nil {
if res.StatusCode == http.StatusUnauthorized {
return ErrUnauthorized
}
return err
}
defer closeResponseBody(res.Body)
c.http.Jar.SetCookies(c.apiURL, res.Cookies())
return nil
}
func (c HTTPKeeperClient) Logout(ctx context.Context) error {
res, err := c.makeRequest(ctx, http.MethodPost, "/auth/logout", nil)
if err != nil {
return err
}
defer closeResponseBody(res.Body)
c.http.Jar.SetCookies(c.apiURL, nil)
return nil
}
func (c HTTPKeeperClient) Register(ctx context.Context, user, password string) error {
res, err := c.makeRequest(ctx, http.MethodPost, "/auth/register", models.UserRequest{
Name: user,
Password: password,
})
if err != nil {
return err
}
defer closeResponseBody(res.Body)
return nil
}
func (c HTTPKeeperClient) DeleteBinary(ctx context.Context, id string) error {
return c.deleteData(ctx, SBinary, id)
}
func (c HTTPKeeperClient) GetAllBinaries(ctx context.Context) ([]models.BinaryResponse, error) {
body, err := c.getAllData(ctx, SBinary)
if err != nil {
return nil, err
}
defer closeResponseBody(body)
var bins []models.BinaryResponse
err = json.NewDecoder(body).Decode(&bins)
return bins, err
}
func (c HTTPKeeperClient) GetBinaryByID(ctx context.Context, id string) (models.BinaryResponse, error) {
var data models.BinaryResponse
body, err := c.getDataByID(ctx, SBinary, id)
if err != nil {
return data, err
}
defer closeResponseBody(body)
err = json.NewDecoder(body).Decode(&data)
return data, err
}
func (c HTTPKeeperClient) StoreBinary(ctx context.Context, name string,
data []byte, note string,
) (string, error) {
return c.storeData(ctx, SBinary, models.BinaryRequest{
Name: name,
Data: data,
Note: note,
})
}
func (c HTTPKeeperClient) DeleteText(ctx context.Context, id string) error {
return c.deleteData(ctx, SText, id)
}
func (c HTTPKeeperClient) GetAllTexts(ctx context.Context) ([]models.TextResponse, error) {
body, err := c.getAllData(ctx, SText)
if err != nil {
return nil, err
}
defer closeResponseBody(body)
var data []models.TextResponse
err = json.NewDecoder(body).Decode(&data)
return data, err
}
func (c HTTPKeeperClient) GetTextByID(ctx context.Context, id string) (models.TextResponse, error) {
var data models.TextResponse
body, err := c.getDataByID(ctx, SText, id)
if err != nil {
return data, err
}
defer closeResponseBody(body)
err = json.NewDecoder(body).Decode(&data)
return data, err
}
func (c HTTPKeeperClient) StoreText(ctx context.Context, name, data, note string) (string, error) {
return c.storeData(ctx, SText, models.TextRequest{
Name: name,
Data: data,
Note: note,
})
}
func (c HTTPKeeperClient) DeleteCard(ctx context.Context, id string) error {
return c.deleteData(ctx, SCard, id)
}
func (c HTTPKeeperClient) GetAllCards(ctx context.Context) ([]models.CardResponse, error) {
body, err := c.getAllData(ctx, SCard)
if err != nil {
return nil, err
}
defer closeResponseBody(body)
var data []models.CardResponse
err = json.NewDecoder(body).Decode(&data)
return data, err
}
func (c HTTPKeeperClient) GetCardByID(ctx context.Context, id string) (models.CardResponse, error) {
var data models.CardResponse
body, err := c.getDataByID(ctx, SCard, id)
if err != nil {
return data, err
}
defer closeResponseBody(body)
err = json.NewDecoder(body).Decode(&data)
return data, err
}
func (c HTTPKeeperClient) StoreCard(ctx context.Context,
name, number, holder, expDate, cvv, note string,
) (string, error) {
return c.storeData(ctx, SCard, models.CardRequest{
Name: name,
Number: number,
Holder: holder,
ExpDate: expDate,
CVV: cvv,
Note: note,
})
}
func (c HTTPKeeperClient) DeletePassword(ctx context.Context, id string) error {
return c.deleteData(ctx, SPassword, id)
}
func (c HTTPKeeperClient) GetAllPasswords(ctx context.Context) ([]models.PasswordResponse, error) {
body, err := c.getAllData(ctx, SPassword)
if err != nil {
return nil, err
}
defer closeResponseBody(body)
var data []models.PasswordResponse
err = json.NewDecoder(body).Decode(&data)
return data, err
}
func (c HTTPKeeperClient) GetPasswordByID(ctx context.Context, id string) (models.PasswordResponse, error) {
var data models.PasswordResponse
body, err := c.getDataByID(ctx, SPassword, id)
if err != nil {
return data, err
}
defer closeResponseBody(body)
err = json.NewDecoder(body).Decode(&data)
return data, err
}
func (c HTTPKeeperClient) StorePassword(ctx context.Context, name, user, password, note string) (string, error) {
return c.storeData(ctx, SPassword, models.PasswordRequest{
Name: name,
User: user,
Password: password,
Note: note,
})
}
func (c HTTPKeeperClient) deleteData(ctx context.Context, url, id string) error {
_, err := c.makeRequest(ctx, http.MethodDelete, url+id, nil)
return err
}
func (c HTTPKeeperClient) getAllData(ctx context.Context, url string) (io.ReadCloser, error) {
res, err := c.makeRequest(ctx, http.MethodGet, url, nil)
if err != nil {
return nil, err
}
return res.Body, nil
}
func (c HTTPKeeperClient) getDataByID(ctx context.Context, url, id string) (io.ReadCloser, error) {
res, err := c.makeRequest(ctx, http.MethodGet, url+id, nil)
if err != nil {
return nil, err
}
return res.Body, nil
}
func (c HTTPKeeperClient) storeData(ctx context.Context, url string, data any) (string, error) {
res, err := c.makeRequest(ctx, http.MethodPost, url, data)
if err != nil {
return "", err
}
defer closeResponseBody(res.Body)
id, err := io.ReadAll(res.Body)
if err != nil {
return "", err
}
return string(id), err
}
func (c HTTPKeeperClient) makeRequest(ctx context.Context, method, url string, data any) (*http.Response, error) {
body, err := json.Marshal(data)
if err != nil {
return nil, err
}
var reader io.Reader
if len(body) > 0 {
reader = bytes.NewBuffer(body)
}
req, err := http.NewRequestWithContext(ctx, method, c.apiURL.String()+url, reader)
if err != nil {
return nil, err
}
res, err := c.http.Do(req)
if err != nil {
return nil, err
}
if res.StatusCode != 200 {
return res, errors.New("response code")
}
return res, err
}
func closeResponseBody(b io.Closer) {
if err := b.Close(); err != nil {
log.Error(err)
}
}