@@ -12,8 +12,8 @@ import (
12
12
"io"
13
13
"net/http"
14
14
15
+ "github.com/xmidt-org/ancla/auth"
15
16
"github.com/xmidt-org/ancla/model"
16
- "github.com/xmidt-org/bascule/acquire"
17
17
"go.uber.org/zap"
18
18
)
19
19
@@ -58,24 +58,18 @@ type BasicClientConfig struct {
58
58
59
59
// Auth provides the mechanism to add auth headers to outgoing requests.
60
60
// (Optional) If not provided, no auth headers are added.
61
- Auth Auth
61
+ Auth auth. Acquirer
62
62
}
63
63
64
64
// BasicClient is the client used to make requests to Argus.
65
65
type BasicClient struct {
66
66
client * http.Client
67
- auth acquire .Acquirer
67
+ auth auth .Acquirer
68
68
storeBaseURL string
69
69
bucket string
70
70
getLogger func (context.Context ) * zap.Logger
71
71
}
72
72
73
- // Auth contains authorization data for requests to Argus.
74
- type Auth struct {
75
- JWT acquire.RemoteBearerTokenAcquirerOptions
76
- Basic string
77
- }
78
-
79
73
type response struct {
80
74
Body []byte
81
75
ArgusErrorHeader string
@@ -101,19 +95,13 @@ func NewBasicClient(config BasicClientConfig,
101
95
return nil , err
102
96
}
103
97
104
- tokenAcquirer , err := buildTokenAcquirer (config .Auth )
105
- if err != nil {
106
- return nil , err
107
- }
108
- clientStore := & BasicClient {
98
+ return & BasicClient {
109
99
client : config .HTTPClient ,
110
- auth : tokenAcquirer ,
100
+ auth : config . Auth ,
111
101
bucket : config .Bucket ,
112
102
storeBaseURL : config .Address + storeAPIPath ,
113
103
getLogger : getLogger ,
114
- }
115
-
116
- return clientStore , nil
104
+ }, nil
117
105
}
118
106
119
107
// GetItems fetches all items that belong to a given owner.
@@ -213,32 +201,39 @@ func (c *BasicClient) sendRequest(ctx context.Context, owner, method, url string
213
201
if err != nil {
214
202
return response {}, fmt .Errorf (errWrappedFmt , errNewRequestFailure , err .Error ())
215
203
}
216
- err = acquire .AddAuth (r , c .auth )
217
- if err != nil {
218
- return response {}, fmt .Errorf (errWrappedFmt , ErrAuthAcquirerFailure , err .Error ())
219
- }
204
+
220
205
if len (owner ) > 0 {
221
206
r .Header .Set (ItemOwnerHeaderKey , owner )
222
207
}
208
+
209
+ if c .auth != nil {
210
+ auth , err := c .auth .Acquire ()
211
+ if err != nil {
212
+ return response {}, errors .Join (ErrAuthAcquirerFailure , err )
213
+ }
214
+
215
+ r .Header .Set ("Authorization" , auth )
216
+ }
217
+
223
218
resp , err := c .client .Do (r )
224
219
if err != nil {
225
220
return response {}, fmt .Errorf (errWrappedFmt , errDoRequestFailure , err .Error ())
226
221
}
222
+
227
223
defer resp .Body .Close ()
228
- var sqResp = response {
224
+
225
+ sqResp := response {
229
226
Code : resp .StatusCode ,
230
227
ArgusErrorHeader : resp .Header .Get (XmidtErrorHeaderKey ),
231
228
}
232
229
bodyBytes , err := io .ReadAll (resp .Body )
233
230
if err != nil {
234
231
return sqResp , fmt .Errorf (errWrappedFmt , errReadingBodyFailure , err .Error ())
235
232
}
233
+
236
234
sqResp .Body = bodyBytes
237
- return sqResp , nil
238
- }
239
235
240
- func isEmpty (options acquire.RemoteBearerTokenAcquirerOptions ) bool {
241
- return len (options .AuthURL ) < 1 || options .Buffer == 0 || options .Timeout == 0
236
+ return sqResp , nil
242
237
}
243
238
244
239
// translateNonSuccessStatusCode returns as specific error
@@ -254,15 +249,6 @@ func translateNonSuccessStatusCode(code int) error {
254
249
}
255
250
}
256
251
257
- func buildTokenAcquirer (auth Auth ) (acquire.Acquirer , error ) {
258
- if ! isEmpty (auth .JWT ) {
259
- return acquire .NewRemoteBearerTokenAcquirer (auth .JWT )
260
- } else if len (auth .Basic ) > 0 {
261
- return acquire .NewFixedAuthAcquirer (auth .Basic )
262
- }
263
- return & acquire.DefaultAcquirer {}, nil
264
- }
265
-
266
252
func validateBasicConfig (config * BasicClientConfig ) error {
267
253
if config .Address == "" {
268
254
return ErrAddressEmpty
0 commit comments