/
client.go
47 lines (39 loc) · 988 Bytes
/
client.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
package main
import (
"fmt"
gohttp "net/http"
"strings"
"time"
libp2pcrypto "github.com/libp2p/go-libp2p/core/crypto"
"github.com/photon-storage/go-gw3/common/auth"
"github.com/photon-storage/go-gw3/common/http"
)
type authClient struct {
*gohttp.Client
sk libp2pcrypto.PrivKey
}
func newAuthClient(sk libp2pcrypto.PrivKey) *authClient {
return &authClient{
Client: gohttp.DefaultClient,
sk: sk,
}
}
func (c *authClient) Do(req *gohttp.Request) (*gohttp.Response, error) {
if c.sk != nil {
args := http.NewArgs().
SetArg(http.ArgP3Unixtime, fmt.Sprintf("%v", time.Now().Unix())).
SetArg(http.ArgP3Node, "localhost")
for k, v := range req.URL.Query() {
args.SetParam(k, strings.Join(v, ";;;"))
}
req.URL.RawQuery = ""
for k, v := range req.Header {
args.SetHeader(k, strings.Join(v, ";;;"))
}
req.Header = gohttp.Header{}
if err := auth.SignRequest(req, args, c.sk); err != nil {
return nil, err
}
}
return c.Client.Do(req)
}