forked from mvdan/bitw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
99 lines (89 loc) · 2.39 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
// Copyright (c) 2019, Daniel Martí <[email protected]>
// See LICENSE for licensing information
package main
import (
"bytes"
"context"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"os"
"strings"
"time"
)
var httpClient = &http.Client{
// Specific http calls can use lower timeouts via context.
Timeout: 10 * time.Second,
}
type errStatusCode struct {
code int
body []byte
}
func (e *errStatusCode) Error() string {
return fmt.Sprintf("%s: %s", http.StatusText(e.code), e.body)
}
type authToken struct{}
func jsonPOST(ctx context.Context, urlstr string, recv, send interface{}) error {
var r io.Reader
contentType := "application/json"
authEmail := ""
if values, ok := send.(url.Values); ok {
// Some endpoints only accept urlencoded bodies.
r = strings.NewReader(values.Encode())
contentType = "application/x-www-form-urlencoded"
if email := values.Get("username"); email != "" && values.Get("scope") != "" {
authEmail = email
}
} else {
buf := new(bytes.Buffer)
if err := json.NewEncoder(buf).Encode(send); err != nil {
return err
}
r = buf
}
req, err := http.NewRequest("POST", urlstr, r)
if err != nil {
return err
}
req.Header.Set("Content-Type", contentType)
if authEmail != "" {
// For login requests, the upstream bitwarden server wants an extra header.
// They also require the value to be base64-encoded, for some reason.
// See: https://github.com/bitwarden/server/blob/6b629feb030e01966189ca1b5339ab85fa5e690c/src/Core/IdentityServer/ResourceOwnerPasswordValidator.cs#L139
req.Header.Set("Auth-Email", base64.URLEncoding.EncodeToString([]byte(authEmail)))
}
return httpDo(ctx, req, recv)
}
func jsonGET(ctx context.Context, urlstr string, recv interface{}) error {
req, err := http.NewRequest("GET", urlstr, nil)
if err != nil {
return err
}
return httpDo(ctx, req, recv)
}
func httpDo(ctx context.Context, req *http.Request, recv interface{}) error {
if token, ok := ctx.Value(authToken{}).(string); ok {
req.Header.Set("Authorization", "Bearer "+token)
}
res, err := httpClient.Do(req)
if err != nil {
return err
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return err
}
if res.StatusCode != 200 {
return &errStatusCode{res.StatusCode, body}
}
if err := json.Unmarshal(body, recv); err != nil {
fmt.Fprintln(os.Stderr, string(body))
return err
}
return nil
}