-
Notifications
You must be signed in to change notification settings - Fork 2
/
client.go
80 lines (71 loc) · 1.9 KB
/
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
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
package main
import (
"bytes"
"encoding/json"
"fmt"
"github.com/JackOfMostTrades/spiffe-user-demo/common"
"io/ioutil"
"net/http"
)
type UserClient interface {
GetUserX509(req *common.GetUserX509Request) (*common.GetUserX509Response, error)
GetUserJwt(req *common.GetUserJwtRequest) (*common.GetUserJwtResponse, error)
GetJwks() (*common.GetJwksResponse, error)
}
type UserClientImpl struct {
AuthToken string
Hostname string
}
func (c *UserClientImpl) GetUserX509(req *common.GetUserX509Request) (*common.GetUserX509Response, error) {
req.AuthToken = c.AuthToken
resp := new(common.GetUserX509Response)
err := c.doRequest(req, resp, "/x509")
if err != nil {
return nil, err
}
return resp, nil
}
func (c *UserClientImpl) GetUserJwt(req *common.GetUserJwtRequest) (*common.GetUserJwtResponse, error) {
req.AuthToken = c.AuthToken
resp := new(common.GetUserJwtResponse)
err := c.doRequest(req, resp, "/jwt")
if err != nil {
return nil, err
}
return resp, nil
}
func (c *UserClientImpl) doRequest(req interface{}, resp interface{}, path string) error {
body, err := json.Marshal(req)
if err != nil {
return err
}
res, err := http.Post(fmt.Sprintf("%s%s", c.Hostname, path), "application/json", bytes.NewReader(body))
if err != nil {
return err
}
if res.Body != nil {
defer res.Body.Close()
}
if res.StatusCode < 200 || res.StatusCode >= 300 {
debugData, _ := ioutil.ReadAll(res.Body)
return fmt.Errorf("non 2xx status code: %d: %s", res.StatusCode, string(debugData))
}
err = json.NewDecoder(res.Body).Decode(resp)
if err != nil {
return err
}
return nil
}
func (c *UserClientImpl) GetJwks() (*common.GetJwksResponse, error) {
res, err := http.Get(fmt.Sprintf("%s/jwks", c.Hostname))
if err != nil {
return nil, err
}
defer res.Body.Close()
resp := new(common.GetJwksResponse)
err = json.NewDecoder(res.Body).Decode(resp)
if err != nil {
return nil, err
}
return resp, nil
}