Skip to content

Commit

Permalink
Do not attach authorization header in bearerAuthPlugin if response is…
Browse files Browse the repository at this point in the history
… a redirect

Signed-off-by: carabasdaniel <[email protected]>
  • Loading branch information
carabasdaniel committed Jan 24, 2025
1 parent b032e3b commit 5414806
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion v1/plugins/rest/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,14 @@ type bearerAuthPlugin struct {
// encode is set to true for the OCIDownloader because
// it expects tokens in plain text but needs them in base64.
encode bool
logger logging.Logger
}

func (ap *bearerAuthPlugin) NewClient(c Config) (*http.Client, error) {
t, err := DefaultTLSConfig(c)

ap.logger = c.logger

if err != nil {
return nil, err
}
Expand Down Expand Up @@ -166,7 +170,12 @@ func (ap *bearerAuthPlugin) Prepare(req *http.Request) error {
token = base64.StdEncoding.EncodeToString([]byte(token))
}

req.Header.Add("Authorization", fmt.Sprintf("%v %v", ap.Scheme, token))
if req.Response != nil && (req.Response.StatusCode == http.StatusPermanentRedirect || req.Response.StatusCode == http.StatusTemporaryRedirect) {
ap.logger.Debug("not attaching authorization header as the response contains a redirect")
} else {
ap.logger.Debug("attaching authorization header")
req.Header.Add("Authorization", fmt.Sprintf("%v %v", ap.Scheme, token))
}
return nil
}

Expand Down

0 comments on commit 5414806

Please sign in to comment.