-
Notifications
You must be signed in to change notification settings - Fork 2
/
client.go
130 lines (105 loc) · 2.93 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
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
package lpastore
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"time"
"github.com/golang-jwt/jwt/v5"
"github.com/ministryofjustice/opg-modernising-lpa/internal/actor/actoruid"
"github.com/ministryofjustice/opg-modernising-lpa/internal/secrets"
)
const (
issuer = "opg.poas.makeregister"
statusActive = "active"
statusReplacement = "replacement"
)
var ErrNotFound = errors.New("lpa not found in lpa-store")
type responseError struct {
name string
body any
}
func (e responseError) Error() string { return e.name }
func (e responseError) Title() string { return e.name }
func (e responseError) Data() any { return e.body }
type Doer interface {
Do(*http.Request) (*http.Response, error)
}
type SecretsClient interface {
Secret(ctx context.Context, name string) (string, error)
}
type Client struct {
baseURL string
secretsClient SecretsClient
doer Doer
now func() time.Time
}
func New(baseURL string, secretsClient SecretsClient, lambdaClient Doer) *Client {
return &Client{baseURL: baseURL, secretsClient: secretsClient, doer: lambdaClient, now: time.Now}
}
func (c *Client) do(ctx context.Context, actorUID actoruid.UID, req *http.Request, data any) error {
req.Header.Add("Content-Type", "application/json")
token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.RegisteredClaims{
Issuer: issuer,
IssuedAt: jwt.NewNumericDate(c.now()),
Subject: actorUID.PrefixedString(),
})
secretKey, err := c.secretsClient.Secret(ctx, secrets.LpaStoreJwtSecretKey)
if err != nil {
return err
}
auth, err := token.SignedString([]byte(secretKey))
if err != nil {
return err
}
req.Header.Add("X-Jwt-Authorization", "Bearer "+auth)
resp, err := c.doer.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
switch req.Method {
case http.MethodGet:
switch resp.StatusCode {
case http.StatusOK:
return json.NewDecoder(resp.Body).Decode(data)
case http.StatusNotFound:
return ErrNotFound
default:
body, _ := io.ReadAll(resp.Body)
return responseError{
name: fmt.Sprintf("expected 200 response but got %d", resp.StatusCode),
body: string(body),
}
}
case http.MethodPost, http.MethodPut:
if resp.StatusCode != http.StatusCreated {
body, _ := io.ReadAll(resp.Body)
return responseError{
name: fmt.Sprintf("expected 201 response but got %d", resp.StatusCode),
body: string(body),
}
}
return nil
}
return fmt.Errorf("unexpected http method %s", req.Method)
}
func (c *Client) CheckHealth(ctx context.Context) error {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, c.baseURL+"/health-check", nil)
if err != nil {
return err
}
resp, err := c.doer.Do(req)
if err != nil {
return err
}
if err := resp.Body.Close(); err != nil {
return err
}
if resp.StatusCode != http.StatusOK {
return responseError{name: fmt.Sprintf("expected 200 response but got %d", resp.StatusCode)}
}
return nil
}