Skip to content

Commit 6aa7882

Browse files
authored
refactor: use const for headers (#1026)
Signed-off-by: Terry Howe <terrylhowe@gmail.com>
1 parent 74b361e commit 6aa7882

File tree

1 file changed

+18
-10
lines changed

1 file changed

+18
-10
lines changed

registry/remote/auth/client.go

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@ import (
3131
"oras.land/oras-go/v2/registry/remote/retry"
3232
)
3333

34+
// HTTP header names used in authentication.
35+
const (
36+
headerAuthorization = "Authorization"
37+
headerContentType = "Content-Type"
38+
headerUserAgent = "User-Agent"
39+
headerWWWAuthenticate = "Www-Authenticate"
40+
)
41+
3442
// ErrBasicCredentialNotFound is returned when the credential is not found for
3543
// basic auth.
3644
var ErrBasicCredentialNotFound = errors.New("basic credential not found")
@@ -39,7 +47,7 @@ var ErrBasicCredentialNotFound = errors.New("basic credential not found")
3947
var DefaultClient = &Client{
4048
Client: retry.DefaultClient,
4149
Header: http.Header{
42-
"User-Agent": {"oras-go"},
50+
headerUserAgent: {"oras-go"},
4351
},
4452
Cache: DefaultCache,
4553
}
@@ -161,7 +169,7 @@ func (c *Client) SetUserAgent(userAgent string) {
161169
if c.Header == nil {
162170
c.Header = http.Header{}
163171
}
164-
c.Header.Set("User-Agent", userAgent)
172+
c.Header.Set(headerUserAgent, userAgent)
165173
}
166174

167175
// Do sends the request to the remote server, attempting to resolve
@@ -171,7 +179,7 @@ func (c *Client) SetUserAgent(userAgent string) {
171179
// - Do returns error if it fails to fetch token for bearer auth.
172180
// - Do returns the registry response without error for basic auth.
173181
func (c *Client) Do(originalReq *http.Request) (*http.Response, error) {
174-
if auth := originalReq.Header.Get("Authorization"); auth != "" {
182+
if auth := originalReq.Header.Get(headerAuthorization); auth != "" {
175183
return c.send(originalReq)
176184
}
177185

@@ -188,14 +196,14 @@ func (c *Client) Do(originalReq *http.Request) (*http.Response, error) {
188196
case SchemeBasic:
189197
token, err := cache.GetToken(ctx, host, SchemeBasic, "")
190198
if err == nil {
191-
req.Header.Set("Authorization", "Basic "+token)
199+
req.Header.Set(headerAuthorization, "Basic "+token)
192200
}
193201
case SchemeBearer:
194202
scopes := GetAllScopesForHost(ctx, host)
195203
attemptedKey = strings.Join(scopes, " ")
196204
token, err := cache.GetToken(ctx, host, SchemeBearer, attemptedKey)
197205
if err == nil {
198-
req.Header.Set("Authorization", "Bearer "+token)
206+
req.Header.Set(headerAuthorization, "Bearer "+token)
199207
}
200208
}
201209
}
@@ -209,7 +217,7 @@ func (c *Client) Do(originalReq *http.Request) (*http.Response, error) {
209217
}
210218

211219
// attempt again with credentials for recognized schemes
212-
challenge := resp.Header.Get("Www-Authenticate")
220+
challenge := resp.Header.Get(headerWWWAuthenticate)
213221
scheme, params := parseChallenge(challenge)
214222
switch scheme {
215223
case SchemeBasic:
@@ -223,7 +231,7 @@ func (c *Client) Do(originalReq *http.Request) (*http.Response, error) {
223231
}
224232

225233
req = originalReq.Clone(ctx)
226-
req.Header.Set("Authorization", "Basic "+token)
234+
req.Header.Set(headerAuthorization, "Basic "+token)
227235
case SchemeBearer:
228236
resp.Body.Close()
229237

@@ -239,7 +247,7 @@ func (c *Client) Do(originalReq *http.Request) (*http.Response, error) {
239247
if key != attemptedKey {
240248
if token, err := cache.GetToken(ctx, host, SchemeBearer, key); err == nil {
241249
req = originalReq.Clone(ctx)
242-
req.Header.Set("Authorization", "Bearer "+token)
250+
req.Header.Set(headerAuthorization, "Bearer "+token)
243251
if err := rewindRequestBody(req); err != nil {
244252
return nil, err
245253
}
@@ -266,7 +274,7 @@ func (c *Client) Do(originalReq *http.Request) (*http.Response, error) {
266274
}
267275

268276
req = originalReq.Clone(ctx)
269-
req.Header.Set("Authorization", "Bearer "+token)
277+
req.Header.Set(headerAuthorization, "Bearer "+token)
270278
default:
271279
return resp, nil
272280
}
@@ -389,7 +397,7 @@ func (c *Client) fetchOAuth2Token(ctx context.Context, realm, service string, sc
389397
if err != nil {
390398
return "", err
391399
}
392-
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
400+
req.Header.Set(headerContentType, "application/x-www-form-urlencoded")
393401

394402
resp, err := c.send(req)
395403
if err != nil {

0 commit comments

Comments
 (0)